/** * 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; }
@Override public String messageName() { return message.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()); } }
public String protoToString() { StringBuilder sb = new StringBuilder(); appendLine(sb, "syntax = \"%s\";", proto.getSyntax()); for (Import importValue : proto.getImports()) { appendLine(sb, "import \"%s\";", importValue.getValue()); } appendLine(sb, "package %s;\n", proto.getPackage().getValue()); for (Message message : proto.getMessages()) { messageToString(message, sb); } for (Enum enumValue : proto.getEnums()) { enumToString(enumValue, sb); } for (Service service : proto.getServices()) { serviceToString(service, sb); } return sb.toString(); }
protected void enumToString(Enum enumValue, StringBuilder sb) { appendLine(sb, "enum %s {", enumValue.getName()); for (EnumConstant enumConstant : enumValue.getConstants()) { appendLine(sb, " %s = %s;", enumConstant.getName(), enumConstant.getValue()); } sb.append("}\n\n"); }
@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); }
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 serviceToString(Service service, StringBuilder sb) { appendLine(sb, "service %s {", service.getName()); for (ServiceMethod serviceMethod : service.getMethods()) { commentsToString(serviceMethod.getCommentLines(), sb, 2); appendLine(sb, " rpc %s (%s) returns (%s);\n", serviceMethod.getName(), serviceMethod.getArgTypeName(), serviceMethod.getReturnTypeName()); } if (!service.getMethods().isEmpty()) { sb.setLength(sb.length() - 1); } appendLine(sb, "}"); }
private void messageToString(Message message, StringBuilder sb) { commentsToString(message.getCommentLines(), sb, 0); appendLine(sb, "message %s {", message.getName()); for (Field field : message.getFields()) { sb.append(" "); fieldToString(field, field.isRepeated(), sb); } appendLine(sb, "}\n"); }
public static boolean isAnyField(Field protoField) { return protoField.getType().getCanonicalName().equals(ProtoConst.ANY.getCanonicalName()); }
private void addImports(Proto proto) { imports.add(proto.getFilename()); for (Message message : proto.getMessages()) { messages.add(message.getCanonicalName()); } }
public static boolean isPacked(Field protoField) { DynamicMessage.Value dynamicMessageValue = protoField.getOptions().get("packed"); if (dynamicMessageValue != null) { return dynamicMessageValue.getBoolean(); } return isSupportPacked(protoField); }
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 Message getMessageFromCanonicaName(String messageCanonicalName) { for (Message message : proto.getMessages()) { if (message.getCanonicalName().equals(messageCanonicalName)) { return message; } } return null; }
private SchemaWriter<Object> createEntryWriter(String actualTypeName, Object _value) { Message message = protoMapper.getProto().getMessage(actualTypeName); if (message == null) { // not standard, protobuf can not support or not define this type , just extend return this::jsonExtend; } // standard pack RootSerializer valueSerializer = protoMapper.createRootSerializer(message, _value.getClass()); String valueCanonicalName = message.getCanonicalName(); return (output, value) -> { standardPack(output, value, valueCanonicalName, valueSerializer); }; }
protected String generateCacheKey(Message message, JavaType javaType) { return message.getCanonicalName() + ":" + javaType.toCanonical(); }
public static boolean isWrapProperty(FieldContainer fieldContainer) { return fieldContainer.getCommentLines().contains(ProtoConst.ANNOTATION_WRAP_PROPERTY); }
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())); }
@Override public String messageName() { return message.getName(); }
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); }