An open API service indexing awesome lists of open source software.

https://github.com/kostyay/protoc-gen-go-access-modifiers

This is a POC for a go protoc plugin that adds access modifiers to proto fields and methods
https://github.com/kostyay/protoc-gen-go-access-modifiers

grpc grpc-golang protoc protocol-buffers

Last synced: about 2 months ago
JSON representation

This is a POC for a go protoc plugin that adds access modifiers to proto fields and methods

Awesome Lists containing this project

README

        

protoc-gen-go-access-modifiers
======================

__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.

# Usage
```bash
go install github.com/kostyay/protoc-gen-go-access-modifiers@latest
```

Add to `buf.gen.yaml`:
```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:
```proto
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:
```proto
import "access/v1/access.proto";

service PrivateService {
rpc PublicMethod(PublicRequest) returns (PublicResponse) {}
rpc PrivateMethod(PrivateRequest) returns (PrivateResponse) {
option (access.v1.mo).private = true;
}
}
```