for (Type type : protoFile.types()) { register(type); for (Extend extend : protoFile.extendList()) { extend.link(linker); for (Type type : protoFile.types()) { type.link(linker); for (Service service : protoFile.services()) { service.link(linker); protoFile.linkOptions(linker); for (Type type : protoFile.types()) { type.linkOptions(linker); for (Service service : protoFile.services()) { service.linkOptions(linker); publicImports.putAll(protoFile.location().path(), protoFile.publicImports()); Collection<String> sink = imports.get(protoFile.location().path()); addImports(sink, protoFile.imports(), publicImports); addImports(sink, protoFile.publicImports(), publicImports); for (Type type : protoFile.types()) { type.validate(linker); for (Service service : protoFile.services()) {
private static String javaPackage(ProtoFile protoFile) { String javaPackage = protoFile.javaPackage(); if (javaPackage != null) { return javaPackage; } else if (protoFile.packageName() != null) { return protoFile.packageName(); } else { return ""; } }
/** * 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); } }
/** Returns the proto file at {@code path}, or null if this schema has no such file. */ public ProtoFile protoFile(String path) { for (ProtoFile protoFile : protoFiles) { if (protoFile.location().path().equals(path)) { return protoFile; } } return null; }
private static ImmutableMap<String, Type> buildTypesIndex(Iterable<ProtoFile> protoFiles) { Map<String, Type> result = new LinkedHashMap<>(); for (ProtoFile protoFile : protoFiles) { for (Type type : protoFile.types()) { index(result, type); } } return ImmutableMap.copyOf(result); }
/** Returns the current package name from the context stack. */ String packageName() { for (Object context : contextStack) { if (context instanceof ProtoFile) return ((ProtoFile) context).packageName(); } return null; }
private static ImmutableMap<String, Service> buildServicesIndex(Iterable<ProtoFile> protoFiles) { ImmutableMap.Builder<String, Service> result = ImmutableMap.builder(); for (ProtoFile protoFile : protoFiles) { for (Service service : protoFile.services()) { result.put(service.type().toString(), service); } } return result.build(); }
/** Returns a new proto file that omits types and services not in {@code identifiers}. */ ProtoFile retainAll(Schema schema, MarkSet markSet) { ImmutableList.Builder<Type> retainedTypes = ImmutableList.builder(); for (Type type : types) { Type retainedType = type.retainAll(schema, markSet); if (retainedType != null) { retainedTypes.add(retainedType); } } ImmutableList.Builder<Service> retainedServices = ImmutableList.builder(); for (Service service : services) { Service retainedService = service.retainAll(schema, markSet); if (retainedService != null) { retainedServices.add(retainedService); } } ProtoFile result = new ProtoFile(location, imports, publicImports, packageName, retainedTypes.build(), retainedServices.build(), extendList, options.retainAll(schema, markSet), syntax); result.javaPackage = javaPackage; return result; }
@Test public void extendTypeImported() throws Exception { Schema schema = new RepoBuilder() .add("a.proto", "" + "package pa;\n" + "import \"b.proto\";\n" + "extend pb.B {\n" + " optional string a = 1;\n" + "}\n") .add("b.proto", "" + "package pb;\n" + "message B {\n" + " extensions 1;\n" + "}\n") .schema(); Extend extendB = schema.protoFiles().get(0).extendList().get(0); MessageType b = (MessageType) schema.getType("pb.B"); assertThat(extendB.type()).isEqualTo(b.type()); }
@Test public void specialOptionsNotPruned() throws Exception { Schema schema = new RepoBuilder() .add("message.proto", "" + "option java_package = \"p\";\n" + "\n" + "message Message {\n" + " optional int32 a = 1 [packed = true, deprecated = true, default = 5];\n" + "}\n" + "enum Enum {\n" + " option allow_alias = true;\n" + " A = 1;\n" + " B = 1;\n" + "}\n") .schema(); Schema pruned = schema.prune(new IdentifierSet.Builder() .exclude("google.protobuf.*") .build()); ProtoFile protoFile = pruned.protoFile("message.proto"); assertThat(protoFile.javaPackage()).isEqualTo("p"); MessageType message = (MessageType) pruned.getType("Message"); Field field = message.field("a"); assertThat(field.getDefault()).isEqualTo("5"); assertThat(field.isDeprecated()).isTrue(); assertThat(field.isPacked()).isTrue(); EnumType enumType = (EnumType) pruned.getType("Enum"); assertThat(enumType.allowAlias()).isTrue(); } }
public static JavaGenerator get(Schema schema) { Map<ProtoType, ClassName> nameToJavaName = new LinkedHashMap<>(BUILT_IN_TYPES_MAP); for (ProtoFile protoFile : schema.protoFiles()) { String javaPackage = javaPackage(protoFile); putAll(nameToJavaName, javaPackage, null, protoFile.types()); for (Service service : protoFile.services()) { ClassName className = ClassName.get(javaPackage, service.type().simpleName()); nameToJavaName.put(service.type(), className); } } return new JavaGenerator(schema, nameToJavaName, new Profile(), false, false, false); }
private int countTypes(Schema prunedSchema) { int result = 0; for (ProtoFile protoFile : prunedSchema.protoFiles()) { result += protoFile.types().size(); } return result; }
private String resolveContext() { for (int i = contextStack.size() - 1; i >= 0; i--) { Object context = contextStack.get(i); if (context instanceof Type) { return ((Type) context).type().toString(); } else if (context instanceof ProtoFile) { String packageName = ((ProtoFile) context).packageName(); return packageName != null ? packageName : ""; } else if (context instanceof Field && ((Field) context).isExtension()) { String packageName = ((Field) context).packageName(); return packageName != null ? packageName : ""; } } throw new IllegalStateException(); }
private static ImmutableMap<String, Service> buildServicesIndex(Iterable<ProtoFile> protoFiles) { ImmutableMap.Builder<String, Service> result = ImmutableMap.builder(); for (ProtoFile protoFile : protoFiles) { for (Service service : protoFile.services()) { result.put(service.type().toString(), service); } } return result.build(); }
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()); }
for (Type type : protoFile.types()) { register(type); for (Extend extend : protoFile.extendList()) { extend.link(linker); for (Type type : protoFile.types()) { type.link(linker); for (Service service : protoFile.services()) { service.link(linker); protoFile.linkOptions(linker); for (Type type : protoFile.types()) { type.linkOptions(linker); for (Service service : protoFile.services()) { service.linkOptions(linker); publicImports.putAll(protoFile.location().path(), protoFile.publicImports()); Collection<String> sink = imports.get(protoFile.location().path()); addImports(sink, protoFile.imports(), publicImports); addImports(sink, protoFile.publicImports(), publicImports); for (Type type : protoFile.types()) { type.validate(linker); for (Service service : protoFile.services()) {