【问题标题】:proto file, generated code in python and goproto 文件,在 python 中生成代码并运行
【发布时间】:2021-12-29 14:25:45
【问题描述】:

我正在尝试使用相同的 proto 文件生成 python 和 go 代码。

我的 proto 文件如下所示:

message City {
  string name = 1;
  int32 street_no = 2;
}

当我使用以下命令在 python 中生成代码时:

python3 -m grpc_tools.protoc  -I proto/ --python_out=openconfig/ --grpc_python_out=openconfig/ proto/city.proto

然后变量名称保留在snake_case中,但是当在go代码中生成proto文件时,使用以下:

${PROTOC} --gogofaster_out=proto --proto_path proto proto/city.proto

然后生成的变量在 CamelCase 中。 因此,street_no 更改为 StreetNo

有没有什么方法可以在 CamelCase 中转换 python 代码而不是 snake_case? 如何使用相同的 proto 文件以不同的语言工作?

我尝试了protoc 以及以下:

${PROTOC} --python_out=proto --proto_path proto/city.proto

不过,它还是会在 snake_case 中生成 python 代码。

谢谢,

  • M-

【问题讨论】:

    标签: python go protocol-buffers proto grpc-python


    【解决方案1】:

    Protobufs 以给定语言的标准格式生成(又名 CamelCase 用于 Go,snake_case 用于 Python。如果它以不同的方式生成,您将在 Python 中使用 CamelCase 的 linter 标志。是否有任何理由需要变量在不同的语言中使用相同的名称?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-02-13
      • 2021-06-03
      • 2021-09-05
      • 2022-10-05
      • 1970-01-01
      • 2012-12-03
      • 2017-08-18
      相关资源
      最近更新 更多