public RepoBuilder add(String name, String protoFile) { if (name.endsWith(".proto")) { schemaLoader.addProto(name); } else if (!name.endsWith(".wire")) { throw new IllegalArgumentException("unexpected file extension: " + name); } Path relativePath = fs.getPath(name); try { Path resolvedPath = root.resolve(relativePath); Path parent = resolvedPath.getParent(); if (parent != null) { Files.createDirectories(parent); } Files.write(resolvedPath, protoFile.getBytes(UTF_8)); } catch (IOException e) { throw new AssertionError(e); } return this; }
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(); }
@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(); }
schemaLoader.addProto(proto);