prankstrisse commented on code in PR #11499:
URL: https://github.com/apache/nifi/pull/11499#discussion_r4122672272


##########
nifi-extension-bundles/nifi-protobuf-bundle/nifi-protobuf-services/src/main/java/org/apache/nifi/services/protobuf/converter/ProtobufDataSerializer.java:
##########
@@ -0,0 +1,308 @@
+/*
+ * 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.CodedOutputStream;
+import com.squareup.wire.schema.EnumConstant;
+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.Record;
+import org.apache.nifi.serialization.record.util.DataTypeUtils;
+import org.apache.nifi.services.protobuf.FieldType;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.math.BigInteger;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+
+/**
+ * Serializes a NiFi {@link Record} into Protocol Buffers binary payload using 
a Square Wire
+ * {@link Schema}. This is the write-side inverse of {@code 
ProtobufDataConverter}: it walks the
+ * declared fields of the target message type and encodes each present Record 
value to a
+ * {@link CodedOutputStream} following the Protocol Buffers wire format.
+ * <p>
+ * This class has no dependency on the NiFi framework and can be exercised 
directly with plain
+ * unit tests.
+ */
+public class ProtobufDataSerializer {
+
+    private static final int WIRETYPE_VARINT = 0;
+    private static final int WIRETYPE_FIXED64 = 1;
+    private static final int WIRETYPE_LENGTH_DELIMITED = 2;
+    private static final int WIRETYPE_FIXED32 = 5;
+
+    private static final int MAP_KEY_TAG = 1;
+    private static final int MAP_VALUE_TAG = 2;
+
+    private final Schema schema;
+    private final String rootMessageType;
+
+    public ProtobufDataSerializer(final Schema schema, final String 
rootMessageType) {
+        this.schema = schema;
+        this.rootMessageType = rootMessageType;
+    }
+
+    /**
+     * Serializes the provided Record into Protocol Buffers binary format for 
the configured root
+     * message type.
+     *
+     * @param record the record to serialize
+     * @return the serialized protobuf payload
+     * @throws IOException if the record cannot be encoded
+     */
+    public byte[] serialize(final Record record) throws IOException {
+        final MessageType messageType = (MessageType) 
schema.getType(rootMessageType);
+        Objects.requireNonNull(messageType, String.format("Message with name 
[%s] not found in the provided proto files", rootMessageType));
+
+        return serializeMessage(messageType, record);
+    }
+
+    private byte[] serializeMessage(final MessageType messageType, final 
Record record) throws IOException {
+        final ByteArrayOutputStream output = new ByteArrayOutputStream();
+        final CodedOutputStream codedOutput = 
CodedOutputStream.newInstance(output);
+
+        for (final Field field : messageType.getDeclaredFields()) {
+            writeField(codedOutput, field, record.getValue(field.getName()));
+        }
+        for (final Field field : messageType.getExtensionFields()) {
+            writeField(codedOutput, field, record.getValue(field.getName()));
+        }
+        for (final OneOf oneOf : messageType.getOneOfs()) {
+            for (final Field field : oneOf.getFields()) {
+                writeField(codedOutput, field, 
record.getValue(field.getName()));
+            }
+        }
+
+        codedOutput.flush();
+        return output.toByteArray();
+    }
+
+    private void writeField(final CodedOutputStream output, final Field field, 
final Object value) throws IOException {
+        if (value == null) {
+            // proto2 parsers reject messages without required fields, so fail 
here instead of producing unreadable content
+            if (field.isRequired()) {
+                throw new IOException(String.format("Required field [%s] has 
no value", field.getName()));
+            }
+            return;
+        }
+
+        final int tag = field.getTag();
+        final ProtoType protoType = field.getType();
+
+        if (protoType.isMap()) {
+            writeMap(output, tag, protoType, value);
+        } else if (field.isRepeated()) {
+            writeRepeated(output, tag, protoType, value);
+        } else {
+            writeSingleValue(output, tag, protoType, value);
+        }
+    }
+
+    private void writeRepeated(final CodedOutputStream output, final int tag, 
final ProtoType protoType, final Object value) throws IOException {
+        final Object[] values = toArray(value);
+
+        if (isPackable(protoType)) {

Review Comment:
   Yes, done. Repeated fields now follow the schema: proto2 is unpacked unless 
[packed = true], and proto3 is packed unless [packed = false]. I used 
Field.isPacked(), with one addition: Wire applies the proto3 packed default 
only to scalar fields, not to enums, so proto3 repeated enums without an 
explicit option are handled separately



-- 
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