public static boolean isAnyField(Field protoField) { return protoField.getType().getCanonicalName().equals(ProtoConst.ANY.getCanonicalName()); }
/** * 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; }
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; }
protected <T> FieldSchema<T> createScalarField(Field protoField, PropertyDescriptor propertyDescriptor) { if (protoField.getType().isEnum()) { return EnumWriteSchemas.create(protoField, propertyDescriptor);
private void resolveTypeReferences(ProtoContext context) { Proto proto = context.getProto(); Deque<String> scopeLookupList = createScopeLookupList(proto); for (Service service : proto.getServices()) { for (ServiceMethod method : service.getMethods()) { String argTypeName = method.getArgTypeName(); FieldType argType = resolveFieldType(method, context, scopeLookupList, argTypeName); if (!(argType instanceof Message)) { String format = "Cannot use '%s' as a service method argument type: not a message"; throw new ParserException(method, format, argType.getName()); } method.setArgType((Message) argType); String returnTypeName = method.getReturnTypeName(); FieldType returnType = resolveFieldType(method, context, scopeLookupList, returnTypeName); if (!(returnType instanceof Message)) { String format = "Cannot use '%s' as a service method return type: not a message"; throw new ParserException(method, format, returnType.getName()); } method.setReturnType((Message) returnType); } } resolveTypeReferences(context, scopeLookupList, proto); }
@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); }
static BeanFactory createFactory(Field field) { // map also is repeated, so must determine first if (field.isMap()) { return BeanFactory::mapFactory; } if (field.isRepeated()) { return BeanFactory::listFactory; } if (field.getType().isScalar()) { // no need a factory return null; } return BeanFactory::mapFactory; }
protected <T> FieldSchema<T> createScalarField(Field protoField, PropertyDescriptor propertyDescriptor) { if (protoField.getType().isEnum()) { return EnumsReadSchemas.create(protoField, propertyDescriptor);
private void resolveTypeReferences(ProtoContext context) { Proto proto = context.getProto(); Deque<String> scopeLookupList = createScopeLookupList(proto); for (Service service : proto.getServices()) { for (ServiceMethod method : service.getMethods()) { String argTypeName = method.getArgTypeName(); FieldType argType = resolveFieldType(method, context, scopeLookupList, argTypeName); if (!(argType instanceof Message)) { String format = "Cannot use '%s' as a service method argument type: not a message"; throw new ParserException(method, format, argType.getName()); } method.setArgType((Message) argType); String returnTypeName = method.getReturnTypeName(); FieldType returnType = resolveFieldType(method, context, scopeLookupList, returnTypeName); if (!(returnType instanceof Message)) { String format = "Cannot use '%s' as a service method return type: not a message"; throw new ParserException(method, format, returnType.getName()); } method.setReturnType((Message) returnType); } } resolveTypeReferences(context, scopeLookupList, proto); }
@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; } }
/** * Generate part of toString method for a single field. */ public static String toStringPart(Field field) { String getterName; if (field.isMap()) { getterName = getMapGetterName(field); } else if (field.isRepeated()) { getterName = getRepeatedFieldGetterName(field); } else { getterName = getFieldGetterName(field); } if (field.getType().isEnum() && !field.isRepeated()) { return "\"" + getFieldName(field) + "=\" + " + getterName + "() + '(' + " + getEnumFieldValueGetterName(field) + "() + ')'"; } return "\"" + getFieldName(field) + "=\" + " + getterName + "()"; }
@Override protected <T> FieldSchema<T> createRepeatedSchema(Field protoField, PropertyDescriptor propertyDescriptor) { boolean packed = ProtoUtils.isPacked(protoField); if (protoField.getType().isEnum()) { return packed ? EnumPackedReadSchemas.create(protoField, propertyDescriptor) : EnumNotPackedReadSchemas.create(protoField, propertyDescriptor); if (protoField.getType().isScalar()) { switch ((ScalarFieldType) protoField.getType()) { case INT32: if (protoField.getType().isMessage()) { JavaType contentType = propertyDescriptor.getJavaType().getContentType(); if (contentType == null) {
protected boolean isAnyField(Field protoField, boolean repeated) { return !repeated && protoField.getType().getCanonicalName().equals(ProtoConst.ANY.getCanonicalName()); }
/** * Generate part of toString method for a single field. */ public static String toStringPart(Field field) { String getterName; if (field.isMap()) { getterName = getMapGetterName(field); } else if (field.isRepeated()) { getterName = getRepeatedFieldGetterName(field); } else { getterName = getFieldGetterName(field); } if (field.getType().isEnum() && !field.isRepeated()) { return "\"" + getFieldName(field) + "=\" + " + getterName + "() + '(' + " + getEnumFieldValueGetterName(field) + "() + ')'"; } return "\"" + getFieldName(field) + "=\" + " + getterName + "()"; }
@Override protected <T> FieldSchema<T> createRepeatedSchema(Field protoField, PropertyDescriptor propertyDescriptor) { boolean packed = ProtoUtils.isPacked(protoField); if (protoField.getType().isEnum()) { return packed ? EnumPackedWriteSchemas.create(protoField, propertyDescriptor) : EnumNotPackedWriteSchemas.create(protoField, propertyDescriptor); if (protoField.getType().isScalar()) { switch ((ScalarFieldType) protoField.getType()) { case INT32: if (protoField.getType().isMessage()) { JavaType contentType = propertyDescriptor.getJavaType().getContentType(); if (contentType == null) {
protected void logError(FieldSchema fieldSchema, String action, Throwable e) { if (fieldSchema == null) { return; } io.protostuff.compiler.model.Field protoField = fieldSchema.getProtoField(); LOGGER.error("Failed to {}, field={}:{}, type={}", action, protoField.getType().getCanonicalName(), protoField.getName(), protoField.getTypeName(), e.getMessage()); }