在导入的 .proto 文件中扩展 protobuf.FieldOptions [英] Extending protobuf.FieldOptions in imported .proto file

查看:51
本文介绍了在导入的 .proto 文件中扩展 protobuf.FieldOptions的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 google 协议缓冲区中定义我的自定义字段选项.如果我创建这样的文件,一切正常:

I'm trying to define my custom field option in google protocol buffers. If I create such a file, everything works ok:

import "google/protobuf/descriptor.proto";

package tutorial;

extend google.protobuf.FieldOptions {
  optional int32 myopt = 70000;
}

message Persona {
  required string name = 1 [(myopt)=5];
}

但是,如果我尝试将myopt"定义移动到另一个文件,编译会失败:

However, if I try to move "myopt" definition to another file, compilation fails:

myext.proto:

package myext;

import "google/protobuf/descriptor.proto";

extend google.protobuf.FieldOptions {
  optional int32 myopt = 70000;
}

addressbook.proto:

import "google/protobuf/descriptor.proto";
import "myext.proto";

package tutorial;


message Persona {
  required string name = 1 [(myopt)=5];
}

编译:

$ protoc --cpp_out=. -I/usr/include -I. addressbook.proto
addressbook.proto:8:29: Option "(myopt)" unknown.

除了使用它的文件之外,还有什么方法可以在其他文件中定义自定义字段选项?如果我想在多个 .proto 文件中使用我的选项,将公共部分移动到一个公共文件中很重要.

Is there any way to define custom field options in other file than the one that use it? It is important to move common part to a common file if I want to use my option in several .proto files.

推荐答案

因为您已经使用新的 proto 文件创建了一个新包,所以您需要引用该包的命名空间.

Because you've made a new package with your new proto file, you'll need to reference the package's namespace.

正如您在评论中指出的,只需使用(myext.myopt)"而不是(myopt)",它看起来像这样:

As you noted in your comment, just use "(myext.myopt)" instead of "(myopt)", so it looks like this:

未修改您显示的内容

package myext;

import "google/protobuf/descriptor.proto";

extend google.protobuf.FieldOptions {
  optional int32 myopt = 70000;
}

addressbook.proto:

将(myopt)"替换为(myext.myopt)"

addressbook.proto:

Replace "(myopt)" with "(myext.myopt)"

import "google/protobuf/descriptor.proto";
import "myext.proto";

package tutorial;


message Persona {
  required string name = 1 [(myext.myopt)=5];
}

这篇关于在导入的 .proto 文件中扩展 protobuf.FieldOptions的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