Skip to content

liov/protopatch2

Repository files navigation

protopatch

Patch protoc plugin output with Go-specific features. The protoc-gen-go-patch command wraps calls to Go code generators like protoc-gen-go or protoc-gen-go-grpc and patches the Go syntax before being written to disk.

Features

Patches are defined via an Options extension on messages, fields, oneof fields, enums, and enum values.

  • go.message — message options, which modify the generated Go struct for a message.
  • go.field — message field options, which modify Go struct fields and getter methods.
  • go.oneofoneof field options, which modify struct fields, interface types, and wrapper types.
  • go.enumenum options, which modify Go enum types and values.
  • go.valueenum value options, which modify Go const values.

Custom Names

import "patch/go.proto";

message OldName {
	option (go.message) = {name: 'NewName'};
	int id = 1 [(go.field) = {name: 'ID'}];
}

enum Errors {
	option (go.enum) = {name: 'ProtocolErrors'};
	INVALID = 1 [(go.value) = {name: 'ErrInvalid'}];
	NOT_FOUND = 2 [(go.value) = {name: 'ErrNotFound'}];
	TOO_FUN = 3 [(go.value) = {name: 'ErrTooFun'}];
}

Struct Tags

message ToDo {
	int32 id = 1 [(go.field) = {name: 'ID', tags: '`xml:"id,attr"`'}];
	string description = 2 [(go.field) = {tags: '`xml:"desc"`'}];
}

TODO: More Documentation

Install

go install github.com/liov/protopatch2/cmd/protoc-gen-go-patch

Usage

After installing protoc-gen-go-patch, use it by specifying it with a --go-patch_out=... argument to protoc:

protoc \
	-I . \
	-I `go list -m -f {{.Dir}} github.com/liov/protopatch2` \
	-I `go list -m -f {{.Dir}} google.golang.org/protobuf` \
	--go-patch_out=plugin=go,paths=source_relative:. \
	--go-patch_out=plugin=go-grpc,paths=source_relative:. \
	*.proto

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published