private FileDescriptorProto generateFile(String name, FileContents contents) { FileDescriptorProto.Builder fileBuilder = FileDescriptorProto.newBuilder(); fileBuilder.setName(name); if (!Strings.isNullOrEmpty(contents.packageName)) { fileBuilder.setPackage(contents.packageName); } for (Api api : contents.apis) { fileBuilder.addService(generateApi(api)); } for (Type type : contents.types.values()) { fileBuilder.addMessageType(generateType(type, contents)); } for (Enum e : contents.enums) { fileBuilder.addEnumType(generateEnum(e)); } if (imports.containsKey(name)) { for (String imported : imports.get(name)) { fileBuilder.addDependency(imported); } } return fileBuilder.build(); }
@VisitsBefore void normalize(EnumType enumType) { Enum.Builder coreEnumBuilder = Enum.newBuilder().setName(enumType.getFullName()); coreEnumBuilder.setSourceContext( SourceContext.newBuilder() .setFileName(enumType.getFile().getLocation().getDisplayString())); coreEnumBuilder.setSyntax(enumType.getSyntax()); for (EnumValue value : enumType.getReachableValues()) { com.google.protobuf.EnumValue.Builder coreEnumValueBuilder = com.google.protobuf.EnumValue.newBuilder(); // Use simple name for enum value, as otherwise client has to use // fully qualified name in the request. coreEnumValueBuilder.setName(value.getSimpleName()).setNumber(value.getNumber()); coreEnumValueBuilder.addAllOptions( DescriptorNormalization.getOptions(value.getProto(), includeDefaults)); coreEnumBuilder.addEnumvalue(coreEnumValueBuilder.build()); } coreEnumBuilder.addAllOptions( DescriptorNormalization.getOptions(enumType.getProto(), includeDefaults)); enums.add(coreEnumBuilder.build()); }
/** * TODO (guptasu): Only needed to create NullValue enum. Check if this can be removed. * Create the Protobuf.Enum instance from enumDescriptorProto. */ private static Enum createEnum(String enumName, EnumDescriptorProto enumDescriptorProto, String fileName) { com.google.protobuf.Enum.Builder coreEnumBuilder = com.google.protobuf.Enum.newBuilder().setName(enumName); coreEnumBuilder.setSyntax(Syntax.SYNTAX_PROTO3); for (EnumValueDescriptorProto value : enumDescriptorProto.getValueList()) { com.google.protobuf.EnumValue.Builder coreEnumValueBuilder = com.google.protobuf.EnumValue.newBuilder(); coreEnumValueBuilder.setName(value.getName()).setNumber(value.getNumber()); coreEnumBuilder.addEnumvalue(coreEnumValueBuilder.build()); } coreEnumBuilder.setSourceContext(SourceContext.newBuilder().setFileName(fileName)); return coreEnumBuilder.build(); }
private EnumDescriptorProto generateEnum(Enum e) { EnumDescriptorProto.Builder builder = EnumDescriptorProto.newBuilder(); builder.setName(getSimpleName(e.getName())); for (EnumValue value : e.getEnumvalueList()) { EnumValueDescriptorProto.Builder valueBuilder = EnumValueDescriptorProto.newBuilder(); valueBuilder.setName(value.getName()); valueBuilder.setNumber(value.getNumber()); valueBuilder.setOptions(generateEnumValueOptions(value)); builder.addValue(valueBuilder.build()); } builder.setOptions(generateEnumOptions(e)); return builder.build(); }
/** * Creates additional Enums (NullValue) to be added to the Service config. */ // TODO (guptasu): Fix this hack. Find a better way to add the predefined types. // TODO (guptasu): Add them only when required and not in all cases. static Iterable<Enum> createAdditionalServiceEnums() { // TODO (guptasu): Remove this hard coding. Without this, creation of Model from Service throws. // Needs investigation. String fileName = "struct.proto"; List<Enum> additionalEnums = Lists.newArrayList(); additionalEnums.add(TypesBuilderFromDescriptor.createEnum(NullValue.getDescriptor().getFullName(), NullValue.getDescriptor().toProto(), fileName)); return additionalEnums; }
@Test public void testMismatch() throws Exception { Marshaller<Enum> enumMarshaller = ProtoLiteUtils.marshaller(Enum.getDefaultInstance()); // Enum's name and Type's name are both strings with tag 1. Enum altProto = Enum.newBuilder().setName(proto.getName()).build(); assertEquals(proto, marshaller.parse(enumMarshaller.stream(altProto))); }
@Test public void introspection() throws Exception { Marshaller<Enum> enumMarshaller = ProtoLiteUtils.marshaller(Enum.getDefaultInstance()); PrototypeMarshaller<Enum> prototypeMarshaller = (PrototypeMarshaller<Enum>) enumMarshaller; assertSame(Enum.getDefaultInstance(), prototypeMarshaller.getMessagePrototype()); assertSame(Enum.class, prototypeMarshaller.getMessageClass()); }
private EnumOptions generateEnumOptions(Enum e) { EnumOptions.Builder builder = EnumOptions.newBuilder(); setOptions(builder, e.getOptionsList(), ENUM_OPTION_NAME_PREFIX); return builder.build(); }