diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/protobuf/GeneratePbUtils.java b/protocol/src/main/java/com/zfoo/protocol/serializer/protobuf/GeneratePbUtils.java new file mode 100644 index 00000000..a860e3d8 --- /dev/null +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/protobuf/GeneratePbUtils.java @@ -0,0 +1,70 @@ +/* + * Copyright 2021 The edap Project + * + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and limitations under the License. + */ + +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.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; + +public class GeneratePbUtils { + + public static void create(PbBuildOption buildOption) { + var protoPathFile = new File(buildOption.getProtoPath()); + if (!protoPathFile.exists()) { + throw new RuntimeException(StringUtils.format("proto path:[{}] not exist", buildOption.getProtoPath())); + } + + var protoFiles = FileUtils.getAllReadableFiles(protoPathFile) + .stream() + .filter(it -> it.getName().toLowerCase().endsWith(".proto")) + .toList(); + + if (CollectionUtils.isEmpty(protoFiles)) { + 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(); + } + + public static List parseProtoFile(List protoFiles) { + var protos = new ArrayList(); + for (var protoFile : protoFiles) { + var strs = FileUtils.readFileToStringList(protoFile) + .stream() + .filter(StringUtils::isNotBlank) + .toArray(); + var protoString = StringUtils.joinWith(FileUtils.LS, strs); + if (StringUtils.isBlank(protoString)) { + continue; + } + ProtoParser parser = new ProtoParser(protoString); + Proto proto = parser.parse(); + proto.setFile(protoFile); + protos.add(proto); + } + return protos; + } + +} diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/protobuf/PbBuildOption.java b/protocol/src/main/java/com/zfoo/protocol/serializer/protobuf/PbBuildOption.java new file mode 100644 index 00000000..c96e9657 --- /dev/null +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/protobuf/PbBuildOption.java @@ -0,0 +1,47 @@ +/* + * Copyright 2021 The edap Project + * + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and limitations under the License. + */ + +package com.zfoo.protocol.serializer.protobuf; + +public class PbBuildOption { + + private String protoPath; + private String outputPath; + private boolean isNested; + + public String getProtoPath() { + return protoPath; + } + + public void setProtoPath(String protoPath) { + this.protoPath = protoPath; + } + + public String getOutputPath() { + return outputPath; + } + + public void setOutputPath(String outputPath) { + this.outputPath = outputPath; + } + + public boolean isIsNested() { + return isNested; + } + + public PbBuildOption setIsNested(boolean isNested) { + this.isNested = isNested; + return this; + } + +} diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/protobuf/builder/JavaBuilder.java b/protocol/src/main/java/com/zfoo/protocol/serializer/protobuf/builder/JavaBuilder.java new file mode 100644 index 00000000..886a6885 --- /dev/null +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/protobuf/builder/JavaBuilder.java @@ -0,0 +1,407 @@ +/* + * Copyright 2021 The edap Project + * + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and limitations under the License. + */ + +package com.zfoo.protocol.serializer.protobuf.builder; + +import com.zfoo.protocol.collection.CollectionUtils; +import com.zfoo.protocol.serializer.protobuf.PbBuildOption; +import com.zfoo.protocol.serializer.protobuf.internal.CodeBuilder; +import com.zfoo.protocol.serializer.protobuf.wire.*; +import com.zfoo.protocol.serializer.protobuf.wire.Field.Type; +import com.zfoo.protocol.serializer.protobuf.wire.ProtoEnum.EnumEntry; +import com.zfoo.protocol.serializer.protobuf.wire.WireFormat.JavaType; +import com.zfoo.protocol.serializer.protobuf.wire.parser.Proto; +import com.zfoo.protocol.util.StringUtils; + +import java.util.*; + +public class JavaBuilder { + private static final List BASE_TYPES; + + static { + BASE_TYPES = new ArrayList<>(); + String[] types = ("double,float,int32,int64,uint32,uint64,sint32,sint64," + + "fixed32,fixed64,sfixed32,sfixed64,bool,string,bytes").split(","); + BASE_TYPES.addAll(Arrays.asList(types)); + } + + + private String getOptionJavaType(List