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