ahmedabu98 commented on code in PR #27366:
URL: https://github.com/apache/beam/pull/27366#discussion_r1256441783


##########
sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/common/ProtoSchemaUtils.java:
##########
@@ -0,0 +1,180 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.beam.sdk.io.gcp.common;
+
+import com.squareup.wire.schema.Field;
+import com.squareup.wire.schema.Location;
+import com.squareup.wire.schema.internal.parser.EnumElement;
+import com.squareup.wire.schema.internal.parser.FieldElement;
+import com.squareup.wire.schema.internal.parser.MessageElement;
+import com.squareup.wire.schema.internal.parser.ProtoFileElement;
+import com.squareup.wire.schema.internal.parser.ProtoParser;
+import com.squareup.wire.schema.internal.parser.TypeElement;
+import java.util.Map;
+import java.util.Objects;
+import java.util.stream.Collectors;
+import org.apache.beam.sdk.schemas.Schema;
+import org.apache.beam.sdk.schemas.logicaltypes.EnumerationType;
+import org.checkerframework.checker.nullness.qual.Nullable;
+
+/**
+ * The ProtoSchemaUtils class provides utility methods for converting Protocol 
Buffers schema
+ * definitions into Beam schemas.
+ */
+public class ProtoSchemaUtils {
+
+  private static final Location DEFAULT_LOCATION = Location.get("");
+
+  /**
+   * Converts a Protocol Buffers schema definition into a Beam schema.
+   *
+   * @param protobufDef the Protocol Buffers schema definition as a string.
+   * @return the corresponding Beam schema.
+   * @throws SchemaParseException if the Protocol Buffers schema is invalid.
+   */
+  public static Schema beamSchemaFromProtoSchemaDescription(String protobufDef)
+      throws SchemaParseException {
+    ProtoFileElement elm = ProtoParser.Companion.parse(DEFAULT_LOCATION, 
protobufDef);
+    if (elm.getTypes().size() != 1) {
+      throw new SchemaParseException(
+          String.format(
+              "Expected a single type defined in schema input, but found %s. "
+                  + "Provide only a single top-level proto type to use as the "
+                  + "message schema.",
+              elm.getTypes().size()));
+    }
+    TypeElement typeElement = elm.getTypes().stream().findFirst().get();
+
+    if (!(typeElement instanceof MessageElement)) {
+      throw new SchemaParseException(
+          "Expected a message type defined, but found a different type 
definition for "
+              + typeElement.getName());
+    }
+
+    return parsedProtoSchemaToBeamSchema(typeElement, null);
+  }
+
+  /**
+   * Converts a parsed Protocol Buffers schema to a Beam schema.
+   *
+   * @param typeElement the type element representing the Protocol Buffers 
schema.
+   * @param typeCatalog a catalog of types referenced in the schema.
+   * @return the corresponding Beam schema.
+   */
+  @SuppressWarnings("nullness")

Review Comment:
   You can use Optional.ofNullable(<nullable_object>).get() to take care of the 
checks. Example: 
https://github.com/apache/beam/blob/c06247bf79ec3fd02810c26b7f0da58f6a790892/sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/bigtable/BigtableWriteSchemaTransformProvider.java#L168C28-L168C28



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to