mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-10-19 22:16:01 +00:00
perf[protobuf]: generate pojo in protobuf
This commit is contained in:
1 parent
e174fc5480
commit
278956a8ee
4 files changed
+19
-161
No files matched your search
+15
-70
@@ -33,18 +33,6 @@ public class JavaBuilder {
|
||||
}
|
||||
|
||||
|
||||
private String getOptionJavaType(List<Option> options) {
|
||||
String jType = "";
|
||||
if (options != null && !options.isEmpty()) {
|
||||
for (Option option : options) {
|
||||
if ("java_type".equals(option.getName())) {
|
||||
jType = option.getValue();
|
||||
}
|
||||
}
|
||||
}
|
||||
return jType;
|
||||
}
|
||||
|
||||
public String getJavaType(Field field, List<String> imps) {
|
||||
String type = field.getTypeString();
|
||||
if (field instanceof MapField) {
|
||||
@@ -53,43 +41,11 @@ public class JavaBuilder {
|
||||
+ getJavaType(mf.getValue()) + ">";
|
||||
return type;
|
||||
}
|
||||
String jType = getOptionJavaType(field.getOptions());
|
||||
if (!BASE_TYPES.contains(type.toLowerCase(Locale.ENGLISH))) {
|
||||
return type;
|
||||
}
|
||||
JavaType javaType = null;
|
||||
javaType = Type.valueOf(type.toUpperCase(Locale.ENGLISH)).javaType();
|
||||
if (javaType != null && javaType.getTypeString() != null) {
|
||||
String optionJavaType = getOptionJavaType(field.getOptions());
|
||||
if (optionJavaType == null || optionJavaType.isEmpty()) {
|
||||
type = javaType.getTypeString();
|
||||
} else {
|
||||
int lastDot = optionJavaType.lastIndexOf(".");
|
||||
if (lastDot == -1) {
|
||||
type = optionJavaType;
|
||||
} else {
|
||||
if (!imps.contains(optionJavaType)) {
|
||||
imps.add(optionJavaType);
|
||||
}
|
||||
type = optionJavaType.substring(lastDot + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
if ("int64".equals(field.getType()) && jType != null) {
|
||||
switch (jType) {
|
||||
case "LocalDateTime":
|
||||
addImport(imps, "java.time.LocalDateTime");
|
||||
type = jType;
|
||||
break;
|
||||
case "LocalDate":
|
||||
type = jType;
|
||||
addImport(imps, "java.time.LocalDate");
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
return type;
|
||||
JavaType javaType = Type.valueOf(type.toUpperCase(Locale.ENGLISH)).javaType();
|
||||
return javaType.getTypeString();
|
||||
}
|
||||
|
||||
private void addImport(List<String> imps, String imp) {
|
||||
@@ -119,11 +75,12 @@ public class JavaBuilder {
|
||||
}
|
||||
|
||||
private void buildMsgImps(ProtoMessage msg, List<Field> tmp, List<String> imps) {
|
||||
if (msg.getFields() != null) {
|
||||
msg.getFields().forEach(e -> {
|
||||
getJavaType(e, imps);
|
||||
tmp.add(e);
|
||||
});
|
||||
var fields = msg.getFields();
|
||||
if (CollectionUtils.isNotEmpty(fields)) {
|
||||
for (var field : fields) {
|
||||
getJavaType(field, imps);
|
||||
tmp.add(field);
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < tmp.size(); i++) {
|
||||
@@ -158,10 +115,13 @@ public class JavaBuilder {
|
||||
}
|
||||
|
||||
public String buildMessage(Proto proto, ProtoMessage msg, int indent, Map<String, String> defineMsgs, Map<String, Proto> protos) {
|
||||
int level = Math.max(indent, 1);
|
||||
final CodeBuilder cb = new CodeBuilder();
|
||||
List<Field> tmp = new ArrayList<>();
|
||||
List<String> imps = new ArrayList<>();
|
||||
var level = Math.max(indent, 1);
|
||||
var tmp = new ArrayList<Field>();
|
||||
var imps = new ArrayList<String>();
|
||||
var builder = new StringBuilder();
|
||||
|
||||
var cb = new CodeBuilder();
|
||||
|
||||
buildMsgImps(msg, tmp, imps);
|
||||
|
||||
List<Field> fields = new ArrayList<>();
|
||||
@@ -240,7 +200,6 @@ public class JavaBuilder {
|
||||
cb.ln().c(setCodes.get(i));
|
||||
}
|
||||
|
||||
nestMessageMessage(proto, cb, msg.getMessages(), defineMsgs, level, protos);
|
||||
|
||||
cb.t(level - 1).c("}");
|
||||
return cb.toString();
|
||||
@@ -258,18 +217,4 @@ public class JavaBuilder {
|
||||
return type;
|
||||
}
|
||||
|
||||
|
||||
private void nestMessageMessage(Proto proto, CodeBuilder cb,
|
||||
List<ProtoMessage> msgs, Map<String, String> defineMsgs,
|
||||
int level, Map<String, Proto> protos) {
|
||||
if (msgs == null || msgs.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
cb.ln();
|
||||
msgs.stream().sorted(Comparator.comparing(ProtoMessage::getName))
|
||||
.forEach(e -> cb.c(buildMessage(proto, e, level + 1, defineMsgs, protos)));
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -146,10 +146,6 @@ public class Field {
|
||||
* 消息属性值的类型
|
||||
*/
|
||||
private String type;
|
||||
/**
|
||||
* 消息属性值类型的字符串
|
||||
*/
|
||||
protected String typeString;
|
||||
/**
|
||||
* 属性的顺序标记一个消息内不允许重复
|
||||
*/
|
||||
@@ -158,14 +154,6 @@ public class Field {
|
||||
* 消息属性默认值
|
||||
*/
|
||||
private String defaultValue;
|
||||
/**
|
||||
* repeated的属性是否对包含的对象列表打包
|
||||
*/
|
||||
private List<Option> options;
|
||||
/**
|
||||
* 属性是否已经被标示为过时的
|
||||
*/
|
||||
private Boolean deprecated;
|
||||
/**
|
||||
* 消息属性的注释信息
|
||||
*/
|
||||
@@ -287,62 +275,6 @@ public class Field {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* repeated的属性是否对包含的对象列表打包
|
||||
*
|
||||
* @return the options
|
||||
*/
|
||||
public List<Option> getOptions() {
|
||||
if (options == null) {
|
||||
options = new ArrayList<>();
|
||||
}
|
||||
return options;
|
||||
}
|
||||
|
||||
/**
|
||||
* repeated的属性是否对包含的对象列表打包
|
||||
*
|
||||
* @param options the options to set
|
||||
* @return
|
||||
*/
|
||||
public Field setOptions(List<Option> options) {
|
||||
if (options instanceof ArrayList) {
|
||||
this.options = options;
|
||||
} else {
|
||||
getOptions().addAll(options);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 属性是否已经被标示为过时的
|
||||
*
|
||||
* @return the deprecated
|
||||
*/
|
||||
public Boolean getDeprecated() {
|
||||
if (deprecated == null) {
|
||||
for (Option option : getOptions()) {
|
||||
if ("deprecated".equals(option.getName())) {
|
||||
return Option.getBoolean(option.getValue());
|
||||
}
|
||||
}
|
||||
return false;
|
||||
} else {
|
||||
return deprecated;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 属性是否已经被标示为过时的
|
||||
*
|
||||
* @param deprecated the deprecated to set
|
||||
* @return
|
||||
*/
|
||||
public Field setDeprecated(Boolean deprecated) {
|
||||
this.deprecated = deprecated;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 消息属性的注释信息
|
||||
*
|
||||
|
||||
+4
-20
@@ -27,11 +27,11 @@ public class ProtoMessage {
|
||||
/**
|
||||
* 消息包含的Fields列表
|
||||
*/
|
||||
private List<Field> fields;
|
||||
private List<Field> fields = new ArrayList<>();
|
||||
/**
|
||||
* 消息内嵌的Message列表
|
||||
*/
|
||||
private List<ProtoMessage> protoMessages;
|
||||
private List<ProtoMessage> protoMessages = new ArrayList<>();
|
||||
/**
|
||||
* 消息的备注信息
|
||||
*/
|
||||
@@ -78,9 +78,6 @@ public class ProtoMessage {
|
||||
* @return the fields
|
||||
*/
|
||||
public List<Field> getFields() {
|
||||
if (fields == null) {
|
||||
fields = new ArrayList<>();
|
||||
}
|
||||
return fields;
|
||||
}
|
||||
|
||||
@@ -90,12 +87,7 @@ public class ProtoMessage {
|
||||
* @param fields the fields to set
|
||||
*/
|
||||
public ProtoMessage setFields(List<Field> fields) {
|
||||
if (fields instanceof ArrayList) {
|
||||
this.fields = fields;
|
||||
} else {
|
||||
this.fields = new ArrayList<>();
|
||||
this.fields.addAll(fields);
|
||||
}
|
||||
this.fields = fields;
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -105,9 +97,6 @@ public class ProtoMessage {
|
||||
* @return the messages
|
||||
*/
|
||||
public List<ProtoMessage> getMessages() {
|
||||
if (protoMessages == null) {
|
||||
protoMessages = new ArrayList<>();
|
||||
}
|
||||
return protoMessages;
|
||||
}
|
||||
|
||||
@@ -117,12 +106,7 @@ public class ProtoMessage {
|
||||
* @param protoMessages the messages to set
|
||||
*/
|
||||
public ProtoMessage setMessages(List<ProtoMessage> protoMessages) {
|
||||
if (protoMessages instanceof ArrayList) {
|
||||
this.protoMessages = protoMessages;
|
||||
} else {
|
||||
this.protoMessages = new ArrayList<>();
|
||||
this.protoMessages.addAll(protoMessages);
|
||||
}
|
||||
this.protoMessages = protoMessages;
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
-3
@@ -454,9 +454,6 @@ public class ProtoParser {
|
||||
.setTag(tag)
|
||||
.setType(fieldType);
|
||||
field.setComment(comment);
|
||||
if (pv.getOptions() != null && !pv.getOptions().isEmpty()) {
|
||||
field.setOptions(pv.getOptions());
|
||||
}
|
||||
comments.clear();
|
||||
return field;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user