use of com.google.protobuf.Descriptors.FieldDescriptor in project jvm-serializers by eishay.
the class JsonFormat method mergeField.
/**
* Parse a single field from {@code tokenizer} and merge it into {@code builder}. If a ',' is
* detected after the field ends, the next field will be parsed automatically
*/
private static void mergeField(Tokenizer tokenizer, ExtensionRegistry extensionRegistry, Message.Builder builder) throws ParseException {
FieldDescriptor field;
Descriptor type = builder.getDescriptorForType();
ExtensionRegistry.ExtensionInfo extension = null;
if (tokenizer.tryConsume("[")) {
// An extension.
StringBuilder name = new StringBuilder(tokenizer.consumeIdentifier());
while (tokenizer.tryConsume(".")) {
name.append(".");
name.append(tokenizer.consumeIdentifier());
}
extension = extensionRegistry.findExtensionByName(name.toString());
if (extension == null) {
throw tokenizer.parseExceptionPreviousToken("Extension \"" + name + "\" not found in the ExtensionRegistry.");
} else if (extension.descriptor.getContainingType() != type) {
throw tokenizer.parseExceptionPreviousToken("Extension \"" + name + "\" does not extend message type \"" + type.getFullName() + "\".");
}
tokenizer.consume("]");
field = extension.descriptor;
} else {
String name = tokenizer.consumeIdentifier();
field = type.findFieldByName(name);
// 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 = null;
}
if (field == null) {
throw tokenizer.parseExceptionPreviousToken("Message type \"" + type.getFullName() + "\" has no field named \"" + name + "\".");
}
}
tokenizer.consume(":");
boolean array = tokenizer.tryConsume("[");
if (array) {
while (!tokenizer.tryConsume("]")) {
handleValue(tokenizer, extensionRegistry, builder, field, extension);
tokenizer.tryConsume(",");
}
} else {
handleValue(tokenizer, extensionRegistry, builder, field, extension);
}
if (tokenizer.tryConsume(",")) {
// Continue with the next field
mergeField(tokenizer, extensionRegistry, builder);
}
}
use of com.google.protobuf.Descriptors.FieldDescriptor in project j2objc by google.
the class CompatibilityTest method testNewBuilderForField.
public void testNewBuilderForField() throws Exception {
Descriptor descriptor = TypicalData.Builder.getDescriptor();
FieldDescriptor fieldDescriptor = descriptor.findFieldByNumber(11);
TypicalData.Builder dataBuilder = TypicalData.newBuilder();
TypicalDataMessage.Builder messageBuilder = (TypicalDataMessage.Builder) dataBuilder.newBuilderForField(fieldDescriptor);
TypicalDataMessage message = messageBuilder.setMyMessageInt(10).build();
assertEquals(10, message.getMyMessageInt());
fieldDescriptor = descriptor.findFieldByNumber(1);
try {
dataBuilder.newBuilderForField(fieldDescriptor);
fail("Expected UnsupportedOperationException");
} catch (UnsupportedOperationException e) {
// Expected.
}
}
use of com.google.protobuf.Descriptors.FieldDescriptor in project j2objc by google.
the class CompatibilityTest method testExtensionRegistry.
public void testExtensionRegistry() throws Exception {
ExtensionRegistry registry = ExtensionRegistry.newInstance();
Typical.registerAllExtensions(registry);
Descriptor descriptor = TypicalData.Builder.getDescriptor();
FieldDescriptor fieldDescriptor = descriptor.findFieldByNumber(1);
assertFalse(fieldDescriptor.isExtension());
ExtensionRegistry.ExtensionInfo extensionInfo = registry.findExtensionByNumber(descriptor, 1000);
assertNotNull(extensionInfo);
FieldDescriptor extensionFieldDescriptor = extensionInfo.descriptor;
assertNotNull(extensionFieldDescriptor);
assertEquals(1000, extensionFieldDescriptor.getNumber());
assertTrue(extensionFieldDescriptor.isExtension());
Message message = extensionInfo.defaultInstance;
assertTrue(message instanceof TypicalDataMessage);
TypicalDataMessage data = ((TypicalDataMessage.Builder) message.toBuilder()).setMyMessageInt(100).build();
assertEquals(100, data.getMyMessageInt());
// Primitive extension
extensionInfo = registry.findExtensionByNumber(descriptor, 1001);
assertNotNull(extensionInfo);
extensionFieldDescriptor = extensionInfo.descriptor;
assertNotNull(extensionFieldDescriptor);
assertEquals(1001, extensionFieldDescriptor.getNumber());
assertTrue(extensionFieldDescriptor.isExtension());
assertNull(extensionInfo.defaultInstance);
}
use of com.google.protobuf.Descriptors.FieldDescriptor in project j2objc by google.
the class CompatibilityTest method testEmptyFieldOptions.
public void testEmptyFieldOptions() {
Descriptor descriptor = TypicalData.Builder.getDescriptor();
FieldDescriptor intField = descriptor.findFieldByNumber(1);
assertNotNull(intField.getOptions());
}
use of com.google.protobuf.Descriptors.FieldDescriptor in project j2objc by google.
the class CompatibilityTest method testGetAllFields.
public void testGetAllFields() throws Exception {
GeneratedMessage data = TypicalData.newBuilder().setMyInt(1).addRepeatedInt32(2).setExtension(Typical.myExtension, TypicalDataMessage.getDefaultInstance()).setExtension(Typical.myPrimitiveExtension, 3).build();
Map<FieldDescriptor, Object> allFields = data.getAllFields();
assertEquals(4, allFields.size());
assertNotNull(allFields.get(Typical.myExtension.getDescriptor()));
assertEquals(4, data.toBuilder().getAllFields().size());
Descriptor descriptor = TypicalData.Builder.getDescriptor();
FieldDescriptor intField = descriptor.findFieldByNumber(1);
assertEquals(1, allFields.get(intField));
assertEquals(3, allFields.get(Typical.myPrimitiveExtension.getDescriptor()));
}
Aggregations