@Override public void serialize(Value value, JsonGenerator gen, SerializerProvider provider) throws IOException { switch (value.getType()) { case BOOLEAN: gen.writeBoolean(value.getBoolean()); break; case INTEGER: gen.writeNumber(value.getInt64()); break; case FLOAT: gen.writeNumber(value.getDouble()); break; case STRING: gen.writeString(value.getString()); break; case ENUM: gen.writeString(value.getEnumName()); break; case MESSAGE: gen.writeObject(value.getMessage()); break; default: throw new IllegalStateException(String.valueOf(value.getType())); } } }
SourceCodeLocation sourceCodeLocation = getSourceCodeLocation(optionValueContext); if (optionValueContext.textFormat() != null) { optionValue = DynamicMessage.Value.createMessage(sourceCodeLocation, lastTextFormat); } else if (optionValueContext.BOOLEAN_VALUE() != null) { String text = optionValueContext.BOOLEAN_VALUE().getText(); boolean value = Boolean.parseBoolean(text); optionValue = DynamicMessage.Value.createBoolean(sourceCodeLocation, value); } else if (optionValueContext.INTEGER_VALUE() != null) { String text = optionValueContext.INTEGER_VALUE().getText(); String text = optionValueContext.STRING_VALUE().getText(); optionValue = DynamicMessage.Value.createString(sourceCodeLocation, Util.removeFirstAndLastChar(text)); } else if (optionValueContext.IDENT() != null) { String text = optionValueContext.IDENT().getText(); optionValue = DynamicMessage.Value.createEnum(sourceCodeLocation, text); } else if (optionValueContext.FLOAT_VALUE() != null) { String text = optionValueContext.FLOAT_VALUE().getText(); value = Double.parseDouble(text); optionValue = DynamicMessage.Value.createFloat(sourceCodeLocation, value); } else { throw new IllegalStateException();
SourceCodeLocation sourceCodeLocation = getSourceCodeLocation(optionValueContext); if (optionValueContext.textFormat() != null) { optionValue = DynamicMessage.Value.createMessage(sourceCodeLocation, lastTextFormat); } else if (optionValueContext.BOOLEAN_VALUE() != null) { String text = optionValueContext.BOOLEAN_VALUE().getText(); boolean value = Boolean.parseBoolean(text); optionValue = DynamicMessage.Value.createBoolean(sourceCodeLocation, value); } else if (optionValueContext.INTEGER_VALUE() != null) { String text = optionValueContext.INTEGER_VALUE().getText(); String text = optionValueContext.STRING_VALUE().getText(); optionValue = DynamicMessage.Value.createString(sourceCodeLocation, Util.removeFirstAndLastChar(text)); } else if (optionValueContext.IDENT() != null) { String text = optionValueContext.IDENT().getText(); optionValue = DynamicMessage.Value.createEnum(sourceCodeLocation, text); } else if (optionValueContext.FLOAT_VALUE() != null) { String text = optionValueContext.FLOAT_VALUE().getText(); value = Double.parseDouble(text); optionValue = DynamicMessage.Value.createFloat(sourceCodeLocation, value); } else { throw new IllegalStateException();
Key key = createKey(fieldName); Value value = fields.get(key); if (!value.isMessageType()) { throw new ParserException("Invalid option name: %s", name); DynamicMessage msg = value.getMessage(); return msg.get(rest); } else {
Key key = createKey(fieldName); Value value = fields.get(key); if (!value.isMessageType()) { throw new ParserException("Invalid option name: %s", name); DynamicMessage msg = value.getMessage(); return msg.get(rest); } else {
@Override public void exitMap(ProtoParser.MapContext ctx) { final Field field = context.pop(Field.class); final Message message = context.peek(Message.class); String name = ctx.fieldName().getText(); SourceCodeLocation codeLocation = getSourceCodeLocation(ctx); Message map = new Message(message); String mapEntryTypeName = name + "_entry"; map.setName(mapEntryTypeName); map.setSourceCodeLocation(codeLocation); map.getOptions().set(codeLocation, OPTION_MAP_ENTRY, Value.createBoolean(true)); String keyTypeName = ctx.mapKey().getText(); Field keyField = createMapKeyField(map, keyTypeName, codeLocation); map.addField(keyField); String valueTypeName = ctx.mapValue().getText(); Field valueField = createMapValueField(map, valueTypeName, codeLocation); map.addField(valueField); Integer tag = Integer.decode(ctx.tag().getText()); field.setName(name); field.setTag(tag); field.setIndex(message.getFieldCount() + 1); field.setModifier(REPEATED); field.setTypeName(mapEntryTypeName); field.setType(map); field.setSourceCodeLocation(codeLocation); message.addField(field); message.addMessage(map); attachComments(ctx, field, true); }
private DynamicMessage.Value getTextFormatOptionValue(ProtoParser.TextFormatEntryContext ctx) { DynamicMessage.Value optionValue; SourceCodeLocation sourceCodeLocation = getSourceCodeLocation(ctx); if (ctx.textFormat() != null) { optionValue = DynamicMessage.Value.createMessage(sourceCodeLocation, lastTextFormat); } else if (ctx.textFormatOptionValue().BOOLEAN_VALUE() != null) { String text = ctx.textFormatOptionValue().BOOLEAN_VALUE().getText(); boolean value = Boolean.parseBoolean(text); optionValue = DynamicMessage.Value.createBoolean(sourceCodeLocation, value); } else if (ctx.textFormatOptionValue().INTEGER_VALUE() != null) { String text = ctx.textFormatOptionValue().INTEGER_VALUE().getText(); optionValue = parseInteger(sourceCodeLocation, text); } else if (ctx.textFormatOptionValue().STRING_VALUE() != null) { String text = ctx.textFormatOptionValue().STRING_VALUE().getText(); // TODO: unescape optionValue = DynamicMessage.Value.createString(sourceCodeLocation, Util.removeFirstAndLastChar(text)); } else if (ctx.textFormatOptionValue().IDENT() != null) { String text = ctx.textFormatOptionValue().IDENT().getText(); optionValue = DynamicMessage.Value.createEnum(sourceCodeLocation, text); } else if (ctx.textFormatOptionValue().FLOAT_VALUE() != null) { String text = ctx.textFormatOptionValue().FLOAT_VALUE().getText(); double value = Double.parseDouble(text); optionValue = DynamicMessage.Value.createFloat(sourceCodeLocation, value); } else { throw new IllegalStateException(); } return optionValue; }
private DynamicMessage.Value getTextFormatOptionValue(ProtoParser.TextFormatEntryContext ctx) { DynamicMessage.Value optionValue; SourceCodeLocation sourceCodeLocation = getSourceCodeLocation(ctx); if (ctx.textFormat() != null) { optionValue = DynamicMessage.Value.createMessage(sourceCodeLocation, lastTextFormat); } else if (ctx.textFormatOptionValue().BOOLEAN_VALUE() != null) { String text = ctx.textFormatOptionValue().BOOLEAN_VALUE().getText(); boolean value = Boolean.parseBoolean(text); optionValue = DynamicMessage.Value.createBoolean(sourceCodeLocation, value); } else if (ctx.textFormatOptionValue().INTEGER_VALUE() != null) { String text = ctx.textFormatOptionValue().INTEGER_VALUE().getText(); optionValue = parseInteger(sourceCodeLocation, text); } else if (ctx.textFormatOptionValue().STRING_VALUE() != null) { String text = ctx.textFormatOptionValue().STRING_VALUE().getText(); // TODO: unescape optionValue = DynamicMessage.Value.createString(sourceCodeLocation, Util.removeFirstAndLastChar(text)); } else if (ctx.textFormatOptionValue().IDENT() != null) { String text = ctx.textFormatOptionValue().IDENT().getText(); optionValue = DynamicMessage.Value.createEnum(sourceCodeLocation, text); } else if (ctx.textFormatOptionValue().FLOAT_VALUE() != null) { String text = ctx.textFormatOptionValue().FLOAT_VALUE().getText(); double value = Double.parseDouble(text); optionValue = DynamicMessage.Value.createFloat(sourceCodeLocation, value); } else { throw new IllegalStateException(); } return optionValue; }
@Override public void exitMap(ProtoParser.MapContext ctx) { final Field field = context.pop(Field.class); final Message message = context.peek(Message.class); String name = ctx.fieldName().getText(); SourceCodeLocation codeLocation = getSourceCodeLocation(ctx); Message map = new Message(message); String mapEntryTypeName = name + "_entry"; map.setName(mapEntryTypeName); map.setSourceCodeLocation(codeLocation); map.getOptions().set(codeLocation, OPTION_MAP_ENTRY, Value.createBoolean(true)); String keyTypeName = ctx.mapKey().getText(); Field keyField = createMapKeyField(map, keyTypeName, codeLocation); map.addField(keyField); String valueTypeName = ctx.mapValue().getText(); Field valueField = createMapValueField(map, valueTypeName, codeLocation); map.addField(valueField); Integer tag = Integer.decode(ctx.tag().getText()); field.setName(name); field.setTag(tag); field.setIndex(message.getFieldCount() + 1); field.setModifier(REPEATED); field.setTypeName(mapEntryTypeName); field.setType(map); field.setSourceCodeLocation(codeLocation); message.addField(field); message.addMessage(map); attachComments(ctx, field, true); }
/** * Returns a java field default value for proto field. */ public static String getDefaultValue(Field field) { FieldType type = field.getType(); if (type instanceof ScalarFieldType) { return ScalarFieldTypeUtil.getDefaultValue((ScalarFieldType) type); } if (type instanceof Message) { Message m = (Message) type; return UserTypeUtil.getCanonicalName(m) + ".getDefaultInstance()"; } if (type instanceof Enum) { Enum anEnum = (Enum) type; String defaultValue; List<EnumConstant> constants = anEnum.getConstants(); if (constants.isEmpty()) { defaultValue = "UNRECOGNIZED"; } else { DynamicMessage options = field.getOptions(); defaultValue = options.containsKey(DEFAULT) ? options.get(DEFAULT).getEnumName() : constants.get(0).getName(); } return UserTypeUtil.getCanonicalName(anEnum) + "." + defaultValue; } throw new IllegalArgumentException(String.valueOf(type)); }
private void checkFieldValue(ProtoContext context, Descriptor descriptor, Field field, DynamicMessage.Value value) { String fieldName = field.getName(); FieldType fieldType = field.getType(); DynamicMessage.Value.Type valueType = value.getType(); if (fieldType instanceof ScalarFieldType) { ScalarFieldType scalarFieldType = (ScalarFieldType) fieldType; if (!isAssignableFrom(scalarFieldType, value)) { throw new ParserException(value, "Cannot set option '%s': expected %s value", fieldName, fieldType); } } else if (fieldType instanceof Enum) { Enum anEnum = (Enum) fieldType; Set<String> allowedNames = anEnum.getConstantNames(); if (valueType != DynamicMessage.Value.Type.ENUM || !allowedNames.contains(value.getEnumName())) { throw new ParserException(value, "Cannot set option '%s': expected enum = %s", fieldName, allowedNames); } } else if (fieldType instanceof Message) { if (valueType != DynamicMessage.Value.Type.MESSAGE) { throw new ParserException(value, "Cannot set option '%s': expected message value", fieldName); } Message message = (Message) fieldType; processOptions(context, message, descriptor, value.getMessage()); } else { throw new IllegalStateException("Unknown field type: " + fieldType); } }
/** * Returns a java field default value for proto field. */ public static String getDefaultValue(Field field) { FieldType type = field.getType(); if (type instanceof ScalarFieldType) { return ScalarFieldTypeUtil.getDefaultValue((ScalarFieldType) type); } if (type instanceof Message) { Message m = (Message) type; return UserTypeUtil.getCanonicalName(m) + ".getDefaultInstance()"; } if (type instanceof Enum) { Enum anEnum = (Enum) type; String defaultValue; List<EnumConstant> constants = anEnum.getConstants(); if (constants.isEmpty()) { defaultValue = "UNRECOGNIZED"; } else { DynamicMessage options = field.getOptions(); defaultValue = options.containsKey(DEFAULT) ? options.get(DEFAULT).getEnumName() : constants.get(0).getName(); } return UserTypeUtil.getCanonicalName(anEnum) + "." + defaultValue; } throw new IllegalArgumentException(String.valueOf(type)); }
private void checkFieldValue(ProtoContext context, Descriptor descriptor, Field field, DynamicMessage.Value value) { String fieldName = field.getName(); FieldType fieldType = field.getType(); DynamicMessage.Value.Type valueType = value.getType(); if (fieldType instanceof ScalarFieldType) { ScalarFieldType scalarFieldType = (ScalarFieldType) fieldType; if (!isAssignableFrom(scalarFieldType, value)) { throw new ParserException(value, "Cannot set option '%s': expected %s value", fieldName, fieldType); } } else if (fieldType instanceof Enum) { Enum anEnum = (Enum) fieldType; Set<String> allowedNames = anEnum.getConstantNames(); if (valueType != DynamicMessage.Value.Type.ENUM || !allowedNames.contains(value.getEnumName())) { throw new ParserException(value, "Cannot set option '%s': expected enum = %s", fieldName, allowedNames); } } else if (fieldType instanceof Message) { if (valueType != DynamicMessage.Value.Type.MESSAGE) { throw new ParserException(value, "Cannot set option '%s': expected message value", fieldName); } Message message = (Message) fieldType; processOptions(context, message, descriptor, value.getMessage()); } else { throw new IllegalStateException("Unknown field type: " + fieldType); } }
@Override public void serialize(Value value, JsonGenerator gen, SerializerProvider provider) throws IOException { switch (value.getType()) { case BOOLEAN: gen.writeBoolean(value.getBoolean()); break; case INTEGER: gen.writeNumber(value.getInt64()); break; case FLOAT: gen.writeNumber(value.getDouble()); break; case STRING: gen.writeString(value.getString()); break; case ENUM: gen.writeString(value.getEnumName()); break; case MESSAGE: gen.writeObject(value.getMessage()); break; default: throw new IllegalStateException(String.valueOf(value.getType())); } } }
private void checkDuplicateEnumConstantValues(Enum anEnum, List<EnumConstant> constants) { DynamicMessage.Value allowAlias = anEnum.getOptions().get(ALLOW_ALIAS); if (allowAlias != null && allowAlias.isBooleanType() && allowAlias.getBoolean()) { // skip this check if aliases are allowed return; } Map<Integer, EnumConstant> constantByValue = new HashMap<>(); for (EnumConstant constant : constants) { Integer value = constant.getValue(); if (constantByValue.containsKey(value)) { throw new ParserException(constant, "Duplicate enum constant value: %d", value); } constantByValue.put(value, constant); } }
private void checkDuplicateEnumConstantValues(Enum anEnum, List<EnumConstant> constants) { DynamicMessage.Value allowAlias = anEnum.getOptions().get(ALLOW_ALIAS); if (allowAlias != null && allowAlias.isBooleanType() && allowAlias.getBoolean()) { // skip this check if aliases are allowed return; } Map<Integer, EnumConstant> constantByValue = new HashMap<>(); for (EnumConstant constant : constants) { Integer value = constant.getValue(); if (constantByValue.containsKey(value)) { throw new ParserException(constant, "Duplicate enum constant value: %d", value); } constantByValue.put(value, constant); } }
private Object transformValueToObject(Value value) { switch (value.getType()) { case BOOLEAN: return value.getBoolean(); case INTEGER: return value.getInt64(); case FLOAT: return value.getDouble(); case STRING: return value.getString(); case ENUM: return value.getEnumName(); case MESSAGE: return value.getMessage(); default: return value; } }
private Object transformValueToObject(Value value) { switch (value.getType()) { case BOOLEAN: return value.getBoolean(); case INTEGER: return value.getInt64(); case FLOAT: return value.getDouble(); case STRING: return value.getString(); case ENUM: return value.getEnumName(); case MESSAGE: return value.getMessage(); default: return value; } }
private void set(Key key, Value value) { if (fields.containsKey(key) && value.isMessageType()) { // merge Value prevValue = fields.get(key); if (!prevValue.isMessageType()) { throw new ParserException(value, "Can not set '%s': incompatible type", key); } DynamicMessage prevMessage = prevValue.getMessage(); DynamicMessage message = value.getMessage(); prevMessage.merge(message); } else { // create new or override previous value fields.put(key, value); } }
private static boolean canAssignUInt32(DynamicMessage.Value value) { return value.getType() == DynamicMessage.Value.Type.INTEGER; }