zeromicro / go-zero

A cloud-native Go microservices framework with cli tool for productivity.
https://go-zero.dev
MIT License
29.12k stars 3.93k forks source link

[Feature] Support import external proto in goctl. #4268

Open chaozwn opened 2 months ago

chaozwn commented 2 months ago

Is your feature request related to a problem? Please describe. In some scenarios, we may need to perform automatic validation of rpc parameters. for example, protovalidate-go library is introduced to automatically validate req.

  1. This is the proto that I generated. It contains an external proto file and a new validation syntax. image Next, I can verify req by directly generating the code automatically. image

Describe the solution you'd like

  1. In fact, I have tested that it is now perfectly possible to use buf-cli instead of protoc to generate go files for grpc and proto. The following is my solution case.

buf.gen.yaml

version: v2

plugins:
  - remote: buf.build/grpc/go
    out: ../
  - remote: buf.build/protocolbuffers/go
    out: ../
  - remote: buf.build/bufbuild/validate-go
    out: ../

buf.yaml

version: v2

name: buf.build/tutorials/lint
breaking:
  use:
    - FILE
lint:
  use:
    - DEFAULT
  except:
    - SERVICE_SUFFIX
    - FIELD_LOWER_SNAKE_CASE
    - PACKAGE_LOWER_SNAKE_CASE
    - PACKAGE_SAME_SWIFT_PREFIX
    - PACKAGE_VERSION_SUFFIX
    - ENUM_NO_ALLOW_ALIAS
    - BASIC
    - RPC_REQUEST_STANDARD_NAME
    - RPC_RESPONSE_STANDARD_NAME

deps:
  - buf.build/bufbuild/protovalidate

update dep

buf dep update

generate grpc & proto file

buf generate --path ./app/todo/cmd/rpc/pb/todo.proto --output ./app/todo/cmd/rpc/pb

image

In summary, we can get exactly the same grpc file and pb.go file with proto introduced.

But now there is a very fatal problem, I have import other proto files in proto early, resulting in the system's built-in generation function directly reported an error.

image

Describe alternatives you've considered

So I wonder if this syntax can be supported. goctl rpc buf xx.proto --xxx. If you are OK with my idea, I can try to submit a pr along these lines.

Additional context Add any other context or screenshots about the feature request here.

kesonan commented 2 months ago

maybe you can try with -I $external_path, goctl rpc protoc $proto ... -I $external_path --zrpc_out $dir --style=goZero --home $home -m

chaozwn commented 2 months ago

maybe you can try with -I $external_path, goctl rpc protoc $proto ... -I $external_path --zrpc_out $dir --style=goZero --home $home -m

first this directive has no way of taking effect, and the external proto code is not compiled by default