@Override public Object decode(ProtoReader reader) throws IOException { Integer value = ProtoAdapter.UINT32.decode(reader); EnumConstant constant = enumType.constant(value); return constant != null ? constant.name() : value; } }
void validateEnumConstantNameUniqueness(Iterable<Type> nestedTypes) { Multimap<String, EnumType> nameToType = LinkedHashMultimap.create(); for (Type type : nestedTypes) { if (type instanceof EnumType) { EnumType enumType = (EnumType) type; for (EnumConstant enumConstant : enumType.constants()) { nameToType.put(enumConstant.name(), enumType); } } } for (Map.Entry<String, Collection<EnumType>> entry : nameToType.asMap().entrySet()) { if (entry.getValue().size() > 1) { StringBuilder error = new StringBuilder(); String constant = entry.getKey(); int index = 1; error.append(String.format("multiple enums share constant %s:", constant)); for (EnumType enumType : entry.getValue()) { error.append(String.format("\n %s. %s.%s (%s)", index++, enumType.type(), constant, enumType.constant(constant).location())); } addError("%s", error); } } }
void validateEnumConstantNameUniqueness(Iterable<Type> nestedTypes) { Multimap<String, EnumType> nameToType = LinkedHashMultimap.create(); for (Type type : nestedTypes) { if (type instanceof EnumType) { EnumType enumType = (EnumType) type; for (EnumConstant enumConstant : enumType.constants()) { nameToType.put(enumConstant.name(), enumType); } } } for (Map.Entry<String, Collection<EnumType>> entry : nameToType.asMap().entrySet()) { if (entry.getValue().size() > 1) { StringBuilder error = new StringBuilder(); String constant = entry.getKey(); int index = 1; error.append(String.format("multiple enums share constant %s:", constant)); for (EnumType enumType : entry.getValue()) { error.append(String.format("\n %s. %s.%s (%s)", index++, enumType.type(), constant, enumType.constant(constant).location())); } addError("%s", error); } } }
@Override public void encode(ProtoWriter writer, Object value) throws IOException { if (value instanceof String) { EnumConstant constant = enumType.constant((String) value); writer.writeVarint32(constant.tag()); } else if (value instanceof Integer) { writer.writeVarint32((Integer) value); } else { throw new IllegalArgumentException("unexpected " + enumType.type() + ": " + value); } }
void validateEnumConstantNameUniqueness(Iterable<Type> nestedTypes) { Multimap<String, EnumType> nameToType = LinkedHashMultimap.create(); for (Type type : nestedTypes) { if (type instanceof EnumType) { EnumType enumType = (EnumType) type; for (EnumConstant enumConstant : enumType.constants()) { nameToType.put(enumConstant.name(), enumType); } } } for (Map.Entry<String, Collection<EnumType>> entry : nameToType.asMap().entrySet()) { if (entry.getValue().size() > 1) { StringBuilder error = new StringBuilder(); String constant = entry.getKey(); int index = 1; error.append(String.format("multiple enums share constant %s:", constant)); for (EnumType enumType : entry.getValue()) { error.append(String.format("\n %s. %s.%s (%s)", index++, enumType.type(), constant, enumType.constant(constant).location())); } addError("%s", error); } } }
@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); }
@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 enumWithRetainedConstantHasThatConstantRetained() throws Exception { Schema schema = new RepoBuilder() .add("service.proto", "" + "message Message {\n" + " optional Roshambo roshambo = 1;\n" + "}\n" + "enum Roshambo {\n" + " ROCK = 0;\n" + " SCISSORS = 1;\n" + " PAPER = 2;\n" + "}\n") .schema(); Schema pruned = schema.prune(new IdentifierSet.Builder() .include("Message") .include("Roshambo#SCISSORS") .build()); assertThat(pruned.getType("Message")).isNotNull(); assertThat(((MessageType) pruned.getType("Message")).field("roshambo")).isNotNull(); assertThat(pruned.getType("Roshambo")).isNotNull(); 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 excludeEnumExcludesOptions() throws Exception { Schema schema = new RepoBuilder() .add("message.proto", "" + "import \"google/protobuf/descriptor.proto\";\n" + "enum Enum {\n" + " A = 0;\n" + " B = 1 [message.c = 1];\n" + "}\n" + "extend google.protobuf.EnumValueOptions {\n" + " optional Message message = 70000;\n" + "};\n" + "message Message {\n" + " optional int32 c = 1;\n" + "}\n") .schema(); Schema pruned = schema.prune(new IdentifierSet.Builder() .include("Enum") .exclude("Enum#B") .build()); assertThat(((EnumType) pruned.getType("Enum")).constant("A")).isNotNull(); assertThat(((EnumType) pruned.getType("Enum")).constant("B")).isNull(); assertThat(pruned.getType("Message")).isNull(); }
EnumConstant constant = ((EnumType) type).constant(member); if (constant != null) { markOptions(constant.options());
@Override public Object decode(ProtoReader reader) throws IOException { Integer value = ProtoAdapter.UINT32.decode(reader); EnumConstant constant = enumType.constant(value); return constant != null ? constant.name() : value; } }
@Override public Object decode(ProtoReader reader) throws IOException { Integer value = ProtoAdapter.UINT32.decode(reader); EnumConstant constant = enumType.constant(value); return constant != null ? constant.name() : value; } }
@Override public void encode(ProtoWriter writer, Object value) throws IOException { if (value instanceof String) { EnumConstant constant = enumType.constant((String) value); writer.writeVarint32(constant.tag()); } else if (value instanceof Integer) { writer.writeVarint32((Integer) value); } else { throw new IllegalArgumentException("unexpected " + enumType.type() + ": " + value); } }
@Override public void encode(ProtoWriter writer, Object value) throws IOException { if (value instanceof String) { EnumConstant constant = enumType.constant((String) value); writer.writeVarint32(constant.tag()); } else if (value instanceof Integer) { writer.writeVarint32((Integer) value); } else { throw new IllegalArgumentException("unexpected " + enumType.type() + ": " + value); } }
EnumConstant constant = ((EnumType) type).constant(member); if (constant != null) { markOptions(constant.options());
EnumConstant constant = ((EnumType) type).constant(member); if (constant != null) { markOptions(constant.options());