Skip to content

Commit

Permalink
ref[protobuf]: refactor protobuf generate
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysunxiao committed Dec 3, 2023
1 parent 516b3f9 commit 165bb0f
Showing 1 changed file with 7 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import static com.zfoo.protocol.util.FileUtils.LS;
import static com.zfoo.protocol.util.StringUtils.TAB;

public class GeneratePbUtils {
public abstract class GeneratePbUtils {

public static void create(PbGenerateOperation buildOption) {
var protoPathFile = new File(buildOption.getProtoPath());
Expand Down Expand Up @@ -137,17 +137,7 @@ private static String getBoxJavaType(String type) {
return javaType.getBoxedType();
}

private static void addImport(List<String> imps, String imp) {
if (imps == null) {
return;
}
if (!imps.contains(imp)) {
imps.add(imp);
}
}


private static void buildMsgImps(PbMessage msg, List<PbField> tmp, List<String> imps) {
private static void buildMsgImps(PbMessage msg, List<PbField> tmp, Set<String> imps) {
var fields = msg.getFields();
if (CollectionUtils.isNotEmpty(fields)) {
for (var field : fields) {
Expand All @@ -158,9 +148,9 @@ private static void buildMsgImps(PbMessage msg, List<PbField> tmp, List<String>

for (int i = 0; i < tmp.size(); i++) {
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) {
addImport(imps, List.class.getName());
imps.add(List.class.getName());
}
}
}
Expand Down Expand Up @@ -194,18 +184,17 @@ private static String getJavaPackage(Proto proto) {
}

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>();
var imports = new HashSet<String>();
var builder = new StringBuilder();

buildMsgImps(msg, tmp, imps);
buildMsgImps(msg, tmp, imports);

List<PbField> fields = new ArrayList<>();
tmp.stream().sorted(Comparator.comparingInt(PbField::getTag))
.forEach(fields::add);

imps.stream().sorted(Comparator.naturalOrder())
imports.stream().sorted(Comparator.naturalOrder())
.forEach(it -> builder.append(StringUtils.format("import {};", it)).append(LS));

buildDocComment(builder, msg);
Expand Down

0 comments on commit 165bb0f

Please sign in to comment.