A protoc plugin that allows you to add custom Go struct tags to generated protobuf files.
- Add custom struct tags (e.g.,
graphql,xml,validate) to protobuf-generated Go structs - Modify existing tags (e.g., override default
jsontag) - Support for
oneoffields - Preserves original protobuf/json tags while adding new ones
go install github.com/pubgo/protoc-gen-retag@latestimport "proto/retag/retag.proto";syntax = "proto3";
package example;
import "proto/retag/retag.proto";
message User {
// Add a new graphql tag
string name = 1 [
(retag.tags) = {name: "graphql", value: "userName,optional"}
];
// Add multiple custom tags
string email = 2 [
(retag.tags) = {name: "graphql", value: "userEmail"},
(retag.tags) = {name: "validate", value: "required,email"}
];
// Override the default json tag
string user_id = 3 [
(retag.tags) = {name: "json", value: "id,omitempty"}
];
// Support for oneof fields
oneof contact {
option (retag.oneof_tags) = {name: "graphql", value: "contact,optional"};
string phone = 4;
string address = 5;
}
}protoc --go_out=. --retag_out=. your.protoOr with buf:
# buf.gen.yaml
version: v1
plugins:
- plugin: go
out: gen
opt: paths=source_relative
- plugin: retag
out: gen
opt: paths=source_relativeThe generated Go struct will include your custom tags:
type User struct {
Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty" graphql:"userName,optional"`
Email string `protobuf:"bytes,2,opt,name=email,proto3" json:"email,omitempty" graphql:"userEmail" validate:"required,email"`
UserId string `protobuf:"bytes,3,opt,name=user_id,json=userId,proto3" json:"id,omitempty"`
// ...
}| Extension | Scope | Description |
|---|---|---|
retag.tags |
Field | Add/modify tags on message fields |
retag.oneof_tags |
Oneof | Add tags on oneof fields |
This project is inspired by and based on protoc-gen-go-tag.
MIT License