Skip to content

Commit

Permalink
perf[protobuf]: generate pojo in protobuf
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysunxiao committed Dec 3, 2023
1 parent a48c453 commit bc5bf56
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 114 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,16 @@
package com.zfoo.protocol.serializer.protobuf;

import com.zfoo.protocol.collection.CollectionUtils;
import com.zfoo.protocol.serializer.protobuf.codegen.IfaceGenerator;
import com.zfoo.protocol.serializer.protobuf.builder.JavaBuilder;
import com.zfoo.protocol.serializer.protobuf.wire.Option;
import com.zfoo.protocol.serializer.protobuf.wire.ProtoMessage;
import com.zfoo.protocol.serializer.protobuf.wire.parser.Proto;
import com.zfoo.protocol.serializer.protobuf.wire.parser.ProtoParser;
import com.zfoo.protocol.util.FileUtils;
import com.zfoo.protocol.util.StringUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.*;

public class GeneratePbUtils {

Expand All @@ -41,11 +42,8 @@ public static void create(PbBuildOption buildOption) {
throw new RuntimeException(StringUtils.format("There are no proto files to build in proto path:[{}]", buildOption.getProtoPath()));
}

var srcPath = new File(buildOption.getOutputPath());

var protos = parseProtoFile(protoFiles);
IfaceGenerator ifaceGenerator = new IfaceGenerator(srcPath, protos);
ifaceGenerator.generate();
generate(buildOption, protos);
}

public static List<Proto> parseProtoFile(List<File> protoFiles) {
Expand All @@ -67,4 +65,51 @@ public static List<Proto> parseProtoFile(List<File> protoFiles) {
return protos;
}


public static void generate(PbBuildOption 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();
Map<String, String> protoOptions = new HashMap<>();
if (options != null) {
options.forEach(o -> protoOptions.put(o.getName(), o.getValue()));
}

generateDtoMessage(buildOption, proto, allProtos);
}
}


private static void generateDtoMessage(PbBuildOption buildOption, Proto proto, Map<String, Proto> protos) {
JavaBuilder builder = new JavaBuilder();

Map<String, String> msgComments = new HashMap<>();

List<ProtoMessage> msgs = proto.getMessages();
if (CollectionUtils.isNotEmpty(msgs)) {
String msgPath = buildOption.getOutputPath() + File.separator;
msgs.stream()
.sorted(Comparator.comparing(ProtoMessage::getName))
.forEach(it -> {
StringBuilder mc = new StringBuilder();
if (it.getComment() != null) {
mc.append(it.getComment());
} else {
if (it.getComment() != null && it.getComment().getLines() != null) {
it.getComment().getLines().forEach(c -> mc.append(c));
}
}
msgComments.put(it.getName(), mc.toString());
var code = builder.buildMessage(proto, it, 1, null, protos);
var filePath = msgPath + File.separator + it.getName() + ".java";
FileUtils.writeStringToFile(new File(filePath), code, false);
});

}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ private String getOptionJavaType(List<Option> options) {
return jType;
}

public String getJavaType(Field field, List<String> imps, PbBuildOption buildOps) {
public String getJavaType(Field field, List<String> imps) {
String type = field.getTypeString();
if (field instanceof MapField) {
MapField mf = (MapField) field;
Expand Down Expand Up @@ -120,11 +120,10 @@ public String getAnnotationType(Field field) {
}
}

private void buildMsgImps(ProtoMessage msg, List<Field> tmp, List<String> imps,
PbBuildOption buildOps) {
private void buildMsgImps(ProtoMessage msg, List<Field> tmp, List<String> imps) {
if (msg.getFields() != null) {
msg.getFields().forEach(e -> {
getJavaType(e, imps, buildOps);
getJavaType(e, imps);
tmp.add(e);
});
}
Expand Down Expand Up @@ -160,21 +159,21 @@ private String getJavaPackage(Proto proto) {
return StringUtils.EMPTY;
}

public String buildMessage(Proto proto, ProtoMessage msg, int indent, Map<String, String> defineMsgs, PbBuildOption buildOps, Map<String, Proto> protos) {
public String buildMessage(Proto proto, ProtoMessage msg, int indent, Map<String, String> defineMsgs, Map<String, Proto> protos) {
int level = Math.max(indent, 1);
final CodeBuilder cb = new CodeBuilder();
List<Field> tmp = new ArrayList<>();
List<String> imps = new ArrayList<>();
buildMsgImps(msg, tmp, imps, buildOps);
buildMsgImps(msg, tmp, imps);

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

if (!buildOps.isIsNested()) {
imps.stream().sorted(Comparator.naturalOrder())
.forEach(e -> cb.t(level - 1).e("import $cls$;").arg(e).ln());
}
// not nested
imps.stream().sorted(Comparator.naturalOrder())
.forEach(e -> cb.t(level - 1).e("import $cls$;").arg(e).ln());

cb.ln();
buildDocComment(cb, msg.getComment(), level - 1);
cb.t(level - 1).e("public class $name$ {").arg(msg.getName()).ln(2);
Expand All @@ -198,10 +197,10 @@ public String buildMessage(Proto proto, ProtoMessage msg, int indent, Map<String
typeName = Proto.class.getPackage().getName() + ".wire.Field." + typeName;
}
buildDocComment(cb, f.getComment(), level);
String type = getJavaType(f, imps, buildOps);
String type = getJavaType(f, imps);
String name = f.getName();
if (f.getCardinality() == Field.Cardinality.REPEATED) {
String boxedTypeName = getBoxedTypeName(f, buildOps);
String boxedTypeName = getBoxedTypeName(f);
type = "List<" + boxedTypeName + ">";
}

Expand Down Expand Up @@ -249,8 +248,8 @@ public String buildMessage(Proto proto, ProtoMessage msg, int indent, Map<String
return cb.toString();
}

private String getBoxedTypeName(Field f, PbBuildOption buildOps) {
String type = getJavaType(f, null, buildOps);
private String getBoxedTypeName(Field f) {
String type = getJavaType(f, null);
if (BASE_TYPES.contains(f.getTypeString())) {
JavaType javaType = null;
javaType = Type.valueOf(f.getTypeString().toUpperCase(Locale.ENGLISH)).javaType();
Expand All @@ -269,10 +268,8 @@ private void nestMessageMessage(Proto proto, CodeBuilder cb,
return;
}
cb.ln();
PbBuildOption nestedOps = new PbBuildOption();
nestedOps.setIsNested(true);
msgs.stream().sorted(Comparator.comparing(ProtoMessage::getName))
.forEach(e -> cb.c(buildMessage(proto, e, level + 1, defineMsgs, nestedOps, protos)));
.forEach(e -> cb.c(buildMessage(proto, e, level + 1, defineMsgs, protos)));
}


Expand Down

This file was deleted.

0 comments on commit bc5bf56

Please sign in to comment.