static ProtoFile get(ProtoFileElement protoFileElement) { String packageName = protoFileElement.packageName(); ImmutableList<Type> types = Type.fromElements(packageName, protoFileElement.types()); ImmutableList<Service> services = Service.fromElements(packageName, protoFileElement.services()); ImmutableList<Extend> wireExtends = Extend.fromElements(packageName, protoFileElement.extendDeclarations()); Options options = new Options(Options.FILE_OPTIONS, protoFileElement.options()); return new ProtoFile(protoFileElement.location(), protoFileElement.imports(), protoFileElement.publicImports(), packageName, types, services, wireExtends, options, protoFileElement.syntax()); }
ProtoParser(Location location, char[] data) { this.reader = new SyntaxReader(data, location); this.fileBuilder = ProtoFileElement.builder(location); }
@Test public void addMultiplePublicDependencies() { TypeElement element = MessageElement.builder(location).name("Message").build(); ProtoFileElement file = ProtoFileElement.builder(location) .publicImports(ImmutableList.of("example.other", "example.another")) .types(ImmutableList.of(element)) .build(); assertThat(file.publicImports()).hasSize(2); }
@Test public void multipleSingleLineComments() { String proto = "" + "// Test all\n" + "// the things!\n" + "message Test {}"; String expected = "" + "Test all\n" + "the things!"; ProtoFileElement parsed = ProtoParser.parse(location, proto); TypeElement type = parsed.types().get(0); assertThat(type.documentation()).isEqualTo(expected); }
@Test public void addMultipleOptions() { TypeElement element = MessageElement.builder(location).name("Message").build(); OptionElement kitKat = OptionElement.create("kit", Kind.STRING, "kat"); OptionElement fooBar = OptionElement.create("foo", Kind.STRING, "bar"); ProtoFileElement file = ProtoFileElement.builder(location) .options(ImmutableList.of(kitKat, fooBar)) .types(ImmutableList.of(element)) .build(); assertThat(file.options()).hasSize(2); }
@Test public void addMultipleServices() { ServiceElement service1 = ServiceElement.builder(location).name("Service1").build(); ServiceElement service2 = ServiceElement.builder(location).name("Service2").build(); ProtoFileElement file = ProtoFileElement.builder(location) .services(ImmutableList.of(service1, service2)) .build(); assertThat(file.services()).hasSize(2); }
@Test public void addMultipleDependencies() { TypeElement element = MessageElement.builder(location).name("Message").build(); ProtoFileElement file = ProtoFileElement.builder(location) .imports(ImmutableList.of("example.other", "example.another")) .types(ImmutableList.of(element)) .build(); assertThat(file.imports()).hasSize(2); }
@Test public void addMultipleExtends() { ExtendElement extend1 = ExtendElement.builder(location).name("Extend1").build(); ExtendElement extend2 = ExtendElement.builder(location).name("Extend2").build(); ProtoFileElement file = ProtoFileElement.builder(location) .extendDeclarations(ImmutableList.of(extend1, extend2)) .build(); assertThat(file.extendDeclarations()).hasSize(2); }
public boolean protoFileMatchesPackage(ProtoFileElement element) { String packageFound = element.packageName(); if (packageFound == null) { List<OptionElement> options = element.options(); if (options != null) { for (OptionElement option : options) { if (option.name().equals("java_package")) { if (option.value().toString().contains(serviceName)) { System.out.println("Please update protofile for " + serviceName); return true; } } } } } else { return matchesServiceName(packageFound); } return false; }
public List<RpcMethodDefinition> getRpcMethods() throws IOException { List<RpcMethodDefinition> retval = new ArrayList<>(); ProtoParser parser = new ProtoParser(Location.get(input.getCanonicalPath()), gulpFile(input)); ProtoFileElement element = parser.readProtoFile(); if (protoFileMatchesPackage(element)) { List<ServiceElement> services = element.services(); if (services != null) { for (ServiceElement service : services) { String name = service.name(); List<RpcElement> rpcs = service.rpcs(); if (rpcs != null) { for (RpcElement rpc : rpcs) { RpcMethodDefinition def = new RpcMethodDefinition(name + "." + rpc.name(), rpc.requestType(), rpc.responseType(), element.packageName(), fileName ); retval.add(def); } } } } } return retval; }
for (String importPath : element.imports()) { protos.addLast(importPath);
@Test public void syntaxNotRequired() throws Exception { String proto = "message Foo {}"; ProtoFileElement parsed = ProtoParser.parse(location, proto); assertThat(parsed.syntax()).isNull(); }
@Test public void singleLineJavadocComment() { String proto = "" + "/** Test */\n" + "message Test {}"; ProtoFileElement parsed = ProtoParser.parse(location, proto); TypeElement type = parsed.types().get(0); assertThat(type.documentation()).isEqualTo("Test"); }
for (String importPath : element.imports()) { protos.addLast(importPath);
public final String toSchema() { StringBuilder builder = new StringBuilder(); builder.append("// ").append(location()).append('\n'); if (syntax() != null) { builder.append("syntax = \"").append(syntax()).append("\";\n"); if (packageName() != null) { builder.append("package ").append(packageName()).append(";\n"); if (!imports().isEmpty() || !publicImports().isEmpty()) { builder.append('\n'); for (String file : imports()) { builder.append("import \"").append(file).append("\";\n"); for (String file : publicImports()) { builder.append("import public \"").append(file).append("\";\n"); if (!options().isEmpty()) { builder.append('\n'); for (OptionElement option : options()) { builder.append(option.toSchemaDeclaration()); if (!types().isEmpty()) { builder.append('\n'); for (TypeElement typeElement : types()) { builder.append(typeElement.toSchema()); if (!extendDeclarations().isEmpty()) { builder.append('\n'); for (ExtendElement extendDeclaration : extendDeclarations()) {
@Test public void publicImports() throws Exception { String proto = "import public \"src/test/resources/unittest_import.proto\";\n"; ProtoFileElement expected = ProtoFileElement.builder(location) .publicImports(ImmutableList.of("src/test/resources/unittest_import.proto")) .build(); assertThat(ProtoParser.parse(location, proto)).isEqualTo(expected); }
@Test public void simpleToSchema() { TypeElement element = MessageElement.builder(location).name("Message").build(); ProtoFileElement file = ProtoFileElement.builder(location) .types(ImmutableList.of(element)) .build(); String expected = "" + "// file.proto\n" + "\n" + "message Message {}\n"; assertThat(file.toSchema()).isEqualTo(expected); }
@Test public void singleLineComment() { String proto = "" + "// Test all the things!\n" + "message Test {}"; ProtoFileElement parsed = ProtoParser.parse(location, proto); TypeElement type = parsed.types().get(0); assertThat(type.documentation()).isEqualTo("Test all the things!"); }