Search in sources :

Example 26 with FileDescriptor

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

the class SquareProtoParser method parse.

/**
 * Parses a set of .proto files but does not resolve type dependencies and does not detect semantic errors like
 * duplicate type definitions. If the {@link FileDescriptorSource} parameter does not include a progress callback
 * parsing will stop on first encountered error. If a callback exists all files will be processed; only one error per
 * file is reported and parsing will continue with the next file.
 *
 * @param fileDescriptorSource the set of descriptors to parse
 * @return a map of successfully parsed {@link FileDescriptor} objects keyed by with their names
 * @throws DescriptorParserException if parsing errors were encountered and no progress callback was specified in the
 *                                   {@link FileDescriptorSource}
 */
public Map<String, FileDescriptor> parse(FileDescriptorSource fileDescriptorSource) throws DescriptorParserException {
    Map<String, String> input = fileDescriptorSource.getFiles();
    Map<String, FileDescriptor> fileDescriptorMap = new LinkedHashMap<>(input.size());
    for (Map.Entry<String, String> entry : input.entrySet()) {
        String fileName = entry.getKey();
        try {
            ProtoFile protoFile = ProtoParser.parse(fileName, new StringReader(entry.getValue()));
            checkUniqueFileOptions(protoFile);
            FileDescriptor fileDescriptor = PROTOFILE_MAPPER.map(protoFile);
            fileDescriptor.setConfiguration(configuration);
            fileDescriptorMap.put(fileName, fileDescriptor);
        } catch (DescriptorParserException e) {
            reportParsingError(fileDescriptorSource, fileDescriptorMap, fileName, e);
        } catch (IOException | RuntimeException e) {
            reportParsingError(fileDescriptorSource, fileDescriptorMap, fileName, new DescriptorParserException(e));
        }
    }
    return fileDescriptorMap;
}
Also used : DescriptorParserException(org.infinispan.protostream.DescriptorParserException) ProtoFile(com.squareup.protoparser.ProtoFile) IOException(java.io.IOException) FileDescriptor(org.infinispan.protostream.descriptors.FileDescriptor) LinkedHashMap(java.util.LinkedHashMap) StringReader(java.io.StringReader) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 27 with FileDescriptor

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

the class DescriptorsTest method testImportAndPackage.

@Test
public void testImportAndPackage() {
    String file1 = "package p;\n" + "message A {\n" + "   optional int32 f1 = 1;\n" + "}";
    String file2 = "package org.infinispan;\n" + "import \"file1.proto\";\n" + "message B {\n" + "   required p.A ma = 1;\n" + "}";
    FileDescriptorSource fileDescriptorSource = new FileDescriptorSource();
    fileDescriptorSource.addProtoFile("file1.proto", file1);
    fileDescriptorSource.addProtoFile("file2.proto", file2);
    Map<String, FileDescriptor> descriptors = parseAndResolve(fileDescriptorSource);
    assertEquals(2, descriptors.size());
    assertTrue(descriptors.containsKey("file1.proto"));
    assertTrue(descriptors.containsKey("file2.proto"));
    assertTrue(descriptors.get("file1.proto").getTypes().containsKey("p.A"));
    assertTrue(descriptors.get("file2.proto").getTypes().containsKey("org.infinispan.B"));
}
Also used : FileDescriptorSource(org.infinispan.protostream.FileDescriptorSource) FileDescriptor(org.infinispan.protostream.descriptors.FileDescriptor) Test(org.junit.Test)

Example 28 with FileDescriptor

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

the class DescriptorsTest method testAnnotationParser.

@Test
public void testAnnotationParser() throws Exception {
    Configuration config = Configuration.builder().annotationsConfig().annotation("Indexed", AnnotationElement.AnnotationTarget.MESSAGE).attribute(AnnotationElement.Annotation.VALUE_DEFAULT_ATTRIBUTE).type(AnnotationElement.AttributeType.BOOLEAN).defaultValue(true).metadataCreator((descriptor, annotation) -> annotation.getDefaultAttributeValue().getValue()).build();
    FileDescriptorSource fileDescriptorSource = FileDescriptorSource.fromResources("sample_bank_account/bank.proto");
    Map<String, FileDescriptor> descriptors = parseAndResolve(fileDescriptorSource, config);
    FileDescriptor fileDescriptor = descriptors.get("sample_bank_account/bank.proto");
    List<Descriptor> messageTypes = fileDescriptor.getMessageTypes();
    Descriptor userMessageType = messageTypes.get(0);
    assertEquals("sample_bank_account.User", userMessageType.getFullName());
    assertEquals(Boolean.TRUE, userMessageType.getProcessedAnnotation("Indexed"));
    Descriptor accountMessageType = messageTypes.get(1);
    assertEquals("sample_bank_account.Account", accountMessageType.getFullName());
    assertEquals(Boolean.TRUE, accountMessageType.getProcessedAnnotation("Indexed"));
}
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) 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) Test(org.junit.Test)

