Code Monkey home page Code Monkey logo

protoc-go-inject-tag's Introduction

protoc-go-inject-tag

Build Status Go Report Card Coverage Status

Why?

Golang protobuf doesn't support custom tags to generated structs. This tool injects custom tags to generated protobuf files, which is commonly used for validating fields, omitting fields from JSON data, etc.

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 install github.com/chen-wenzheng/protoc-go-inject-tag@latest or download the binaries from the releases page.

Usage

$ protoc-go-inject-tag -h
Usage of protoc-go-inject-tag:
  -XXX_skip string
        tags that should be skipped (applies 'tag:"-"') for unknown fields (deprecated since protoc-gen-go v1.4.0)
  -input string
        pattern to match input file(s)
  -verbose
        verbose logging
  -remove_tag_comment
        removes tag comments from the generated file(s)

Add a comment with the following syntax before fields, and these will be injected into the resulting .pb.go file. This can be specified above the field, or trailing the field.

// @gotags: custom_tag:"custom_value"

Example

message IP {
  // Address's comment // @gotags: valid:"ip"
  string Address = 1;
  // MAC's comment
  string MAC = 2; // @gotags: validate:"omitempty"
  string ABC = 3; // ABC's comment // @gotags: validate:"require"
}

Generate your .pb.go files with the protoc command as normal:

protoc --proto_path=. --go_out=paths=source_relative:. test.proto

Then run protoc-go-inject-tag against the generated files (e.g test.pb.go):

protoc-go-inject-tag --input=./test.pb.go
# or
protoc-go-inject-tag --input="*.pb.go"

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

type IP struct {
	// Address's comment // @gotags: valid:"ip"
	Address string `protobuf:"bytes,1,opt,name=Address,proto3" json:"Address,omitempty" valid:"ip"`
	// MAC's comment
	MAC string `protobuf:"bytes,2,opt,name=MAC,proto3" json:"MAC,omitempty" validate:"omitempty"` // @gotags: validate:"omitempty"
	ABC string `protobuf:"bytes,3,opt,name=ABC,proto3" json:"ABC,omitempty" validate:"require"` // ABC's comment // @gotags: validate:"require"
}

With --remove_tag_comment

type IP struct {
	// Address's comment
	Address string `protobuf:"bytes,1,opt,name=Address,proto3" json:"Address,omitempty" valid:"ip"`
	// MAC's comment
	MAC string `protobuf:"bytes,2,opt,name=MAC,proto3" json:"MAC,omitempty" validate:"omitempty"`
	ABC string `protobuf:"bytes,3,opt,name=ABC,proto3" json:"ABC,omitempty" validate:"require"` // ABC's comment
}

Remove gotag comments from generated output

Utilizing the -remove_tag_comment flag, you can remove the gotag comment that is normally annotated to the generated code. This allows more seamless support with libraries like swag/openapi generators that use code comments to generate openapi files.

Deprecated functionality

Skip XXX_* fields

To skip the tag for the generated XXX_* fields (unknown fields), use the -XXX_skip=yaml,xml flag. This is deprecated, as this functionality hasn't existed in protoc-gen-go since v1.4.x.

inject_tag keyword

inject_tag have removed

protoc-go-inject-tag's People

Contributors

favadi avatar lrstanley avatar matthewtsmith avatar houjunchen avatar gabstv avatar binhnguyenduc avatar msolimans avatar credli avatar kadobot avatar jeannedark avatar chen-wenzheng avatar hackneal avatar komkom avatar superqtqt avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.