Java Code Examples for com.google.protobuf.UnknownFieldSet#Field

The following examples show how to use com.google.protobuf.UnknownFieldSet#Field . 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: ProtobufTestUtil.java    From datacollector with Apache License 2.0 6 votes vote down vote up
public static UnknownFieldSet getEmployeeUnknownFields() {
  // add unknown fields
  UnknownFieldSet.Field unknownStringField = UnknownFieldSet.Field.newBuilder()
    .addLengthDelimited(ByteString.copyFromUtf8("Hello San FRancisco!"))
    .build();
  UnknownFieldSet.Field unknownVarIntField = UnknownFieldSet.Field.newBuilder()
    .addVarint(123456789)
    .build();

  UnknownFieldSet employeeUnknownFields = UnknownFieldSet.newBuilder()
    .addField(345, unknownStringField)
    .addField(456, unknownVarIntField)
    .build();

  return employeeUnknownFields;
}
 
Example 2
Source File: FieldNumberTree.java    From curiostack with MIT License 6 votes vote down vote up
private static FieldNumberTree fromUnknownFieldSet(UnknownFieldSet unknownFieldSet) {
  FieldNumberTree tree = new FieldNumberTree();
  for (int fieldNumber : unknownFieldSet.asMap().keySet()) {
    UnknownFieldSet.Field unknownField = unknownFieldSet.asMap().get(fieldNumber);
    for (UnknownFieldDescriptor unknownFieldDescriptor :
        UnknownFieldDescriptor.descriptors(fieldNumber, unknownField)) {
      Key key = Key.unknown(unknownFieldDescriptor);
      FieldNumberTree childTree = new FieldNumberTree();
      tree.children.put(key, childTree);

      if (unknownFieldDescriptor.type() == UnknownFieldDescriptor.Type.GROUP) {
        for (Object group : unknownFieldDescriptor.type().getValues(unknownField)) {
          childTree.merge(fromUnknownFieldSet((UnknownFieldSet) group));
        }
      }
    }
  }

  return tree;
}
 
Example 3
Source File: ProtoLanguageFileWriter.java    From metastore with Apache License 2.0 5 votes vote down vote up
private Object convertUnknownFieldValue(
    Descriptors.FieldDescriptor fieldDescriptor, UnknownFieldSet.Field field) {

  if (field.getLengthDelimitedList().size() > 0) {
    if (field.getLengthDelimitedList().size() > 1) {
      throw new RuntimeException(
          "Single value should not contrain more then 1 value in the unknown field");
    }
    return convertFieldValue(fieldDescriptor, field.getLengthDelimitedList().get(0));
  }
  if (field.getFixed32List().size() > 0) {
    if (field.getFixed32List().size() > 1) {
      throw new RuntimeException(
          "Single value should not contrain more then 1 value in the unknown field");
    }
    return convertFieldValue(fieldDescriptor, field.getFixed32List().get(0));
  }
  if (field.getFixed64List().size() > 0) {
    if (field.getFixed64List().size() > 1) {
      throw new RuntimeException(
          "Single value should not contrain more then 1 value in the unknown field");
    }
    return convertFieldValue(fieldDescriptor, field.getFixed64List().get(0));
  }
  if (field.getVarintList().size() > 0) {
    if (field.getVarintList().size() > 1) {
      throw new RuntimeException(
          "Single value should not contrain more then 1 value in the unknown field");
    }
    return convertFieldValue(fieldDescriptor, field.getVarintList().get(0));
  }
  if (field.getGroupList().size() > 0) {
    throw new RuntimeException("Groups are not implemented");
  }
  return null;
}
 
Example 4
Source File: UnknownFieldDescriptor.java    From curiostack with MIT License 5 votes vote down vote up
static ImmutableList<UnknownFieldDescriptor> descriptors(
    int fieldNumber, UnknownFieldSet.Field field) {
  ImmutableList.Builder<UnknownFieldDescriptor> builder = ImmutableList.builder();
  for (Type type : Type.all()) {
    if (!type.getValues(field).isEmpty()) {
      builder.add(create(fieldNumber, type));
    }
  }
  return builder.build();
}
 
