perf[protobuf]: generate pojo from proto

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