perf[protobuf]: generate pojo from proto

This commit is contained in:
godotg committed 2023-12-03 13:36:43 +08:00
1 parent d6fc68fc93
commit 04dbdd018d
7 files changed
+41 -182

No files matched your search

@@ -96,12 +96,8 @@ public class GeneratePbUtils {
List<ProtoMessage> msgs = proto.getMessages(); List<ProtoMessage> msgs = proto.getMessages();
for (var msg : msgs) { for (var msg : msgs) {
StringBuilder mc = new StringBuilder(); StringBuilder mc = new StringBuilder();
if (msg.getComment() != null) { if (CollectionUtils.isNotEmpty(msg.getComments())) {
mc.append(msg.getComment()); msg.getComments().forEach(c -> mc.append(c));
} else {
if (msg.getComment() != null && msg.getComment().getLines() != null) {
msg.getComment().getLines().forEach(c -> mc.append(c));
}
} }
msgComments.put(msg.getName(), mc.toString()); msgComments.put(msg.getName(), mc.toString());
var code = builder.buildMessage(proto, msg, 1, null, protos); var code = builder.buildMessage(proto, msg, 1, null, protos);
@@ -91,13 +91,20 @@ public class JavaBuilder {
} }
} }
private void buildDocComment(CodeBuilder cb, Comment comment, int level) { private void buildDocComment(CodeBuilder cb, ProtoMessage msg) {
if (comment == null || comment.getLines() == null || comment.getLines().isEmpty()) { if (CollectionUtils.isEmpty(msg.getComments())) {
return; return;
} }
cb.t(level).c("/**").ln(); cb.t(1).c("/**").ln();
comment.getLines().forEach(c -> cb.t(level).c(" * ").c(c).ln()); msg.getComments().forEach(c -> cb.t(1).c(" * ").c(c).ln());
cb.t(level).c(" */").ln(); cb.t(1).c(" */").ln();
}
private void buildFieldComment(CodeBuilder cb, PbField pbField) {
if (CollectionUtils.isEmpty(pbField.getComments())) {
return;
}
pbField.getComments().forEach(c -> cb.t(1).c("// ").c(c).ln());
} }
private String getJavaPackage(Proto proto) { private String getJavaPackage(Proto proto) {
@@ -131,7 +138,7 @@ public class JavaBuilder {
.forEach(e -> cb.t(level - 1).e("import $cls$;").arg(e).ln()); .forEach(e -> cb.t(level - 1).e("import $cls$;").arg(e).ln());
cb.ln(); cb.ln();
buildDocComment(cb, msg.getComment(), level - 1); buildDocComment(cb, msg);
cb.t(level - 1).e("public class $name$ {").arg(msg.getName()).ln(2); cb.t(level - 1).e("public class $name$ {").arg(msg.getName()).ln(2);
CodeBuilder getCode; CodeBuilder getCode;
@@ -152,7 +159,7 @@ public class JavaBuilder {
if (msg.getName().equals("Type")) { if (msg.getName().equals("Type")) {
typeName = Proto.class.getPackage().getName() + ".wire.Field." + typeName; typeName = Proto.class.getPackage().getName() + ".wire.Field." + typeName;
} }
buildDocComment(cb, f.getComment(), level); buildFieldComment(cb, f);
String type = getJavaType(f); String type = getJavaType(f);
String name = f.getName(); String name = f.getName();
if (f.getCardinality() == PbField.Cardinality.REPEATED) { if (f.getCardinality() == PbField.Cardinality.REPEATED) {
@@ -48,10 +48,6 @@ public class Proto {
* 文档导入的文件的路径列表 * 文档导入的文件的路径列表
*/ */
private List<String> imports = new ArrayList<>(); private List<String> imports = new ArrayList<>();
/**
* 文档的注释
*/
private String comment;
/** /**
* proto文件的单行注释列表 * proto文件的单行注释列表
*/ */
@@ -157,39 +153,11 @@ public class Proto {
return imports; return imports;
} }
/**
* 文档的注释
*
* @return the comment
*/
public String getComment() {
return comment;
}
/** public List<String> getComments() {
* 文档的注释 return comments;
*
* @param comment the comment to set
* @return
*/
public Proto setComment(String comment) {
this.comment = comment;
return this;
} }
/**
* proto文件的单行注释列表
*
* @param comments the comments to set
*/
public void addComments(List<String> comments) {
if (CollectionUtils.isEmpty(comments)) {
return;
}
this.comments.addAll(comments);
}
/** /**
* @return the file * @return the file
*/ */
@@ -14,7 +14,6 @@ package com.zfoo.protocol.serializer.protobuf.parser;
import com.zfoo.protocol.collection.CollectionUtils; import com.zfoo.protocol.collection.CollectionUtils;
import com.zfoo.protocol.serializer.protobuf.wire.*; 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.Cardinality;
import com.zfoo.protocol.util.StringUtils; import com.zfoo.protocol.util.StringUtils;
@@ -53,7 +52,7 @@ public class ProtoParser {
/** /**
* 解析时临时存放解析的注释信息 * 解析时临时存放解析的注释信息
*/ */
protected List<Comment> comments = new ArrayList<>(); protected List<String> comments = new ArrayList<>();
public ProtoParser(String data) { public ProtoParser(String data) {
@@ -72,17 +71,17 @@ public class ProtoParser {
switch (token) { switch (token) {
case "//": case "//":
String line = readSingleLineComment(); String line = readSingleLineComment();
addCommentLines(CommentType.INLINE, Arrays.asList(line)); comments.add(line);
nextLine(); nextLine();
break; break;
case "/*": case "/*":
lines = readMultiLineComment(); lines = readMultiLineComment();
addCommentLines(CommentType.MULTILINE, lines); comments.addAll(lines);
nextLine(); nextLine();
break; break;
case "/**": case "/**":
lines = readMultiLineComment(); lines = readMultiLineComment();
addCommentLines(CommentType.DOCUMENT, lines); comments.addAll(lines);
nextLine(); nextLine();
break; break;
case "syntax": case "syntax":
@@ -149,8 +148,8 @@ public class ProtoParser {
throw new RuntimeException(ROW_MSG + row + "] message name not set"); throw new RuntimeException(ROW_MSG + row + "] message name not set");
} }
msg.setName(name); msg.setName(name);
if (!comments.isEmpty()) { if (CollectionUtils.isNotEmpty(comments)) {
msg.setComment(comments.get(comments.size() - 1)); msg.getComments().addAll(comments);
comments.clear(); comments.clear();
} }
System.out.println("message " + name + START_MSG); System.out.println("message " + name + START_MSG);
@@ -159,7 +158,7 @@ public class ProtoParser {
while (token.length() > 0) { while (token.length() > 0) {
if ("//".equals(token)) { if ("//".equals(token)) {
var singleLine = readSingleLineComment(); var singleLine = readSingleLineComment();
addCommentLines(CommentType.INLINE, List.of(singleLine)); comments.add(singleLine);
nextLine(); nextLine();
} else if ("enum".equals(token)) { } else if ("enum".equals(token)) {
notSupportEnum(); notSupportEnum();
@@ -218,25 +217,12 @@ public class ProtoParser {
} }
// -------------------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------------------
private void addCommentLines(Comment.CommentType type, List<String> lines) {
Comment c;
if (CollectionUtils.isEmpty(comments)) {
comments.add(Comment.valueOf(type, lines));
return;
}
c = comments.get(comments.size() - 1);
if (c.getType() == type) {
c.getLines().addAll(lines);
} else {
comments.add(Comment.valueOf(type, lines));
}
}
private void addCommentsToProto(Proto proto) { private void addCommentsToProto(Proto proto) {
if (CollectionUtils.isEmpty(comments)) { if (CollectionUtils.isEmpty(comments)) {
return; return;
} }
comments.forEach(c -> proto.addComments(c.getLines())); proto.getComments().addAll(comments);
comments.clear(); comments.clear();
} }
@@ -409,15 +395,9 @@ public class ProtoParser {
ProtoValue pv = readValueUtilSemicolon(); ProtoValue pv = readValueUtilSemicolon();
int tag = parseInt(pv.getValue()); int tag = parseInt(pv.getValue());
trim(); trim();
Comment comment = new Comment();
if (!comments.isEmpty()) {
var lastComment = comments.get(comments.size() - 1);
comment.getLines().addAll(lastComment.getLines());
}
String token = readToken(); String token = readToken();
if ("//".equals(token)) { if ("//".equals(token)) {
comment.getLines().add(readSingleLineComment()); comments.add(readSingleLineComment());
} }
nextLine(); nextLine();
PbField field; PbField field;
@@ -431,15 +411,13 @@ public class ProtoParser {
.setName(fieldName) .setName(fieldName)
.setTag(tag) .setTag(tag)
.setType(fieldType); .setType(fieldType);
field.setComment(comment); field.getComments().addAll(comments);
comments.clear(); comments.clear();
return field; return field;
} }
/** /**
* 解析判断语句块是否开始,去掉空格和空行后第一个字符为"{"的返回语句块开始 * 解析判断语句块是否开始,去掉空格和空行后第一个字符为"{"的返回语句块开始
*
* @throws RuntimeException
*/ */
private void blockStarted(String structName) throws RuntimeException { private void blockStarted(String structName) throws RuntimeException {
trim(); trim();
@@ -1,70 +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;
import java.util.ArrayList;
import java.util.List;
/**
* proto文件中注释的结构定义
*/
public class Comment {
public enum CommentType {
/**
* 以"//"开头的注释类型
*/
INLINE,
/**
* 以 "/*"开头的注释类型
*/
MULTILINE,
/**
* 以 "/**"开头的注释类型
*/
DOCUMENT
}
/**
* 注释的类型
*/
private CommentType type = CommentType.INLINE;
/**
* 注释内容文本行列表
*/
private List<String> lines = new ArrayList<>();
public static Comment valueOf(CommentType type, List<String> lines) {
var comment = new Comment();
comment.type = type;
comment.lines.addAll(lines);
return comment;
}
public List<String> getLines() {
return lines;
}
public void setLines(List<String> lines) {
this.lines = lines;
}
public CommentType getType() {
return type;
}
public void setType(CommentType type) {
this.type = type;
}
}
@@ -14,6 +14,9 @@
package com.zfoo.protocol.serializer.protobuf.wire; package com.zfoo.protocol.serializer.protobuf.wire;
import java.util.ArrayList;
import java.util.List;
/** /**
* protocol buffer协议消息体属性数据类型定义 * protocol buffer协议消息体属性数据类型定义
*/ */
@@ -69,7 +72,7 @@ public class PbField {
/** /**
* 消息属性的注释信息 * 消息属性的注释信息
*/ */
private Comment comment; private List<String> comments = new ArrayList<>();
public String getTypeString() { public String getTypeString() {
return this.getType(); return this.getType();
@@ -155,23 +158,11 @@ public class PbField {
return this; return this;
} }
/** public List<String> getComments() {
* 消息属性的注释信息 return comments;
*
* @return the comment
*/
public Comment getComment() {
return comment;
} }
/** public void setComments(List<String> comments) {
* 消息属性的注释信息 this.comments = comments;
*
* @param comment the comment to set
* @return
*/
public PbField setComment(Comment comment) {
this.comment = comment;
return this;
} }
} }
@@ -35,7 +35,7 @@ public class ProtoMessage {
/** /**
* 消息的备注信息 * 消息的备注信息
*/ */
private Comment comment; private List<String> comments = new ArrayList<>();
public ProtoMessage addMessage(ProtoMessage child) { public ProtoMessage addMessage(ProtoMessage child) {
if (child == null) { if (child == null) {
@@ -110,22 +110,11 @@ public class ProtoMessage {
return this; return this;
} }
/** public List<String> getComments() {
* 消息的备注信息 return comments;
*
* @return the comment
*/
public Comment getComment() {
return comment;
} }
/** public void setComments(List<String> comments) {
* 消息的备注信息 this.comments = comments;
*
* @param comment the comment to set
*/
public ProtoMessage setComment(Comment comment) {
this.comment = comment;
return this;
} }
} }