@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"); }
@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"); }
void validateImport(Location location, ProtoType type) { // Map key type is always scalar. No need to validate it. if (type.isMap()) type = type.valueType(); if (type.isScalar()) return; String path = location.path(); String requiredImport = get(type).location().path(); if (!path.equals(requiredImport) && !imports.containsEntry(path, requiredImport)) { addError("%s needs to import %s", path, requiredImport); } }
/** Confirms that {@code protoFiles} link correctly against {@code schema}. */ void validate(Schema schema, ImmutableList<ProfileFileElement> profileFiles) { List<String> errors = new ArrayList<>(); for (ProfileFileElement profileFile : profileFiles) { for (TypeConfigElement typeConfig : profileFile.typeConfigs()) { ProtoType type = importedType(ProtoType.get(typeConfig.type())); if (type == null) continue; Type resolvedType = schema.getType(type); if (resolvedType == null) { errors.add(String.format("unable to resolve %s (%s)", type, typeConfig.location())); continue; } String requiredImport = resolvedType.location().path(); if (!profileFile.imports().contains(requiredImport)) { errors.add(String.format("%s needs to import %s (%s)", typeConfig.location().path(), requiredImport, typeConfig.location())); } } } if (!errors.isEmpty()) { throw new SchemaException(errors); } }
TypeSpec typeSpec = javaGenerator.generateType(type); ClassName javaTypeName = javaGenerator.generatedTypeName(type); writeJavaFile(javaTypeName, typeSpec, type.location().withPathOnly()); getLog().info(String.format("Generated %s in %s", javaTypeName, stopwatch));
public void execute() throws IOException { Schema schema = loadSchema(); if (!identifierSet.isEmpty()) { schema = retainRoots(schema); } JavaGenerator javaGenerator = JavaGenerator.get(schema); ServiceGenerator serviceGenerator = new ServiceGenerator(javaGenerator); for (ProtoFile protoFile : schema.protoFiles()) { for (Type type : protoFile.types()) { Stopwatch stopwatch = Stopwatch.createStarted(); TypeSpec typeSpec = javaGenerator.generateType(type); ClassName javaTypeName = (ClassName) javaGenerator.typeName(type.type()); writeJavaFile(javaTypeName, typeSpec, type.location(), stopwatch); } for (Service service : protoFile.services()) { Stopwatch stopwatch = Stopwatch.createStarted(); ClassName javaTypeName = (ClassName) javaGenerator.typeName(service.type()); TypeSpec typeSpec = serviceGenerator.api(service); writeJavaFile(javaTypeName, typeSpec, service.location(), stopwatch); } } }
void validateImport(Location location, ProtoType type) { // Map key type is always scalar. No need to validate it. if (type.isMap()) type = type.valueType(); if (type.isScalar()) return; String path = location.path(); String requiredImport = get(type).location().path(); if (!path.equals(requiredImport) && !imports.containsEntry(path, requiredImport)) { addError("%s needs to import %s", path, requiredImport); } }
void validateImport(Location location, ProtoType type) { // Map key type is always scalar. No need to validate it. if (type.isMap()) type = type.valueType(); if (type.isScalar()) return; String path = location.path(); String requiredImport = get(type).location().path(); if (!path.equals(requiredImport) && !imports.containsEntry(path, requiredImport)) { addError("%s needs to import %s", path, requiredImport); } }
/** Confirms that {@code protoFiles} link correctly against {@code schema}. */ void validate(Schema schema, ImmutableList<AbstractProfileFileElement> profileFiles) { List<String> errors = new ArrayList<>(); for (AbstractProfileFileElement profileFile : profileFiles) { for (AbstractTypeConfigElement typeConfig : profileFile.typeConfigs()) { ProtoType type = importedType(ProtoType.get(typeConfig.type())); if (type == null) { continue; } Type resolvedType = schema.getType(type); if (resolvedType == null) { errors.add(String.format("unable to resolve %s (%s)", type, typeConfig.location())); continue; } String requiredImport = resolvedType.location().path(); if (!profileFile.imports().contains(requiredImport)) { errors.add(String.format("%s needs to import %s (%s)", typeConfig.location().path(), requiredImport, typeConfig.location())); } } } if (!errors.isEmpty()) { throw new SchemaException(errors); } }
ClassName javaTypeName = javaGenerator.generatedTypeName(type); TypeSpec typeSpec = javaGenerator.generateType(protoFile, type); writeJavaFile(javaTypeName, typeSpec, type.location().withPathOnly());