mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-10-15 08:17:24 +00:00
ref[protobuf]: refactor protobuf generate
This commit is contained in:
1 parent
165bb0f98d
commit
08ddf9b3ce
3 files changed
+21
-36
No files matched your search
@@ -59,6 +59,7 @@ public abstract class GeneratePbUtils {
|
|||||||
}
|
}
|
||||||
ProtoParser parser = new ProtoParser(protoString);
|
ProtoParser parser = new ProtoParser(protoString);
|
||||||
Proto proto = parser.parse();
|
Proto proto = parser.parse();
|
||||||
|
proto.setName(FileUtils.fileSimpleName(protoFile.getName()));
|
||||||
protos.add(proto);
|
protos.add(proto);
|
||||||
}
|
}
|
||||||
return protos;
|
return protos;
|
||||||
@@ -66,41 +67,26 @@ public abstract class GeneratePbUtils {
|
|||||||
|
|
||||||
|
|
||||||
public static void generate(PbGenerateOperation buildOption, List<Proto> protos) {
|
public static void generate(PbGenerateOperation buildOption, List<Proto> protos) {
|
||||||
Map<String, Proto> allProtos = new HashMap<>();
|
var allProtos = new HashMap<String, Proto>();
|
||||||
for (Proto proto : protos) {
|
for (var proto : protos) {
|
||||||
allProtos.put(proto.getName(), proto);
|
allProtos.put(proto.getName(), proto);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var messageOutputPath = buildOption.getOutputPath() + File.separator;
|
||||||
|
if (StringUtils.isNotEmpty(buildOption.getJavaPackage())) {
|
||||||
|
messageOutputPath = messageOutputPath + buildOption.getJavaPackage().replaceAll(StringUtils.PERIOD_REGEX, "/");
|
||||||
|
}
|
||||||
|
|
||||||
for (var proto : protos) {
|
for (var proto : protos) {
|
||||||
List<PbOption> options = proto.getOptions();
|
var pbMessages = proto.getPbMessages();
|
||||||
Map<String, String> protoOptions = new HashMap<>();
|
if (CollectionUtils.isEmpty(pbMessages)) {
|
||||||
if (options != null) {
|
|
||||||
options.forEach(o -> protoOptions.put(o.getName(), o.getValue()));
|
|
||||||
}
|
|
||||||
if (CollectionUtils.isEmpty(proto.getPbMessages())) {
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
for (var pbMessage : pbMessages) {
|
||||||
generateMessage(buildOption, proto, allProtos);
|
var code = buildMessage(proto, pbMessage, 1, null);
|
||||||
}
|
var filePath = StringUtils.format("{}/{}/{}.java", messageOutputPath, proto.getName(), pbMessage.getName());
|
||||||
}
|
FileUtils.writeStringToFile(new File(filePath), code, false);
|
||||||
|
|
||||||
|
|
||||||
private static void generateMessage(PbGenerateOperation buildOption, Proto proto, Map<String, Proto> protos) {
|
|
||||||
String msgPath = buildOption.getOutputPath() + File.separator;
|
|
||||||
|
|
||||||
Map<String, String> msgComments = new HashMap<>();
|
|
||||||
|
|
||||||
List<PbMessage> msgs = proto.getPbMessages();
|
|
||||||
for (var msg : msgs) {
|
|
||||||
StringBuilder mc = new StringBuilder();
|
|
||||||
if (CollectionUtils.isNotEmpty(msg.getComments())) {
|
|
||||||
msg.getComments().forEach(c -> mc.append(c));
|
|
||||||
}
|
}
|
||||||
msgComments.put(msg.getName(), mc.toString());
|
|
||||||
var code = buildMessage(proto, msg, 1, null, protos);
|
|
||||||
var filePath = msgPath + File.separator + msg.getName() + ".java";
|
|
||||||
FileUtils.writeStringToFile(new File(filePath), code, false);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -183,7 +169,7 @@ public abstract class GeneratePbUtils {
|
|||||||
return StringUtils.EMPTY;
|
return StringUtils.EMPTY;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String buildMessage(Proto proto, PbMessage 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) {
|
||||||
var tmp = new ArrayList<PbField>();
|
var tmp = new ArrayList<PbField>();
|
||||||
var imports = new HashSet<String>();
|
var imports = new HashSet<String>();
|
||||||
var builder = new StringBuilder();
|
var builder = new StringBuilder();
|
||||||
|
|||||||
+5
-7
@@ -17,7 +17,7 @@ public class PbGenerateOperation {
|
|||||||
|
|
||||||
private String protoPath;
|
private String protoPath;
|
||||||
private String outputPath;
|
private String outputPath;
|
||||||
private boolean isNested;
|
private String javaPackage;
|
||||||
|
|
||||||
public String getProtoPath() {
|
public String getProtoPath() {
|
||||||
return protoPath;
|
return protoPath;
|
||||||
@@ -35,13 +35,11 @@ public class PbGenerateOperation {
|
|||||||
this.outputPath = outputPath;
|
this.outputPath = outputPath;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isIsNested() {
|
public String getJavaPackage() {
|
||||||
return isNested;
|
return javaPackage;
|
||||||
}
|
}
|
||||||
|
|
||||||
public PbGenerateOperation setIsNested(boolean isNested) {
|
public void setJavaPackage(String javaPackage) {
|
||||||
this.isNested = isNested;
|
this.javaPackage = javaPackage;
|
||||||
return this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -28,6 +28,7 @@ public class AllTypeBuilderTest {
|
|||||||
buildOption.setProtoPath("D:\\github\\zfoo\\protocol\\src\\test\\resources\\proto");
|
buildOption.setProtoPath("D:\\github\\zfoo\\protocol\\src\\test\\resources\\proto");
|
||||||
// buildOption.setProtoPath("D:\\github\\zoo\\app\\zgame\\zgame-common\\src\\main\\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.setOutputPath("D:\\github\\zfoo\\protocol\\src\\test\\tmpsrc/");
|
||||||
|
buildOption.setJavaPackage("com.zfoo.protocol.common");
|
||||||
|
|
||||||
GeneratePbUtils.create(buildOption);
|
GeneratePbUtils.create(buildOption);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user