public boolean isEnum(ProtoType type) { return schema.getType(type) instanceof EnumType; }
private String fieldName(ProtoType type, Field field) { MessageType messageType = (MessageType) schema.getType(type); NameAllocator names = nameAllocators.getUnchecked(messageType); return names.get(field); }
/** * Returns the type with the fully qualified name {@code name}, or null if this schema defines no * such type. */ public Type getType(ProtoType protoType) { return getType(protoType.toString()); }
EnumConstant enumDefault(ProtoType type) { EnumType wireEnum = (EnumType) schema.getType(type); return wireEnum.constants().get(0); }
/** * Returns the Java type of the abstract adapter class generated for a corresponding {@code * protoType}. Returns null if {@code protoType} is not using a custom proto adapter. */ public @Nullable ClassName abstractAdapterName(ProtoType protoType) { TypeName profileJavaName = profile.getTarget(protoType); if (profileJavaName == null) return null; ClassName javaName = nameToJavaName.get(protoType); Type type = schema.getType(protoType); return type instanceof EnumType ? javaName.peerClass(javaName.simpleName() + "Adapter") : javaName.peerClass("Abstract" + javaName.simpleName() + "Adapter"); }
public String generateCode(String typeName, String profile) throws IOException { Schema schema = schema(); JavaGenerator javaGenerator = JavaGenerator.get(schema); if (profile != null) { javaGenerator = javaGenerator.withProfile(profile(profile)); } Type type = schema.getType(typeName); TypeSpec typeSpec = javaGenerator.generateType(type); ClassName typeName1 = javaGenerator.generatedTypeName(type); return JavaFile.builder(typeName1.packageName(), typeSpec).build().toString(); }
public Field getField(ProtoMember protoMember) { Type type = getType(protoMember.type()); if (!(type instanceof MessageType)) return null; Field field = ((MessageType) type).field(protoMember.member()); if (field == null) { field = ((MessageType) type).extensionField(protoMember.member()); } return field; }
@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 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 duplicateEnumConstantTagWithAllowAliasTrueAllowed() throws Exception { Schema schema = new RepoBuilder() .add("message.proto", "" + "enum Enum {\n" + " option allow_alias = true;\n" + " A = 1;\n" + " B = 1;\n" + "}\n") .schema(); EnumType enumType = (EnumType) schema.getType("Enum"); assertThat(enumType.constant("A").tag()).isEqualTo(1); assertThat(enumType.constant("B").tag()).isEqualTo(1); }
public String generateKotlin(String typeName) { Schema schema = schema(); KotlinGenerator kotlinGenerator = KotlinGenerator.get(schema, false, false); com.squareup.kotlinpoet.TypeSpec typeSpec = kotlinGenerator.generateType(schema.getType(typeName)); FileSpec fileSpec = FileSpec.builder("", "_") .addType(typeSpec) .addImport("com.squareup.wire.kotlin", "decodeMessage") .build(); return fileSpec.toString(); } }
@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 scalarFieldIsPacked() throws Exception { Schema schema = new RepoBuilder() .add("message.proto", "" + "message Message {\n" + " repeated int32 a = 1;\n" + " repeated int32 b = 2 [packed=false];\n" + " repeated int32 c = 3 [packed=true];\n" + "}\n") .schema(); MessageType message = (MessageType) schema.getType("Message"); assertThat(message.field("a").isPacked()).isFalse(); assertThat(message.field("b").isPacked()).isFalse(); assertThat(message.field("c").isPacked()).isTrue(); }
@Test public void fieldIsDeprecated() throws Exception { Schema schema = new RepoBuilder() .add("message.proto", "" + "message Message {\n" + " optional int32 a = 1;\n" + " optional int32 b = 2 [deprecated=false];\n" + " optional int32 c = 3 [deprecated=true];\n" + "}\n") .schema(); MessageType message = (MessageType) schema.getType("Message"); assertThat(message.field("a").isDeprecated()).isFalse(); assertThat(message.field("b").isDeprecated()).isFalse(); assertThat(message.field("c").isDeprecated()).isTrue(); }
@Test public void enumFieldIsPacked() throws Exception { Schema schema = new RepoBuilder() .add("message.proto", "" + "message Message {\n" + " repeated HabitablePlanet home_planet = 1 [packed=true];\n" + " enum HabitablePlanet {\n" + " EARTH = 1;\n" + " }\n" + "}\n") .schema(); MessageType message = (MessageType) schema.getType("Message"); assertThat(message.field("home_planet").isPacked()).isTrue(); }
/** 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 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 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 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 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(); }