Example 5
Source File: ProtobufTestUtil.java    From datacollector with Apache License 2.0 5 votes vote down vote up
public static UnknownFieldSet getPersonUnknownFields() throws IOException {
  // add unknown fields
  UnknownFieldSet.Field unknownIntField = UnknownFieldSet.Field.newBuilder()
    .addFixed32(1234)
    .build();
  UnknownFieldSet.Field unknownLongField = UnknownFieldSet.Field.newBuilder()
    .addFixed64(12345678)
    .build();
  UnknownFieldSet unknownFieldSet = UnknownFieldSet.newBuilder()
    .addField(123, unknownIntField)
    .addField(234, unknownLongField)
    .build();

  return unknownFieldSet;
}
 
Example 6
Source File: DynamicMessageRecordSerializer.java    From fdb-record-layer with Apache License 2.0 5 votes vote down vote up
@Nonnull
protected DynamicMessage deserializeUnion(@Nonnull final Descriptors.Descriptor unionDescriptor,
                                          @Nonnull final Tuple primaryKey,
                                          @Nonnull final byte[] serialized,
                                          int metaDataVersion) {
    final DynamicMessage unionMessage = deserializeFromBytes(unionDescriptor, serialized);
    final Map<Descriptors.FieldDescriptor, Object> allFields = unionMessage.getAllFields();
    final Map<Integer, UnknownFieldSet.Field> unknownFields = unionMessage.getUnknownFields().asMap();
    if (!(allFields.size() == 1 && unknownFields.isEmpty())) {
        final String detailedMessage;
        if (!unknownFields.isEmpty()) {
            detailedMessage = " because there are unknown fields";
        } else if (allFields.size() > 1) {
            detailedMessage = " because there are extra known fields";
        } else {
            detailedMessage = " because there are no fields";
        }
        final String message = "Could not deserialize union message" + detailedMessage;
        final RecordSerializationException ex = new RecordSerializationException(message)
                .addLogInfo("unknownFields", unknownFields.keySet())
                .addLogInfo("fields", getFieldNames(allFields.keySet()))
                .addLogInfo("primaryKey", primaryKey)
                .addLogInfo("metaDataVersion", metaDataVersion);
        throw ex;
    }
    return unionMessage;
}
 
Example 7
Source File: ProtoDiff.java    From metastore with Apache License 2.0 5 votes vote down vote up
private ByteString serializeUnknownField(int optionNumber, UnknownFieldSet.Field field) {
  ByteBuffer byteBuffer = ByteBuffer.allocate(field.getSerializedSize(optionNumber));
  CodedOutputStream stream = CodedOutputStream.newInstance(byteBuffer);
  try {
    field.writeTo(optionNumber, stream);
  } catch (IOException e) {
    throw new RuntimeException(
        "failed to serialize unknown field with number " + optionNumber, e);
  }
  return ByteString.copyFrom(byteBuffer);
}
 
Example 8
Source File: ProtoLanguageFileWriter.java    From metastore with Apache License 2.0 5 votes vote down vote up
/**
 * https://developers.google.com/protocol-buffers/docs/encoding#structure
 *
 * @param fieldDescriptor
 * @param field
 * @return
 */
private Object convertUnknownFieldList(
    Descriptors.FieldDescriptor fieldDescriptor, UnknownFieldSet.Field field) {
  List list = new ArrayList();
  if (field.getLengthDelimitedList().size() > 0) {
    field
        .getLengthDelimitedList()
        .forEach(value -> list.add(convertFieldValue(fieldDescriptor, value)));
  }
  if (field.getFixed32List().size() > 0) {
    field
        .getFixed32List()
        .forEach(value -> list.add(convertFieldValue(fieldDescriptor, value)));
  }
  if (field.getFixed64List().size() > 0) {
    field
        .getFixed64List()
        .forEach(value -> list.add(convertFieldValue(fieldDescriptor, value)));
  }
  if (field.getVarintList().size() > 0) {
    field.getVarintList().forEach(value -> list.add(convertFieldValue(fieldDescriptor, value)));
  }
  if (field.getGroupList().size() > 0) {
    throw new RuntimeException("Groups are not implemented");
  }
  return list;
}
 
Example 9
Source File: MessageDifferencer.java    From startup-os with Apache License 2.0 4 votes vote down vote up
@Override
public List<?> getValues(UnknownFieldSet.Field field) {
  return field.getFixed32List();
}
 
Example 10
Source File: MessageDifferencer.java    From startup-os with Apache License 2.0 4 votes vote down vote up
@Override
public List<?> getValues(UnknownFieldSet.Field field) {
  return field.getFixed64List();
}
 
