Java : 原型解析器库
Java : Proto parser library
我想解析一个原型文件。想检查是否有可用的 java 库可以解析原型文件。根据我的要求,我不能使用 descriptor parseFrom 方法 或protoc 命令。请提前建议谢谢。
$ 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());
感谢建议谢谢
可能的解决方案:io.protostuff:protostuff-parser
库
让我们将 io.protostuff:protostuff-parser
库的 3.1.38
版本视为当前版本。
- Maven 依赖项:Maven Repository: io.protostuff » protostuff-parser » 3.1.38.
protostuff-parser
GitHub 存储库中的 Maven 模块目录:protostuff-compiler/protostuff-parser at v3.1.38 · protostuff/protostuff-compiler.
- GitHub 存储库:protostuff/protostuff-compiler at v3.1.38.
示例程序
请将下面的示例程序视为草稿,以开始使用该库。
输入文件
假设 /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
:依赖项
<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")),
"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));
}
}
程序输出:
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={}}}]
我想解析一个原型文件。想检查是否有可用的 java 库可以解析原型文件。根据我的要求,我不能使用 descriptor parseFrom 方法 或protoc 命令。请提前建议谢谢。
$ 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());
感谢建议谢谢
可能的解决方案:io.protostuff:protostuff-parser
库
让我们将 io.protostuff:protostuff-parser
库的 3.1.38
版本视为当前版本。
- Maven 依赖项:Maven Repository: io.protostuff » protostuff-parser » 3.1.38.
protostuff-parser
GitHub 存储库中的 Maven 模块目录:protostuff-compiler/protostuff-parser at v3.1.38 · protostuff/protostuff-compiler.- GitHub 存储库:protostuff/protostuff-compiler at v3.1.38.
示例程序
请将下面的示例程序视为草稿,以开始使用该库。
输入文件
假设 /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
:依赖项
<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")),
"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));
}
}
程序输出:
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={}}}]