Search in sources :

Example 1 with Configuration

use of org.infinispan.protostream.config.Configuration in project protostream by infinispan.

the class ProtoSchemaBuilder method main.

public static void main(String[] args) throws Exception {
    CommandLine cmd = parseCommandLine(args);
    if (cmd == null) {
        return;
    }
    String packageName = cmd.getOptionValue(PACKAGE_LONG_OPT);
    Configuration config = Configuration.builder().build();
    SerializationContext ctx = ProtobufUtil.newSerializationContext(config);
    Properties schemas = cmd.getOptionProperties(SCHEMA_LONG_OPT);
    if (schemas != null) {
        for (String schema : schemas.stringPropertyNames()) {
            String file = schemas.getProperty(schema);
            try (FileInputStream in = new FileInputStream(file)) {
                ctx.registerProtoFiles(new FileDescriptorSource().addProtoFile(schema, in));
            }
        }
    }
    String[] marshallers = cmd.getOptionValues(MARSHALLER_LONG_OPT);
    if (marshallers != null) {
        for (String marshallerClass : marshallers) {
            BaseMarshaller<?> bm = (BaseMarshaller<?>) Class.forName(marshallerClass).newInstance();
            ctx.registerMarshaller(bm);
        }
    }
    File file = cmd.hasOption(FILE_LONG_OPT) ? new File(cmd.getOptionValue(FILE_LONG_OPT)) : null;
    String fileName = file == null ? DEFAULT_GENERATED_SCHEMA_NAME : file.getName();
    ProtoSchemaBuilder protoSchemaBuilder = new ProtoSchemaBuilder().fileName(fileName).packageName(packageName);
    for (String className : cmd.getArgs()) {
        protoSchemaBuilder.addClass(Class.forName(className));
    }
    String schemaFile = protoSchemaBuilder.build(ctx);
    if (file != null) {
        try (PrintStream out = new PrintStream(new FileOutputStream(file))) {
            out.print(schemaFile);
            out.flush();
        }
    } else {
        System.out.print(schemaFile);
    }
}
Also used : SerializationContext(org.infinispan.protostream.SerializationContext) PrintStream(java.io.PrintStream) Configuration(org.infinispan.protostream.config.Configuration) Properties(java.util.Properties) FileInputStream(java.io.FileInputStream) CommandLine(org.apache.commons.cli.CommandLine) FileOutputStream(java.io.FileOutputStream) BaseMarshaller(org.infinispan.protostream.BaseMarshaller) FileDescriptorSource(org.infinispan.protostream.FileDescriptorSource) File(java.io.File)

Example 2 with Configuration

use of org.infinispan.protostream.config.Configuration 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));
}
Also used : Configuration(org.infinispan.protostream.config.Configuration) AnnotationElement(org.infinispan.protostream.descriptors.AnnotationElement) 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) List(java.util.List) FileDescriptorSource(org.infinispan.protostream.FileDescriptorSource) FileDescriptor(org.infinispan.protostream.descriptors.FileDescriptor) Test(org.junit.Test)

Example 3 with Configuration

use of org.infinispan.protostream.config.Configuration in project protostream by infinispan.

the class DescriptorsTest method testRepeatedAnnotation.

@Test
public void testRepeatedAnnotation() {
    Configuration config = Configuration.builder().annotationsConfig().annotation("Field", AnnotationElement.AnnotationTarget.FIELD).repeatable("Fields").attribute(AnnotationElement.Annotation.VALUE_DEFAULT_ATTRIBUTE).type(AnnotationElement.AttributeType.BOOLEAN).defaultValue(true).build();
    String testProto = "message M {\n" + "  /** @Field @Field */\n" + "  optional int32 field1 = 1; \n" + "}";
    FileDescriptorSource fileDescriptorSource = FileDescriptorSource.fromString("test.proto", testProto);
    Map<String, FileDescriptor> descriptors = parseAndResolve(fileDescriptorSource, config);
    Map<String, AnnotationElement.Annotation> annotations = descriptors.get("test.proto").getMessageTypes().get(0).getFields().get(0).getAnnotations();
    assertFalse(annotations.containsKey("Field"));
    assertTrue(annotations.containsKey("Fields"));
    List<AnnotationElement.Annotation> innerAnnotations = (List<AnnotationElement.Annotation>) annotations.get("Fields").getDefaultAttributeValue().getValue();
    assertEquals(2, innerAnnotations.size());
}
Also used : Configuration(org.infinispan.protostream.config.Configuration) AnnotationElement(org.infinispan.protostream.descriptors.AnnotationElement) List(java.util.List) FileDescriptorSource(org.infinispan.protostream.FileDescriptorSource) FileDescriptor(org.infinispan.protostream.descriptors.FileDescriptor) Test(org.junit.Test)

