Java Code Examples for com.google.protobuf.UnknownFieldSet#newBuilder()

The following examples show how to use com.google.protobuf.UnknownFieldSet#newBuilder() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ShadowApply.java    From metastore with Apache License 2.0 6 votes vote down vote up
private UnknownFieldSet buildUnknownFieldSet(List<OptionChangeInfo> optionChanges) {
  UnknownFieldSet.Builder unknownFieldSetBuilder = UnknownFieldSet.newBuilder();
  for (OptionChangeInfo optionChange : optionChanges) {
    switch (optionChange.getChangeType()) {
      case ADDITION:
        unknownFieldSetBuilder =
            mergeUnknownField(unknownFieldSetBuilder, optionChange.getPayloadNew());
        break;
      case REMOVAL:
        unknownFieldSetBuilder.clearField(optionChange.getOptionNumber());
        break;
      case PAYLOAD_CHANGED:
        unknownFieldSetBuilder.clearField(optionChange.getOptionNumber());
        unknownFieldSetBuilder =
            mergeUnknownField(unknownFieldSetBuilder, optionChange.getPayloadNew());
        break;
    }
  }
  return unknownFieldSetBuilder.build();
}
 
Example 2
Source File: ProtobufTypeUtil.java    From datacollector with Apache License 2.0 6 votes vote down vote up
private static void handleUnknownFields(
    Record record,
    String fieldPath,
    DynamicMessage.Builder builder
) throws IOException {
  String path = fieldPath.isEmpty() ? FORWARD_SLASH : fieldPath;
  String attribute = record.getHeader().getAttribute(ProtobufTypeUtil.PROTOBUF_UNKNOWN_FIELDS_PREFIX + path);
  if (attribute != null) {
    UnknownFieldSet.Builder unknownFieldBuilder = UnknownFieldSet.newBuilder();
    unknownFieldBuilder.mergeDelimitedFrom(
        new ByteArrayInputStream(
            org.apache.commons.codec.binary.Base64.decodeBase64(attribute.getBytes(StandardCharsets.UTF_8))
        )
    );
    UnknownFieldSet unknownFieldSet = unknownFieldBuilder.build();
    builder.setUnknownFields(unknownFieldSet);
  }
}
 
Example 3
Source File: ProtobufTestUtil.java    From datacollector with Apache License 2.0 6 votes vote down vote up
public static void checkRecordForUnknownFields(Record record, int i) throws IOException {
  // unknown fields are expected in paths for person and employee
  String attribute = record.getHeader().getAttribute(ProtobufTypeUtil.PROTOBUF_UNKNOWN_FIELDS_PREFIX + "/");
  UnknownFieldSet.Builder builder = UnknownFieldSet.newBuilder();
  builder.mergeDelimitedFrom(new ByteArrayInputStream(org.apache.commons.codec.binary.Base64.decodeBase64(attribute.getBytes())));
  UnknownFieldSet unknownFieldSet = builder.build();

  UnknownFieldsUtil.checkEmployeeUnknownFields(unknownFieldSet);

  if(i%2 == 0) {
    attribute = record.getHeader().getAttribute(ProtobufTypeUtil.PROTOBUF_UNKNOWN_FIELDS_PREFIX + "/engineer/person");
  } else {
    attribute = record.getHeader().getAttribute(ProtobufTypeUtil.PROTOBUF_UNKNOWN_FIELDS_PREFIX + "/exec/person");
  }
  builder = UnknownFieldSet.newBuilder();
  builder.mergeDelimitedFrom(new ByteArrayInputStream(org.apache.commons.codec.binary.Base64.decodeBase64(attribute.getBytes())));
  unknownFieldSet = builder.build();

  UnknownFieldsUtil.checkPersonUnknownFields(unknownFieldSet);
}
 
Example 4
Source File: State.java    From zetasketch with Apache License 2.0 5 votes vote down vote up
/**
 * Parses a serialized HyperLogLog++ {@link AggregatorStateProto} and populates this object's
 * fields. If the {@code input} supports aliasing (for byte arrays and {@link ByteBuffer}, see
 * {@link CodedInputStream#enableAliasing(boolean) for details}), {@link #data} and {@link
 * #sparseData} will <em>alias</em> the given bytes &mdash; sharing the same memory.
 *
 * @throws IOException If the stream does not contain a serialized {@link AggregatorStateProto} or
 *     if fields are set that would typically not belong
 */
public void parse(CodedInputStream input) throws IOException {
  // Reset defaults as values set to the default will not be encoded in the protocol buffer.
  clear();

  UnknownFieldSet.Builder ignoredFields = UnknownFieldSet.newBuilder();
  while (!input.isAtEnd()) {
    int tag = input.readTag();
    switch (tag) {
      case TYPE_TAG:
        type = AggregatorType.forNumber(input.readEnum());
        break;
      case NUM_VALUES_TAG:
        numValues = input.readInt64();
        break;
      case ENCODING_VERSION_TAG:
        encodingVersion = input.readInt32();
        break;
      case VALUE_TYPE_TAG:
        valueType = ValueType.forNumber(input.readEnum());
        break;
      case HYPERLOGLOGPLUS_UNIQUE_STATE_TAG:
        parseHll(input, input.readInt32());
        break;
      default:
        ignoredFields.mergeFieldFrom(tag, input);
    }
  }
}
 
