prankstrisse commented on code in PR #11499: URL: https://github.com/apache/nifi/pull/11499#discussion_r4094443234
########## nifi-extension-bundles/nifi-protobuf-bundle/nifi-protobuf-services/src/main/java/org/apache/nifi/services/protobuf/converter/ProtobufDataSerializer.java: ########## @@ -0,0 +1,300 @@ +/* + * 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())); + } + } + Review Comment: I've seen in the tests for the read part - it handles proto2, so I kept proto2 support in the writer for consistency and made a missing required field fail with an IOException. -- 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]
