private Schema getSchema(EnumDescriptor d) { List<String> symbols = new ArrayList<>(); for (EnumValueDescriptor e : d.getValues()) { symbols.add(e.getName()); } return Schema.createEnum(d.getName(), null, getNamespace(d.getFile(), d.getContainingType()), symbols); }
@SuppressWarnings("unchecked") private static Class<? extends Enum> getEnumClassByEnumDescriptor(Descriptors.EnumDescriptor descriptor, BeanContainer beanContainer) { String name = StringUtils.join(getFullyQualifiedClassName(descriptor.getContainingType(), descriptor.getFile().getOptions(), descriptor.getName()), '.'); return (Class<? extends Enum>)MappingUtils.loadClass(name, beanContainer); }
private Schema getSchema(EnumDescriptor d) { List<String> symbols = new ArrayList<String>(); for (EnumValueDescriptor e : d.getValues()) { symbols.add(e.getName()); } return Schema.createEnum(d.getName(), null, getNamespace(d.getFile(), d.getContainingType()), symbols); }
private EnumValueDescriptor parseEnum(EnumDescriptor enumDescriptor, JsonElement json) throws InvalidProtocolBufferException { String value = json.getAsString(); EnumValueDescriptor result = enumDescriptor.findValueByName(value); if (result == null) { // Try to interpret the value as a number. try { int numericValue = parseInt32(json); if (enumDescriptor.getFile().getSyntax() == FileDescriptor.Syntax.PROTO3) { result = enumDescriptor.findValueByNumberCreatingIfUnknown(numericValue); } else { result = enumDescriptor.findValueByNumber(numericValue); } } catch (InvalidProtocolBufferException e) { // Fall through. This exception is about invalid int32 value we get from parseInt32() but // that's not the exception we want the user to see. Since result == null, we will throw // an exception later. } if (result == null) { throw new InvalidProtocolBufferException( "Invalid enum value: " + value + " for enum type: " + enumDescriptor.getFullName()); } } return result; }
private EnumValueDescriptor parseEnum(EnumDescriptor enumDescriptor, JsonElement json) throws InvalidProtocolBufferException { String value = json.getAsString(); EnumValueDescriptor result = enumDescriptor.findValueByName(value); if (result == null) { // Try to interpret the value as a number. try { int numericValue = parseInt32(json); if (enumDescriptor.getFile().getSyntax() == FileDescriptor.Syntax.PROTO3) { result = enumDescriptor.findValueByNumberCreatingIfUnknown(numericValue); } else { result = enumDescriptor.findValueByNumber(numericValue); } } catch (InvalidProtocolBufferException e) { // Fall through. This exception is about invalid int32 value we get from parseInt32() but // that's not the exception we want the user to see. Since result == null, we will throw // an exception later. } if (result == null) { throw new InvalidProtocolBufferException( "Invalid enum value: " + value + " for enum type: " + enumDescriptor.getFullName()); } } return result; }
private EnumValueDescriptor parseEnum(EnumDescriptor enumDescriptor, JsonElement json) throws InvalidProtocolBufferException { String value = json.getAsString(); EnumValueDescriptor result = enumDescriptor.findValueByName(value); if (result == null) { // Try to interpret the value as a number. try { int numericValue = parseInt32(json); if (enumDescriptor.getFile().getSyntax() == FileDescriptor.Syntax.PROTO3) { result = enumDescriptor.findValueByNumberCreatingIfUnknown(numericValue); } else { result = enumDescriptor.findValueByNumber(numericValue); } } catch (InvalidProtocolBufferException e) { // Fall through. This exception is about invalid int32 value we get from parseInt32() but // that's not the exception we want the user to see. Since result == null, we will throw // an exception later. } if (result == null) { throw new InvalidProtocolBufferException( "Invalid enum value: " + value + " for enum type: " + enumDescriptor.getFullName()); } } return result; }
private EnumValueDescriptor parseEnum(EnumDescriptor enumDescriptor, JsonElement json) throws InvalidProtocolBufferException { String value = json.getAsString(); EnumValueDescriptor result = enumDescriptor.findValueByName(value); if (result == null) { // Try to interpret the value as a number. try { int numericValue = parseInt32(json); if (enumDescriptor.getFile().getSyntax() == FileDescriptor.Syntax.PROTO3) { result = enumDescriptor.findValueByNumberCreatingIfUnknown(numericValue); } else { result = enumDescriptor.findValueByNumber(numericValue); } } catch (InvalidProtocolBufferException e) { // Fall through. This exception is about invalid int32 value we get from parseInt32() but // that's not the exception we want the user to see. Since result == null, we will throw // an exception later. } if (result == null) { throw new InvalidProtocolBufferException( "Invalid enum value: " + value + " for enum type: " + enumDescriptor.getFullName()); } } return result; }
@Override Object protoFromSoy(SoyValue field) { // The proto reflection api wants the EnumValueDescriptor, not the actual enum instance int value = field.integerValue(); // in proto3 we preserve unknown enum values (for consistency with jbcsrc), but for proto2 // we don't, and so if the field is unknown we will return null which will trigger an NPE // again, for consistency with jbcsrc. if (enumDescriptor.getFile().getSyntax() == Syntax.PROTO3) { return enumDescriptor.findValueByNumberCreatingIfUnknown(value); } return enumDescriptor.findValueByNumber(value); } };
@SuppressWarnings("unchecked") private static Class<? extends Enum> getEnumClassByEnumDescriptor(Descriptors.EnumDescriptor descriptor, BeanContainer beanContainer) { String name = StringUtils.join(getFullyQualifiedClassName(descriptor.getContainingType(), descriptor.getFile().getOptions(), descriptor.getName()), '.'); return (Class<? extends Enum>)MappingUtils.loadClass(name, beanContainer); }
/** * Gets the fully qualified name for generated classes in Java convention. Nested classes will be * separated using '$' instead of '.'. */ public static String getClassName(EnumDescriptor descriptor, ProtoFlavor flavor) { return getClassName(classNameWithoutPackage(descriptor, flavor), descriptor.getFile(), flavor); }
@Override Object protoFromSoy(SoyValue field) { // The proto reflection api wants the EnumValueDescriptor, not the actual enum instance int value = field.integerValue(); // in proto3 we preserve unknown enum values (for consistency with jbcsrc), but for proto2 // we don't, and so if the field is unknown we will return null which will trigger an NPE // again, for consistency with jbcsrc. if (enumDescriptor.getFile().getSyntax() == Syntax.PROTO3) { return enumDescriptor.findValueByNumberCreatingIfUnknown(value); } return enumDescriptor.findValueByNumber(value); } };
/** * Gets the fully qualified name for generated classes in Java convention. Nested classes will be * separated using '$' instead of '.'. */ public static String getClassName(EnumDescriptor descriptor, ProtoFlavor flavor) { return getClassName(classNameWithoutPackage(descriptor, flavor), descriptor.getFile(), flavor); }