Custom protoc plugin that generates proto field getters for go stubs. This is primarily useful for the https://pkg.go.dev/go.alis.build/validator package.
Important
This plugin is designed to be used alongside the protoc-gen-go and protoc-gen-go-grpc plugins. It is not designed to be used standalone.
This plugin generates getters for each field in a proto message. It adds a {Message}_FieldGetters
struct which contains the different getters.
A new instance of {Message}_FieldGetters
can be created by calling the New{Message}FieldGetters()
function.
Note
Note: {Message} is the name of the proto message.
You can the use it as so
// Create a new instance of the field getters
fieldGetters := New{Message}FieldGetters()
fieldGetters.StringGetter(&pb.Message{}, "field_name")
go install github.com/alis-exchange/protoc-gen-fieldgetters/cmd/protoc-gen-go-fieldgetters@latest
protoc --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative --go-fieldgetters_out=. --go-fieldgetters_opt=paths=source_relative,include_msg_methods=true path/to/your.proto
StringGetter
- Returns the string value of the field.func(msg protoreflect.ProtoMessage, path string) (string, error)
StringListGetter
- Returns the list of strings of the field.func(msg protoreflect.ProtoMessage, path string) ([]string, error)
BoolGetter
- Returns the bool value of the field.func(msg protoreflect.ProtoMessage, path string) (bool, error)
BoolListGetter
- Returns the list of bools of the field.func(msg protoreflect.ProtoMessage, path string) ([]bool, error)
IntGetter
- Returns the int value of the field.func(msg protoreflect.ProtoMessage, path string) (int64, error)
IntListGetter
- Returns the list of ints of the field.func(msg protoreflect.ProtoMessage, path string) ([]int64, error)
FloatGetter
- Returns the float value of the field.func(msg protoreflect.ProtoMessage, path string) (float64, error)
FloatListGetter
- Returns the list of floats of the field.func(msg protoreflect.ProtoMessage, path string) ([]float64, error)
EnumGetter
- Returns the enum value of the field.func(msg protoreflect.ProtoMessage, path string) (protoreflect.EnumNumber, error)
EnumListGetter
- Returns the list of enums of the field.func(msg protoreflect.ProtoMessage, path string) ([]protoreflect.EnumNumber, error)
SubMessageGetter
- Returns the submessage of the field.func(msg protoreflect.ProtoMessage, path string) (protoreflect.ProtoMessage, error)
SubMessageListGetter
- Returns the list of submessages of the field.func(msg protoreflect.ProtoMessage, path string) ([]protoreflect.ProtoMessage, error)
-
--fieldgetters_out
- Output directory for the generated files. -
--fieldgetters_opt
- Comma-separated list of options to pass to the fieldgetters plugin.paths
- {string} -include_msg_methods
- {bool} - Include the methods on the message itself. Default: false
func (m *Message) StringGetter(path string) (string, error) { ... }
Which can be used as so
myMessage := &pb.Message{} value, err := myMessage.StringGetter("field_name")