mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-10-13 02:14:52 +00:00
perf[protobuf]: use prefix number of proto name as start protocol ID
This commit is contained in:
1 parent
fe733c0386
commit
687945f7c3
8 files changed
+17
-28
No files matched your search
@@ -172,8 +172,6 @@ your protocol number a little more compactly, so that your protocol number will
|
||||
```
|
||||
syntax = "proto3";
|
||||
package test.message;
|
||||
// start_protocol_id = 500
|
||||
// The above starting ID indicates which protocol number of the entire file starts with
|
||||
|
||||
message SimpleObject {
|
||||
int64 aa = 1;
|
||||
|
||||
@@ -149,8 +149,6 @@ cpu: i9900k
|
||||
```
|
||||
syntax = "proto3";
|
||||
package test.message;
|
||||
// start_protocol_id = 500
|
||||
// 上面这个起始id表示整个文件的协议号从哪个开始
|
||||
|
||||
message SimpleObject {
|
||||
int64 aa = 1;
|
||||
|
||||
@@ -22,6 +22,7 @@ import com.zfoo.protocol.model.Pair;
|
||||
import com.zfoo.protocol.serializer.protobuf.parser.Proto;
|
||||
import com.zfoo.protocol.serializer.protobuf.parser.ProtoParser;
|
||||
import com.zfoo.protocol.util.FileUtils;
|
||||
import com.zfoo.protocol.util.NumberUtils;
|
||||
import com.zfoo.protocol.util.StringUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@@ -55,7 +56,7 @@ public abstract class GeneratePbUtils {
|
||||
}
|
||||
// java package path
|
||||
if (StringUtils.isNotEmpty(pbGenerateOperation.getJavaPackage())) {
|
||||
protocolOutputPath = protocolOutputPath + File.separator+ pbGenerateOperation.getJavaPackage().replaceAll(StringUtils.PERIOD_REGEX, "/");
|
||||
protocolOutputPath = protocolOutputPath + File.separator + pbGenerateOperation.getJavaPackage().replaceAll(StringUtils.PERIOD_REGEX, "/");
|
||||
}
|
||||
|
||||
var protoPathFile = new File(pbGenerateOperation.getProtoPath());
|
||||
@@ -91,11 +92,20 @@ public abstract class GeneratePbUtils {
|
||||
// parse protobuf proto
|
||||
ProtoParser parser = new ProtoParser(protoString);
|
||||
Proto proto = parser.parse();
|
||||
proto.setName(FileUtils.fileSimpleName(protoFile.getName()));
|
||||
var protoSimpleName = FileUtils.fileSimpleName(protoFile.getName());
|
||||
|
||||
var protoName = StringUtils.substringAfterFirst(protoSimpleName, "_");
|
||||
var startProtocolIdStr = StringUtils.substringBeforeFirst(protoSimpleName, "_");
|
||||
if (!NumberUtils.isNumeric(startProtocolIdStr)) {
|
||||
throw new RuntimeException(StringUtils.format("proto:[{}] name format is number_xxx.proto", protoName));
|
||||
}
|
||||
|
||||
var startProtocolId = Short.parseShort(startProtocolIdStr);
|
||||
proto.setName(protoName);
|
||||
proto.setStartProtocolId(startProtocolId);
|
||||
protos.add(proto);
|
||||
|
||||
// auto generate protocolId
|
||||
var startProtocolId = proto.getStartProtocolId();
|
||||
for (var pbMessage : proto.getPbMessages()) {
|
||||
pbMessage.setProtocolId(startProtocolId++);
|
||||
}
|
||||
@@ -110,7 +120,7 @@ public abstract class GeneratePbUtils {
|
||||
var protocolName = pbMessage.getName();
|
||||
if (protocolIdMap.containsKey(protocolId)) {
|
||||
var pair = protocolIdMap.get(protocolId);
|
||||
throw new RuntimeException(StringUtils.format("duplicate protocolId:[{}] in [{}]:[{}] and [{}]:[{}], consider proto start_protocol_id is too close"
|
||||
throw new RuntimeException(StringUtils.format("duplicate protocolId:[{}] in [{}]:[{}] and [{}]:[{}], consider proto start protocol id is too close"
|
||||
, protocolId, pair.getKey().getName(), pair.getValue().getName(), proto.getName(), protocolName));
|
||||
}
|
||||
if (protocolNameMap.containsKey(protocolName)) {
|
||||
|
||||
+1
-12
@@ -65,19 +65,8 @@ public class ProtoParser {
|
||||
switch (token) {
|
||||
case "//":
|
||||
String line = readSingleLineComment();
|
||||
// 解析协议开始的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);
|
||||
}
|
||||
comments.add(line);
|
||||
nextLine();
|
||||
break;
|
||||
case "/*":
|
||||
|
||||
File renamed without changes.
+2
-3
@@ -1,8 +1,7 @@
|
||||
syntax = "proto3";
|
||||
package test.message;
|
||||
import "one_map.proto";
|
||||
import "one_message.proto";
|
||||
// start_protocol_id = 300
|
||||
import "400_one_map.proto";
|
||||
import "500_one_message.proto";
|
||||
|
||||
|
||||
// 如果字段的tag超过1000,则视这个字段为需要兼容的协议字段
|
||||
-2
@@ -1,7 +1,5 @@
|
||||
syntax = "proto3";
|
||||
package test.message;
|
||||
// start_protocol_id = 400
|
||||
|
||||
|
||||
message OneMap {
|
||||
map<string, Project> value = 1;
|
||||
-3
@@ -1,8 +1,5 @@
|
||||
syntax = "proto3";
|
||||
package test.message;
|
||||
// start_protocol_id = 500
|
||||
// 上面这个起始id表示整个文件的协议号从哪个开始
|
||||
|
||||
|
||||
message OneMessage {
|
||||
Proj value = 1;
|
||||
Reference in new issue
Block a user