-
Notifications
You must be signed in to change notification settings - Fork 410
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat[protobuf]: generate pojo file from proto of protobuf
- Loading branch information
1 parent
f6aa27c
commit c1b859c
Showing
28 changed files
with
4,348 additions
and
1 deletion.
There are no files selected for viewing
70 changes: 70 additions & 0 deletions
70
protocol/src/main/java/com/zfoo/protocol/serializer/protobuf/GeneratePbUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Proto> parseProtoFile(List<File> protoFiles) { | ||
var protos = new ArrayList<Proto>(); | ||
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; | ||
} | ||
|
||
} |
47 changes: 47 additions & 0 deletions
47
protocol/src/main/java/com/zfoo/protocol/serializer/protobuf/PbBuildOption.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
|
||
} |
Oops, something went wrong.