Search in sources :

Example 26 with Descriptor

use of org.infinispan.protostream.descriptors.Descriptor in project protostream by infinispan.

the class DescriptorsTest method testDocAnnotations.

@Test
public void testDocAnnotations() {
    String file1 = "package test1;\n" + "/**  \n" + " *  @Foo(fooValue) \n" + "  *    some more doc text \n" + "      **/\n\n" + "message X {\n" + " /**\n" + "  * @Bar(barValue)  \n\n" + "  */\n" + "  optional int32 field1 = 1;\n" + "}\n";
    Configuration config = Configuration.builder().annotationsConfig().annotation("Foo", AnnotationElement.AnnotationTarget.MESSAGE).attribute(AnnotationElement.Annotation.VALUE_DEFAULT_ATTRIBUTE).type(AnnotationElement.AttributeType.IDENTIFIER).metadataCreator((descriptor, annotation) -> annotation.getDefaultAttributeValue().getValue()).annotation("Bar", AnnotationElement.AnnotationTarget.FIELD).attribute(AnnotationElement.Annotation.VALUE_DEFAULT_ATTRIBUTE).type(AnnotationElement.AttributeType.IDENTIFIER).metadataCreator((fieldDescriptor, annotation) -> annotation.getDefaultAttributeValue().getValue()).build();
    FileDescriptorSource fileDescriptorSource = new FileDescriptorSource();
    fileDescriptorSource.addProtoFile("file1.proto", file1);
    Map<String, FileDescriptor> descriptors = parseAndResolve(fileDescriptorSource, config);
    assertEquals(1, descriptors.size());
    assertTrue(descriptors.containsKey("file1.proto"));
    Map<String, GenericDescriptor> types = descriptors.get("file1.proto").getTypes();
    Descriptor typeX = (Descriptor) types.get("test1.X");
    assertNotNull(typeX);
    assertEquals(1, typeX.getFields().size());
    FieldDescriptor field1 = typeX.getFields().get(0);
    assertEquals("@Foo(fooValue) \n   some more doc text", typeX.getDocumentation());
    Map<String, AnnotationElement.Annotation> typeAnnotations = typeX.getAnnotations();
    assertEquals("fooValue", typeAnnotations.get("Foo").getDefaultAttributeValue().getValue());
    assertEquals("fooValue", typeX.getProcessedAnnotation("Foo"));
    assertEquals("@Bar(barValue)", field1.getDocumentation());
    Map<String, AnnotationElement.Annotation> fieldAnnotations = field1.getAnnotations();
    assertEquals("barValue", fieldAnnotations.get("Bar").getDefaultAttributeValue().getValue());
    assertEquals("barValue", field1.getProcessedAnnotation("Bar"));
}
Also used : Type(org.infinispan.protostream.descriptors.Type) URL(java.net.URL) HashMap(java.util.HashMap) Descriptor(org.infinispan.protostream.descriptors.Descriptor) EnumDescriptor(org.infinispan.protostream.descriptors.EnumDescriptor) Map(java.util.Map) ExpectedException(org.junit.rules.ExpectedException) AnnotationElement(org.infinispan.protostream.descriptors.AnnotationElement) FileDescriptorSource(org.infinispan.protostream.FileDescriptorSource) FileDescriptor(org.infinispan.protostream.descriptors.FileDescriptor) Label(org.infinispan.protostream.descriptors.Label) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) ExtendDescriptor(org.infinispan.protostream.descriptors.ExtendDescriptor) Test(org.junit.Test) File(java.io.File) DescriptorParserException(org.infinispan.protostream.DescriptorParserException) FieldDescriptor(org.infinispan.protostream.descriptors.FieldDescriptor) AnnotationParserException(org.infinispan.protostream.AnnotationParserException) GenericDescriptor(org.infinispan.protostream.descriptors.GenericDescriptor) List(java.util.List) Assert.assertNull(org.junit.Assert.assertNull) Ignore(org.junit.Ignore) Assert.assertFalse(org.junit.Assert.assertFalse) Configuration(org.infinispan.protostream.config.Configuration) ResolutionContext(org.infinispan.protostream.descriptors.ResolutionContext) Assertions.assertThat(org.fest.assertions.api.Assertions.assertThat) Assert.assertEquals(org.junit.Assert.assertEquals) JavaType(org.infinispan.protostream.descriptors.JavaType) Configuration(org.infinispan.protostream.config.Configuration) GenericDescriptor(org.infinispan.protostream.descriptors.GenericDescriptor) Descriptor(org.infinispan.protostream.descriptors.Descriptor) EnumDescriptor(org.infinispan.protostream.descriptors.EnumDescriptor) FileDescriptor(org.infinispan.protostream.descriptors.FileDescriptor) ExtendDescriptor(org.infinispan.protostream.descriptors.ExtendDescriptor) FieldDescriptor(org.infinispan.protostream.descriptors.FieldDescriptor) GenericDescriptor(org.infinispan.protostream.descriptors.GenericDescriptor) FileDescriptorSource(org.infinispan.protostream.FileDescriptorSource) FileDescriptor(org.infinispan.protostream.descriptors.FileDescriptor) FieldDescriptor(org.infinispan.protostream.descriptors.FieldDescriptor) Test(org.junit.Test)