Example 29 with FileDescriptor

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

the class DescriptorsTest method testAllowAliasOfEnumConstantValue.

@Ignore("Test disabled due to https://issues.jboss.org/browse/IPROTO-14")
@Test
public void testAllowAliasOfEnumConstantValue() {
    String file1 = "package test1;\n" + "enum E {\n" + "   option allow_alias = true;\n" + "   A = 1;\n" + "   B = 1;\n" + "}";
    FileDescriptorSource source = new FileDescriptorSource();
    source.addProtoFile("test.proto", file1);
    Map<String, FileDescriptor> descriptors = parseAndResolve(source);
    FileDescriptor descriptor = descriptors.get("test.proto");
    assertThat(descriptor.getEnumTypes()).hasSize(1);
    EnumDescriptor enumDescriptor = descriptor.getEnumTypes().get(0);
    assertThat(enumDescriptor.getName()).isEqualTo("E");
    assertThat(enumDescriptor.getValues().size()).isEqualTo(2);
    assertThat(enumDescriptor.getValues().get(0).getName()).isEqualTo("A");
    assertThat(enumDescriptor.getValues().get(0).getNumber()).isEqualTo(1);
    assertThat(enumDescriptor.getValues().get(1).getName()).isEqualTo("B");
    assertThat(enumDescriptor.getValues().get(1).getNumber()).isEqualTo(1);
}
Also used : EnumDescriptor(org.infinispan.protostream.descriptors.EnumDescriptor) FileDescriptorSource(org.infinispan.protostream.FileDescriptorSource) FileDescriptor(org.infinispan.protostream.descriptors.FileDescriptor) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 30 with FileDescriptor

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

the class DescriptorsTest method testDuplicateAnnotation.

@Test
public void testDuplicateAnnotation() {
    exception.expect(AnnotationParserException.class);
    exception.expectMessage("Error: 1,8: duplicate annotation definition \"Field\"");
    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" + "  /** @Field @Field */\n" + "  optional int32 field1 = 1; \n" + "}";
    FileDescriptorSource fileDescriptorSource = FileDescriptorSource.fromString("test.proto", testProto);
    Map<String, FileDescriptor> descriptors = parseAndResolve(fileDescriptorSource, config);
    // todo [anistor] this is waaay too lazy
    descriptors.get("test.proto").getMessageTypes().get(0).getFields().get(0).getAnnotations();
}
Also used : Configuration(org.infinispan.protostream.config.Configuration) FileDescriptorSource(org.infinispan.protostream.FileDescriptorSource) FileDescriptor(org.infinispan.protostream.descriptors.FileDescriptor) Test(org.junit.Test)

Aggregations

FileDescriptor (org.infinispan.protostream.descriptors.FileDescriptor)43 FileDescriptorSource (org.infinispan.protostream.FileDescriptorSource)24 Test (org.junit.Test)23 Descriptor (org.infinispan.protostream.descriptors.Descriptor)12 Configuration (org.infinispan.protostream.config.Configuration)11 EnumDescriptor (org.infinispan.protostream.descriptors.EnumDescriptor)11 FieldDescriptor (org.infinispan.protostream.descriptors.FieldDescriptor)11 List (java.util.List)8 SerializationContext (org.infinispan.protostream.SerializationContext)8 AnnotationElement (org.infinispan.protostream.descriptors.AnnotationElement)8 GenericDescriptor (org.infinispan.protostream.descriptors.GenericDescriptor)6 ArrayList (java.util.ArrayList)5 Map (java.util.Map)5 DescriptorParserException (org.infinispan.protostream.DescriptorParserException)5 ExtendDescriptor (org.infinispan.protostream.descriptors.ExtendDescriptor)5 EntityIndexDescriptor (org.kie.kogito.persistence.api.schema.EntityIndexDescriptor)4 HashMap (java.util.HashMap)3 ResolutionContext (org.infinispan.protostream.descriptors.ResolutionContext)3 Ignore (org.junit.Ignore)3 Test (org.junit.jupiter.api.Test)3