kennknowles commented on code in PR #35150: URL: https://github.com/apache/beam/pull/35150#discussion_r2356127553
########## sdks/java/extensions/protobuf/src/main/java/org/apache/beam/sdk/extensions/protobuf/ProtoBeamConverter.java: ########## @@ -0,0 +1,588 @@ +/* + * 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.extensions.protobuf; + +import com.google.protobuf.ByteString; +import com.google.protobuf.Descriptors; +import com.google.protobuf.DynamicMessage; +import com.google.protobuf.Message; +import com.google.protobuf.Timestamp; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.time.Duration; +import java.time.Instant; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; +import org.apache.beam.sdk.schemas.Schema; +import org.apache.beam.sdk.schemas.logicaltypes.EnumerationType; +import org.apache.beam.sdk.schemas.logicaltypes.NanosDuration; +import org.apache.beam.sdk.schemas.logicaltypes.NanosInstant; +import org.apache.beam.sdk.schemas.logicaltypes.OneOfType; +import org.apache.beam.sdk.transforms.SerializableFunction; +import org.apache.beam.sdk.values.Row; +import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.base.Preconditions; +import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.collect.ImmutableList; +import org.checkerframework.checker.initialization.qual.UnknownInitialization; +import org.checkerframework.checker.nullness.qual.EnsuresNonNull; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; + +/** + * Provides converts between Protobuf Message and Beam Row. + * + * <p>Read <a href="https://s.apache.org/beam-protobuf">https://s.apache.org/beam-protobuf</a> + */ +public class ProtoBeamConverter { + + /** Returns a conversion method from Beam Row to Protobuf Message. */ + public static SerializableFunction<Row, Message> toProto(Descriptors.Descriptor descriptor) { + return new ToProto(descriptor); + } + + /** Returns a conversion method from Protobuf Message to Beam Row. */ + public static SerializableFunction<Message, Row> toRow(Schema schema) { + return new FromProto(schema); + } + + static ProtoToBeamConverter<Object, Object> createProtoToBeamConverter( + Schema.FieldType fieldType) { + switch (fieldType.getTypeName()) { + case INT32: + case INT64: + case FLOAT: + case DOUBLE: + case STRING: + case BOOLEAN: + return createWrappableProtoToBeamConverter(ProtoToBeamConverter.identity()); + case BYTES: + return createWrappableProtoToBeamConverter(ByteString::toByteArray); + case ARRAY: + case ITERABLE: + ProtoToBeamConverter<Object, Object> elementConverter = + createProtoToBeamConverter( + Preconditions.checkNotNull(fieldType.getCollectionElementType())); + return proto -> + ((List<Object>) proto) + .stream() + .map(element -> Preconditions.checkNotNull(elementConverter.convert(element))) Review Comment: In general, the SDK's `checkArgumentNotNull` is preferred, because it throws `IllegalArgumentException` so that it clearly blames the caller, as it should in this case. Using Guava's `checkNotNull` causes it to the NPE which a user will correctly interpret as equivalent to a segfault in terms of the coding that led to it. You don't have to fix it in this PR but it is a useful followup to just avoid Guava's `checkNotNull` entirely. I should make a checkstyle rule... -- 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: github-unsubscr...@beam.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org