johnjcasey commented on code in PR #27366: URL: https://github.com/apache/beam/pull/27366#discussion_r1256168087
########## 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: I'd prefer to avoid adding suppression to the repo ########## 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 { Review Comment: Can we add tests for this class? ########## sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/pubsub/PubsubWriteSchemaTransformProvider.java: ########## @@ -76,360 +50,113 @@ * provide no backwards compatibility guarantees, and it should not be implemented outside the Beam * repository. */ -@SuppressWarnings({ - "nullness" // TODO(https://github.com/apache/beam/issues/20497) -}) -@Internal +// @SuppressWarnings({ Review Comment: please remove this. ########## sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/pubsub/PubsubUtils.java: ########## @@ -0,0 +1,110 @@ +/* + * 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.pubsub; + +import com.google.cloud.pubsub.v1.SchemaServiceClient; +import com.google.cloud.pubsub.v1.SubscriptionAdminClient; +import com.google.cloud.pubsub.v1.TopicAdminClient; +import com.google.pubsub.v1.Encoding; +import com.google.pubsub.v1.Schema; +import com.google.pubsub.v1.Topic; +import java.io.IOException; +import org.apache.beam.sdk.io.gcp.common.ProtoSchemaUtils; +import org.apache.beam.sdk.schemas.utils.AvroUtils; +import org.apache.beam.sdk.schemas.utils.JsonUtils; +import org.apache.beam.sdk.transforms.SerializableFunction; +import org.apache.beam.sdk.values.KV; +import org.apache.beam.sdk.values.Row; +import org.checkerframework.checker.nullness.qual.Nullable; + +/** A utility class for working with Google Cloud Pub/Sub topics and subscriptions. */ +public class PubsubUtils { + + /** + * Retrieves the schema and data conversion function for a Pub/Sub topic. + * + * @param topicName the name of the Pub/Sub topic + * @param subscriptionName the name of the subscription to the topic (optional) + * @return a {@link KV} object containing the schema and data conversion function + * @throws IOException if an error occurs while retrieving the schema from Pub/Sub + * @throws IllegalArgumentException if the schema or encoding is not supported + */ + public static KV<org.apache.beam.sdk.schemas.Schema, SerializableFunction<byte[], Row>> Review Comment: This method has several branches. Can we be sure to test each of them? -- 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]
