use of com.squareup.wire.schema.internal.parser.ProtoFileElement in project wire by square.
the class ProtoFileTest method roundTripToElement.
@Test
public void roundTripToElement() {
TypeElement element1 = MessageElement.builder(location.at(11, 1)).name("Message1").documentation("Some comments about Message1").build();
TypeElement element2 = MessageElement.builder(location.at(12, 1)).name("Message2").fields(ImmutableList.<FieldElement>of(FieldElement.builder(location.at(13, 3)).type("string").name("field").tag(1).build())).build();
ExtendElement extend1 = ExtendElement.builder(location.at(16, 1)).name("Extend1").build();
ExtendElement extend2 = ExtendElement.builder(location.at(17, 1)).name("Extend2").build();
OptionElement option1 = OptionElement.create("kit", OptionElement.Kind.STRING, "kat");
OptionElement option2 = OptionElement.create("foo", OptionElement.Kind.STRING, "bar");
ServiceElement service1 = ServiceElement.builder(location.at(19, 1)).name("Service1").rpcs(ImmutableList.<RpcElement>of(RpcElement.builder(location.at(20, 3)).name("MethodA").requestType("Message2").responseType("Message1").options(ImmutableList.<OptionElement>of(OptionElement.create("methodoption", OptionElement.Kind.NUMBER, 1))).build())).build();
ServiceElement service2 = ServiceElement.builder(location.at(24, 1)).name("Service2").build();
ProtoFileElement fileElement = ProtoFileElement.builder(location).packageName("example.simple").imports(ImmutableList.of("example.thing")).publicImports(ImmutableList.of("example.other")).types(ImmutableList.of(element1, element2)).services(ImmutableList.of(service1, service2)).extendDeclarations(ImmutableList.of(extend1, extend2)).options(ImmutableList.of(option1, option2)).build();
ProtoFile file = ProtoFile.get(fileElement);
String expected = "" + "// file.proto\n" + "package example.simple;\n" + "\n" + "import \"example.thing\";\n" + "import public \"example.other\";\n" + "\n" + "option kit = \"kat\";\n" + "option foo = \"bar\";\n" + "\n" + "// Some comments about Message1\n" + "message Message1 {}\n" + "message Message2 {\n" + " string field = 1;\n" + "}\n" + "\n" + "extend Extend1 {}\n" + "extend Extend2 {}\n" + "\n" + "service Service1 {\n" + " rpc MethodA (Message2) returns (Message1) {\n" + " option methodoption = 1;\n" + " };\n" + "}\n" + "service Service2 {}\n";
assertThat(file.toElement().toSchema()).isEqualTo(expected);
assertThat(file.toElement()).isEqualToComparingFieldByField(fileElement);
}
use of com.squareup.wire.schema.internal.parser.ProtoFileElement in project wire by square.
the class SchemaLoader method loadFromDirectories.
private Schema loadFromDirectories(Map<Path, Path> directories) throws IOException {
final Deque<String> protos = new ArrayDeque<>(this.protos);
if (protos.isEmpty()) {
for (final Map.Entry<Path, Path> entry : directories.entrySet()) {
Files.walkFileTree(entry.getValue(), new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
if (file.getFileName().toString().endsWith(".proto")) {
protos.add(entry.getValue().relativize(file).toString());
}
return FileVisitResult.CONTINUE;
}
});
}
}
Map<String, ProtoFile> loaded = new LinkedHashMap<>();
loaded.put(DESCRIPTOR_PROTO, loadDescriptorProto());
while (!protos.isEmpty()) {
String proto = protos.removeFirst();
if (loaded.containsKey(proto)) {
continue;
}
ProtoFileElement element = null;
for (Map.Entry<Path, Path> entry : directories.entrySet()) {
Source source = source(entry.getValue(), proto);
if (source == null) {
continue;
}
Path base = entry.getKey();
try {
Location location = Location.get(base.toString(), proto);
String data = Okio.buffer(source).readUtf8();
element = ProtoParser.parse(location, data);
break;
} catch (IOException e) {
throw new IOException("Failed to load " + proto + " from " + base, e);
} finally {
source.close();
}
}
if (element == null) {
throw new FileNotFoundException("Failed to locate " + proto + " in " + sources);
}
ProtoFile protoFile = ProtoFile.get(element);
loaded.put(proto, protoFile);
// Queue dependencies to be loaded.
for (String importPath : element.imports()) {
protos.addLast(importPath);
}
}
return new Linker(loaded.values()).link();
}
use of com.squareup.wire.schema.internal.parser.ProtoFileElement in project wire by square.
the class SchemaLoader method loadDescriptorProto.
/**
* Returns Google's protobuf descriptor, which defines standard options like default, deprecated,
* and java_package. If the user has provided their own version of the descriptor proto, that is
* preferred.
*/
private ProtoFile loadDescriptorProto() throws IOException {
InputStream resourceAsStream = SchemaLoader.class.getResourceAsStream("/" + DESCRIPTOR_PROTO);
try (BufferedSource buffer = Okio.buffer(Okio.source(resourceAsStream))) {
String data = buffer.readUtf8();
Location location = Location.get("", DESCRIPTOR_PROTO);
ProtoFileElement element = ProtoParser.parse(location, data);
return ProtoFile.get(element);
}
}
Aggregations