protoc-go-inject-tag

command module
v1.0.0-3rein Latest Latest
Warning

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

Go to latest
Published: Jan 23, 2020 License: BSD-2-Clause Imports: 11 Imported by: 0

README

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

For oneof fields, a different syntax needs to be used. Add a comment with syntax // @inject_tag_oneof: subfield_name: custom_tag:"custom_value" for each sub-fields that required a tag.

Example:

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

package pb;

message IP {
  // @inject_tag_oneof: Foo: validate:"required"
  // @inject_tag_oneof: Bar: validate:"non-zero"
  // @inject_tag: valid:"ip"
  oneof data {
    string Foo = 1;
    uint32 Bar = 2;
    string Baz = 3;
}

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.

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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