Dippatel98 commented on code in PR #27366: URL: https://github.com/apache/beam/pull/27366#discussion_r1256429111
########## 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: If I don't add any suppression, then [line 98](https://github.com/apache/beam/pull/27366/files/79342ae4afbcb6101b19fba6b623ee53418978ca#diff-d7a7d5fd76e08aa3ede8a967b406a5fb0896d7026b079f1ba32fcfd522850b62R98) complains about dereferencing null values. If you look at the line, the if condition explicitly checks for nulls and wouldn't proceed if `f.getLable()` is null. -- 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]
