use of com.google.protobuf.ExtensionRegistry 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.ExtensionRegistry in project retrofit by square.
the class ProtoConverterFactoryTest method setUp.
@Before
public void setUp() {
Retrofit retrofit = new Retrofit.Builder().baseUrl(server.url("/")).addConverterFactory(ProtoConverterFactory.create()).build();
service = retrofit.create(Service.class);
ExtensionRegistry registry = ExtensionRegistry.newInstance();
PhoneProtos.registerAllExtensions(registry);
Retrofit retrofitWithRegistry = new Retrofit.Builder().baseUrl(server.url("/")).addConverterFactory(ProtoConverterFactory.createWithRegistry(registry)).build();
serviceWithRegistry = retrofitWithRegistry.create(ServiceWithRegistry.class);
}
use of com.google.protobuf.ExtensionRegistry 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.ExtensionRegistry in project j2objc by google.
the class CompatibilityTest method testEmptyFile.
public void testEmptyFile() throws Exception {
ExtensionRegistry registry = ExtensionRegistry.newInstance();
// Should be a noop, test that this compiles.
EmptyFile.registerAllExtensions(registry);
}
use of com.google.protobuf.ExtensionRegistry in project j2objc by google.
the class CompatibilityTest method testMergeAndParseDelimitedFromInputStream.
public void testMergeAndParseDelimitedFromInputStream() throws Exception {
ExtensionRegistry registry = ExtensionRegistry.newInstance();
registry.add(Typical.myPrimitiveExtension);
byte[] rawData = asBytes(new int[] { 0x0C, 0x08, 0x06, 0x60, 0x01, 0x7A, 0x03, 0x62, 0x61, 0x72, 0xC8, 0x3E, 0x2D, 0x0C, 0x08, 0x06, 0x60, 0x01, 0x7A, 0x03, 0x62, 0x61, 0x72, 0xC8, 0x3E, 0x2D });
ByteArrayInputStream in = new ByteArrayInputStream(rawData);
TypicalData.Builder dataBuilder = TypicalData.newBuilder();
assertTrue(dataBuilder.mergeDelimitedFrom(in, registry));
checkMergeAndParse(dataBuilder.build(), true);
// Test that the second message reads correctly.
dataBuilder = TypicalData.newBuilder();
assertTrue(dataBuilder.mergeDelimitedFrom(in, registry));
checkMergeAndParse(dataBuilder.build(), true);
// Test the parseDelimitedFrom API.
in = new ByteArrayInputStream(rawData);
checkMergeAndParse(TypicalData.parseDelimitedFrom(in, registry), true);
// Test that the second message reads correctly.
checkMergeAndParse(TypicalData.parseDelimitedFrom(in, registry), true);
// test API without ExtensionRegistry
dataBuilder = TypicalData.newBuilder();
assertTrue(dataBuilder.mergeDelimitedFrom(new ByteArrayInputStream(rawData)));
checkMergeAndParse(dataBuilder.build(), false);
checkMergeAndParse(TypicalData.parseDelimitedFrom(new ByteArrayInputStream(rawData)), false);
}
Aggregations