NameAllocator nameAllocator = nameAllocators.getUnchecked(type); String value = nameAllocator.get("value"); ClassName javaType = (ClassName) typeName(type.type()); .addSuperinterface(WireEnum.class); if (!type.documentation().isEmpty()) { builder.addJavadoc("$L\n", sanitizeJavadoc(type.documentation())); for (EnumConstant constant : type.constants()) { for (ProtoMember protoMember : constant.options().map().keySet()) { Field optionField = schema.getField(protoMember); for (EnumConstant constant : type.constants()) { Object[] enumArgs = new Object[allOptionMembers.size() + 1]; enumArgs[0] = constant.tag(); FieldSpec options = optionsField(ENUM_OPTIONS, "ENUM_OPTIONS", type.options()); if (options != null) { builder.addField(options);
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 Type retainAll(Schema schema, MarkSet markSet) { // If this type is not retained, prune it. if (!markSet.contains(protoType)) return null; ImmutableList.Builder<EnumConstant> retainedConstants = ImmutableList.builder(); for (EnumConstant constant : constants) { if (markSet.contains(ProtoMember.get(protoType, constant.name()))) { retainedConstants.add(constant.retainAll(schema, markSet)); } } EnumType result = new EnumType(protoType, location, documentation, name, retainedConstants.build(), options.retainAll(schema, markSet)); result.allowAlias = allowAlias; return result; }
@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); } }
/** Returns the constant named {@code name}, or null if this enum has no such constant. */ public EnumConstant constant(String name) { for (EnumConstant constant : constants()) { if (constant.name().equals(name)) { return constant; } } return null; }
@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; } }
private String getDescriptionFromEnumType(EnumType type) { String format = "* %d - %s %s\n"; StringBuffer sb = new StringBuffer(); sb.append(type.documentation()); sb.append("\n"); for (EnumConstant enumConstant : type.constants()) { sb.append(String.format(format, enumConstant.tag(), enumConstant.name(),enumConstant.documentation())); } return sb.toString(); }
public static Type get(String packageName, ProtoType protoType, TypeElement type) { if (type instanceof EnumElement) { return EnumType.fromElement(protoType, (EnumElement) type); } else if (type instanceof MessageElement) { return MessageType.fromElement(packageName, protoType, (MessageElement) type); } else { throw new IllegalArgumentException("unexpected type: " + type.getClass()); } }
@Override void validate(Linker linker) { linker = linker.withContext(this); if (!"true".equals(allowAlias)) { validateTagUniqueness(linker); } }
static TypeElement toElement(Type type) { if (type instanceof EnumType) { return ((EnumType) type).toElement(); } else if (type instanceof MessageType) { return ((MessageType) type).toElement(); } else if (type instanceof EnclosingType) { return ((EnclosingType) type).toElement(); } else { throw new IllegalArgumentException("unexpected type: " + type.getClass()); } }
EnumConstant enumDefault(ProtoType type) { EnumType wireEnum = (EnumType) schema.getType(type); return wireEnum.constants().get(0); }
@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); } }
@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 static Type get(String packageName, ProtoType protoType, TypeElement type) { if (type instanceof EnumElement) { return EnumType.fromElement(protoType, (EnumElement) type); } else if (type instanceof MessageElement) { return MessageType.fromElement(packageName, protoType, (MessageElement) type); } else { throw new IllegalArgumentException("unexpected type: " + type.getClass()); } }
@Override void validate(Linker linker) { linker = linker.withContext(this); if (!"true".equals(allowAlias)) { validateTagUniqueness(linker); } }
static TypeElement toElement(Type type) { if (type instanceof EnumType) { return ((EnumType) type).toElement(); } else if (type instanceof MessageType) { return ((MessageType) type).toElement(); } else { throw new IllegalArgumentException("unexpected type: " + type.getClass()); } }