Skip to content

Commit

Permalink
fix[pb]: the note of class generated by the protobuf protocol is inco…
Browse files Browse the repository at this point in the history
…rrect
  • Loading branch information
jaysunxiao committed Jan 23, 2024
1 parent 4bf6025 commit 9b325f0
Showing 1 changed file with 37 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,6 @@ public static void generate(PbGenerateOperation pbGenerateOperation, List<Proto>
builder.append(StringUtils.format("public class {} {", outClassName)).append(LS);
// inner class builder
for (var pbMessage : pbMessages) {
// document
var documentComment = buildDocumentComment(pbMessage);
builder.append(GenerateProtocolFile.addTabs(documentComment, 1));
// message
if (pbGenerateOperation.generateRecordClass(pbMessage.getName())) {
var recordBody = buildRecordBody(pbMessage);
Expand All @@ -166,6 +163,7 @@ public static void generate(PbGenerateOperation pbGenerateOperation, List<Proto>
classBody = classBody.replaceFirst("public class ", "public static class ");
builder.append(GenerateProtocolFile.addTabs(classBody, 1));
}
builder.append(LS);
}
builder.append("}");
var filePath = StringUtils.format("{}/{}.java", protocolOutputPath, outClassName);
Expand Down Expand Up @@ -238,10 +236,6 @@ public static String buildMessage(PbGenerateOperation pbGenerateOperation, List<
var imports = buildMessageImports(pbGenerateOperation, protos, proto, pbMessage);
builder.append(imports);

// document
var documentComment = buildDocumentComment(pbMessage);
builder.append(documentComment);

// message
if (pbGenerateOperation.generateRecordClass(pbMessage.getName())) {
var recordBody = buildRecordBody(pbMessage);
Expand Down Expand Up @@ -330,18 +324,32 @@ private static String buildProtoComment(Proto proto) {
return builder.toString();
}

private static String buildDocumentComment(PbMessage msg) {
if (CollectionUtils.isEmpty(msg.getComments())) {
private static String buildClassNote(PbMessage pbMessage) {
if (CollectionUtils.isEmpty(pbMessage.getComments())) {
return StringUtils.EMPTY;
}
var comments = pbMessage.getComments();
var builder = new StringBuilder();
builder.append("/**").append(LS);
msg.getComments().forEach(it -> builder.append(StringUtils.format(" * {}", it)).append(LS));
builder.append(" */").append(LS);
if (comments.size() == 1) {
builder.append(StringUtils.format("@Note(\"{}\")", comments.get(0))).append(LS);
} else {
builder.append("@Note(").append(LS);
for (int i = 0; i < comments.size(); i++) {
var comment = comments.get(i);
if (i == comments.size() - 1) {
builder.append(TAB);
builder.append(StringUtils.format("\"{}\")", comment));
} else {
builder.append(TAB);
builder.append(StringUtils.format("\"{}\\n\" + ", comment, LS));
}
builder.append(LS);
}
}
return builder.toString();
}

private static String buildFieldComment(PbField pbField) {
private static String buildFieldNote(PbField pbField) {
var comments = pbField.getComments();
if (CollectionUtils.isEmpty(comments)) {
return StringUtils.EMPTY;
Expand All @@ -368,7 +376,12 @@ private static String buildFieldComment(PbField pbField) {

private static String buildRecordBody(PbMessage pbMessage) {
var builder = new StringBuilder();
// protocol id
builder.append(StringUtils.format("@Protocol(id = {})", pbMessage.getProtocolId())).append(LS);
// note
var documentComment = buildClassNote(pbMessage);
builder.append(documentComment);
// class body
builder.append(StringUtils.format("public record {} (", pbMessage.getName())).append(LS);

var pbFields = pbMessage.getFields()
Expand All @@ -381,7 +394,7 @@ private static String buildRecordBody(PbMessage pbMessage) {
var type = getJavaType(pbField);
var name = pbField.getName();

var fieldComment = buildFieldComment(pbField);
var fieldComment = buildFieldNote(pbField);
builder.append(fieldComment);
if (isCompatiblePbField(pbField)) {
var tag = pbField.getTag() - COMPATIBLE_FIELD_TAG;
Expand All @@ -400,7 +413,12 @@ private static String buildRecordBody(PbMessage pbMessage) {

private static String buildClassBody(PbGenerateOperation pbGenerateOperation, PbMessage pbMessage) {
var builder = new StringBuilder();
// protocol id
builder.append(StringUtils.format("@Protocol(id = {})", pbMessage.getProtocolId())).append(LS);
// note
var documentComment = buildClassNote(pbMessage);
builder.append(documentComment);
// class body
builder.append(StringUtils.format("public class {} {", pbMessage.getName())).append(LS);

var pbFields = pbMessage.getFields()
Expand All @@ -417,7 +435,7 @@ private static String buildClassBody(PbGenerateOperation pbGenerateOperation, Pb
var type = getJavaType(pbField);
var name = pbField.getName();

var fieldComment = buildFieldComment(pbField);
var fieldComment = buildFieldNote(pbField);
fieldBuilder.append(fieldComment);
if (isCompatiblePbField(pbField)) {
var tag = pbField.getTag() - COMPATIBLE_FIELD_TAG;
Expand Down Expand Up @@ -492,6 +510,10 @@ private static String buildOneProtocolMessageImports(PbGenerateOperation pbGener
var imports = new HashSet<String>();
imports.add(Protocol.class.getName());

if (CollectionUtils.isNotEmpty(proto.getComments())) {
imports.add(Note.class.getName());
}

for (var pbMessage : proto.getPbMessages()) {
var pbFields = pbMessage.getFields();
if (CollectionUtils.isEmpty(pbFields)) {
Expand Down

0 comments on commit 9b325f0

Please sign in to comment.