该功能特性从v2.4版本开始提供。

基本介绍

该命令用于编译proto文件,生成对应的protobuf go文件以及对应的控制器文件。

命令使用

$ gf gen pb -h
USAGE
    gf gen pb [OPTION]

OPTION
    -p, --path   protobuf file folder path
    -a, --api    output folder path storing generated go files of api
    -c, --ctrl   output folder path storing generated go files of controller
    -h, --help   more information about this command

EXAMPLE
    gf gen pb
    gf gen pb -p . -a . -p .

如果使用框架推荐的项目工程脚手架,并且系统安装了make工具,也可以使用make pb快捷指令。

参数说明:

名称必须默认值含义
pathmanifest/protobuf指向proto协议定义文件
apiapi指向生成的接口文件存放目录
ctrlinternal/controller指向生成的控制器文件存放目录

注意事项

  • 在生成控制器文件时,会自动识别是否已经存在对应的接口实现方法,如果已经存在则不再重复生成对应的接口方法,防止覆盖。
  • 如果在proto目录执行该命令,并且指定的path目录不存在时,那么将会自动编译本地proto文件,且编译后的文件生成到当前目录,并自动关闭控制器文件的生成功能。



Content Menu

  • No labels

16 Comments

  1. 尝试了一下这个工具,发现一些小问题:

    1. 需要将protobuf文件放在xxx/v1下controller层生成才可以正确生效(因为我这里需要共享协议,将protobuf作为

      git submodule,而且还因为一些原因需要支持http协议)

          2. path 参数不在mainfest的规定目录下,生成的文件会出现目录错乱,比如:

    gf gen pb -p proto/xxxservice/a.proto -a api/proto


          3.因为生成的时候会有改写文件的逻辑,而如果携带类似

    gRPC-Gateway这样的结构则会出现报错:

    https://grpc-ecosystem.github.io/grpc-gateway/docs/tutorials/adding_annotations/#using-protoc

    比如,如下protobuf

    syntax = "proto3";
    
    package helloworld;
    
    import "google/api/annotations.proto";
    
    // Here is the overall greeting service definition where we define all our endpoints
    service Greeter {
      // Sends a greeting
      rpc SayHello (HelloRequest) returns (HelloReply) {
        option (google.api.http) = {
          post: "/v1/example/echo"
          body: "*"
        };
      }
    }
    
    // The request message containing the user's name
    message HelloRequest {
      string name = 1;
    }
    
    // The response message containing the greetings
    message HelloReply {
      string message = 1;
    }
    1. 有解决嘛这个问题

      1. 把缺失的文件复制到manifest/protobuf下,然后执行make pb, 然后把api下面生成多余的文件删除即可, 比如api/google,   留下业务代码就行。

  2. 我同时使用了 gf gen pb的三个参数:-a -c -p ,我发现 只有 -p生效了,-a 与 -c 根本就没生效,依然是采用的api及internal/controller。而不是我自定义的值。下面是我的用法,看看有没有问题?


    gf gen pb  -a "app/platform-service/api" -c "app/platform-service/internal/controller" -p "protocol/platform"

    一直在提示我api不存在,我在执行目录 下新建了api目录后,就换成一直提示我internal/controller不存在了。很明显api及controller一直是用的默认值api及internal/controller


    我Debug了下gf,发现问题出在 CGenPbInput的定义上,Api与Ctrl用的OutputApi及OutputCtrl,导致进行mergeDefaultStructValue时匹配不到OutputApi及OutputCtrl。我自己先把CGenPbInput的字段名改成了Api及Ctrl。暂时能用了。不过我这个修改应该只是一个应急的方式,还是需要调整mergeDefaultStructValue,采用tag:name进行匹配才合适。

    1. 确实。改api参数压根没用

      1. 我之前自己调整了参数后可以用了。现在用的最新版的工具,好像也可以。应该是修复了。

        1. 请问用2.5.1版本么

          1. 我用的是2.5.0,看了下。好像是我自己编译的。可能是之前改过那里。

            1. 好吧 我更到2.5.1 还是这个问题

  3. 执行:gf gen pb     
    返回:command "protoc" not found in your environment, please install protoc first: https://grpc.io/docs/languages/go/quickstart/

    请问这个需要怎么解决?? gogf cil tool 2.5.1,goframe 2.51. windows10环境

    1. 没有配置protoc,你要下载protoc,然后按上面回复你的链接,去下载和配置

      go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.28
      go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.2
  4. gf gen pb  -c external/controller,运行还是报output controller folder "internal/controller" does not exist,-c 没有作用

  5. gf gen pbentity 命令,在mainfest/protobuf下生成proto文件

    gf gen pb 命令,在api/pbentity下生成了文件,但是internal/controller下没有生成文件,输出日志只有api的生成文件

    gogf最新版本2.5.1

    1. 需要在proto文件 定义方法

  6. gf gen pb

    项目路径不能有中文
    切记切记