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; }
private Schema retainRoots(IdentifierSet identifierSet, Schema schema) { Stopwatch stopwatch = Stopwatch.createStarted(); int oldSize = countTypes(schema); Schema prunedSchema = schema.prune(identifierSet); int newSize = countTypes(prunedSchema); for (String rule : identifierSet.unusedIncludes()) { getLog().warn(String.format("Unused include: %s", rule)); } for (String rule : identifierSet.unusedExcludes()) { getLog().warn(String.format("Unused exclude: %s", rule)); } getLog().info(String.format("Pruned schema from %s types to %s types in %s", oldSize, newSize, stopwatch)); return prunedSchema; }
@Test public void retainedOptionRetainsOptionsType() throws Exception { Schema schema = new RepoBuilder() .add("service.proto", "" + "import \"google/protobuf/descriptor.proto\";\n" + "extend google.protobuf.FieldOptions {\n" + " optional string a = 22001;\n" + "}\n" + "message Message {\n" + " optional string f = 1 [a = \"a\"];\n" + "}\n") .schema(); Schema pruned = schema.prune(new IdentifierSet.Builder() .include("Message#f") .build()); assertThat(((MessageType) pruned.getType("Message")).field("f")).isNotNull(); assertThat(((MessageType) pruned.getType("google.protobuf.FieldOptions"))).isNotNull(); }
@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(); }
@Test public void retainType() throws Exception { Schema schema = new RepoBuilder() .add("service.proto", "" + "message MessageA {\n" + "}\n" + "message MessageB {\n" + "}\n") .schema(); Schema pruned = schema.prune(new IdentifierSet.Builder() .include("MessageA") .build()); assertThat(pruned.getType("MessageA")).isNotNull(); assertThat(pruned.getType("MessageB")).isNull(); }
@Test public void retainExtension() throws Exception { Schema schema = new RepoBuilder() .add("service.proto", "" + "message Message {\n" + " optional string a = 1;\n" + "}\n" + "extend Message {\n" + " optional string b = 2;\n" + "}\n") .schema(); Schema pruned = schema.prune(new IdentifierSet.Builder() .include("Message") .build()); assertThat(((MessageType) pruned.getType("Message")).field("a")).isNotNull(); assertThat(((MessageType) pruned.getType("Message")).extensionField("b")).isNotNull(); }
@Test public void retainFieldPrunesOneOf() throws Exception { Schema schema = new RepoBuilder() .add("service.proto", "" + "message Message {\n" + " oneof selection {\n" + " string a = 1;\n" + " string b = 2;\n" + " }\n" + " optional string c = 3;\n" + "}\n") .schema(); Schema pruned = schema.prune(new IdentifierSet.Builder() .include("Message#c") .build()); assertThat(((MessageType) pruned.getType("Message")).oneOfs()).isEmpty(); }
@Test public void includePackage() throws Exception { Schema schema = new RepoBuilder() .add("a/b/messages.proto", "" + "package a.b;\n" + "message MessageAB {\n" + "}\n") .add("a/c/messages.proto", "" + "package a.c;\n" + "message MessageAC {\n" + "}\n") .schema(); Schema pruned = schema.prune(new IdentifierSet.Builder() .include("a.b.*") .build()); assertThat(pruned.getType("a.b.MessageAB")).isNotNull(); assertThat(pruned.getType("a.c.MessageAC")).isNull(); }
@Test public void excludePackage() throws Exception { Schema schema = new RepoBuilder() .add("a/b/messages.proto", "" + "package a.b;\n" + "message MessageAB {\n" + "}\n") .add("a/c/messages.proto", "" + "package a.c;\n" + "message MessageAC {\n" + "}\n") .schema(); Schema pruned = schema.prune(new IdentifierSet.Builder() .exclude("a.c.*") .build()); assertThat(pruned.getType("a.b.MessageAB")).isNotNull(); assertThat(pruned.getType("a.c.MessageAC")).isNull(); }
/** When we include excludes only, the mark phase is skipped. */ @Test public void excludeWithoutInclude() throws Exception { Schema schema = new RepoBuilder() .add("service.proto", "" + "message MessageA {\n" + " optional string b = 1;\n" + " optional string c = 2;\n" + "}\n") .schema(); Schema pruned = schema.prune(new IdentifierSet.Builder() .exclude("MessageA#c") .build()); assertThat(((MessageType) pruned.getType("MessageA")).field("b")).isNotNull(); assertThat(((MessageType) pruned.getType("MessageA")).field("c")).isNull(); }
@Test public void excludeMap() 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") .exclude("MessageA#maps") .build()); assertThat(pruned.getType("MessageA")).isNotNull(); assertThat(pruned.getField(ProtoMember.get("MessageA#maps"))).isNull(); }
@Test public void excludeTypeExcludesField() throws Exception { Schema schema = new RepoBuilder() .add("service.proto", "" + "message MessageA {\n" + " optional MessageB b = 1;\n" + " map<string, MessageC> c = 2;\n" + "}\n" + "message MessageB {\n" + "}\n" + "message MessageC {\n" + "}\n") .schema(); Schema pruned = schema.prune(new IdentifierSet.Builder() .include("MessageA") .exclude("MessageC") .build()); assertThat(pruned.getType("MessageB")).isNotNull(); assertThat(((MessageType) pruned.getType("MessageA")).field("b")).isNotNull(); assertThat(pruned.getType("MessageC")).isNull(); assertThat(((MessageType) pruned.getType("MessageA")).field("c")).isNull(); }
@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 retainField() throws Exception { Schema schema = new RepoBuilder() .add("service.proto", "" + "message MessageA {\n" + " optional string b = 1;\n" + " map<string, string> c = 2;\n" + "}\n" + "message MessageB {\n" + "}\n") .schema(); Schema pruned = schema.prune(new IdentifierSet.Builder() .include("MessageA#b") .build()); assertThat(((MessageType) pruned.getType("MessageA")).field("b")).isNotNull(); assertThat(((MessageType) pruned.getType("MessageA")).field("c")).isNull(); assertThat(pruned.getType("MessageB")).isNull(); }
@Test public void excludeField() throws Exception { Schema schema = new RepoBuilder() .add("service.proto", "" + "message MessageA {\n" + " optional string b = 1;\n" + " optional string c = 2;\n" + "}\n") .schema(); Schema pruned = schema.prune(new IdentifierSet.Builder() .include("MessageA") .exclude("MessageA#c") .build()); assertThat(((MessageType) pruned.getType("MessageA")).field("b")).isNotNull(); assertThat(((MessageType) pruned.getType("MessageA")).field("c")).isNull(); }
@Test public void retainEnumConstant() throws Exception { Schema schema = new RepoBuilder() .add("service.proto", "" + "enum Roshambo {\n" + " ROCK = 0;\n" + " SCISSORS = 1;\n" + " PAPER = 2;\n" + "}\n") .schema(); Schema pruned = schema.prune(new IdentifierSet.Builder() .include("Roshambo#SCISSORS") .build()); assertThat(((EnumType) pruned.getType("Roshambo")).constant("ROCK")).isNull(); assertThat(((EnumType) pruned.getType("Roshambo")).constant("SCISSORS")).isNotNull(); assertThat(((EnumType) pruned.getType("Roshambo")).constant("PAPER")).isNull(); }
@Test public void retainExtensionMembers() throws Exception { Schema schema = new RepoBuilder() .add("service.proto", "" + "message Message {\n" + " optional string a = 1;\n" + " optional string b = 2;\n" + "}\n" + "extend Message {\n" + " optional string c = 3;\n" + " optional string d = 4;\n" + "}\n") .schema(); Schema pruned = schema.prune(new IdentifierSet.Builder() .include("Message#a") .include("Message#c") .build()); assertThat(((MessageType) pruned.getType("Message")).field("a")).isNotNull(); assertThat(((MessageType) pruned.getType("Message")).field("b")).isNull(); assertThat(((MessageType) pruned.getType("Message")).extensionField("c")).isNotNull(); assertThat(((MessageType) pruned.getType("Message")).extensionField("d")).isNull(); }
@Test public void prunedOptionDoesNotRetainOptionsType() throws Exception { Schema schema = new RepoBuilder() .add("service.proto", "" + "import \"google/protobuf/descriptor.proto\";\n" + "extend google.protobuf.FieldOptions {\n" + " optional string a = 22001;\n" + "}\n" + "message Message {\n" + " optional string f = 1 [a = \"a\"];\n" + " optional string g = 2;\n" + "}\n") .schema(); Schema pruned = schema.prune(new IdentifierSet.Builder() .include("Message#g") .build()); assertThat(((MessageType) pruned.getType("google.protobuf.FieldOptions"))).isNull(); }
@Test public void excludedFieldPrunesTopLevelOption() throws Exception { Schema schema = new RepoBuilder() .add("service.proto", "" + "import \"google/protobuf/descriptor.proto\";\n" + "extend google.protobuf.FieldOptions {\n" + " optional string a = 22001;\n" + " optional string b = 22002;\n" + "}\n" + "message Message {\n" + " optional string f = 1 [a = \"a\", b = \"b\"];\n" + "}\n") .schema(); Schema pruned = schema.prune(new IdentifierSet.Builder() .exclude("google.protobuf.FieldOptions#b") .build()); Field field = ((MessageType) pruned.getType("Message")).field("f"); assertThat(field.options().get(ProtoMember.get(FIELD_OPTIONS, "a"))).isEqualTo("a"); assertThat(field.options().get(ProtoMember.get(FIELD_OPTIONS, "b"))).isNull(); }
@Test public void excludeOptions() throws Exception { Schema schema = new RepoBuilder() .add("service.proto", "" + "import \"google/protobuf/descriptor.proto\";\n" + "extend google.protobuf.FieldOptions {\n" + " optional string a = 22001;\n" + " optional string b = 22002;\n" + "}\n" + "message Message {\n" + " optional string f = 1 [ a = \"a\", b = \"b\" ];\n" + "}\n") .schema(); Schema pruned = schema.prune(new IdentifierSet.Builder() .exclude("google.protobuf.FieldOptions") .build()); Field field = ((MessageType) pruned.getType("Message")).field("f"); assertThat(field.options().map()).isEmpty(); }