use of org.infinispan.protostream.descriptors.Descriptor in project protostream by infinispan.
the class DescriptorsTest method testArrayAnnotationAttributeNormalizing.
@Test
public void testArrayAnnotationAttributeNormalizing() {
Configuration config = Configuration.builder().annotationsConfig().annotation("Xyz", AnnotationElement.AnnotationTarget.MESSAGE).attribute("elem1").type(AnnotationElement.AttributeType.BOOLEAN).defaultValue(true).multiple(true).build();
String testProto = "/** @Xyz(elem1 = true) */\n" + "message M {\n" + " optional int32 field1 = 1; \n" + "}\n";
FileDescriptorSource fileDescriptorSource = FileDescriptorSource.fromString("test.proto", testProto);
Map<String, FileDescriptor> descriptors = parseAndResolve(fileDescriptorSource, config);
FileDescriptor fileDescriptor = descriptors.get("test.proto");
List<Descriptor> messageTypes = fileDescriptor.getMessageTypes();
Descriptor messageType = messageTypes.get(0);
assertEquals("M", messageType.getFullName());
AnnotationElement.Annotation annotation = messageType.getAnnotations().get("Xyz");
assertNotNull(annotation);
AnnotationElement.Value attr = annotation.getAttributeValue("elem1");
assertTrue(attr instanceof AnnotationElement.Array);
assertTrue(attr.getValue() instanceof List);
List values = (List) attr.getValue();
assertEquals(1, values.size());
assertEquals(true, values.get(0));
}
use of org.infinispan.protostream.descriptors.Descriptor in project protostream by infinispan.
the class DescriptorsTest method assertSearchRequestFields.
private void assertSearchRequestFields(List<FieldDescriptor> fields) {
FieldDescriptor queryField = fields.get(0);
assertThat(queryField.getLabel()).isEqualTo(Label.REQUIRED);
assertThat(queryField.getType()).isEqualTo(Type.STRING);
assertThat(queryField.getJavaType()).isEqualTo(JavaType.STRING);
assertThat(queryField.getFullName()).isEqualTo("org.infinispan.protostream.test.SearchRequest.query");
assertThat(queryField.getName()).isEqualTo("query");
assertThat(queryField.getNumber()).isEqualTo(1);
assertThat(queryField.getMessageType()).isNull();
assertThat(queryField.getContainingMessage().getName()).isEqualTo("SearchRequest");
FieldDescriptor pageNumberField = fields.get(1);
assertThat(pageNumberField.getLabel()).isEqualTo(Label.OPTIONAL);
assertThat(pageNumberField.getType()).isEqualTo(Type.INT32);
assertThat(pageNumberField.getJavaType()).isEqualTo(JavaType.INT);
assertThat(pageNumberField.getName()).isEqualTo("page_number");
assertThat(pageNumberField.getNumber()).isEqualTo(2);
assertThat(pageNumberField.hasDefaultValue()).isTrue();
assertThat(pageNumberField.getDefaultValue()).isEqualTo(10);
assertThat(pageNumberField.getMessageType()).isNull();
FieldDescriptor flagField = fields.get(3);
assertThat(flagField.getLabel()).isEqualTo(Label.REPEATED);
assertThat(flagField.getType()).isEqualTo(Type.INT32);
assertThat(flagField.getJavaType()).isEqualTo(JavaType.INT);
assertThat(flagField.getOptionByName("packed")).isEqualTo("true");
FieldDescriptor dntField = fields.get(4);
assertThat(dntField.getLabel()).isEqualTo(Label.OPTIONAL);
assertThat(dntField.getType()).isEqualTo(Type.ENUM);
assertThat(dntField.getEnumType().findValueByName("DONT_CARE").getNumber()).isEqualTo(2);
assertThat(dntField.getEnumType().findValueByNumber(1).getName()).isEqualTo("TRACK_FOR_SURE");
assertThat(dntField.getEnumType().getFileDescriptor()).isNotNull();
assertThat(dntField.getFileDescriptor()).isNotNull();
assertThat(dntField.getJavaType()).isEqualTo(JavaType.ENUM);
FieldDescriptor reqEnumField = fields.get(5);
assertThat(reqEnumField.getLabel()).isEqualTo(Label.REQUIRED);
assertThat(reqEnumField.getType()).isEqualTo(Type.ENUM);
assertThat(reqEnumField.getJavaType()).isEqualTo(JavaType.ENUM);
assertThat(reqEnumField.hasDefaultValue()).isFalse();
assertThat(reqEnumField.getEnumType()).isNotNull();
assertThat(reqEnumField.getEnumType().findValueByNumber(0).getFileDescriptor()).isNotNull();
assertThat(reqEnumField.getEnumType().findValueByNumber(0).getContainingEnum()).isEqualTo(reqEnumField.getEnumType());
assertThat(reqEnumField.getOptionByName("deprecated")).isEqualTo("true");
FieldDescriptor labelField = fields.get(6);
assertThat(labelField.hasDefaultValue()).isTrue();
assertThat(labelField.getDefaultValue()).isEqualTo("whatever");
FieldDescriptor typedField = fields.get(7);
Descriptor messageType = typedField.getMessageType();
assertThat(messageType.getFullName()).isEqualTo("org.infinispan.protostream.test.SearchRequest.Inner");
assertThat(messageType.getContainingType().getName()).isEqualTo("SearchRequest");
assertThat(typedField.getType()).isEqualTo(Type.MESSAGE);
assertThat(typedField.getFullName()).isEqualTo("org.infinispan.protostream.test.SearchRequest.typed");
}
use of org.infinispan.protostream.descriptors.Descriptor in project protostream by infinispan.
the class DescriptorsTest method testMultipleAnnotationAttribute.
@Test
public void testMultipleAnnotationAttribute() {
Configuration config = Configuration.builder().annotationsConfig().annotation("Xyz", AnnotationElement.AnnotationTarget.MESSAGE).attribute("elem1").type(AnnotationElement.AttributeType.BOOLEAN).defaultValue(true).multiple(true).build();
String testProto = "/** @Xyz(elem1 = {true, false, true}) */\n" + "message M {\n" + " optional int32 field1 = 1; \n" + "}\n";
FileDescriptorSource fileDescriptorSource = FileDescriptorSource.fromString("test.proto", testProto);
Map<String, FileDescriptor> descriptors = parseAndResolve(fileDescriptorSource, config);
FileDescriptor fileDescriptor = descriptors.get("test.proto");
List<Descriptor> messageTypes = fileDescriptor.getMessageTypes();
Descriptor messageType = messageTypes.get(0);
assertEquals("M", messageType.getFullName());
AnnotationElement.Annotation annotation = messageType.getAnnotations().get("Xyz");
assertNotNull(annotation);
AnnotationElement.Value attr = annotation.getAttributeValue("elem1");
assertTrue(attr instanceof AnnotationElement.Array);
assertTrue(attr.getValue() instanceof List);
List values = (List) attr.getValue();
assertEquals(3, values.size());
assertEquals(true, values.get(0));
assertEquals(false, values.get(1));
assertEquals(true, values.get(2));
}
use of org.infinispan.protostream.descriptors.Descriptor in project protostream by infinispan.
the class JsonUtils method processObject.
private static void processObject(ImmutableSerializationContext ctx, JsonParser parser, TagWriter writer, Descriptor messageDescriptor, Integer fieldNumber, boolean topLevel) throws IOException {
Set<String> requiredFields = messageDescriptor.getFields().stream().filter(FieldDescriptor::isRequired).map(FieldDescriptor::getName).collect(Collectors.toCollection(HashSet::new));
ByteArrayOutputStream baos = new ByteArrayOutputStream(ProtobufUtil.DEFAULT_ARRAY_BUFFER_SIZE);
TagWriter nestedWriter = TagWriterImpl.newInstanceNoBuffer(ctx, baos);
String currentField = null;
out: while (true) {
JsonToken token = parser.nextToken();
if (token == null) {
break;
}
switch(token) {
case END_OBJECT:
break out;
case START_ARRAY:
processArray(ctx, messageDescriptor.getFullName(), currentField, parser, nestedWriter);
break;
case START_OBJECT:
{
FieldDescriptor fd = messageDescriptor.findFieldByName(currentField);
Descriptor messageType = fd.getMessageType();
if (messageType == null) {
throw new IllegalStateException("Field '" + currentField + "' is not an object");
}
processObject(ctx, parser, nestedWriter, messageType, fd.getNumber(), false);
requiredFields.remove(currentField);
break;
}
case FIELD_NAME:
currentField = parser.getCurrentName();
break;
case VALUE_STRING:
case VALUE_NUMBER_INT:
case VALUE_NUMBER_FLOAT:
case VALUE_TRUE:
case VALUE_FALSE:
{
FieldDescriptor fd = messageDescriptor.findFieldByName(currentField);
if (fd == null) {
throw new IllegalStateException("The field '" + currentField + "' was not found in the Protobuf schema");
}
if (fd.getType() == Type.ENUM) {
writeEnumField(parser, nestedWriter, fd);
} else {
writeField(parser, nestedWriter, fd.getType(), fd.getNumber());
}
requiredFields.remove(currentField);
break;
}
case VALUE_NULL:
// we got null in, we write nothing out
break;
}
}
if (!requiredFields.isEmpty()) {
String missing = requiredFields.iterator().next();
throw new IllegalStateException("Required field '" + missing + "' missing");
}
if (topLevel) {
Integer topLevelTypeId = messageDescriptor.getTypeId();
if (topLevelTypeId == null) {
writer.writeString(WRAPPED_TYPE_NAME, messageDescriptor.getFullName());
} else {
writer.writeUInt32(WRAPPED_TYPE_ID, topLevelTypeId);
}
nestedWriter.flush();
writer.writeBytes(WRAPPED_MESSAGE, baos.toByteArray());
} else {
nestedWriter.flush();
writer.writeBytes(fieldNumber, baos.toByteArray());
}
writer.flush();
}
use of org.infinispan.protostream.descriptors.Descriptor in project protostream by infinispan.
the class ProtoSchemaBuilderTest method testDocumentation.
@Test
public void testDocumentation() throws Exception {
SerializationContext ctx = createContext();
ProtoSchemaBuilder protoSchemaBuilder = new ProtoSchemaBuilder();
String schemaFile = protoSchemaBuilder.fileName("test1.proto").packageName("test_package1").addClass(TestEnum.class).addClass(TestClass.class).build(ctx);
FileDescriptorSource fileDescriptorSource = FileDescriptorSource.fromString("test1.proto", schemaFile);
Map<String, FileDescriptor> fileDescriptors = new SquareProtoParser(ctx.getConfiguration()).parse(fileDescriptorSource);
FileDescriptor fd = fileDescriptors.get("test1.proto");
assertNotNull(fd);
Map<String, EnumDescriptor> enums = new HashMap<>();
for (EnumDescriptor e : fd.getEnumTypes()) {
enums.put(e.getFullName(), e);
}
Map<String, Descriptor> messages = new HashMap<>();
for (Descriptor m : fd.getMessageTypes()) {
messages.put(m.getFullName(), m);
}
EnumDescriptor testEnum = enums.get("test_package1.TestEnumABC");
assertNotNull(testEnum);
assertEquals("bla bla bla\nand some more bla\n@TypeId(100777)", testEnum.getDocumentation());
assertEquals("This should never be read.", testEnum.getValues().get(0).getDocumentation());
Descriptor testClass = messages.get("test_package1.TestClass");
assertNotNull(testClass);
assertEquals("@Indexed()\nbla bla bla\nand some more bla", testClass.getDocumentation());
assertEquals("The surname, of course", testClass.getFields().get(0).getDocumentation());
}
Aggregations