Example 5
Source File: State.java    From zetasketch with Apache License 2.0 5 votes vote down vote up
/**
 * Parses a {@link HyperLogLogPlusUniqueStateProto} message. Since the message is nested within an
 * {@link AggregatorStateProto}, we limit ourselves to reading only the bytes of the specified
 * message length.
 */
private void parseHll(CodedInputStream input, int size) throws IOException {
  int limit = input.getTotalBytesRead() + size;

  ByteBuffer buffer;
  UnknownFieldSet.Builder ignoredFields = UnknownFieldSet.newBuilder();
  while (input.getTotalBytesRead() < limit && !input.isAtEnd()) {
    int tag = input.readTag();
    switch (tag) {
      case SPARSE_SIZE_TAG:
        sparseSize = input.readInt32();
        break;
      case PRECISION_OR_NUM_BUCKETS_TAG:
        precision = input.readInt32();
        break;
      case SPARSE_PRECISION_OR_NUM_BUCKETS_TAG:
        sparsePrecision = input.readInt32();
        break;
      case DATA_TAG:
        buffer = input.readByteBuffer();
        data = ByteSlice.copyOnWrite(buffer);
        break;
      case SPARSE_DATA_TAG:
        buffer = input.readByteBuffer();
        sparseData = GrowingByteSlice.copyOnWrite(buffer);
        break;
      default:
        ignoredFields.mergeFieldFrom(tag, input);
    }
  }
}
 
Example 6
Source File: JsonJacksonFormat.java    From jigsaw-payment with Apache License 2.0 4 votes vote down vote up
/**
 * Parse a single field from {@code parser} and merge it into {@code builder}. If a ',' is
 * detected after the field ends, the next field will be parsed automatically
 * @throws IOException
 * @throws JsonParseException
 */
protected void mergeField(JsonParser parser,
                               ExtensionRegistry extensionRegistry,
                               Message.Builder builder) throws JsonParseException, IOException {
    FieldDescriptor field = null;
    Descriptor type = builder.getDescriptorForType();
    boolean unknown = false;
    ExtensionRegistry.ExtensionInfo extension = null;
    JsonToken token = parser.getCurrentToken();

    if (token != null) {
        String name = parser.getCurrentName();

        if (name.contains(".")) {
        	// should be an extension
        	extension = extensionRegistry.findExtensionByName(name);
            if (extension == null) {
                throw new RuntimeException("Extension \""
                		+ name + "\" not found in the ExtensionRegistry.");
            } else if (extension.descriptor.getContainingType() != type) {
                throw new RuntimeException("Extension \"" + name
                		+ "\" does not extend message type \""
                		+ type.getFullName() + "\".");
            }

        	field = extension.descriptor;
        } else {
        	field = type.findFieldByName(name);
        }

        // Group names are expected to be capitalized as they appear in the
        // .proto file, which actually matches their type names, not their field
        // names.
        if (field == null) {
            // Explicitly specify US locale so that this code does not break when
            // executing in Turkey.
            String lowerName = name.toLowerCase(Locale.US);
            field = type.findFieldByName(lowerName);
            // If the case-insensitive match worked but the field is NOT a group,
            if ((field != null) && (field.getType() != FieldDescriptor.Type.GROUP)) {
                field = null;
            }
        }
        // Again, special-case group names as described above.
        if ((field != null) && (field.getType() == FieldDescriptor.Type.GROUP)
            && !field.getMessageType().getName().equals(name)
            && !field.getMessageType().getFullName().equalsIgnoreCase(name) /* extension */) {
            field = null;
        }

        // Last try to lookup by field-index if 'name' is numeric,
        // which indicates a possible unknown field
        if (field == null && TextUtils.isDigits(name)) {
            field = type.findFieldByNumber(Integer.parseInt(name));
            unknown = true;
        }

        // no throwing exceptions if field not found, since it could be a different version.
        if (field == null) {
        	UnknownFieldSet.Builder unknownsBuilder = UnknownFieldSet.newBuilder();
        	handleMissingField(name, parser, extensionRegistry, unknownsBuilder);
        	builder.setUnknownFields(unknownsBuilder.build());
        }
    }

    if (field != null) {
    	token = parser.nextToken();

        boolean array = token.equals(JsonToken.START_ARRAY);

        if (array) {
        	token = parser.nextToken();
            while (!token.equals(JsonToken.END_ARRAY)) {
                handleValue(parser, extensionRegistry, builder, field, extension, unknown);
                token = parser.nextToken();
            }
        } else {
            handleValue(parser, extensionRegistry, builder, field, extension, unknown);
        }
    }
}