protoc-gen-go-access-modifiers

module
v0.0.0-...-7307553 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 1, 2023 License: MIT

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

    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;
        }
    }

Directories

Path Synopsis
pkg
mw

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL