perf[protobuf]: generate pojo protocolId in @Protocol

This commit is contained in:
godotg
2023-12-03 20:21:02 +08:00
parent 3f963a3991
commit 1fc7caf492
7 changed files with 42 additions and 10 deletions
@@ -234,7 +234,12 @@ public abstract class GeneratePbUtils {
var documentComment = buildDocumentComment(pbMessage);
builder.append(documentComment);
// message
var startProtocolId = proto.getStartProtocolId();
builder.append(StringUtils.format("@Protocol(id = {})", startProtocolId)).append(LS);
builder.append(StringUtils.format("public class {} {", pbMessage.getName())).append(LS);
startProtocolId++;
proto.setStartProtocolId(startProtocolId);
var pbFields = pbMessage.getFields()
.stream()
@@ -54,6 +54,10 @@ public class Proto {
* proto文件的单行注释列表
*/
private List<String> comments = new ArrayList<>();
/**
* 协议的起始Id
*/
private short startProtocolId;
public String getName() {
return name;
@@ -110,4 +114,12 @@ public class Proto {
public void setComments(List<String> comments) {
this.comments = comments;
}
public short getStartProtocolId() {
return startProtocolId;
}
public void setStartProtocolId(short startProtocolId) {
this.startProtocolId = startProtocolId;
}
}
@@ -16,6 +16,7 @@ package com.zfoo.protocol.serializer.protobuf.parser;
import com.zfoo.protocol.collection.CollectionUtils;
import com.zfoo.protocol.serializer.protobuf.*;
import com.zfoo.protocol.serializer.protobuf.PbField.Cardinality;
import com.zfoo.protocol.util.NumberUtils;
import com.zfoo.protocol.util.StringUtils;
import java.util.*;
@@ -64,7 +65,19 @@ public class ProtoParser {
switch (token) {
case "//":
String line = readSingleLineComment();
comments.add(line);
// 解析协议开始的start_protocol_id
line = StringUtils.trim(line);
if (line.startsWith("start_protocol_id")) {
var splits = line.split("=");
for (var s : splits) {
s = s.trim();
if (NumberUtils.isInteger(s)) {
proto.setStartProtocolId(Short.parseShort(s));
}
}
} else {
comments.add(line);
}
nextLine();
break;
case "/*":
@@ -1,6 +1,5 @@
/*
* Copyright 2021 The edap Project
*
* Copyright (C) 2020 The zfoo Authors
* 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
*
@@ -11,7 +10,7 @@
* See the License for the specific language governing permissions and limitations under the License.
*/
package com.zfoo.protocol.protobuf;
package com.zfoo.protocol.generate;
import com.zfoo.protocol.serializer.protobuf.GeneratePbUtils;
import com.zfoo.protocol.serializer.protobuf.PbGenerateOperation;
@@ -20,15 +19,14 @@ import org.junit.Test;
@Ignore
public class AllTypeBuilderTest {
public class GenerateProtobufTesting {
@Test
public void testAllTypeTest() {
var buildOption = new PbGenerateOperation();
buildOption.setProtoPath("D:\\github\\zfoo\\protocol\\src\\test\\resources\\proto");
// buildOption.setProtoPath("D:\\github\\zoo\\app\\zgame\\zgame-common\\src\\main\\resources\\proto");
buildOption.setOutputPath("D:\\github\\zfoo\\protocol\\src\\test\\tmpsrc/");
buildOption.setJavaPackage("com.zfoo.protocol.common");
buildOption.setJavaPackage("com.zfoo.protocol.generate.test");
GeneratePbUtils.create(buildOption);
}
@@ -1,12 +1,12 @@
syntax = "proto3";
package test.message;
import "one_map.proto";
import "one_message.proto";
option java_package = "io.edap.protobuf.test.message.v3";
//option java_outer_classname = "MessageOuter";
option java_package = "start";
// start_protocol_id = 100
message AllType {
bool field1 = 1;
@@ -5,6 +5,8 @@ package test.message;
option java_package = "io.edap.protobuf.test.message.v3";
// start_protocol_id = 200
message OneMap {
map<string, Project> value = 1;
}
@@ -5,6 +5,8 @@ package test.message;
option java_package = "io.edap.protobuf.test.message.v3";
// start_protocol_id = 200
message OneMessage {
Proj value = 1;
}