mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-10-13 04:15:22 +00:00
ref[protobuf]: refactor protobuf generate
This commit is contained in:
1 parent
6624e5b053
commit
0ba1c6e419
10 files changed
+62
-71
No files matched your search
@@ -15,10 +15,6 @@ package com.zfoo.protocol.serializer.protobuf;
|
||||
|
||||
import com.zfoo.protocol.collection.CollectionUtils;
|
||||
import com.zfoo.protocol.serializer.protobuf.parser.TypeProtobuf;
|
||||
import com.zfoo.protocol.serializer.protobuf.wire.MapField;
|
||||
import com.zfoo.protocol.serializer.protobuf.wire.Option;
|
||||
import com.zfoo.protocol.serializer.protobuf.wire.PbField;
|
||||
import com.zfoo.protocol.serializer.protobuf.wire.ProtoMessage;
|
||||
import com.zfoo.protocol.serializer.protobuf.parser.Proto;
|
||||
import com.zfoo.protocol.serializer.protobuf.parser.ProtoParser;
|
||||
import com.zfoo.protocol.util.FileUtils;
|
||||
@@ -32,7 +28,7 @@ import static com.zfoo.protocol.util.StringUtils.TAB;
|
||||
|
||||
public class GeneratePbUtils {
|
||||
|
||||
public static void create(PbBuildOption buildOption) {
|
||||
public static void create(PbGenerateOperation buildOption) {
|
||||
var protoPathFile = new File(buildOption.getProtoPath());
|
||||
if (!protoPathFile.exists()) {
|
||||
throw new RuntimeException(StringUtils.format("proto path:[{}] not exist", buildOption.getProtoPath()));
|
||||
@@ -71,14 +67,14 @@ public class GeneratePbUtils {
|
||||
}
|
||||
|
||||
|
||||
public static void generate(PbBuildOption buildOption, List<Proto> protos) {
|
||||
public static void generate(PbGenerateOperation buildOption, List<Proto> protos) {
|
||||
Map<String, Proto> allProtos = new HashMap<>();
|
||||
for (Proto proto : protos) {
|
||||
allProtos.put(proto.getName(), proto);
|
||||
}
|
||||
|
||||
for (var proto : protos) {
|
||||
List<Option> options = proto.getOptions();
|
||||
List<PbOption> options = proto.getOptions();
|
||||
Map<String, String> protoOptions = new HashMap<>();
|
||||
if (options != null) {
|
||||
options.forEach(o -> protoOptions.put(o.getName(), o.getValue()));
|
||||
@@ -92,12 +88,12 @@ public class GeneratePbUtils {
|
||||
}
|
||||
|
||||
|
||||
private static void generateDtoMessage(PbBuildOption buildOption, Proto proto, Map<String, Proto> protos) {
|
||||
private static void generateDtoMessage(PbGenerateOperation buildOption, Proto proto, Map<String, Proto> protos) {
|
||||
String msgPath = buildOption.getOutputPath() + File.separator;
|
||||
|
||||
Map<String, String> msgComments = new HashMap<>();
|
||||
|
||||
List<ProtoMessage> msgs = proto.getMessages();
|
||||
List<PbMessage> msgs = proto.getMessages();
|
||||
for (var msg : msgs) {
|
||||
StringBuilder mc = new StringBuilder();
|
||||
if (CollectionUtils.isNotEmpty(msg.getComments())) {
|
||||
@@ -113,8 +109,8 @@ public class GeneratePbUtils {
|
||||
// -------------------------------------------------------------------------------------------------------------
|
||||
public static String getJavaType(PbField field) {
|
||||
String type = field.getType();
|
||||
if (field instanceof MapField) {
|
||||
var mapField = (MapField) field;
|
||||
if (field instanceof PbMapField) {
|
||||
var mapField = (PbMapField) field;
|
||||
type = StringUtils.format("Map<{}, {}>", getBoxJavaType(mapField.getKey().value()), getBoxJavaType(mapField.getValue()));
|
||||
return type;
|
||||
}
|
||||
@@ -153,7 +149,7 @@ public class GeneratePbUtils {
|
||||
}
|
||||
|
||||
|
||||
private static void buildMsgImps(ProtoMessage msg, List<PbField> tmp, List<String> imps) {
|
||||
private static void buildMsgImps(PbMessage msg, List<PbField> tmp, List<String> imps) {
|
||||
var fields = msg.getFields();
|
||||
if (CollectionUtils.isNotEmpty(fields)) {
|
||||
for (var field : fields) {
|
||||
@@ -163,7 +159,7 @@ public class GeneratePbUtils {
|
||||
}
|
||||
|
||||
for (int i = 0; i < tmp.size(); i++) {
|
||||
if (tmp.get(i) instanceof MapField) {
|
||||
if (tmp.get(i) instanceof PbMapField) {
|
||||
addImport(imps, Map.class.getName());
|
||||
} else if (tmp.get(i).getCardinality() == PbField.Cardinality.REPEATED) {
|
||||
addImport(imps, List.class.getName());
|
||||
@@ -171,7 +167,7 @@ public class GeneratePbUtils {
|
||||
}
|
||||
}
|
||||
|
||||
private static void buildDocComment(StringBuilder builder, ProtoMessage msg) {
|
||||
private static void buildDocComment(StringBuilder builder, PbMessage msg) {
|
||||
if (CollectionUtils.isEmpty(msg.getComments())) {
|
||||
return;
|
||||
}
|
||||
@@ -191,7 +187,7 @@ public class GeneratePbUtils {
|
||||
if (CollectionUtils.isEmpty(proto.getOptions())) {
|
||||
return StringUtils.EMPTY;
|
||||
}
|
||||
for (Option option : proto.getOptions()) {
|
||||
for (PbOption option : proto.getOptions()) {
|
||||
if ("java_package".equalsIgnoreCase(option.getName())) {
|
||||
return option.getValue();
|
||||
}
|
||||
@@ -199,7 +195,7 @@ public class GeneratePbUtils {
|
||||
return StringUtils.EMPTY;
|
||||
}
|
||||
|
||||
public static String buildMessage(Proto proto, ProtoMessage msg, int indent, Map<String, String> defineMsgs, Map<String, Proto> protos) {
|
||||
public static String buildMessage(Proto proto, PbMessage msg, int indent, Map<String, String> defineMsgs, Map<String, Proto> protos) {
|
||||
var level = Math.max(indent, 1);
|
||||
var tmp = new ArrayList<PbField>();
|
||||
var imps = new ArrayList<String>();
|
||||
|
||||
+2
-3
@@ -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.serializer.protobuf.wire;
|
||||
package com.zfoo.protocol.serializer.protobuf;
|
||||
|
||||
|
||||
import java.util.ArrayList;
|
||||
+2
-2
@@ -13,7 +13,7 @@
|
||||
|
||||
package com.zfoo.protocol.serializer.protobuf;
|
||||
|
||||
public class PbBuildOption {
|
||||
public class PbGenerateOperation {
|
||||
|
||||
private String protoPath;
|
||||
private String outputPath;
|
||||
@@ -39,7 +39,7 @@ public class PbBuildOption {
|
||||
return isNested;
|
||||
}
|
||||
|
||||
public PbBuildOption setIsNested(boolean isNested) {
|
||||
public PbGenerateOperation setIsNested(boolean isNested) {
|
||||
this.isNested = isNested;
|
||||
return this;
|
||||
}
|
||||
+3
-4
@@ -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,14 +10,14 @@
|
||||
* See the License for the specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
package com.zfoo.protocol.serializer.protobuf.wire;
|
||||
package com.zfoo.protocol.serializer.protobuf;
|
||||
|
||||
import com.zfoo.protocol.serializer.protobuf.parser.TypeProtobuf;
|
||||
|
||||
/**
|
||||
* protocol buffer中Map的Field的结构体定义
|
||||
*/
|
||||
public class MapField extends PbField {
|
||||
public class PbMapField extends PbField {
|
||||
|
||||
private TypeProtobuf key;
|
||||
private String value;
|
||||
+8
-9
@@ -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.serializer.protobuf.wire;
|
||||
package com.zfoo.protocol.serializer.protobuf;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@@ -19,7 +18,7 @@ import java.util.List;
|
||||
/**
|
||||
* protocol buffer消息体的结构定义
|
||||
*/
|
||||
public class ProtoMessage {
|
||||
public class PbMessage {
|
||||
/**
|
||||
* 消息的名称
|
||||
*/
|
||||
@@ -31,7 +30,7 @@ public class ProtoMessage {
|
||||
/**
|
||||
* 消息内嵌的Message列表
|
||||
*/
|
||||
private List<ProtoMessage> protoMessages = new ArrayList<>();
|
||||
private List<PbMessage> pbMessages = new ArrayList<>();
|
||||
/**
|
||||
* 消息的备注信息
|
||||
*/
|
||||
@@ -54,12 +53,12 @@ public class ProtoMessage {
|
||||
this.fields = fields;
|
||||
}
|
||||
|
||||
public List<ProtoMessage> getProtoMessages() {
|
||||
return protoMessages;
|
||||
public List<PbMessage> getProtoMessages() {
|
||||
return pbMessages;
|
||||
}
|
||||
|
||||
public void setProtoMessages(List<ProtoMessage> protoMessages) {
|
||||
this.protoMessages = protoMessages;
|
||||
public void setProtoMessages(List<PbMessage> pbMessages) {
|
||||
this.pbMessages = pbMessages;
|
||||
}
|
||||
|
||||
public List<String> getComments() {
|
||||
+3
-4
@@ -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,12 +10,12 @@
|
||||
* See the License for the specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
package com.zfoo.protocol.serializer.protobuf.wire;
|
||||
package com.zfoo.protocol.serializer.protobuf;
|
||||
|
||||
/**
|
||||
* protocol buffer协议的Option的数据类型定义
|
||||
*/
|
||||
public class Option {
|
||||
public class PbOption {
|
||||
|
||||
/**
|
||||
* 选项名称
|
||||
+2
-3
@@ -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.serializer.protobuf.wire;
|
||||
package com.zfoo.protocol.serializer.protobuf;
|
||||
|
||||
/**
|
||||
* protocol buffer的协议版本
|
||||
@@ -13,8 +13,9 @@
|
||||
|
||||
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.PbMessage;
|
||||
import com.zfoo.protocol.serializer.protobuf.PbOption;
|
||||
import com.zfoo.protocol.serializer.protobuf.Syntax;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
@@ -32,7 +33,7 @@ public class Proto {
|
||||
/**
|
||||
* Proto的可选信息
|
||||
*/
|
||||
private List<Option> options = new ArrayList<>();
|
||||
private List<PbOption> options = new ArrayList<>();
|
||||
/**
|
||||
* 文档版本没有指定默认为proto3
|
||||
*/
|
||||
@@ -44,7 +45,7 @@ public class Proto {
|
||||
/**
|
||||
* 定义的消息列表
|
||||
*/
|
||||
private List<ProtoMessage> protoMessages = new ArrayList<>();
|
||||
private List<PbMessage> pbMessages = new ArrayList<>();
|
||||
/**
|
||||
* 文档导入的文件的路径列表
|
||||
*/
|
||||
@@ -63,7 +64,7 @@ public class Proto {
|
||||
return this;
|
||||
}
|
||||
|
||||
public Proto addOption(Option option) {
|
||||
public Proto addOption(PbOption option) {
|
||||
getOptions().add(option);
|
||||
return this;
|
||||
}
|
||||
@@ -73,7 +74,7 @@ public class Proto {
|
||||
return this;
|
||||
}
|
||||
|
||||
public Proto addMsg(ProtoMessage msg) {
|
||||
public Proto addMsg(PbMessage msg) {
|
||||
getMessages().add(msg);
|
||||
return this;
|
||||
}
|
||||
@@ -103,7 +104,7 @@ public class Proto {
|
||||
*
|
||||
* @return the options
|
||||
*/
|
||||
public List<Option> getOptions() {
|
||||
public List<PbOption> getOptions() {
|
||||
return options;
|
||||
}
|
||||
|
||||
@@ -141,8 +142,8 @@ public class Proto {
|
||||
*
|
||||
* @return the messages
|
||||
*/
|
||||
public List<ProtoMessage> getMessages() {
|
||||
return protoMessages;
|
||||
public List<PbMessage> getMessages() {
|
||||
return pbMessages;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+18
-18
@@ -14,11 +14,11 @@
|
||||
package com.zfoo.protocol.serializer.protobuf.parser;
|
||||
|
||||
import com.zfoo.protocol.collection.CollectionUtils;
|
||||
import com.zfoo.protocol.serializer.protobuf.wire.MapField;
|
||||
import com.zfoo.protocol.serializer.protobuf.wire.Option;
|
||||
import com.zfoo.protocol.serializer.protobuf.wire.PbField;
|
||||
import com.zfoo.protocol.serializer.protobuf.wire.PbField.Cardinality;
|
||||
import com.zfoo.protocol.serializer.protobuf.wire.ProtoMessage;
|
||||
import com.zfoo.protocol.serializer.protobuf.PbMapField;
|
||||
import com.zfoo.protocol.serializer.protobuf.PbOption;
|
||||
import com.zfoo.protocol.serializer.protobuf.PbField;
|
||||
import com.zfoo.protocol.serializer.protobuf.PbField.Cardinality;
|
||||
import com.zfoo.protocol.serializer.protobuf.PbMessage;
|
||||
import com.zfoo.protocol.util.StringUtils;
|
||||
|
||||
import java.util.*;
|
||||
@@ -106,7 +106,7 @@ public class ProtoParser {
|
||||
readValueSeparator('=');
|
||||
ProtoValue pv = readValueUtilSemicolon();
|
||||
String optionValue = pv.getValue();
|
||||
Option option = new Option();
|
||||
PbOption option = new PbOption();
|
||||
option.setName(optionLabel);
|
||||
option.setValue(optionValue);
|
||||
proto.addOption(option);
|
||||
@@ -120,7 +120,7 @@ public class ProtoParser {
|
||||
comments.clear();
|
||||
break;
|
||||
case "message":
|
||||
ProtoMessage msg = parseMessage();
|
||||
PbMessage msg = parseMessage();
|
||||
proto.addMsg(msg);
|
||||
comments.clear();
|
||||
break;
|
||||
@@ -143,8 +143,8 @@ public class ProtoParser {
|
||||
return proto;
|
||||
}
|
||||
|
||||
private ProtoMessage parseMessage() throws RuntimeException {
|
||||
ProtoMessage msg = new ProtoMessage();
|
||||
private PbMessage parseMessage() throws RuntimeException {
|
||||
PbMessage msg = new PbMessage();
|
||||
trim();
|
||||
String name = readToken();
|
||||
if (name.length() == 0) {
|
||||
@@ -406,7 +406,7 @@ public class ProtoParser {
|
||||
PbField field;
|
||||
if (gType != null) {
|
||||
TypeProtobuf type = TypeProtobuf.valueOf(gType.getKeyType().toUpperCase(Locale.ENGLISH));
|
||||
var mapField = new MapField();
|
||||
var mapField = new PbMapField();
|
||||
mapField.setKey(type);
|
||||
mapField.setValue(gType.getValueType());
|
||||
field = mapField;
|
||||
@@ -571,7 +571,7 @@ public class ProtoParser {
|
||||
|
||||
class ProtoValue {
|
||||
private String value;
|
||||
private List<Option> options;
|
||||
private List<PbOption> options;
|
||||
|
||||
/**
|
||||
* @return the value
|
||||
@@ -591,14 +591,14 @@ public class ProtoParser {
|
||||
/**
|
||||
* @return the options
|
||||
*/
|
||||
public List<Option> getOptions() {
|
||||
public List<PbOption> getOptions() {
|
||||
return options;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param options the options to set
|
||||
*/
|
||||
public ProtoValue setOptions(List<Option> options) {
|
||||
public ProtoValue setOptions(List<PbOption> options) {
|
||||
this.options = options;
|
||||
return this;
|
||||
}
|
||||
@@ -627,7 +627,7 @@ public class ProtoParser {
|
||||
case ' ':
|
||||
trim();
|
||||
if (data[pos] == '[') {
|
||||
List<Option> options = readFieldOptions();
|
||||
List<PbOption> options = readFieldOptions();
|
||||
pv.setValue(v.toString()).setOptions(options);
|
||||
return pv;
|
||||
}
|
||||
@@ -638,7 +638,7 @@ public class ProtoParser {
|
||||
return pv;
|
||||
}
|
||||
case '[':
|
||||
List<Option> options = readFieldOptions();
|
||||
List<PbOption> options = readFieldOptions();
|
||||
pv.setValue(v.toString()).setOptions(options);
|
||||
return pv;
|
||||
case '\n':
|
||||
@@ -788,8 +788,8 @@ public class ProtoParser {
|
||||
throw new RuntimeException("row " + row + " value not end with \"\\" + quote + "\"");
|
||||
}
|
||||
|
||||
private List<Option> readFieldOptions() throws RuntimeException {
|
||||
List<Option> options = new ArrayList<>();
|
||||
private List<PbOption> readFieldOptions() throws RuntimeException {
|
||||
List<PbOption> options = new ArrayList<>();
|
||||
pos++;
|
||||
while (data[pos] != ']') {
|
||||
if (data[pos] == ';') {
|
||||
@@ -801,7 +801,7 @@ public class ProtoParser {
|
||||
|
||||
readValueSeparator('=');
|
||||
String value = readOptionValue();
|
||||
Option option = new Option();
|
||||
PbOption option = new PbOption();
|
||||
option.setName(name);
|
||||
option.setValue(value);
|
||||
options.add(option);
|
||||
|
||||
@@ -14,8 +14,7 @@
|
||||
package com.zfoo.protocol.protobuf;
|
||||
|
||||
import com.zfoo.protocol.serializer.protobuf.GeneratePbUtils;
|
||||
import com.zfoo.protocol.serializer.protobuf.PbBuildOption;
|
||||
import com.zfoo.protocol.util.ThreadUtils;
|
||||
import com.zfoo.protocol.serializer.protobuf.PbGenerateOperation;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
@@ -25,7 +24,7 @@ public class AllTypeBuilderTest {
|
||||
|
||||
@Test
|
||||
public void testAllTypeTest() {
|
||||
var buildOption = new PbBuildOption();
|
||||
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/");
|
||||
|
||||
Reference in new issue
Block a user