perf[protobuf]: generate pojo from proto

This commit is contained in:
godotg
2023-12-03 11:49:15 +08:00
parent ebfeb84d3b
commit d6fc68fc93
6 changed files with 72 additions and 61 deletions
@@ -14,9 +14,9 @@
package com.zfoo.protocol.serializer.protobuf.builder;
import com.zfoo.protocol.collection.CollectionUtils;
import com.zfoo.protocol.serializer.protobuf.parser.JavaType;
import com.zfoo.protocol.serializer.protobuf.parser.TypeJava;
import com.zfoo.protocol.serializer.protobuf.parser.TypeProtobuf;
import com.zfoo.protocol.serializer.protobuf.wire.*;
import com.zfoo.protocol.serializer.protobuf.wire.PbField.Type;
import com.zfoo.protocol.serializer.protobuf.parser.Proto;
import com.zfoo.protocol.util.StringUtils;
@@ -43,7 +43,7 @@ public class JavaBuilder {
if (!BASE_TYPES.contains(type.toLowerCase(Locale.ENGLISH))) {
return type;
}
JavaType javaType = Type.valueOf(type.toUpperCase(Locale.ENGLISH)).javaType();
TypeJava javaType = TypeProtobuf.valueOf(type.toUpperCase(Locale.ENGLISH)).javaType();
return javaType.getTypeString();
}
@@ -58,7 +58,7 @@ public class JavaBuilder {
public String getJavaType(String type) {
if (BASE_TYPES.contains(type.toLowerCase(Locale.ENGLISH))) {
return Type.valueOf(type.toUpperCase(Locale.ENGLISH)).javaType().getTypeString();
return TypeProtobuf.valueOf(type.toUpperCase(Locale.ENGLISH)).javaType().getTypeString();
} else {
return type;
}
@@ -67,9 +67,9 @@ public class JavaBuilder {
public String getAnnotationType(PbField field) {
String type = field.getTypeString();
if (BASE_TYPES.contains(type.toLowerCase(Locale.ENGLISH))) {
return "Type." + Type.valueOf(type.toUpperCase(Locale.ENGLISH));
return "Type." + TypeProtobuf.valueOf(type.toUpperCase(Locale.ENGLISH));
} else {
return "Type." + Type.MESSAGE;
return "Type." + TypeProtobuf.MESSAGE;
}
}
@@ -206,8 +206,8 @@ public class JavaBuilder {
private String getBoxedTypeName(PbField f) {
String type = getJavaType(f);
if (BASE_TYPES.contains(f.getTypeString())) {
JavaType javaType = null;
javaType = Type.valueOf(f.getTypeString().toUpperCase(Locale.ENGLISH)).javaType();
TypeJava javaType = null;
javaType = TypeProtobuf.valueOf(f.getTypeString().toUpperCase(Locale.ENGLISH)).javaType();
if (javaType != null && javaType.getTypeString() != null) {
type = javaType.getBoxedType();
}
@@ -16,7 +16,6 @@ import com.zfoo.protocol.collection.CollectionUtils;
import com.zfoo.protocol.serializer.protobuf.wire.*;
import com.zfoo.protocol.serializer.protobuf.wire.Comment.CommentType;
import com.zfoo.protocol.serializer.protobuf.wire.PbField.Cardinality;
import com.zfoo.protocol.serializer.protobuf.wire.PbField.Type;
import com.zfoo.protocol.util.StringUtils;
import java.util.*;
@@ -286,9 +285,6 @@ public class ProtoParser {
/**
* 读取一个标识符
*
* @return
* @throws RuntimeException
*/
private String readToken() throws RuntimeException {
trim();
@@ -426,7 +422,7 @@ public class ProtoParser {
nextLine();
PbField field;
if (gType != null) {
Type type = Type.valueOf(gType.getKeyType().toUpperCase(Locale.ENGLISH));
TypeProtobuf type = TypeProtobuf.valueOf(gType.getKeyType().toUpperCase(Locale.ENGLISH));
field = new MapField().setKey(type).setValue(gType.getValueType());
} else {
field = new PbField();
@@ -21,7 +21,7 @@ import com.zfoo.protocol.collection.ArrayUtils;
/**
* java的数据类型和protocol buffer的数据类型的对应关系以及默认值
*/
public enum JavaType {
public enum TypeJava {
INT("int", "Integer", 0),
LONG("long", "Long", 0L),
FLOAT("float", "Float", 0F),
@@ -34,7 +34,7 @@ public enum JavaType {
OBJECT("Object", "Object", null),
MAP("Map", "Map", null);
JavaType(final String typeString, final String boxedType, final Object defaultValue) {
TypeJava(final String typeString, final String boxedType, final Object defaultValue) {
this.typeString = typeString;
this.boxedType = boxedType;
this.defaultValue = defaultValue;
@@ -0,0 +1,56 @@
/*
* Copyright (C) 2020 The zfoo Authors
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and limitations under the License.
*/
package com.zfoo.protocol.serializer.protobuf.parser;
/**
* @author godotg
*/
public enum TypeProtobuf {
FLOAT("float", TypeJava.FLOAT),
DOUBLE("double", TypeJava.DOUBLE),
INT32("int32", TypeJava.INT),
INT64("int64", TypeJava.LONG),
UINT32("uint32", TypeJava.INT),
UINT64("uint64", TypeJava.LONG),
SINT32("sint32", TypeJava.INT),
SINT64("sint64", TypeJava.LONG),
FIXED32("fixed32", TypeJava.INT),
FIXED64("fixed64", TypeJava.LONG),
SFIXED32("sfixed32", TypeJava.INT),
SFIXED64("sfixed64", TypeJava.LONG),
BOOL("bool", TypeJava.BOOLEAN),
ENUM("enum", TypeJava.ENUM),
STRING("string", TypeJava.STRING),
BYTES("bytes", TypeJava.BYTES),
MESSAGE("", TypeJava.MESSAGE),
OBJECT("OBJECT", TypeJava.OBJECT),
GROUP("group", TypeJava.MESSAGE),
MAP("", TypeJava.MAP);
private final String value;
private final TypeJava javaType;
TypeProtobuf(String value, TypeJava javaType) {
this.value = value;
this.javaType = javaType;
}
public TypeJava javaType() {
return javaType;
}
public String value() {
return this.value;
}
}
@@ -13,12 +13,14 @@
package com.zfoo.protocol.serializer.protobuf.wire;
import com.zfoo.protocol.serializer.protobuf.parser.TypeProtobuf;
/**
* protocol buffer中Map的Field的结构体定义
*/
public class MapField extends PbField {
private Type key;
private TypeProtobuf key;
private String value;
@Override
@@ -36,7 +38,7 @@ public class MapField extends PbField {
/**
* @return the key
*/
public Type getKey() {
public TypeProtobuf getKey() {
return key;
}
@@ -44,7 +46,7 @@ public class MapField extends PbField {
* @param key the key to set
* @return
*/
public MapField setKey(Type key) {
public MapField setKey(TypeProtobuf key) {
this.key = key;
return this;
}
@@ -13,7 +13,6 @@
package com.zfoo.protocol.serializer.protobuf.wire;
import com.zfoo.protocol.serializer.protobuf.parser.JavaType;
/**
* protocol buffer协议消息体属性数据类型定义
@@ -51,48 +50,6 @@ public class PbField {
}
}
public enum Type {
FLOAT("float", JavaType.FLOAT),
DOUBLE("double", JavaType.DOUBLE),
INT32("int32", JavaType.INT),
INT64("int64", JavaType.LONG),
UINT32("uint32", JavaType.INT),
UINT64("uint64", JavaType.LONG),
SINT32("sint32", JavaType.INT),
SINT64("sint64", JavaType.LONG),
FIXED32("fixed32", JavaType.INT),
FIXED64("fixed64", JavaType.LONG),
SFIXED32("sfixed32", JavaType.INT),
SFIXED64("sfixed64", JavaType.LONG),
BOOL("bool", JavaType.BOOLEAN),
ENUM("enum", JavaType.ENUM),
STRING("string", JavaType.STRING),
BYTES("bytes", JavaType.BYTES),
MESSAGE("", JavaType.MESSAGE),
OBJECT("OBJECT", JavaType.OBJECT),
GROUP("group", JavaType.MESSAGE),
MAP("", JavaType.MAP);
private final String value;
private final JavaType javaType;
Type(String value,JavaType javaType) {
this.value = value;
this.javaType = javaType;
}
public JavaType javaType() {
return javaType;
}
public String value() {
return this.value;
}
}
/**
* 消息属性的规则
*/