public static void throwNotSupportWrite(Field protoField, Class<?> cls) throws IllegalStateException { throw new IllegalStateException( String.format("not support serialize from %s to proto %s, field=%s:%s", cls.getName(), protoField.getTypeName(), ((Type) protoField.getParent()).getCanonicalName(), protoField.getName())); }
private void fieldMapToString(Field field, StringBuilder sb) { Message entryMessage = (Message) field.getType(); Field keyField = entryMessage.getField(1); Field valueField = entryMessage.getField(2); // map<string, string> name = 1; appendLine(sb, "map<%s, %s> %s = %d;", keyField.getTypeName(), valueField.getTypeName(), field.getName(), field.getTag()); } }
@Override protected <T> SchemaEx<T> newMessageSchema(Message message, JavaType javaType) { if (ProtoUtils.isWrapProperty(message) && javaType.getRawClass() != PropertyWrapper.class) { Field protoField = message.getField(1); if (javaType.isJavaLangObject()) { javaType = protoField.isRepeated() && protoField.getType().isMessage() && !protoField.isMap() ? ProtoConst.LIST_TYPE : ProtoConst.MAP_TYPE; } javaType = TypeFactory.defaultInstance().constructParametricType(PropertyWrapper.class, javaType); } if (javaType.isJavaLangObject()) { javaType = ProtoConst.MAP_TYPE; } return new MessageReadSchema<>(protoMapper, message, javaType); }
.map(field -> { ImmutableMessageField.Builder builder = ImmutableMessageField.builder() .name(field.getName()) .typeId(field.getType().getCanonicalName()) .modifier(getModifier(field)) .tag(field.getTag()) .description(createFieldDescription(field)) .oneof(field.isOneofPart() ? field.getOneof().getName() : null) .options(field.getOptions().toMap()); boolean isMap = field.isMap(); if (isMap) { builder.map(true);
/** * Returns a field converter class name for proto repeated enum field. */ public static String getRepeatedEnumConverterName(Field field) { if (field.isRepeated()) { return "__" + Formatter.toCamelCase(field.getName()) + "Converter"; } throw new IllegalArgumentException(field.toString()); }
public FieldSchema(Field protoField, JavaType javaType) { this.protoField = protoField; this.name = protoField.getName(); this.fieldNumber = protoField.getTag(); this.packed = ProtoUtils.isPacked(protoField); int wireType = packed && protoField.isRepeated() ? WireFormat.WIRETYPE_LENGTH_DELIMITED : FieldTypeUtils.convert(protoField.getType()).wireType; this.tag = WireFormat.makeTag(fieldNumber, wireType); this.tagSize = ProtobufOutputEx.computeRawVarint32Size(tag); this.javaType = javaType; this.primitive = javaType.isPrimitive(); }
private void fieldToString(Field field, boolean repeated, StringBuilder sb) { if (field.isMap()) { fieldMapToString(field, sb); return; } if (repeated) { fieldRepeatedToString(field, sb); return; } appendLine(sb, "%s %s = %d;", field.getTypeName(), field.getName(), field.getTag()); }
public <T> FieldSchema<T> createSchemaField(Field protoField, PropertyDescriptor propertyDescriptor) { // map is a special repeated if (protoField.isMap()) { return createMapFieldSchema(protoField, propertyDescriptor); } if (protoField.isRepeated()) { return createRepeatedSchema(protoField, propertyDescriptor); } if (isAnyField(protoField)) { return new AnySchema<>(protoMapper, protoField, propertyDescriptor); } if (protoField.getType().isScalar()) { return createScalarField(protoField, propertyDescriptor); } // message if (protoField.getType().isMessage()) { SchemaEx<Object> messageSchema = getOrCreateMessageSchema((Message) protoField.getType(), propertyDescriptor.getJavaType()); if (isWrapProperty((Message) protoField.getType())) { return new PropertyWrapperAsFieldSchema<>(protoField, propertyDescriptor, messageSchema); } return new MessageAsFieldSchema<>(protoField, propertyDescriptor, messageSchema); } if (protoField.isOneofPart()) { throw new IllegalStateException("not IMPL oneof now."); } ProtoUtils.throwNotSupportWrite(protoField, propertyDescriptor.getJavaType().getRawClass()); return null; }
@Override public void mergeFrom(InputEx input, T message) throws IOException { FieldSchema<T> fieldSchema = null; try { for (int n = input.readFieldNumber(); n != 0; ) { fieldSchema = fieldMap.getFieldByNumber(n); if (fieldSchema != null) { n = fieldSchema.mergeFrom(input, message); continue; } input.handleUnknownField(n); n = input.readFieldNumber(); } } catch (Throwable e) { Field protoField = fieldSchema.getProtoField(); LOGGER.error("Failed to mergeFrom, field={}:{}, type={}", protoField.getType().getCanonicalName(), protoField.getName(), protoField.getTypeName(), e.getMessage()); throw e; } }
public AbstractWriters(Field protoField, Class<T[]> arrayClass) { this.protoField = protoField; int wireType = ProtoUtils.isPacked(protoField) && protoField.isRepeated() ? WireFormat.WIRETYPE_LENGTH_DELIMITED : FieldTypeUtils.convert(protoField.getType()).wireType; this.tag = WireFormat.makeTag(protoField.getTag(), wireType); this.tagSize = ProtobufOutputEx.computeRawVarint32Size(tag); if (arrayClass == null) { arrayClass = getFieldArgument(this.getClass(), "arrayWriter"); } this.arrayClass = arrayClass; }
public AbstractReaders(Field protoField, Class<T[]> arrayClass) { this.protoField = protoField; this.fieldNumber = protoField.getTag(); if (arrayClass == null) { arrayClass = getFieldArgument(this.getClass(), "arrayClass"); } this.arrayClass = arrayClass; } }
protected FieldSchema createSchemaField(SchemaCreateContext context, Field protoField, boolean repeated) { if (protoField.isMap()) { Message entryMessage = (Message) protoField.getType(); FieldSchema keySchema = createSchemaField(context, entryMessage.getField(1), false); FieldSchema valueSchema = createSchemaField(context, entryMessage.getField(2), false); if (protoField.isOneofPart()) { throw new IllegalStateException("not IMPL oneof now."); if (protoField.getType().isEnum()) { return new EnumSerializerSchema(protoField); if (protoField.getType().isScalar()) { return createScalarField(protoField); MessageSchema messageSchema = createSchema(context, (Message) protoField.getType()); MessageAsFieldSchema messageAsFieldSchema = new MessageAsFieldSchema(protoField, messageSchema); return messageAsFieldSchema;
/** * all supported type, default to packed * @param protoField * @return */ public static boolean isSupportPacked(Field protoField) { if (protoField.getType().isEnum()) { return true; } if (protoField.getType().isScalar()) { ScalarFieldType scalarFieldType = (ScalarFieldType) protoField.getType(); return scalarFieldType != ScalarFieldType.STRING && scalarFieldType != ScalarFieldType.BYTES; } return false; }
private void updateFieldTypes(ProtoContext context, Deque<String> scopeLookupList, FieldContainer fieldContainer) { // check if field type isn't already set fieldContainer.getFields() .stream() .filter(field -> field.getType() == null) // for map fields it is set by parser .forEach(field -> { String typeName = field.getTypeName(); FieldType fieldType = resolveFieldType(field, context, scopeLookupList, typeName); field.setType(fieldType); }); }
private void checkFieldModifier(Message message, List<Field> fields) { for (Field field : fields) { if (field.isOneofPart() && field.hasModifier()) { throw new ParserException(field, "Oneof field cannot have modifier: %s", field.getModifier()); } } }
/** * 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 checkRanges(Field field, List<Range> ranges) { int tag = field.getTag(); boolean inRange = false; for (Range range : ranges) { int from = range.getFrom(); int to = range.getTo(); if (tag >= from && tag <= to) { inRange = true; } } if (!inRange) { String format = "Extension field '%s' tag=%d is out of allowed range"; throw new ParserException(field, format, field.getName(), tag); } }
public FieldMapEx<Map<Object, Object>> createMapFields(Message message) { List<FieldSchema<Map<Object, Object>>> fieldSchemas = new ArrayList<>(); for (Field protoField : message.getFields()) { PropertyDescriptor propertyDescriptor = new PropertyDescriptor(); propertyDescriptor.setJavaType(ProtoConst.OBJECT_TYPE); propertyDescriptor.setGetter(new MapGetter<>(protoField.getName())); propertyDescriptor.setSetter(new MapSetter<>(protoField.getName())); FieldSchema<Map<Object, Object>> fieldSchema = createSchemaField(protoField, propertyDescriptor); fieldSchemas.add(fieldSchema); } return FieldMapEx.createFieldMap(fieldSchemas); }