This is a proof of concept plugin for protoc that generates Go code.
It introduces the notion of access modifiers for message fields and methods.
For message fields it allows defining them as private and generates a AsPublic()
method that empties the private fields into a public struct.
It is provided as a part of my blog post on gRPC API gateways.
This is not the final version of the plugin, but it is a good starting point for getting the idea how to use it.
In addition to the plugin you will find a gRPC client middleware strips private fields off incoming client responses.
go install github.com/kostyay/protoc-gen-go-access-modifiers@latest
Add to buf.gen.yaml
:
plugins:
- name: protoc-gen-go-access-modifiers
out: .
opt: paths=source_relative
To mark a field as private, add the (access.v1.fo).private = true
option to the field:
import "access/v1/access.proto";
message PrivateMessage {
string public_field = 1;
string private_field = 2 [(access.v1.fo).private = true];
}
To mark a method as private, add the (access.v1.mo).private = true
option to the method:
import "access/v1/access.proto";
service PrivateService {
rpc PublicMethod(PublicRequest) returns (PublicResponse) {}
rpc PrivateMethod(PrivateRequest) returns (PrivateResponse) {
option (access.v1.mo).private = true;
}
}