zoukankan      html  css  js  c++  java
  • 六.Protobuf3引入其他.proto文件

    Protobuf3 使用其他消息类型

      您可以使用其他消息类型作为字段类型。例如,假设您希望在每个SearchResponse消息中包含Result消息,为此,您可以在.proto中定义结果消息类型,然后在SearchResponse中指定Result类型字段:

    message SearchResponse {
      repeated Result results = 1;
    }
    
    message Result {
      string url = 1;
      string title = 2;
      repeated string snippets = 3;
    }

    导入定义

      在上面的例子中,Result 消息类型和SearchResponse定义在同一个文件中——如果你想用作字段类型的消息类型已经在另一个.proto 文件中定义了呢?

      您可以通过导入其他.proto文件来使用文件中的定义的类型。 您可以在文件的顶部添加一条import语句:

    import "myproject/other_protos.proto";

      默认情况下,您只能使用直接导入的 .proto文件定义。然而,有时你可能需要移动一个 .proto文件到一个新的位置,但不想为此更新了所有调用它的.proto文件,现在你可以在文件原始位置放置一个仿造的 .proto文件,使用import public将所有导入转发到新位置。任何包含import public语句的proto的人都可以临时依赖import public依赖。例如:

    // new.proto
    // All definitions are moved here
    // old.proto
    // This is the proto that all clients are importing.
    import public "new.proto";
    import "other.proto";
    // client.proto
    import "old.proto";
    // You use definitions from old.proto and new.proto, but not other.proto

      协议编译器使用-I/--proto_path标志在协议编译器命令行指定的一组目录中搜索导入的文件。如果没有给出标志,它会在调用编译器的目录中查找。通常,您应该将--proto_path标志设置为项目的根目录,并对所有导入使用完全限定的名称。

    使用proto2消息类型

      可以导入proto2消息类型并在proto3消息中使用它们,反之亦然。但是,proto2枚举不能直接在proto3语法中使用(如果导入的proto 2消息使用它们也没关系)。

  • 相关阅读:
    6. Flask请求和响应
    5. Flask模板
    FW:Software Testing
    What is the difference between modified duration, effective duration and duration?
    How to push master to QA branch in GIT
    FTPS Firewall
    Query performance optimization of Vertica
    (Forward)5 Public Speaking Tips That'll Prepare You for Any Interview
    (转)The remote certificate is invalid according to the validation procedure
    Change
  • 原文地址:https://www.cnblogs.com/kexianting/p/11507573.html
Copyright © 2011-2022 走看看