perf[protobuf]: map box type of protobuf

This commit is contained in:
godotg committed 2023-12-03 15:59:29 +08:00
1 parent c8b83210a0
commit d5c4badff6
5 files changed
+46 -49

No files matched your search

@@ -29,30 +29,39 @@ import static com.zfoo.protocol.util.FileUtils.LS;
import static com.zfoo.protocol.util.StringUtils.TAB;
public class JavaBuilder {
private static final List<String> BASE_TYPES;
static {
BASE_TYPES = new ArrayList<>();
String[] types = ("double,float,int32,int64,uint32,uint64,sint32,sint64,"
+ "fixed32,fixed64,sfixed32,sfixed64,bool,string,bytes").split(",");
BASE_TYPES.addAll(Arrays.asList(types));
}
public String getJavaType(PbField field) {
String type = field.getTypeString();
String type = field.getType();
if (field instanceof MapField) {
MapField mf = (MapField) field;
type = "Map<" + getJavaType(mf.getKey().value()) + ", " + getJavaType(mf.getValue()) + ">";
var mapField = (MapField) field;
type = StringUtils.format("Map<{}, {}>", getBoxJavaType(mapField.getKey().value()), getBoxJavaType(mapField.getValue()));
return type;
}
if (!BASE_TYPES.contains(type.toLowerCase(Locale.ENGLISH))) {
return getJavaType(type);
}
public String getJavaType(String type) {
var typeProtobuf = TypeProtobuf.typeOfProtobuf(type);
if (typeProtobuf == null) {
return type;
}
TypeJava javaType = TypeProtobuf.valueOf(type.toUpperCase(Locale.ENGLISH)).javaType();
var javaType = typeProtobuf.javaType();
return javaType.getTypeString();
}
private String getBoxJavaType(PbField field) {
return getBoxJavaType(field.getType());
}
private String getBoxJavaType(String type) {
var typeProtobuf = TypeProtobuf.typeOfProtobuf(type);
if (typeProtobuf == null) {
return type;
}
var javaType = typeProtobuf.javaType();
return javaType.getBoxedType();
}
private void addImport(List<String> imps, String imp) {
if (imps == null) {
return;
@@ -62,13 +71,6 @@ public class JavaBuilder {
}
}
public String getJavaType(String type) {
if (BASE_TYPES.contains(type.toLowerCase(Locale.ENGLISH))) {
return TypeProtobuf.valueOf(type.toUpperCase(Locale.ENGLISH)).javaType().getTypeString();
} else {
return type;
}
}
private void buildMsgImps(ProtoMessage msg, List<PbField> tmp, List<String> imps) {
var fields = msg.getFields();
@@ -144,7 +146,7 @@ public class JavaBuilder {
String type = getJavaType(f);
String name = f.getName();
if (f.getCardinality() == PbField.Cardinality.REPEATED) {
String boxedTypeName = getBoxedTypeName(f);
String boxedTypeName = getBoxJavaType(f);
type = "List<" + boxedTypeName + ">";
}
@@ -172,16 +174,4 @@ public class JavaBuilder {
return builder.toString();
}
private String getBoxedTypeName(PbField f) {
String type = getJavaType(f);
if (BASE_TYPES.contains(f.getTypeString())) {
TypeJava javaType = null;
javaType = TypeProtobuf.valueOf(f.getTypeString().toUpperCase(Locale.ENGLISH)).javaType();
if (javaType != null && javaType.getTypeString() != null) {
type = javaType.getBoxedType();
}
}
return type;
}
}
@@ -12,6 +12,9 @@
package com.zfoo.protocol.serializer.protobuf.parser;
import java.util.HashMap;
import java.util.Map;
/**
* @author godotg
*/
@@ -40,6 +43,14 @@ public enum TypeProtobuf {
private final String value;
private final TypeJava javaType;
private static final Map<String, TypeProtobuf> map = new HashMap<>();
static {
for (var ele : TypeProtobuf.values()) {
map.put(ele.value, ele);
}
}
TypeProtobuf(String value, TypeJava javaType) {
this.value = value;
this.javaType = javaType;
@@ -53,4 +64,8 @@ public enum TypeProtobuf {
public String value() {
return this.value;
}
public static TypeProtobuf typeOfProtobuf(String str) {
return map.get(str);
}
}
@@ -23,18 +23,6 @@ public class MapField extends PbField {
private TypeProtobuf key;
private String value;
@Override
public String getTypeString() {
StringBuilder sb = new StringBuilder();
String keyStr = "";
if (getKey() != null) {
keyStr = getKey().value();
}
sb.append("map<").append(keyStr).append(", ");
sb.append(getValue()).append(">");
return sb.toString();
}
/**
* @return the key
*/
@@ -88,9 +88,6 @@ public class PbField {
*/
private List<String> comments = new ArrayList<>();
public String getTypeString() {
return this.getType();
}
/**
* 消息属性的规则
@@ -9,6 +9,10 @@ message OneMessage {
Proj value = 1;
}
message SimpleObject {
int64 aa = 1;
}
// 测试pb协议1
message Proj {
int64 id = 1;
@@ -16,4 +20,7 @@ message Proj {
string repoPath = 3;
//zzzzzzz
repeated int32 a = 4;//rrrrrrrrrrrrrrrrrrrrrrr
map<int32, int64> b = 5;
map<int32, string> c = 6;
map<int32, SimpleObject> d = 7;
}