mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-06-07 18:14:19 +00:00
perf[protobuf]: generate pojo from proto
This commit is contained in:
+11
-11
@@ -15,7 +15,7 @@ package com.zfoo.protocol.serializer.protobuf.builder;
|
||||
|
||||
import com.zfoo.protocol.collection.CollectionUtils;
|
||||
import com.zfoo.protocol.serializer.protobuf.wire.*;
|
||||
import com.zfoo.protocol.serializer.protobuf.wire.Field.Type;
|
||||
import com.zfoo.protocol.serializer.protobuf.wire.PbField.Type;
|
||||
import com.zfoo.protocol.serializer.protobuf.wire.WireFormat.JavaType;
|
||||
import com.zfoo.protocol.serializer.protobuf.wire.parser.Proto;
|
||||
import com.zfoo.protocol.util.StringUtils;
|
||||
@@ -33,7 +33,7 @@ public class JavaBuilder {
|
||||
}
|
||||
|
||||
|
||||
public String getJavaType(Field field) {
|
||||
public String getJavaType(PbField field) {
|
||||
String type = field.getTypeString();
|
||||
if (field instanceof MapField) {
|
||||
MapField mf = (MapField) field;
|
||||
@@ -64,7 +64,7 @@ public class JavaBuilder {
|
||||
}
|
||||
}
|
||||
|
||||
public String getAnnotationType(Field field) {
|
||||
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));
|
||||
@@ -73,7 +73,7 @@ public class JavaBuilder {
|
||||
}
|
||||
}
|
||||
|
||||
private void buildMsgImps(ProtoMessage msg, List<Field> tmp, List<String> imps) {
|
||||
private void buildMsgImps(ProtoMessage msg, List<PbField> tmp, List<String> imps) {
|
||||
var fields = msg.getFields();
|
||||
if (CollectionUtils.isNotEmpty(fields)) {
|
||||
for (var field : fields) {
|
||||
@@ -85,7 +85,7 @@ public class JavaBuilder {
|
||||
for (int i = 0; i < tmp.size(); i++) {
|
||||
if (tmp.get(i) instanceof MapField) {
|
||||
addImport(imps, Map.class.getName());
|
||||
} else if (tmp.get(i).getCardinality() == Field.Cardinality.REPEATED) {
|
||||
} else if (tmp.get(i).getCardinality() == PbField.Cardinality.REPEATED) {
|
||||
addImport(imps, List.class.getName());
|
||||
}
|
||||
}
|
||||
@@ -114,7 +114,7 @@ public class JavaBuilder {
|
||||
|
||||
public String buildMessage(Proto proto, ProtoMessage msg, int indent, Map<String, String> defineMsgs, Map<String, Proto> protos) {
|
||||
var level = Math.max(indent, 1);
|
||||
var tmp = new ArrayList<Field>();
|
||||
var tmp = new ArrayList<PbField>();
|
||||
var imps = new ArrayList<String>();
|
||||
var builder = new StringBuilder();
|
||||
|
||||
@@ -122,8 +122,8 @@ public class JavaBuilder {
|
||||
|
||||
buildMsgImps(msg, tmp, imps);
|
||||
|
||||
List<Field> fields = new ArrayList<>();
|
||||
tmp.stream().sorted(Comparator.comparingInt(Field::getTag))
|
||||
List<PbField> fields = new ArrayList<>();
|
||||
tmp.stream().sorted(Comparator.comparingInt(PbField::getTag))
|
||||
.forEach(fields::add);
|
||||
|
||||
// not nested
|
||||
@@ -144,7 +144,7 @@ public class JavaBuilder {
|
||||
|
||||
CodeBuilder cons = new CodeBuilder();
|
||||
for (int i = 0; i < size; i++) {
|
||||
Field f = fields.get(i);
|
||||
PbField f = fields.get(i);
|
||||
getCode = new CodeBuilder();
|
||||
setCode = new CodeBuilder();
|
||||
|
||||
@@ -155,7 +155,7 @@ public class JavaBuilder {
|
||||
buildDocComment(cb, f.getComment(), level);
|
||||
String type = getJavaType(f);
|
||||
String name = f.getName();
|
||||
if (f.getCardinality() == Field.Cardinality.REPEATED) {
|
||||
if (f.getCardinality() == PbField.Cardinality.REPEATED) {
|
||||
String boxedTypeName = getBoxedTypeName(f);
|
||||
type = "List<" + boxedTypeName + ">";
|
||||
}
|
||||
@@ -203,7 +203,7 @@ public class JavaBuilder {
|
||||
return cb.toString();
|
||||
}
|
||||
|
||||
private String getBoxedTypeName(Field f) {
|
||||
private String getBoxedTypeName(PbField f) {
|
||||
String type = getJavaType(f);
|
||||
if (BASE_TYPES.contains(f.getTypeString())) {
|
||||
JavaType javaType = null;
|
||||
|
||||
@@ -16,7 +16,7 @@ package com.zfoo.protocol.serializer.protobuf.wire;
|
||||
/**
|
||||
* protocol buffer中Map的Field的结构体定义
|
||||
*/
|
||||
public class MapField extends Field {
|
||||
public class MapField extends PbField {
|
||||
|
||||
private Type key;
|
||||
private String value;
|
||||
|
||||
+27
-70
@@ -13,14 +13,10 @@
|
||||
|
||||
package com.zfoo.protocol.serializer.protobuf.wire;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
/**
|
||||
* protocol buffer协议消息体属性数据类型定义
|
||||
*/
|
||||
public class Field {
|
||||
public class PbField {
|
||||
/**
|
||||
* 属性是否赋值类型
|
||||
*/
|
||||
@@ -55,83 +51,44 @@ public class Field {
|
||||
|
||||
public enum Type {
|
||||
|
||||
FLOAT("float", WireFormat.JavaType.FLOAT, WireType.FIXED32),
|
||||
DOUBLE("double", WireFormat.JavaType.DOUBLE, WireType.FIXED64),
|
||||
INT32("int32", WireFormat.JavaType.INT, WireType.VARINT),
|
||||
INT64("int64", WireFormat.JavaType.LONG, WireType.VARINT),
|
||||
UINT32("uint32", WireFormat.JavaType.INT, WireType.VARINT),
|
||||
UINT64("uint64", WireFormat.JavaType.LONG, WireType.VARINT),
|
||||
SINT32("sint32", WireFormat.JavaType.INT, WireType.VARINT),
|
||||
SINT64("sint64", WireFormat.JavaType.LONG, WireType.VARINT),
|
||||
FIXED32("fixed32", WireFormat.JavaType.INT, WireType.FIXED32),
|
||||
FIXED64("fixed64", WireFormat.JavaType.LONG, WireType.FIXED64),
|
||||
SFIXED32("sfixed32", WireFormat.JavaType.INT, WireType.FIXED32),
|
||||
SFIXED64("sfixed64", WireFormat.JavaType.LONG, WireType.FIXED64),
|
||||
BOOL("bool", WireFormat.JavaType.BOOLEAN, WireType.VARINT),
|
||||
ENUM("enum", WireFormat.JavaType.ENUM, WireType.VARINT),
|
||||
STRING("string", WireFormat.JavaType.STRING, WireType.LENGTH_DELIMITED) {
|
||||
@Override
|
||||
public boolean packable() {
|
||||
return false;
|
||||
}
|
||||
},
|
||||
BYTES("bytes", WireFormat.JavaType.BYTES, WireType.LENGTH_DELIMITED) {
|
||||
@Override
|
||||
public boolean packable() {
|
||||
return false;
|
||||
}
|
||||
},
|
||||
MESSAGE("", WireFormat.JavaType.MESSAGE, WireType.LENGTH_DELIMITED) {
|
||||
@Override
|
||||
public boolean packable() {
|
||||
return false;
|
||||
}
|
||||
},
|
||||
OBJECT("OBJECT", WireFormat.JavaType.OBJECT, WireType.LENGTH_DELIMITED) {
|
||||
@Override
|
||||
public boolean packable() {
|
||||
return false;
|
||||
}
|
||||
},
|
||||
GROUP("group", WireFormat.JavaType.MESSAGE, WireType.START_GROUP) {
|
||||
@Override
|
||||
public boolean packable() {
|
||||
return false;
|
||||
}
|
||||
},
|
||||
MAP("", WireFormat.JavaType.MAP, WireType.LENGTH_DELIMITED) {
|
||||
@Override
|
||||
public boolean packable() {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
FLOAT("float", WireFormat.JavaType.FLOAT),
|
||||
DOUBLE("double", WireFormat.JavaType.DOUBLE),
|
||||
INT32("int32", WireFormat.JavaType.INT),
|
||||
INT64("int64", WireFormat.JavaType.LONG),
|
||||
UINT32("uint32", WireFormat.JavaType.INT),
|
||||
UINT64("uint64", WireFormat.JavaType.LONG),
|
||||
SINT32("sint32", WireFormat.JavaType.INT),
|
||||
SINT64("sint64", WireFormat.JavaType.LONG),
|
||||
FIXED32("fixed32", WireFormat.JavaType.INT),
|
||||
FIXED64("fixed64", WireFormat.JavaType.LONG),
|
||||
SFIXED32("sfixed32", WireFormat.JavaType.INT),
|
||||
SFIXED64("sfixed64", WireFormat.JavaType.LONG),
|
||||
BOOL("bool", WireFormat.JavaType.BOOLEAN),
|
||||
ENUM("enum", WireFormat.JavaType.ENUM),
|
||||
STRING("string", WireFormat.JavaType.STRING),
|
||||
BYTES("bytes", WireFormat.JavaType.BYTES),
|
||||
MESSAGE("", WireFormat.JavaType.MESSAGE),
|
||||
OBJECT("OBJECT", WireFormat.JavaType.OBJECT),
|
||||
GROUP("group", WireFormat.JavaType.MESSAGE),
|
||||
MAP("", WireFormat.JavaType.MAP);
|
||||
|
||||
private final String value;
|
||||
private final WireFormat.JavaType javaType;
|
||||
private final WireType wireType;
|
||||
|
||||
Type(String value, WireFormat.JavaType javaType, WireType wireType) {
|
||||
Type(String value, WireFormat.JavaType javaType) {
|
||||
this.value = value;
|
||||
this.javaType = javaType;
|
||||
this.wireType = wireType;
|
||||
}
|
||||
|
||||
public WireFormat.JavaType javaType() {
|
||||
return javaType;
|
||||
}
|
||||
|
||||
public WireType wireType() {
|
||||
return wireType;
|
||||
}
|
||||
|
||||
public String value() {
|
||||
return this.value;
|
||||
}
|
||||
|
||||
public boolean packable() {
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -174,7 +131,7 @@ public class Field {
|
||||
* @param cardinality the cardinality to set
|
||||
* @return
|
||||
*/
|
||||
public Field setCardinality(Cardinality cardinality) {
|
||||
public PbField setCardinality(Cardinality cardinality) {
|
||||
this.cardinality = cardinality;
|
||||
return this;
|
||||
}
|
||||
@@ -194,7 +151,7 @@ public class Field {
|
||||
* @param name the name to set
|
||||
* @return
|
||||
*/
|
||||
public Field setName(String name) {
|
||||
public PbField setName(String name) {
|
||||
this.name = name;
|
||||
return this;
|
||||
}
|
||||
@@ -214,7 +171,7 @@ public class Field {
|
||||
* @param type the type to set
|
||||
* @return
|
||||
*/
|
||||
public Field setType(String type) {
|
||||
public PbField setType(String type) {
|
||||
this.type = type;
|
||||
return this;
|
||||
}
|
||||
@@ -234,7 +191,7 @@ public class Field {
|
||||
* @param tag the tag to set
|
||||
* @return
|
||||
*/
|
||||
public Field setTag(int tag) {
|
||||
public PbField setTag(int tag) {
|
||||
this.tag = tag;
|
||||
return this;
|
||||
}
|
||||
@@ -254,7 +211,7 @@ public class Field {
|
||||
* @param comment the comment to set
|
||||
* @return
|
||||
*/
|
||||
public Field setComment(Comment comment) {
|
||||
public PbField setComment(Comment comment) {
|
||||
this.comment = comment;
|
||||
return this;
|
||||
}
|
||||
@@ -27,7 +27,7 @@ public class ProtoMessage {
|
||||
/**
|
||||
* 消息包含的Fields列表
|
||||
*/
|
||||
private List<Field> fields = new ArrayList<>();
|
||||
private List<PbField> fields = new ArrayList<>();
|
||||
/**
|
||||
* 消息内嵌的Message列表
|
||||
*/
|
||||
@@ -45,7 +45,7 @@ public class ProtoMessage {
|
||||
return this;
|
||||
}
|
||||
|
||||
public ProtoMessage addField(Field field) {
|
||||
public ProtoMessage addField(PbField field) {
|
||||
if (field == null) {
|
||||
return this;
|
||||
}
|
||||
@@ -77,7 +77,7 @@ public class ProtoMessage {
|
||||
*
|
||||
* @return the fields
|
||||
*/
|
||||
public List<Field> getFields() {
|
||||
public List<PbField> getFields() {
|
||||
return fields;
|
||||
}
|
||||
|
||||
@@ -86,7 +86,7 @@ public class ProtoMessage {
|
||||
*
|
||||
* @param fields the fields to set
|
||||
*/
|
||||
public ProtoMessage setFields(List<Field> fields) {
|
||||
public ProtoMessage setFields(List<PbField> fields) {
|
||||
this.fields = fields;
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -97,16 +97,6 @@ public class WireFormat {
|
||||
return tag >>> TAG_TYPE_BITS;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据属性的Tag值,以及数据类型,获取一个编码后的int值
|
||||
*
|
||||
* @param tagNum tag的编号
|
||||
* @param wireType 数据类型
|
||||
* @return
|
||||
*/
|
||||
public static int makeTag(final int tagNum, final WireType wireType) {
|
||||
return (tagNum << TAG_TYPE_BITS) | wireType.getValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* java的数据类型和protocol buffer的数据类型的对应关系以及默认值
|
||||
|
||||
@@ -1,81 +0,0 @@
|
||||
/*
|
||||
* Copyright 2021 The edap Project
|
||||
*
|
||||
* 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.wire;
|
||||
|
||||
/**
|
||||
* protocol buffer的协议数据类型
|
||||
*/
|
||||
public enum WireType {
|
||||
/**
|
||||
* 变长整数
|
||||
*/
|
||||
VARINT(0),
|
||||
/**
|
||||
* 定长64位
|
||||
*/
|
||||
FIXED64(1),
|
||||
/**
|
||||
* 指定长度的编码类型
|
||||
*/
|
||||
LENGTH_DELIMITED(2),
|
||||
/**
|
||||
* 组开始标记
|
||||
*/
|
||||
START_GROUP(3),
|
||||
/**
|
||||
* 组结束标记
|
||||
*/
|
||||
END_GROUP(4),
|
||||
/**
|
||||
* 定长32位
|
||||
*/
|
||||
FIXED32(5),
|
||||
/**
|
||||
* OBJECT类型,可以包含任何类型的数据由OBJECT编解码器进行编解码
|
||||
*/
|
||||
OBJECT(6);
|
||||
|
||||
WireType(int value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
private final int value;
|
||||
|
||||
public int getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public static WireType fromValue(int value) {
|
||||
switch (value) {
|
||||
case 0:
|
||||
return VARINT;
|
||||
case 1:
|
||||
return FIXED64;
|
||||
case 2:
|
||||
return LENGTH_DELIMITED;
|
||||
case 3:
|
||||
return START_GROUP;
|
||||
case 4:
|
||||
return END_GROUP;
|
||||
case 5:
|
||||
return FIXED32;
|
||||
case 6:
|
||||
return OBJECT;
|
||||
default:
|
||||
throw new IllegalArgumentException("no enum value WireType " + value);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
+7
-7
@@ -16,8 +16,8 @@ package com.zfoo.protocol.serializer.protobuf.wire.parser;
|
||||
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.Field.Cardinality;
|
||||
import com.zfoo.protocol.serializer.protobuf.wire.Field.Type;
|
||||
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.*;
|
||||
@@ -177,10 +177,10 @@ public class ProtoParser {
|
||||
// msg.addMessage(parseMessage());
|
||||
notSupportInnerMessage();
|
||||
} else if (fieldCardinalities.contains(token)) {
|
||||
Field field = parseField(Cardinality.valueOf(token.toUpperCase(Locale.ENGLISH)), null);
|
||||
PbField field = parseField(Cardinality.valueOf(token.toUpperCase(Locale.ENGLISH)), null);
|
||||
msg.addField(field);
|
||||
} else {
|
||||
Field field = parseField(Cardinality.OPTIONAL, token);
|
||||
PbField field = parseField(Cardinality.OPTIONAL, token);
|
||||
msg.addField(field);
|
||||
}
|
||||
boolean isEnd = blockEnd();
|
||||
@@ -414,7 +414,7 @@ public class ProtoParser {
|
||||
}
|
||||
}
|
||||
|
||||
private Field parseField(Cardinality cardinality, String fieldType) throws RuntimeException {
|
||||
private PbField parseField(Cardinality cardinality, String fieldType) throws RuntimeException {
|
||||
trim();
|
||||
if (fieldType == null) {
|
||||
fieldType = readToken();
|
||||
@@ -442,12 +442,12 @@ public class ProtoParser {
|
||||
comment.getLines().add(readSingleLineComment());
|
||||
}
|
||||
nextLine();
|
||||
Field field;
|
||||
PbField field;
|
||||
if (gType != null) {
|
||||
Type type = Type.valueOf(gType.getKeyType().toUpperCase(Locale.ENGLISH));
|
||||
field = new MapField().setKey(type).setValue(gType.getValueType());
|
||||
} else {
|
||||
field = new Field();
|
||||
field = new PbField();
|
||||
}
|
||||
field.setCardinality(cardinality)
|
||||
.setName(fieldName)
|
||||
|
||||
Reference in New Issue
Block a user