public static void throwNotSupportNullElement(Field protoField) throws IllegalStateException { throw new IllegalStateException( String.format("not support serialize null element, field=%s:%s", ((Type) protoField.getParent()).getCanonicalName(), protoField.getName())); } }
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())); }
public static void throwNotSupportMerge(Field protoField, JavaType javaType) throws IllegalStateException { throw new IllegalStateException( String.format("not support deserialize proto %s as %s, field=%s:%s", protoField.getTypeName(), javaType.toCanonical(), ((Type) protoField.getParent()).getCanonicalName(), protoField.getName())); }
protected final void numberWrite(OutputEx output, Number value) throws IOException { int enumValue = value.intValue(); if (!enumMeta.containsValue(enumValue)) { throw new IllegalStateException( String.format("invalid enum value %d for proto %s, field=%s:%s", enumValue, protoField.getTypeName(), ((Type) protoField.getParent()).getCanonicalName(), protoField.getName())); } output.writeScalarInt32(tag, tagSize, enumValue); }
protected final void stringWrite(OutputEx output, String enumName) throws IOException { Integer enumValue = enumMeta.getValueByName(enumName); if (enumValue == null) { throw new IllegalStateException( String.format("invalid enum name %s for proto %s, field=%s:%s", enumName, protoField.getTypeName(), ((Type) protoField.getParent()).getCanonicalName(), protoField.getName())); } output.writeScalarInt32(tag, tagSize, enumValue); }
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); }
private void writeEnumCollection(OutputEx output, Collection<Enum<?>> collection) throws IOException { for (Enum<?> element : collection) { if (element == null) { ProtoUtils.throwNotSupportNullElement(protoField); return; } String name = element.name(); Integer enumValue = enumMeta.getValueByName(name); if (enumValue == null) { throw new IllegalStateException( String.format("invalid enum name %s for proto %s, field=%s:%s", name, protoField.getTypeName(), ((Type) protoField.getParent()).getCanonicalName(), protoField.getName())); } output.writeEnum(tag, tagSize, enumValue); } } }
private void writeEnumCollection(OutputEx output, Collection<Enum<?>> collection) throws IOException { for (Enum<?> element : collection) { if (element == null) { ProtoUtils.throwNotSupportNullElement(protoField); return; } String name = element.name(); Integer enumValue = enumMeta.getValueByName(name); if (enumValue == null) { throw new IllegalStateException( String.format("invalid enum name %s for proto %s, field=%s:%s", name, protoField.getTypeName(), ((Type) protoField.getParent()).getCanonicalName(), protoField.getName())); } output.writePackedEnum(enumValue); } } }
@Override public int mergeFrom(InputEx input, T message) throws IOException { int value = input.readInt32(); Enum<?> enumValue = enumMeta.getEnumByValue(value); if (enumValue != null) { setter.set(message, enumValue); return input.readFieldNumber(); } throw new IllegalStateException( String.format("invalid enum value %d for %s, proto field=%s:%s", value, javaType.getRawClass().getName(), ((Type) protoField.getParent()).getCanonicalName(), protoField.getName())); } }
private void writeStringCollection(OutputEx output, Collection<String> collection) throws IOException { for (String element : collection) { if (element == null) { ProtoUtils.throwNotSupportNullElement(protoField); return; } Integer enumValue = enumMeta.getValueByName(element); if (enumValue == null) { throw new IllegalStateException( String.format("invalid enum name %s for proto %s, field=%s:%s", element, protoField.getTypeName(), ((Type) protoField.getParent()).getCanonicalName(), protoField.getName())); } output.writeEnum(tag, tagSize, enumValue); } }
private void writeStringCollection(OutputEx output, Collection<String> collection) throws IOException { for (String element : collection) { if (element == null) { ProtoUtils.throwNotSupportNullElement(protoField); return; } Integer enumValue = enumMeta.getValueByName(element); if (enumValue == null) { throw new IllegalStateException( String.format("invalid enum name %s for proto %s, field=%s:%s", element, protoField.getTypeName(), ((Type) protoField.getParent()).getCanonicalName(), protoField.getName())); } output.writePackedEnum(enumValue); } }
private FieldMapEx<T> createPropertyWrapperFields(JavaType javaType) { Field protoField = message.getField(1); PropertyDescriptor propertyDescriptor = new PropertyDescriptor(); propertyDescriptor.setName(protoField.getName()); propertyDescriptor.setJavaType(javaType); FieldSchema<T> fieldSchema = protoMapper.getSerializerSchemaManager() .createSchemaField(protoField, propertyDescriptor); return FieldMapEx.createFieldMap(Arrays.asList(fieldSchema)); }
private FieldMapEx<T> createPojoFields(Type type) { SerializerSchemaManager serializerSchemaManager = protoMapper.getSerializerSchemaManager(); BeanDescriptor beanDescriptor = protoMapper.getBeanDescriptorManager().getOrCreateBeanDescriptor(type); List<FieldSchema<T>> fieldSchemas = new ArrayList<>(); for (Field protoField : message.getFields()) { PropertyDescriptor propertyDescriptor = beanDescriptor.getPropertyDescriptors().get(protoField.getName()); if (propertyDescriptor == null) { continue; } Object getter = propertyDescriptor.getGetter(); if (getter == null) { continue; } FieldSchema<T> fieldSchema = serializerSchemaManager.createSchemaField(protoField, propertyDescriptor); fieldSchemas.add(fieldSchema); } return FieldMapEx.createFieldMap(fieldSchemas); }
public EnumPackedReaders(Field protoField, JavaType javaType) { super(protoField, EnumSchemaUtils.constructEnumArrayClass(javaType)); this.enumMeta = new EnumMeta(protoField, javaType.getContentType()); collectionReader = (input, collection) -> { while (true) { int value = input.readPackedEnum(); Enum<?> enumValue = enumMeta.getEnumByValue(value); if (enumValue == null) { throw new IllegalStateException( String.format("invalid enum value %d for %s, proto field=%s:%s", value, javaType.getRawClass().getName(), ((Type) protoField.getParent()).getCanonicalName(), protoField.getName())); } collection.add(enumValue); int fieldNumber = input.readFieldNumber(); if (fieldNumber != this.fieldNumber) { return fieldNumber; } } }; } }
public EnumNotPackedReaders(Field protoField, JavaType javaType) { super(protoField, EnumSchemaUtils.constructEnumArrayClass(javaType)); this.enumMeta = new EnumMeta(protoField, javaType.getContentType()); collectionReader = (input, collection) -> { while (true) { int value = input.readEnum(); Enum<?> enumValue = enumMeta.getEnumByValue(value); if (enumValue == null) { throw new IllegalStateException( String.format("invalid enum value %d for %s, proto field=%s:%s", value, javaType.getRawClass().getName(), ((Type) protoField.getParent()).getCanonicalName(), protoField.getName())); } collection.add(enumValue); int fieldNumber = input.readFieldNumber(); if (fieldNumber != this.fieldNumber) { return fieldNumber; } } }; } }
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()); }
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 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 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(); }