Example 27 with Descriptor

use of org.infinispan.protostream.descriptors.Descriptor in project kogito-runtimes by kiegroup.

the class ProtostreamProtobufAdapterTypeProvider method buildMessageTypes.

private FileDescriptorProto buildMessageTypes(FileDescriptor descriptor) {
    FileDescriptorProto.Builder protoFileBuilder = FileDescriptorProto.newBuilder();
    protoFileBuilder.setPackage(descriptor.getPackage());
    protoFileBuilder.setName(descriptor.getName());
    for (Descriptor message : descriptor.getMessageTypes()) {
        protoFileBuilder.addMessageType(buildMessage(message));
    }
    return protoFileBuilder.build();
}
Also used : Descriptor(org.infinispan.protostream.descriptors.Descriptor) EnumDescriptor(org.infinispan.protostream.descriptors.EnumDescriptor) EnumValueDescriptor(org.infinispan.protostream.descriptors.EnumValueDescriptor) FileDescriptor(org.infinispan.protostream.descriptors.FileDescriptor) FieldDescriptor(org.infinispan.protostream.descriptors.FieldDescriptor) FileDescriptorProto(com.google.protobuf.DescriptorProtos.FileDescriptorProto)

Example 28 with Descriptor

use of org.infinispan.protostream.descriptors.Descriptor in project kogito-runtimes by kiegroup.

the class AbstractMarshallerGenerator method javaTypeForMessage.

protected String javaTypeForMessage(FileDescriptor d, String messageName, SerializationContext serializationContext) {
    Map<String, FileDescriptor> descriptors = serializationContext.getFileDescriptors();
    for (Entry<String, FileDescriptor> entry : descriptors.entrySet()) {
        List<Descriptor> messages = entry.getValue().getMessageTypes();
        for (Descriptor msg : messages) {
            if (messageName.equals(msg.getName())) {
                return packageFromOption(d, msg) + "." + messageName;
            } else if (messageName.equals(msg.getFullName())) {
                return packageFromOption(d, msg) + "." + msg.getName();
            }
        }
        List<EnumDescriptor> enums = entry.getValue().getEnumTypes();
        for (EnumDescriptor msg : enums) {
            if (messageName.equals(msg.getName())) {
                return packageFromOption(d, msg) + "." + messageName;
            } else if (messageName.equals(msg.getFullName())) {
                return packageFromOption(d, msg) + "." + msg.getName();
            }
        }
    }
    return null;
}
Also used : FieldDescriptor(org.infinispan.protostream.descriptors.FieldDescriptor) Descriptor(org.infinispan.protostream.descriptors.Descriptor) EnumDescriptor(org.infinispan.protostream.descriptors.EnumDescriptor) FileDescriptor(org.infinispan.protostream.descriptors.FileDescriptor) EnumDescriptor(org.infinispan.protostream.descriptors.EnumDescriptor) FileDescriptor(org.infinispan.protostream.descriptors.FileDescriptor)

Aggregations

Descriptor (org.infinispan.protostream.descriptors.Descriptor)28 FieldDescriptor (org.infinispan.protostream.descriptors.FieldDescriptor)24 EnumDescriptor (org.infinispan.protostream.descriptors.EnumDescriptor)19 FileDescriptor (org.infinispan.protostream.descriptors.FileDescriptor)13 GenericDescriptor (org.infinispan.protostream.descriptors.GenericDescriptor)11 List (java.util.List)9 Test (org.junit.Test)8 Map (java.util.Map)7 FileDescriptorSource (org.infinispan.protostream.FileDescriptorSource)7 Configuration (org.infinispan.protostream.config.Configuration)7 EnumValueDescriptor (org.infinispan.protostream.descriptors.EnumValueDescriptor)7 ExtendDescriptor (org.infinispan.protostream.descriptors.ExtendDescriptor)6 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 AnnotationElement (org.infinispan.protostream.descriptors.AnnotationElement)5 IOException (java.io.IOException)4 Objects (java.util.Objects)3 Set (java.util.Set)3 Function (java.util.function.Function)3 Predicate (java.util.function.Predicate)3