Example 11
Source File: MessageDifferencer.java    From startup-os with Apache License 2.0 4 votes vote down vote up
@Override
public List<?> getValues(UnknownFieldSet.Field field) {
  return field.getLengthDelimitedList();
}
 
Example 12
Source File: MessageDifferencer.java    From startup-os with Apache License 2.0 4 votes vote down vote up
@Override
public List<?> getValues(UnknownFieldSet.Field field) {
  return field.getGroupList();
}
 
Example 13
Source File: MessageDifferencer.java    From startup-os with Apache License 2.0 4 votes vote down vote up
@Override
public List<?> getValues(UnknownFieldSet.Field field) {
  return field.getVarintList();
}
 
Example 14
Source File: ProtoTruthMessageDifferencer.java    From curiostack with MIT License 4 votes vote down vote up
private UnknownFieldSetDiff diffUnknowns(
    UnknownFieldSet actual, UnknownFieldSet expected, FluentEqualityConfig config) {
  UnknownFieldSetDiff.Builder builder = UnknownFieldSetDiff.newBuilder();

  Map<Integer, UnknownFieldSet.Field> actualFields = actual.asMap();
  Map<Integer, UnknownFieldSet.Field> expectedFields = expected.asMap();
  for (int fieldNumber : Sets.union(actualFields.keySet(), expectedFields.keySet())) {
    @NullableDecl UnknownFieldSet.Field actualField = actualFields.get(fieldNumber);
    @NullableDecl UnknownFieldSet.Field expectedField = expectedFields.get(fieldNumber);
    for (UnknownFieldDescriptor.Type type : UnknownFieldDescriptor.Type.all()) {
      List<?> actualValues =
          actualField != null ? type.getValues(actualField) : Collections.emptyList();
      List<?> expectedValues =
          expectedField != null ? type.getValues(expectedField) : Collections.emptyList();
      if (actualValues.isEmpty() && expectedValues.isEmpty()) {
        continue;
      }

      UnknownFieldDescriptor unknownFieldDescriptor =
          UnknownFieldDescriptor.create(fieldNumber, type);
      FieldDescriptorOrUnknown fieldDescriptorOrUnknown =
          FieldDescriptorOrUnknown.fromUnknown(unknownFieldDescriptor);
      FieldScopeResult compareFields =
          config.compareFieldsScope().policyFor(rootDescriptor, fieldDescriptorOrUnknown);
      if (compareFields == FieldScopeResult.EXCLUDED_RECURSIVELY) {
        builder.addSingularField(
            fieldNumber, SingularField.ignored(name(unknownFieldDescriptor)));
        continue;
      }

      builder.addAllSingularFields(
          fieldNumber,
          compareUnknownFieldList(
              actualValues,
              expectedValues,
              compareFields == FieldScopeResult.EXCLUDED_NONRECURSIVELY,
              unknownFieldDescriptor,
              config.subScope(rootDescriptor, fieldDescriptorOrUnknown)));
    }
  }

  return builder.build();
}
 
Example 15
Source File: UnknownFieldDescriptor.java    From curiostack with MIT License 4 votes vote down vote up
@Override
public List<?> getValues(UnknownFieldSet.Field field) {
  return field.getVarintList();
}
 
Example 16
Source File: UnknownFieldDescriptor.java    From curiostack with MIT License 4 votes vote down vote up
@Override
public List<?> getValues(UnknownFieldSet.Field field) {
  return field.getFixed32List();
}
 
Example 17
Source File: UnknownFieldDescriptor.java    From curiostack with MIT License 4 votes vote down vote up
@Override
public List<?> getValues(UnknownFieldSet.Field field) {
  return field.getFixed64List();
}
 
Example 18
Source File: UnknownFieldDescriptor.java    From curiostack with MIT License 4 votes vote down vote up
@Override
public List<?> getValues(UnknownFieldSet.Field field) {
  return field.getLengthDelimitedList();
}
 
Example 19
Source File: UnknownFieldDescriptor.java    From curiostack with MIT License 4 votes vote down vote up
@Override
public List<?> getValues(UnknownFieldSet.Field field) {
  return field.getGroupList();
}
 
Example 20
Source File: UnknownFieldDescriptor.java    From curiostack with MIT License 4 votes vote down vote up
/** Returns the corresponding values from the given field. */
abstract List<?> getValues(UnknownFieldSet.Field field);