public static ProtoMember get(String typeAndMember) { int hash = typeAndMember.indexOf('#'); if (hash == -1) throw new IllegalArgumentException("expected a '#' in " + typeAndMember); ProtoType type = ProtoType.get(typeAndMember.substring(0, hash)); String member = typeAndMember.substring(hash + 1); return new ProtoMember(type, member); }
public static ProtoType get(String name) { ProtoType scalar = SCALAR_TYPES.get(name); if (scalar != null) return scalar; if (name == null || name.isEmpty() || name.contains("#")) { throw new IllegalArgumentException("unexpected name: " + name); } if (name.startsWith("map<") && name.endsWith(">")) { int comma = name.indexOf(','); if (comma == -1) throw new IllegalArgumentException("expected ',' in map type: " + name); ProtoType key = get(name.substring(4, comma).trim()); ProtoType value = get(name.substring(comma + 1, name.length() - 1).trim()); return new ProtoType(key, value, name); } return new ProtoType(false, name); }
static ImmutableList<Service> fromElements(String packageName, ImmutableList<ServiceElement> elements) { ImmutableList.Builder<Service> services = ImmutableList.builder(); for (ServiceElement service : elements) { ProtoType protoType = ProtoType.get(packageName, service.name()); services.add(Service.fromElement(protoType, service)); } return services.build(); }
static ImmutableList<Type> fromElements(String packageName, ImmutableList<TypeElement> elements) { ImmutableList.Builder<Type> types = new ImmutableList.Builder<>(); for (TypeElement element : elements) { ProtoType protoType = ProtoType.get(packageName, element.name()); types.add(Type.get(packageName, protoType, element)); } return types.build(); }
@Test public void get() throws Exception { assertThat(ProtoType.get("int32")).isSameAs(ProtoType.INT32); assertThat(ProtoType.get("Person")).isEqualTo(ProtoType.get("Person")); assertThat(ProtoType.get("squareup.protos.person", "Person")) .isEqualTo(ProtoType.get("squareup.protos.person.Person")); }
@Test public void simpleName() throws Exception { ProtoType person = ProtoType.get("squareup.protos.person.Person"); assertThat(person.simpleName()).isEqualTo("Person"); }
@Test public void mapFormat() throws Exception { try { ProtoType.get("map<string>"); fail(); } catch (IllegalArgumentException e) { assertThat(e).hasMessage("expected ',' in map type: map<string>"); } }
@Test public void isScalar() throws Exception { assertThat(ProtoType.INT32.isScalar()).isTrue(); assertThat(ProtoType.STRING.isScalar()).isTrue(); assertThat(ProtoType.BYTES.isScalar()).isTrue(); assertThat(ProtoType.get("squareup.protos.person.Person").isScalar()).isFalse(); } }
@Test public void mapKeyScalarType() throws Exception { try { ProtoType.get("map<bytes, string>"); fail(); } catch (IllegalArgumentException expected) { } try { ProtoType.get("map<double, string>"); fail(); } catch (IllegalArgumentException expected) { } try { ProtoType.get("map<float, string>"); fail(); } catch (IllegalArgumentException expected) { } try { ProtoType.get("map<some.Message, string>"); fail(); } catch (IllegalArgumentException expected) { } }
@Test public void messageToString() throws Exception { ProtoType person = ProtoType.get("squareup.protos.person.Person"); assertThat(person.toString()).isEqualTo("squareup.protos.person.Person"); ProtoType phoneType = person.nestedType("PhoneType"); assertThat(phoneType.toString()).isEqualTo("squareup.protos.person.Person.PhoneType"); }
@Test public void enclosingTypeOrPackage() throws Exception { assertThat(ProtoType.STRING.enclosingTypeOrPackage()).isNull(); ProtoType person = ProtoType.get("squareup.protos.person.Person"); assertThat(person.enclosingTypeOrPackage()).isEqualTo("squareup.protos.person"); ProtoType phoneType = person.nestedType("PhoneType"); assertThat(phoneType.enclosingTypeOrPackage()).isEqualTo("squareup.protos.person.Person"); }
@Test public void mapsCannotNest() throws Exception { try { ProtoType.get("map<string, string>").nestedType("PhoneType"); fail(); } catch (UnsupportedOperationException expected) { } }
private Policy policy(IdentifierSet set, String identifier) { if (identifier.contains("#")) { ProtoMember protoMember = ProtoMember.get(identifier); if (set.includes(protoMember)) return Policy.INCLUDED; if (set.excludes(protoMember)) return Policy.EXCLUDED; return Policy.UNSPECIFIED; } else { ProtoType protoType = ProtoType.get(identifier); if (set.includes(protoType)) return Policy.INCLUDED; if (set.excludes(protoType)) return Policy.EXCLUDED; return Policy.UNSPECIFIED; } }
@Test public void trackingUnusedPrecedence() throws Exception { IdentifierSet set = new IdentifierSet.Builder() .include("a.*") .exclude("a.*") .build(); set.includes(ProtoType.get("a.Message")); assertThat(set.unusedExcludes()).isEmpty(); assertThat(set.unusedIncludes()).containsExactly("a.*"); }
@Test public void importResolvesNestedPackageSuffix() throws Exception { Schema schema = new RepoBuilder() .add("a_b.proto", "" + "package a.b;\n" + "\n" + "import \"a_b_c.proto\";\n" + "\n" + "message MessageB {\n" + " optional c.MessageC message_c = 1;\n" + "}\n") .add("a_b_c.proto", "" + "package a.b.c;\n" + "\n" + "message MessageC {\n" + "}\n") .schema(); MessageType messageC = (MessageType) schema.getType("a.b.MessageB"); assertThat(messageC.field("message_c").type()).isEqualTo(ProtoType.get("a.b.c.MessageC")); }
@Test public void trackingUnusedExcludes() throws Exception { IdentifierSet set = new IdentifierSet.Builder() .exclude("a.*") .exclude("b.ExcludedType") .exclude("c.ExcludedMember#member") .build(); assertThat(set.unusedExcludes()).containsExactly( "a.*", "b.ExcludedType", "c.ExcludedMember#member"); set.includes(ProtoType.get("a.*")); assertThat(set.unusedExcludes()).containsExactly( "b.ExcludedType", "c.ExcludedMember#member"); set.includes(ProtoType.get("b.ExcludedType")); assertThat(set.unusedExcludes()).containsExactly( "c.ExcludedMember#member"); set.includes(ProtoMember.get("c.ExcludedMember#member")); assertThat(set.unusedExcludes()).isEmpty(); }
@Test public void importResolvesEnclosingPackageSuffix() throws Exception { Schema schema = new RepoBuilder() .add("a_b.proto", "" + "package a.b;\n" + "\n" + "message MessageB {\n" + "}\n") .add("a_b_c.proto", "" + "package a.b.c;\n" + "\n" + "import \"a_b.proto\";\n" + "\n" + "message MessageC {\n" + " optional b.MessageB message_b = 1;\n" + "}\n") .schema(); MessageType messageC = (MessageType) schema.getType("a.b.c.MessageC"); assertThat(messageC.field("message_b").type()).isEqualTo(ProtoType.get("a.b.MessageB")); }
@Test public void trackingUnusedIncludes() throws Exception { IdentifierSet set = new IdentifierSet.Builder() .include("a.*") .include("b.IncludedType") .include("c.IncludedMember#member") .build(); assertThat(set.unusedIncludes()).containsExactly( "a.*", "b.IncludedType", "c.IncludedMember#member"); set.includes(ProtoType.get("a.*")); assertThat(set.unusedIncludes()).containsExactly( "b.IncludedType", "c.IncludedMember#member"); set.includes(ProtoType.get("b.IncludedType")); assertThat(set.unusedIncludes()).containsExactly( "c.IncludedMember#member"); set.includes(ProtoMember.get("c.IncludedMember#member")); assertThat(set.unusedIncludes()).isEmpty(); }