Java : io.protostuff.compiler.parser.ParserException: 无法加载原型

Java : io.protostuff.compiler.parser.ParserException: Can not load proto

我正在使用 guice 解析器来解析原型文件。它无法解析导入。关于如何解决导入 public 以及同一项目中的导入的任何建议专家?

原型文件

syntax = "proto3";
     package grpc;
     
     import "envoyproxy/protoc-gen-validate/validate/validate.proto";
     import "google/api/annotations.proto";
     import "google/protobuf/timestamp.proto";
     import "google/rpc/status.proto";
     import "google/protobuf/struct.proto";
     
     option go_package = "bitbucket.mycompany.com";
     option java_multiple_files = true;
     
     message SendRequest {
         string data = 1 ;
         bytes document = 2 ;
     }
     
     message SendResponse {
         string Status = 1;
         string Message = 2;
         repeated string DocumentList = 3;
     }
  

Guice 解析器 pom.xml 中的依赖项

<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>

我的主要应用代码

    Path currentDir = Paths.get("src\main\resources");
    System.out.println(currentDir.toAbsolutePath());

    final Injector injector = Guice.createInjector(new ParserModule());
    final Importer importer = injector.getInstance(Importer.class); 
    final ProtoContext protoContext = importer.importFile(new LocalFileReader(currentDir), "test.proto");
        
    final Proto proto = protoContext.getProto();
    final List<Message> messages = proto.getMessages();
    System.out.println(String.format("Messages: %s", messages));   

当我 运行 应用程序

时,我遇到异常
Exception in thread "main" io.protostuff.compiler.parser.ParserException: Can not load proto: envoyproxy/protoc-gen-validate/validate/validate.proto not found
    at io.protostuff.compiler.parser.FileDescriptorLoaderImpl.load(FileDescriptorLoaderImpl.java:35)
    at io.protostuff.compiler.parser.ImporterImpl.importFile(ImporterImpl.java:34)

简介

让我们将 io.protostuff:protostuff-parser 库的 3.1.38 version 视为当前版本。

解决方案

需要使用 FileReaderFactoryFileReader 接口来获得 «Include directories» 支持。

解决方案的灵感来自:protostuff-compiler/ProtostuffCompiler.java at v3.1.38 · protostuff/protostuff-compiler

示例程序

假设 /some/directory/imports 目录用于导入。

/some/directory/imports$ tree -d -L 2
.
├── envoyproxy
│   └── protoc-gen-validate
└── google
    ├── api
    ├── protobuf
    └── rpc

程序源码:

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.FileReader;
import io.protostuff.compiler.parser.FileReaderFactory;
import io.protostuff.compiler.parser.Importer;
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 FileReaderFactory fileReaderFactory = injector.getInstance(FileReaderFactory.class);
        final List<Path> includePaths = List.of(
            Path.of("/some/directory/imports")
        );
        final FileReader fileReader = fileReaderFactory.create(includePaths);

        final Importer importer = injector.getInstance(Importer.class);
        final ProtoContext protoContext = importer.importFile(
            fileReader,
            "/some/other/directory/test.proto"
        );

        final Proto proto = protoContext.getProto();

        final List<Message> messages = proto.getMessages();
        System.out.println(String.format("Messages: %s", messages));
    }
}

程序输出:

Messages: [Message{name=SendRequest, fullyQualifiedName=.grpc.SendRequest, fields=[Field{name=data, typeName=string, tag=1, options=DynamicMessage{fields={}}}, Field{name=document, typeName=bytes, tag=2, options=DynamicMessage{fields={}}}], options=DynamicMessage{fields={}}}, Message{name=SendResponse, fullyQualifiedName=.grpc.SendResponse, fields=[Field{name=Status, typeName=string, tag=1, options=DynamicMessage{fields={}}}, Field{name=Message, typeName=string, tag=2, options=DynamicMessage{fields={}}}, Field{name=DocumentList, modifier=repeated, typeName=string, tag=3, options=DynamicMessage{fields={}}}], options=DynamicMessage{fields={}}}]