Open AVM-Martin opened 1 year ago
I understand your needs very well, but there are two problems:
If the final file structure is like this:
github.com/example/project
├── gen
│ ├── mock
│ │ └── example_grpc_mock.pb.go
│ ├── example.pb.go
│ └── example_grpc.pb.go
└── proto
└── example.proto
Then the example_grpc_mock.pb.go
file should import the parent package:
// example_grpc_mock.pb.go
import (
. "github.com/example/project/gen"
)
We tend to use . /;package_name
in go_package
to generate code to the relative path. However, with google.golang.org/protobuf/compiler/protogen
alone, we cannot get the real import path of the parent package in code generation.
Of course, you could declare the full package path in the go_package, but this would be too demanding and not a good implementation.
There is a small chance of causing a conflict, for example, if there is already a mock
subdirectory:
github.com/example/project
├── proto
│ ├── mock
│ │ └── example2.proto
│ ├── example1.proto
For the reasons above, generating code to a subdirectory doesn't seem to be a common behavior of Go proto plugins. I haven’t found a good solution to above problems yet, so if you have a good idea, feel free to discuss.
Hi, sorry for a late reply
go_package
(see this eliza.proto from bufbuild/connect-demo). I am using this convention because my protobuf contract will be imported from another repository and generated locally, so I need to define go_package
on buf.gen.yaml
(equivalent with --go_opt=M=$FILENAME=$IMPORT_PATH
arg)managed:
enabled: true
go_package_prefix:
default: github.com/bufbuild/connect-demo/internal/gen
gen/mock
directory, maybe under /mock
directory on the repository root. This is my specific use case (and that means I won’t have proto/mock
directory)I'm open to contribute on this plugin, but I need some time to learn about the codebase
Is it possible to separate the package? We only need mock service when running unit test and won't generate it
Background
We need put generated mock inside
gen/mock
directory. The directory tree will look similar to the followingand
gen/mock/example_grpc_mock.pb.go
file will import structs fromgithub.com/example/project/gen
For further directory sample, you may see the official gomock example here
Further Discussion
If it is possible, we can refer the main generated golang code based on provided flag (i.e.
proto_import_path
)