/** Add directory or zip file source from which proto files will be loaded. */ public SchemaLoader addSource(File file) { return addSource(file.toPath()); }
private Schema loadSchema(List<String> directories, List<String> protos) throws IOException { Stopwatch stopwatch = Stopwatch.createStarted(); SchemaLoader schemaLoader = new SchemaLoader(); for (String directory : directories) { schemaLoader.addSource(new File(directory)); } for (String proto : protos) { schemaLoader.addProto(proto); } Schema schema = schemaLoader.load(); getLog().info(String.format("Loaded %s proto files in %s", schema.protoFiles().size(), stopwatch)); return schema; }
private Schema loadSchema() throws IOException { Stopwatch stopwatch = Stopwatch.createStarted(); SchemaLoader loader = new SchemaLoader(); for (String source : sources) { loader.addSource(new File(source)); } for (String proto : protos) { loader.addProto(proto); } Schema schema = loader.load(); log.info("Loaded %s proto files in %s", schema.protoFiles().size(), stopwatch); return schema; }
@Test public void locateInMultiplePaths() throws IOException { Files.createDirectories(fileSystem.getPath("/source1")); Files.createDirectories(fileSystem.getPath("/source2")); Files.createFile(fileSystem.getPath("/source1/file1.proto")); Files.createFile(fileSystem.getPath("/source2/file2.proto")); new SchemaLoader() .addSource(fileSystem.getPath("/source1")) .addSource(fileSystem.getPath("/source2")) .addProto("file1.proto") .addProto("file2.proto") .load(); }
new SchemaLoader().addSource(ROOT).load(); System.out.println("All files linked successfully.");
@Test public void failLocateInZipFile() throws IOException { Files.createDirectories(fileSystem.getPath("/source")); Path zip = fileSystem.getPath("/source/protos.zip"); ZipOutputStream zipOutputStream = new ZipOutputStream(Files.newOutputStream(zip)); zipOutputStream.putNextEntry(new ZipEntry("a/b/trix.proto")); zipOutputStream.write("message Trix {}".getBytes(UTF_8)); zipOutputStream.close(); try { new SchemaLoader() .addSource(zip) .addProto("a/b/message.proto") .load(); fail(); } catch (FileNotFoundException expected) { } }
@Test public void failLocate() throws IOException { Files.createDirectories(fileSystem.getPath("/source1")); Files.createDirectories(fileSystem.getPath("/source2")); Files.createFile(fileSystem.getPath("/source2/file2.proto")); SchemaLoader loader = new SchemaLoader() .addSource(fileSystem.getPath("/source1")) .addProto("file2.proto"); try { loader.load(); fail(); } catch (FileNotFoundException expected) { } }
@Test public void locateInZipFile() throws IOException { Files.createDirectories(fileSystem.getPath("/source")); Path zip = fileSystem.getPath("/source/protos.zip"); ZipOutputStream zipOutputStream = new ZipOutputStream(Files.newOutputStream(zip)); zipOutputStream.putNextEntry(new ZipEntry("a/b/message.proto")); zipOutputStream.write("message Message {}".getBytes(UTF_8)); zipOutputStream.close(); Schema schema = new SchemaLoader() .addSource(zip) .addProto("a/b/message.proto") .load(); Type message = schema.getType("Message"); assertThat(message).isNotNull(); assertThat(message.location().base()).isEqualTo("/source/protos.zip"); assertThat(message.location().path()).isEqualTo("a/b/message.proto"); }
private Schema schema(Map<String, String> fileToProto) throws IOException { SchemaLoader schemaLoader = new SchemaLoader(); schemaLoader.addSource(temporaryFolder.getRoot()); for (Map.Entry<String, String> entry : fileToProto.entrySet()) { File file = new File(temporaryFolder.getRoot(), entry.getKey()); file.getParentFile().mkdirs(); try (BufferedSink out = Okio.buffer(Okio.sink(file))) { out.writeUtf8(entry.getValue()); } schemaLoader.addProto(entry.getKey()); } return schemaLoader.load(); }
@Test public void earlierSourcesTakePrecedenceOverLaterSources() throws IOException { Files.createDirectories(fileSystem.getPath("/source1")); Files.createDirectories(fileSystem.getPath("/source2")); writeFile("/source1/message.proto", "" + "message Message {\n" + " optional string a = 1;\n" + "}"); writeFile("/source2/message.proto", "" + "message Message {\n" + " optional string b = 2;\n" + "}"); Schema schema = new SchemaLoader() .addSource(fileSystem.getPath("/source1")) .addSource(fileSystem.getPath("/source2")) .load(); MessageType message = (MessageType) schema.getType("Message"); assertThat(message.field("a")).isNotNull(); }
@Test public void loadAllFilesWhenNoneSpecified() throws IOException { Files.createDirectories(fileSystem.getPath("/source")); writeFile("/source/message1.proto", "message Message1 {}"); writeFile("/source/message2.proto", "message Message2 {}"); writeFile("/source/readme.txt", "Here be protos!"); Schema schema = new SchemaLoader() .addSource(fileSystem.getPath("/source")) .load(); Type message1 = schema.getType("Message1"); assertThat(message1).isNotNull(); assertThat(message1.location().base()).isEqualTo("/source"); assertThat(message1.location().path()).isEqualTo("message1.proto"); Type message2 = schema.getType("Message2"); assertThat(message2).isNotNull(); assertThat(message2.location().base()).isEqualTo("/source"); assertThat(message2.location().path()).isEqualTo("message2.proto"); }
@Test public void profileInZip() throws IOException { FileSystem fileSystem = Jimfs.newFileSystem(Configuration.unix()); Files.createDirectories(fileSystem.getPath("/source")); Path zip = fileSystem.getPath("/source/protos.zip"); ZipOutputStream zipOutputStream = new ZipOutputStream(Files.newOutputStream(zip)); writeFile(zipOutputStream, "a/b/message.proto", "" + "package a.b;\n" + "message Message {" + "}"); writeFile(zipOutputStream, "a/b/android.wire", "" + "syntax = \"wire2\";\n" + "package a.b;\n" + "import \"a/b/message.proto\";\n" + "type a.b.Message {\n" + " target java.lang.Object using com.example.Message#ADAPTER;\n" + "}"); zipOutputStream.close(); Schema schema = new SchemaLoader() .addSource(zip) .load(); Profile profile = new ProfileLoader(fileSystem, "android") .schema(schema) .load(); ProtoType message = ProtoType.get("a.b.Message"); assertThat(profile.getTarget(message)).isEqualTo(ClassName.OBJECT); assertThat(profile.getAdapter(message)).isEqualTo( new AdapterConstant("com.example.Message#ADAPTER")); }
/** Add directory or zip file source from which proto files will be loaded. */ public SchemaLoader addSource(File file) { return addSource(file.toPath()); }
/** Add directory or zip file source from which proto files will be loaded. */ public SchemaLoader addSource(File file) { return addSource(file.toPath()); }
schemaLoader.addSource(new File(directory));