Skip to content

Commit

Permalink
perf[protobuf]: generate pojo from proto
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysunxiao committed Dec 3, 2023
1 parent 278956a commit ec17790
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,11 @@ public class JavaBuilder {
}


public String getJavaType(Field field, List<String> imps) {
public String getJavaType(Field field) {
String type = field.getTypeString();
if (field instanceof MapField) {
MapField mf = (MapField) field;
type = "Map<" + getJavaType(mf.getKey().value()) + ", "
+ getJavaType(mf.getValue()) + ">";
type = "Map<" + getJavaType(mf.getKey().value()) + ", " + getJavaType(mf.getValue()) + ">";
return type;
}
if (!BASE_TYPES.contains(type.toLowerCase(Locale.ENGLISH))) {
Expand Down Expand Up @@ -78,17 +77,16 @@ private void buildMsgImps(ProtoMessage msg, List<Field> tmp, List<String> imps)
var fields = msg.getFields();
if (CollectionUtils.isNotEmpty(fields)) {
for (var field : fields) {
getJavaType(field, imps);
getJavaType(field);
tmp.add(field);
}
}

for (int i = 0; i < tmp.size(); i++) {
if (tmp.get(i) instanceof MapField) {
addImport(imps, "java.util.Map");
addImport(imps, Map.class.getName());
} else if (tmp.get(i).getCardinality() == Field.Cardinality.REPEATED) {
addImport(imps, "java.util.List");
addImport(imps, "java.util.ArrayList");
addImport(imps, List.class.getName());
}
}
}
Expand Down Expand Up @@ -155,7 +153,7 @@ public String buildMessage(Proto proto, ProtoMessage msg, int indent, Map<String
typeName = Proto.class.getPackage().getName() + ".wire.Field." + typeName;
}
buildDocComment(cb, f.getComment(), level);
String type = getJavaType(f, imps);
String type = getJavaType(f);
String name = f.getName();
if (f.getCardinality() == Field.Cardinality.REPEATED) {
String boxedTypeName = getBoxedTypeName(f);
Expand Down Expand Up @@ -206,7 +204,7 @@ public String buildMessage(Proto proto, ProtoMessage msg, int indent, Map<String
}

private String getBoxedTypeName(Field f) {
String type = getJavaType(f, null);
String type = getJavaType(f);
if (BASE_TYPES.contains(f.getTypeString())) {
JavaType javaType = null;
javaType = Type.valueOf(f.getTypeString().toUpperCase(Locale.ENGLISH)).javaType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,6 @@ public boolean packable() {
* 属性的顺序标记一个消息内不允许重复
*/
private int tag;
/**
* 消息属性默认值
*/
private String defaultValue;
/**
* 消息属性的注释信息
*/
Expand Down Expand Up @@ -243,38 +239,6 @@ public Field setTag(int tag) {
return this;
}

/**
* 消息属性默认值
*
* @return the defaultValue
*/
public String getDefaultValue() {
if (defaultValue == null) {
try {
Type fieldType = Type.valueOf(type.toUpperCase(Locale.ENGLISH));
if (fieldType.javaType() != null && fieldType.javaType().defaultValue() != null) {
return String.valueOf(fieldType.javaType().defaultValue());
}
} catch (Exception e) {

}
} else {
return defaultValue;
}
return null;
}

/**
* 消息属性默认值
*
* @param defaultValue the defaultValue to set
* @return
*/
public Field setDefaultValue(String defaultValue) {
this.defaultValue = defaultValue;
return this;
}

/**
* 消息属性的注释信息
*
Expand Down

0 comments on commit ec17790

Please sign in to comment.