private int countTypes(Schema prunedSchema) { int result = 0; for (ProtoFile protoFile : prunedSchema.protoFiles()) { result += protoFile.types().size(); } return result; }
@Test public void retainTypeRetainsFieldTypesTransitively() throws Exception { Schema schema = new RepoBuilder() .add("service.proto", "" + "message MessageA {\n" + " optional MessageB b = 1;\n" + "}\n" + "message MessageB {\n" + " map<string, MessageC> c = 1;\n" + "}\n" + "message MessageC {\n" + "}\n" + "message MessageD {\n" + "}\n") .schema(); Schema pruned = schema.prune(new IdentifierSet.Builder() .include("MessageA") .build()); assertThat(pruned.getType("MessageA")).isNotNull(); assertThat(pruned.getType("MessageB")).isNotNull(); assertThat(pruned.getType("MessageC")).isNotNull(); assertThat(pruned.getType("MessageD")).isNull(); }
@Test public void retainMap() throws Exception { Schema schema = new RepoBuilder() .add("service.proto", "" + "message MessageA {\n" + " map<string, MessageB> maps = 1;\n" + " message MessageB {\n" + " }\n" + "}\n") .schema(); Schema pruned = schema.prune(new IdentifierSet.Builder() .include("MessageA") .build()); assertThat(pruned.getType("MessageA")).isNotNull(); assertThat(pruned.getField(ProtoMember.get("MessageA#maps"))).isNotNull(); }
Schema(Iterable<ProtoFile> protoFiles) { this.protoFiles = PATH_ORDER.immutableSortedCopy(protoFiles); this.typesIndex = buildTypesIndex(protoFiles); this.servicesIndex = buildServicesIndex(protoFiles); }
public Schema prune() { markRoots(); markReachable(); ImmutableList.Builder<ProtoFile> retained = ImmutableList.builder(); for (ProtoFile protoFile : schema.protoFiles()) { retained.add(protoFile.retainAll(schema, marks)); } return new Schema(retained.build()); }
public boolean isEnum(ProtoType type) { return schema.getType(type) instanceof EnumType; }
@Test public void retainRpcRetainsRequestAndResponseTypes() throws Exception { Schema schema = new RepoBuilder() .add("service.proto", "" + "message RequestA {\n" + "}\n" + "message ResponseA {\n" + "}\n" + "message RequestB {\n" + "}\n" + "message ResponseB {\n" + "}\n" + "service Service {\n" + " rpc CallA (RequestA) returns (ResponseA);\n" + " rpc CallB (RequestB) returns (ResponseB);\n" + "}\n") .schema(); Schema pruned = schema.prune(new IdentifierSet.Builder() .include("Service#CallA") .build()); assertThat(pruned.getService("Service").rpc("CallA")).isNotNull(); assertThat(pruned.getType("RequestA")).isNotNull(); assertThat(pruned.getType("ResponseA")).isNotNull(); assertThat(pruned.getService("Service").rpc("CallB")).isNull(); assertThat(pruned.getType("RequestB")).isNull(); assertThat(pruned.getType("ResponseB")).isNull(); }
mark(protoMember.type()); String member = ((ProtoMember) root).member(); Type type = schema.getType(protoMember.type()); if (type instanceof MessageType) { Field field = ((MessageType) type).field(member); Service service = schema.getService(protoMember.type()); if (service != null) { Rpc rpc = service.rpc(member); Type type = schema.getType(protoType); if (type != null) { markType(type); Service service = schema.getService(protoType); if (service != null) { markService(service);
@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()); }
ProtoMember protoMember = (ProtoMember) entry.getKey(); if (!markSet.contains(protoMember)) continue; // Prune this field. Field field = schema.getField(protoMember); Object retainedValue = retainAll(schema, markSet, field.type(), entry.getValue()); if (retainedValue != null) {
/** * Returns the service with the fully qualified name {@code name}, or null if this schema defines * no such service. */ public Service getService(ProtoType protoType) { return getService(protoType.toString()); }
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); }
private Schema retainRoots(Schema schema) { Stopwatch stopwatch = Stopwatch.createStarted(); int oldSize = countTypes(schema); Schema prunedSchema = schema.prune(identifierSet); int newSize = countTypes(prunedSchema); log.info("Pruned schema from %s types to %s types in %s", oldSize, newSize, stopwatch); return prunedSchema; }
return new Schema(protoFiles);
private String fieldName(ProtoType type, Field field) { MessageType messageType = (MessageType) schema.getType(type); NameAllocator names = nameAllocators.getUnchecked(messageType); return names.get(field); }
@Test public void excludeRpcExcludesTypes() throws Exception { Schema schema = new RepoBuilder() .add("service.proto", "" + "service ServiceA {\n" + " rpc CallB (MessageB) returns (MessageB);\n" + " rpc CallC (MessageC) returns (MessageC);\n" + "}\n" + "message MessageB {\n" + "}\n" + "message MessageC {\n" + "}\n") .schema(); Schema pruned = schema.prune(new IdentifierSet.Builder() .include("ServiceA") .exclude("ServiceA#CallC") .build()); assertThat(pruned.getType("MessageB")).isNotNull(); assertThat(pruned.getService("ServiceA").rpc("CallB")).isNotNull(); assertThat(pruned.getType("MessageC")).isNull(); assertThat(pruned.getService("ServiceA").rpc("CallC")).isNull(); }
@Test public void linkService() throws Exception { Schema schema = new RepoBuilder() .add("service.proto", "" + "import \"request.proto\";\n" + "import \"response.proto\";\n" + "service Service {\n" + " rpc Call (Request) returns (Response);\n" + "}\n") .add("request.proto", "" + "message Request {\n" + "}\n") .add("response.proto", "" + "message Response {\n" + "}\n") .schema(); Service service = schema.getService("Service"); Rpc call = service.rpc("Call"); assertThat(call.requestType()).isEqualTo(schema.getType("Request").type()); assertThat(call.responseType()).isEqualTo(schema.getType("Response").type()); }
public Schema prune() { markRoots(); markReachable(); ImmutableList.Builder<ProtoFile> retained = ImmutableList.builder(); for (ProtoFile protoFile : schema.protoFiles()) { retained.add(protoFile.retainAll(schema, marks)); } return new Schema(retained.build()); }
private FieldSpec optionsField(ProtoType optionsType, String fieldName, Options options) { TypeName optionsJavaType = typeName(optionsType); CodeBlock.Builder initializer = CodeBlock.builder(); initializer.add("$[new $T.Builder()", optionsJavaType); boolean empty = true; for (Map.Entry<ProtoMember, ?> entry : options.map().entrySet()) { if (entry.getKey().equals(FIELD_DEPRECATED) || entry.getKey().equals(PACKED)) { continue; } Field optionField = schema.getField(entry.getKey()); initializer.add("\n.$L($L)", fieldName(optionsType, optionField), fieldInitializer(optionField.type(), entry.getValue())); empty = false; } initializer.add("\n.build()$]"); if (empty) return null; return FieldSpec.builder(optionsJavaType, fieldName) .addModifiers(PUBLIC, STATIC, FINAL) .initializer(initializer.build()) .build(); }
Schema(Iterable<ProtoFile> protoFiles) { this.protoFiles = PATH_ORDER.immutableSortedCopy(protoFiles); this.typesIndex = buildTypesIndex(protoFiles); this.servicesIndex = buildServicesIndex(protoFiles); }