[英]Java : Proto parser library
I want to parse a proto file.我想解析一个原型文件。 Wanted to check is there any java library available which can parse proto files.
想检查是否有任何可用的 java 库可以解析 proto 文件。 Based on my requirement I cannot use descriptor parseFrom method or protoc command .
根据我的要求,我不能使用描述符 parseFrom 方法或protoc 命令。 Please suggest thanks in advance.
请提前提出感谢。
$ protoc --include_imports --descriptor_set_out temp *.proto // I don't want to do this manual step
or
DescriptorProtos.FileDescriptorProto descriptorProto = DescriptorProtos.FileDescriptorProto.parseFrom(proto.getBytes());
Appreciate suggestion thanks欣赏建议 谢谢
io.protostuff:protostuff-parser
libraryio.protostuff:protostuff-parser
library Let's consider the 3.1.38
version of the io.protostuff:protostuff-parser
library as the current version.让我们将
3.1.38
io.protostuff:protostuff-parser
库的 3.1.38 版本视为当前版本。
protostuff-parser
Maven module directory in GitHub repository: protostuff-compiler/protostuff-parser at v3.1.38 · protostuff/protostuff-compiler . protostuff-parser
Maven 模块目录在 GitHub 存储库中: protostuff-compiler/protostuff-parser at v3.1.38 · protostuff/protostuff-compiler 。Please, consider the below example program as a draft to get started with the library.请考虑以下示例程序作为开始使用该库的草稿。
Let's assume the /some/directory/data/test.proto
file exist with the following content:假设
/some/directory/data/test.proto
文件存在,内容如下:
message SearchRequest {
string query = 1;
int32 page_number = 2;
int32 result_per_page = 3;
enum ContentType {
WEB = 1;
IMAGES = 2;
VIDEO = 3;
}
ContentType content_type = 4;
}
pom.xml
: Dependencies pom.xml
:依赖关系<project>
<dependencies>
<dependency>
<groupId>io.protostuff</groupId>
<artifactId>protostuff-parser</artifactId>
<version>3.1.38</version>
</dependency>
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>5.1.0</version>
</dependency>
</dependencies>
</project>
import com.google.inject.Guice;
import com.google.inject.Injector;
import io.protostuff.compiler.ParserModule;
import io.protostuff.compiler.model.Message;
import io.protostuff.compiler.model.Proto;
import io.protostuff.compiler.parser.Importer;
import io.protostuff.compiler.parser.LocalFileReader;
import io.protostuff.compiler.parser.ProtoContext;
import java.nio.file.Path;
import java.util.List;
public final class Program {
public static void main(final String[] args) {
final Injector injector = Guice.createInjector(new ParserModule());
final Importer importer = injector.getInstance(Importer.class);
final ProtoContext protoContext = importer.importFile(
new LocalFileReader(Path.of("/some/directory/data/data")),
"test.proto"
);
final Proto proto = protoContext.getProto();
final List<Message> messages = proto.getMessages();
System.out.println(String.format("Messages: %s", messages));
final Message searchRequestMessage = proto.getMessage("SearchRequest");
System.out.println(String.format("SearchRequest message: %s", searchRequestMessage));
final List<Enum> searchRequestMessageEnums = searchRequestMessage.getEnums();
System.out.println(String.format("SearchRequest message enums: %s", searchRequestMessageEnums));
}
}
The program output:程序 output:
Messages: [Message{name=SearchRequest, fullyQualifiedName=..SearchRequest, fields=[Field{name=query, typeName=string, tag=1, options=DynamicMessage{fields={}}}, Field{name=page_number, typeName=int32, tag=2, options=DynamicMessage{fields={}}}, Field{name=result_per_page, typeName=int32, tag=3, options=DynamicMessage{fields={}}}, Field{name=content_type, typeName=ContentType, tag=4, options=DynamicMessage{fields={}}}], enums=[Enum{name=ContentType, fullyQualifiedName=..SearchRequest.ContentType, constants=[EnumConstant{name=WEB, value=1, options=DynamicMessage{fields={}}}, EnumConstant{name=IMAGES, value=2, options=DynamicMessage{fields={}}}, EnumConstant{name=VIDEO, value=3, options=DynamicMessage{fields={}}}], options=DynamicMessage{fields={}}}], options=DynamicMessage{fields={}}}]
SearchRequest message: Message{name=SearchRequest, fullyQualifiedName=..SearchRequest, fields=[Field{name=query, typeName=string, tag=1, options=DynamicMessage{fields={}}}, Field{name=page_number, typeName=int32, tag=2, options=DynamicMessage{fields={}}}, Field{name=result_per_page, typeName=int32, tag=3, options=DynamicMessage{fields={}}}, Field{name=content_type, typeName=ContentType, tag=4, options=DynamicMessage{fields={}}}], enums=[Enum{name=ContentType, fullyQualifiedName=..SearchRequest.ContentType, constants=[EnumConstant{name=WEB, value=1, options=DynamicMessage{fields={}}}, EnumConstant{name=IMAGES, value=2, options=DynamicMessage{fields={}}}, EnumConstant{name=VIDEO, value=3, options=DynamicMessage{fields={}}}], options=DynamicMessage{fields={}}}], options=DynamicMessage{fields={}}}
SearchRequest message enums: [Enum{name=ContentType, fullyQualifiedName=..SearchRequest.ContentType, constants=[EnumConstant{name=WEB, value=1, options=DynamicMessage{fields={}}}, EnumConstant{name=IMAGES, value=2, options=DynamicMessage{fields={}}}, EnumConstant{name=VIDEO, value=3, options=DynamicMessage{fields={}}}], options=DynamicMessage{fields={}}}]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.