Skip to content
Inject custom tags to protobuf golang struct
Go Shell
Branch: master
Clone or download
favadi Merge pull request #28 from msolimans/master
XXX skip fields sample with unit test
Latest commit c2c1884 Oct 8, 2018
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
pb XXX fields sample with unit tests Oct 7, 2018
vendor add vendor Aug 2, 2018
.gitignore ignore binary file Apr 6, 2016
.travis.yml enabled goveralls May 21, 2018
LICENSE Initial commit Apr 6, 2016
README.md
file.go skipped if field.Names are empty Oct 3, 2018
go.mod add vendor Aug 2, 2018
go.sum add vendor Aug 2, 2018
main.go
main_test.go
parse.go preserve spaces in tag values May 25, 2018

README.md

protoc-go-inject-tag

Build Status Go Report Card Coverage Status

Why?

Golang protobuf doesn't support custom tags to generated structs. This script injects custom tags to generated protobuf files, useful for things like validation struct tags.

Install

  • protobuf version 3

    For OS X:

    brew install protobuf
    
  • go support for protobuf: go get -u github.com/golang/protobuf/{proto,protoc-gen-go}

  • go get github.com/favadi/protoc-go-inject-tag or download the binaries from releases page.

Usage

Add a comment with syntax // @inject_tag: custom_tag:"custom_value" before fields to add custom tag to in .proto files.

Example:

// file: test.proto
syntax = "proto3";

package pb;

message IP {
  // @inject_tag: valid:"ip"
  string Address = 1;
}

Generate with protoc command as normal.

protoc --go_out=. test.proto

Run protoc-go-inject-tag with generated file test.pb.go.

protoc-go-inject-tag -input=./test.pb.go

The custom tags will be injected to test.pb.go.

type IP struct {
	// @inject_tag: valid:"ip"
	Address string `protobuf:"bytes,1,opt,name=Address,json=address" json:"Address,omitempty" valid:"ip"`
}

To skip the tag for the generated XXX_* fields, use -XXX_skip=yaml,xml flag.

You can’t perform that action at this time.