tpalfy commented on code in PR #8250:
URL: https://github.com/apache/nifi/pull/8250#discussion_r1491461479


##########
nifi-nar-bundles/nifi-protobuf-bundle/nifi-protobuf-services/src/main/java/org/apache/nifi/services/protobuf/converter/ProtobufDataConverter.java:
##########
@@ -0,0 +1,380 @@
+/*
+ * 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.nifi.services.protobuf.converter;
+
+import com.google.protobuf.ByteString;
+import com.google.protobuf.CodedInputStream;
+import com.google.protobuf.InvalidProtocolBufferException;
+import com.google.protobuf.UnknownFieldSet;
+import com.squareup.wire.schema.EnumType;
+import com.squareup.wire.schema.Field;
+import com.squareup.wire.schema.MessageType;
+import com.squareup.wire.schema.OneOf;
+import com.squareup.wire.schema.ProtoType;
+import com.squareup.wire.schema.Schema;
+import org.apache.nifi.serialization.record.DataType;
+import org.apache.nifi.serialization.record.MapRecord;
+import org.apache.nifi.serialization.record.RecordField;
+import org.apache.nifi.serialization.record.RecordSchema;
+import org.apache.nifi.serialization.record.type.RecordDataType;
+import org.apache.nifi.serialization.record.util.DataTypeUtils;
+import org.apache.nifi.services.protobuf.FieldType;
+import org.apache.nifi.services.protobuf.schema.ProtoSchemaParser;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.math.BigInteger;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.function.Function;
+
+import static com.google.protobuf.CodedInputStream.decodeZigZag32;
+import static com.google.protobuf.TextFormat.unsignedToString;
+
+/**
+ * The class is responsible for creating Record by mapping the provided proto 
schema fields with the list of Unknown fields parsed from encoded proto data.
+ */
+public class ProtobufDataConverter {
+
+    public static final String MAP_KEY_FIELD_NAME = "key";
+    public static final String MAP_VALUE_FIELD_NAME = "value";
+    public static final String ANY_TYPE_URL_FIELD_NAME = "type_url";
+    public static final String ANY_VALUE_FIELD_NAME = "value";
+    public static final String ANY_MESSAGE_TYPE = "google.protobuf.Any";
+
+    private final Schema schema;
+    private final String message;
+    private final RecordSchema rootRecordSchema;
+    private final boolean coerceTypes;
+    private final boolean dropUnknownFields;
+
+    private boolean containsAnyField = false;
+
+    public ProtobufDataConverter(Schema schema, String message, RecordSchema 
recordSchema, boolean coerceTypes, boolean dropUnknownFields) {
+        this.schema = schema;
+        this.message = message;
+        this.rootRecordSchema = recordSchema;
+        this.coerceTypes = coerceTypes;
+        this.dropUnknownFields = dropUnknownFields;
+    }
+
+    /**
+     * Creates a record from the root message.
+     *
+     * @return created record
+     * @throws IOException failed to read input stream
+     */
+    public MapRecord createRecord(InputStream data) throws IOException {
+        final MessageType rootType = (MessageType) schema.getType(message);
+        Objects.requireNonNull(rootType, String.format("Message with name [%s] 
not found in the provided proto files", message));
+
+        MapRecord record = createRecord(rootType, ByteString.readFrom(data), 
rootRecordSchema);
+        if (containsAnyField) {
+            record.regenerateSchema();
+        }
+
+        return record;
+    }
+
+    /**
+     * Creates a record for the provided message.
+     *
+     * @param messageType  message to create a record from
+     * @param data         proto message data
+     * @param recordSchema record schema for the created record
+     * @return created record
+     * @throws InvalidProtocolBufferException failed to parse input data
+     */
+    private MapRecord createRecord(MessageType messageType, ByteString data, 
RecordSchema recordSchema) throws InvalidProtocolBufferException {
+        final UnknownFieldSet unknownFieldSet = 
UnknownFieldSet.parseFrom(data);
+
+        if ((ANY_MESSAGE_TYPE).equals(messageType.getType().toString())) {
+            containsAnyField = true;
+            return handleAnyField(unknownFieldSet);
+        }
+
+        return new MapRecord(recordSchema, processMessageFields(messageType, 
unknownFieldSet), false, dropUnknownFields);

Review Comment:
   Better to not have a single expression do 2 things that could be considered 
business logic.
   
   ```suggestion
           final Map<String, Object> fieldValues = 
processMessageFields(messageType, unknownFieldSet);
           return new MapRecord(recordSchema, fieldValues, false, 
dropUnknownFields);
   ```



-- 
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: issues-unsubscr...@nifi.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to