Example 4 with Configuration

use of org.infinispan.protostream.config.Configuration in project protostream by infinispan.

the class DescriptorsTest method testAnnotationParserMissingRequiredAttribute.

@Test
public void testAnnotationParserMissingRequiredAttribute() {
    exception.expect(DescriptorParserException.class);
    exception.expectMessage("org.infinispan.protostream.AnnotationParserException: Attribute 'value' of annotation 'AnnotationWithRequiredAttribute' on M is required");
    Configuration config = Configuration.builder().annotationsConfig().annotation("AnnotationWithRequiredAttribute", AnnotationElement.AnnotationTarget.MESSAGE).attribute(AnnotationElement.Annotation.VALUE_DEFAULT_ATTRIBUTE).type(AnnotationElement.AttributeType.BOOLEAN).build();
    String testProto = "/** @AnnotationWithRequiredAttribute */\n" + "message M {\n" + "  optional int32 field1 = 1; \n" + "}";
    FileDescriptorSource fileDescriptorSource = FileDescriptorSource.fromString("test.proto", testProto);
    parseAndResolve(fileDescriptorSource, config);
}
Also used : Configuration(org.infinispan.protostream.config.Configuration) FileDescriptorSource(org.infinispan.protostream.FileDescriptorSource) Test(org.junit.Test)

Example 5 with Configuration

use of org.infinispan.protostream.config.Configuration in project protostream by infinispan.

the class DescriptorsTest method testDuplicateUndefinedAnnotation.

@Test
public void testDuplicateUndefinedAnnotation() {
    Configuration config = Configuration.builder().annotationsConfig().annotation("Field", AnnotationElement.AnnotationTarget.FIELD).attribute(AnnotationElement.Annotation.VALUE_DEFAULT_ATTRIBUTE).type(AnnotationElement.AttributeType.BOOLEAN).defaultValue(true).build();
    String testProto = "message M {\n" + "  /** @SomeAnnotation @SomeAnnotation @Field */\n" + "  optional int32 field1 = 1; \n" + "}";
    FileDescriptorSource fileDescriptorSource = FileDescriptorSource.fromString("test.proto", testProto);
    Map<String, FileDescriptor> descriptors = parseAndResolve(fileDescriptorSource, config);
    // todo [anistor] The processing of annotations is waaay too lazy
    AnnotationElement.Annotation someAnnotation = descriptors.get("test.proto").getMessageTypes().get(0).getFields().get(0).getAnnotations().get("SomeAnnotation");
    // 'SomeAnnotation' annotation is not defined, but we accept it silently
    assertNull(someAnnotation);
    AnnotationElement.Annotation fieldAnnotation = descriptors.get("test.proto").getMessageTypes().get(0).getFields().get(0).getAnnotations().get("Field");
    assertNotNull(fieldAnnotation);
    assertEquals("Field", fieldAnnotation.getName());
}
Also used : Configuration(org.infinispan.protostream.config.Configuration) AnnotationElement(org.infinispan.protostream.descriptors.AnnotationElement) FileDescriptorSource(org.infinispan.protostream.FileDescriptorSource) FileDescriptor(org.infinispan.protostream.descriptors.FileDescriptor) Test(org.junit.Test)

Aggregations

Configuration (org.infinispan.protostream.config.Configuration)13 FileDescriptorSource (org.infinispan.protostream.FileDescriptorSource)12 Test (org.junit.Test)12 FileDescriptor (org.infinispan.protostream.descriptors.FileDescriptor)9 AnnotationElement (org.infinispan.protostream.descriptors.AnnotationElement)7 List (java.util.List)5 Descriptor (org.infinispan.protostream.descriptors.Descriptor)5 FieldDescriptor (org.infinispan.protostream.descriptors.FieldDescriptor)5 EnumDescriptor (org.infinispan.protostream.descriptors.EnumDescriptor)4 ExtendDescriptor (org.infinispan.protostream.descriptors.ExtendDescriptor)4 GenericDescriptor (org.infinispan.protostream.descriptors.GenericDescriptor)4 File (java.io.File)3 URL (java.net.URL)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Assertions.assertThat (org.fest.assertions.api.Assertions.assertThat)2 AnnotationParserException (org.infinispan.protostream.AnnotationParserException)2 DescriptorParserException (org.infinispan.protostream.DescriptorParserException)2 JavaType (org.infinispan.protostream.descriptors.JavaType)2 Label (org.infinispan.protostream.descriptors.Label)2