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;
}
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"));
}
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"));
}
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);
}
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();
}
Aggregations