mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-10-16 02:15:51 +00:00
ref[protobuf]: refactor protobuf generate
This commit is contained in:
1 parent
516b3f93ab
commit
165bb0f98d
1 file changed
+7
-18
@@ -25,7 +25,7 @@ import java.util.*;
|
|||||||
import static com.zfoo.protocol.util.FileUtils.LS;
|
import static com.zfoo.protocol.util.FileUtils.LS;
|
||||||
import static com.zfoo.protocol.util.StringUtils.TAB;
|
import static com.zfoo.protocol.util.StringUtils.TAB;
|
||||||
|
|
||||||
public class GeneratePbUtils {
|
public abstract class GeneratePbUtils {
|
||||||
|
|
||||||
public static void create(PbGenerateOperation buildOption) {
|
public static void create(PbGenerateOperation buildOption) {
|
||||||
var protoPathFile = new File(buildOption.getProtoPath());
|
var protoPathFile = new File(buildOption.getProtoPath());
|
||||||
@@ -137,17 +137,7 @@ public class GeneratePbUtils {
|
|||||||
return javaType.getBoxedType();
|
return javaType.getBoxedType();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void addImport(List<String> imps, String imp) {
|
private static void buildMsgImps(PbMessage msg, List<PbField> tmp, Set<String> imps) {
|
||||||
if (imps == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (!imps.contains(imp)) {
|
|
||||||
imps.add(imp);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
private static void buildMsgImps(PbMessage msg, List<PbField> tmp, List<String> imps) {
|
|
||||||
var fields = msg.getFields();
|
var fields = msg.getFields();
|
||||||
if (CollectionUtils.isNotEmpty(fields)) {
|
if (CollectionUtils.isNotEmpty(fields)) {
|
||||||
for (var field : fields) {
|
for (var field : fields) {
|
||||||
@@ -158,9 +148,9 @@ public class GeneratePbUtils {
|
|||||||
|
|
||||||
for (int i = 0; i < tmp.size(); i++) {
|
for (int i = 0; i < tmp.size(); i++) {
|
||||||
if (tmp.get(i) instanceof PbMapField) {
|
if (tmp.get(i) instanceof PbMapField) {
|
||||||
addImport(imps, Map.class.getName());
|
imps.add(Map.class.getName());
|
||||||
} else if (tmp.get(i).getCardinality() == PbField.Cardinality.REPEATED) {
|
} else if (tmp.get(i).getCardinality() == PbField.Cardinality.REPEATED) {
|
||||||
addImport(imps, List.class.getName());
|
imps.add(List.class.getName());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -194,18 +184,17 @@ public class GeneratePbUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
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, Map<String, Proto> protos) {
|
||||||
var level = Math.max(indent, 1);
|
|
||||||
var tmp = new ArrayList<PbField>();
|
var tmp = new ArrayList<PbField>();
|
||||||
var imps = new ArrayList<String>();
|
var imports = new HashSet<String>();
|
||||||
var builder = new StringBuilder();
|
var builder = new StringBuilder();
|
||||||
|
|
||||||
buildMsgImps(msg, tmp, imps);
|
buildMsgImps(msg, tmp, imports);
|
||||||
|
|
||||||
List<PbField> fields = new ArrayList<>();
|
List<PbField> fields = new ArrayList<>();
|
||||||
tmp.stream().sorted(Comparator.comparingInt(PbField::getTag))
|
tmp.stream().sorted(Comparator.comparingInt(PbField::getTag))
|
||||||
.forEach(fields::add);
|
.forEach(fields::add);
|
||||||
|
|
||||||
imps.stream().sorted(Comparator.naturalOrder())
|
imports.stream().sorted(Comparator.naturalOrder())
|
||||||
.forEach(it -> builder.append(StringUtils.format("import {};", it)).append(LS));
|
.forEach(it -> builder.append(StringUtils.format("import {};", it)).append(LS));
|
||||||
|
|
||||||
buildDocComment(builder, msg);
|
buildDocComment(builder, msg);
|
||||||
|
|||||||
Reference in new issue
Block a user