Giter VIP home page Giter VIP 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/favadi/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

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

package pb;
option go_package = "/pb";

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

  // Or:
  string MAC = 2; // @gotags: validate:"omitempty"
}

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 {
 // @gotags: valid:"ip"
 Address string `protobuf:"bytes,1,opt,name=Address,json=address" json:"Address,omitempty" valid:"ip"`
}

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

Since v1.3.0, we recommend using @gotags: rather than @inject_tags:, as @gotags is more indicative of the language the comment is for. We don't plan on removing @inject_tags: support anytime soon, however we strongly recommend switching to @gotags.

protoc-go-inject-tag's People

Contributors

binhnguyenduc avatar credli avatar dependabot[bot] avatar favadi avatar gabstv avatar hackneal avatar houjunchen avatar jeannedark avatar kadobot avatar komkom avatar lrstanley avatar matthewtsmith avatar msolimans avatar superqtqt avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

protoc-go-inject-tag's Issues

Is it possible to inject multiple tags into one field of a message

Hi, I tried to inject two different tag names into the same field of a protobuf message like this

message Product {
    // @inject_tag: form:"type" validate:"min=0,max=3"
    int32 type = 1;
}

But it actually injects nothing
So I'm wondering if I'm misunderstanding the usage or not?

Your help would be greatly appreciated.

Fails with index out of range on streams

protoc-go-inject-tag fails when I have a stream:

$ protoc-go-inject-tag.exe -input=./stuff.pb.go                                                                                                                             2018/09/19 19:18:32 parsing file "./stuff.pb.go" for inject tag comments
panic: runtime error: index out of range

goroutine 1 [running]:
main.parseFile(0xc0420960c7, 0x35, 0x0, 0x0, 0x0, 0x535d10, 0x21, 0xc042062210, 0xc04208bf20, 0x4e6dbb)
        C:/Users/xxx/go/src/github.com/favadi/protoc-go-inject-tag/file.go:74 +0xc77
main.main()
        C:/Users/xxx/go/src/github.com/favadi/protoc-go-inject-tag/main.go:26 +0x152

I've managed to reduce my proto file to the following while still getting the error:

syntax = "proto3";

package stuff;
option go_package = "proto";

service Stuff {
	rpc GetStuff (Params) returns (stream Record) {}
}


message Params {
	// @inject_tag: validate:"omitempty"
	string id = 1;
}

message Record {
	string id = 1;
}

I have reduce my protoc command to this, I still have the problem with protoc-go-inject-tag: protoc -I=. ./stuff.proto --go_out=plugins=grpc:.

Go 1.10 now required due to undefined: strings.Builder

This line was introduced in #20

builder := strings.Builder{}

The Builder type was only added to strings in Go 1.10. Go get'ing this package currently results in

go get github.com/favadi/protoc-go-inject-tag
# github.com/favadi/protoc-go-inject-tag
go/src/github.com/favadi/protoc-go-inject-tag/file.go:62:14: undefined: strings.Builder

This should either be corrected to remove the dependence or the docs should be updated to indicate the minimum version requirement. I'm happy to have a crack at either, but comes down to what the project would prefer.

delete the correct comment -remove_tag_comment

The -remove_tag_comment parameter will cause the correct comment to be deleted as well.

eg:

message Result {
       string ruleCode = 1; // @gotags: json:"rule_code" rule code
       string desc = 2; // rule code are explained in detail
}

command:

protoc-go-inject-tag -remove_tag_comment -input=./test/v1/rule.pb.go;

output

type CheckReply_Result struct {
	state         protoimpl.MessageState
	sizeCache     protoimpl.SizeCache
	unknownFields protoimpl.UnknownFields

	RuleCode string                 `protobuf:"bytes,1,opt,name=ruleCode,proto3" json:"rule_code"`  
	Desc         string                 `protobuf:"bytes,2,opt,name=type,proto3" json:"type,omitempty"`         // rule code are explained in detail
}

expect output

type CheckReply_Result struct {
	state         protoimpl.MessageState
	sizeCache     protoimpl.SizeCache
	unknownFields protoimpl.UnknownFields

	RuleCode string                 `protobuf:"bytes,1,opt,name=ruleCode,proto3" json:"rule_code"`           // rule code 
	Desc         string                 `protobuf:"bytes,2,opt,name=type,proto3" json:"type,omitempty"`         // rule code are explained in detail
}

how to solve this。

Thanks to the author

Support Nested Messages

Currently with nested protobuf messages, tags are not injected on the top level message attributes. Is this a known issue/do you have any plans on supporting this?

does input support find command

As I want to inject for multi pb.go files in different director. The directory may be like:

golang=>app=>test=>test.pb.go
golang=>sec=>sec.pb.go

protoc-go-inject-tag -input=$(find . -name "*.pb.go")

But it seems does not work

Cannot inject multiple tags in the same protobuf message

Hi,

It looks like proton-go-inject-tag recognizes only the first tag in the message...

message User {
// @inject_tag: validate:"required" <--- Recognized
string id = 1;
string type = 2;
// @inject_tag: validate:"email" <--- Nope
string email = 3;
bool activated = 4;
// @inject_tag: validate:"required" <--- Nope
string phone = 5;
}

feature: more language-specific comment key

Looking over some of the proto files I've written, and the comments/fields aren't very obvious what their purpose is -- primarily around what language they help annotate. I'm thinking we add support for (and recommend it as the default), for @gotags: or similar, since these entries should really only ever be specific to Go. This should help those who use the proto files in other languages.

This is also very easy to make backwards compatible, so shouldn't cause any issues (e.g. (inject_tags?|gotags?).

Thoughts?

Injecting a tag should overwrite the existing one (i.e. json)

Say if I want to inject a json tag // @inject_tag: json:"timestamp,omitempty,string", to specify field type, protoc-go-inject-tag will just append to existing tags. Given that protoc for go adds json tags by default, the end result looks like below:

Timestamp int64 `json:"timestamp,omitempty" json:"timestamp,omitempty,string"`

What I would like to see is:

Timestamp int64 `json:"timestamp,omitempty,string"`

Feature: add tag to generated XXX_* fields

Protobuf merged their dev branch into master, bringing three new fields to the generated structs.

	XXX_NoUnkeyedLiteral struct{} `json:"-"`
	XXX_unrecognized     []byte   `json:"-"`
	XXX_sizecache        int32    `json:"-"`

This will likely cause all sort of issues for people, issues that are already partially solved by protoc-go-inject-tag - it would be nice to have a cli flag to also add a tag to these 3 fields.

For instance,

protoc-go-inject-tag -input=models/data.pb.go --XXX_skip=xml

would produce

	XXX_NoUnkeyedLiteral struct{} `json:"-" xml:"-"`
	XXX_unrecognized     []byte   `json:"-" xml:"-"`
	XXX_sizecache        int32    `json:"-" xml:"-"`

If it does not end with a json tag, omitempty cannot be removed after executing inject_tag.

Automatically generate pb files based on table structure:

type EntityGiftDetail struct {
	state         protoimpl.MessageState
	sizeCache     protoimpl.SizeCache
	unknownFields protoimpl.UnknownFields

	// @inject_tag: orm:"id,primary"
	Id uint32 `protobuf:"varint,1,opt,name=id,proto3" json:"id,omitempty" orm:"id,primary"` //
	// @inject_tag: orm:"name"
	Name string `protobuf:"bytes,2,opt,name=name,proto3" json:"name,omitempty" orm:"name"` // gift name
	// @inject_tag: orm:"app_id"
	AppId uint32 `protobuf:"varint,3,opt,name=app_id,json=appId,proto3" json:"app_id,omitempty" orm:"app_id"` // app ID
	// @inject_tag: orm:"is_combo"
	IsCombo uint32 `protobuf:"varint,4,opt,name=is_combo,json=isCombo,proto3" json:"is_combo,omitempty" orm:"is_combo"` // combo gift. 0:no,1:yes
}

expect:

type EntityGiftDetail struct {
	state         protoimpl.MessageState
	sizeCache     protoimpl.SizeCache
	unknownFields protoimpl.UnknownFields

	// @inject_tag: orm:"id,primary"
	Id uint32 `protobuf:"varint,1,opt,name=id,proto3" json:"id" orm:"id,primary"` //
	// @inject_tag: orm:"name"
	Name string `protobuf:"bytes,2,opt,name=name,proto3" json:"name" orm:"name"` // gift name
	// @inject_tag: orm:"app_id"
	AppId uint32 `protobuf:"varint,3,opt,name=app_id,json=appId,proto3" json:"app_id,omitempty" orm:"app_id"` // app ID
	// @inject_tag: orm:"is_combo"
	IsCombo uint32 `protobuf:"varint,4,opt,name=is_combo,json=isCombo,proto3" json:"is_combo" orm:"is_combo"` // combo gift. 0:no,1:yes
}

===============================================

But can be parsed normally in custom struct:

message UserVip {
  // @inject_tag: json:"unlock"
  uint32 unlock = 1;//show_tab=vip, Unlock required level
  // @inject_tag: json:"state"
  uint32 state = 2; //state. 0:not unlock,1: unlock already
}

output:

type UserVip struct {
	state         protoimpl.MessageState
	sizeCache     protoimpl.SizeCache
	unknownFields protoimpl.UnknownFields

	// @inject_tag: json:"unlock"
	Unlock uint32 `protobuf:"varint,1,opt,name=unlock,proto3" json:"unlock"` //show_tab=vip, Unlock required level
	// @inject_tag: json:"state"
	State uint32 `protobuf:"varint,2,opt,name=state,proto3" json:"state"` //state. 0:not unlock,1: unlock already
}

Feature: Add support for oneof fields

There seems to be no ability to inject tags on oneof fields. This could be useful to be able to inject both at the root of the oneof and at the level of individual fields inside that oneof such as:

message Object {
        [...]
        // @inject_tag tag:"moomeh"
        oneof moomeh {
	        // @inject_tag tag:"moo"
	        string moo = 16;
	        // @inject_tag tag:"meh"
	        int64 meh = 17;
        }
}

Would thus generate:

        [...]
	// Types that are valid to be assigned to Moomeh:
	//	*Object_Moo
	//	*Object_Meh
	Moomeh isObject_Moomeh `protobuf_oneof:"moomeh" tag:"moomeh"`
}

[...]

type Object_Moo struct {
	Moo string `protobuf:"bytes,16,opt,name=moo,proto3,oneof" tag:"moo"`
}
type Object_Meh struct {
	Meh int64 `protobuf:"varint,17,opt,name=meh,proto3,oneof" tag:"meh"`
}

Possible to cut a release

Our team would like to create a brew formula for this great injector, however there are no published releases.

Would it be possible for you to cut one?

We'd rather point straight to the source and give all credit where credit is due than fork and cut our own release.

[Enhancement] Add comment to existing .proto files automatically

Context

I have a lot of .proto files that contain message definitions,
and adding // @inject_tag: validate:"omitempty" before each field of message is so costly.

Desire

This tool provides an option to add // @inject_tag: validate:"omitempty" before each field of message. The tag prefix (validate for above example) is given by input, and the tag suffix("omitempty" for above example) is generated bases on the name of message field.

Eg:

// message.proto
message SomeMessage {
  string id = 1;
  int32  order_number = 2;
}

Then after run the command, ($./protoc-go-inject-tag -add-cmt tada -input=./message.proto for example) I should get:

// message.proto
message SomeMessage {
  // @inject_tag: tada:"id"
  string id = 1;
  // @inject_tag: tada:"order_number"
  int32  order_number = 2;
}

inputPath, less verbosity

Just wanted to note I took a few minutes and tossed this around - xackery@ad68412

Since I changed the paths to xackery I didn't want to PR back, but highlights are..

  • --path argument to import an entire path
  • removed all logs except for a single log.Fatal() on error

if you want me to change paths and would be interested in a PR let me know, otherwise feel free to close this issue.

Parse error when tag's value contain space

I just like to share some error and solution, in case someone stumbled upon the same issue as me.

I try to find the tag specification, but can't found either in reflect package [1] nor in Go spec [2]. Seems like the common ways is to use no space on tag value.

Oh, BTW thank you for this project 👍 !

Reproduction steps

  • Assume that struct already tagged in test.go, using protoc-go-inject-tag
type T struct {
	// @inject_tag: valid:"email, required"
	Email string `protobuf:"bytes,1,opt,name=email" json:"email,omitempty"`
}
  • Run protoc-go-inject-tag,

    $ protoc-go-inject-tag -input=test.go

Expected output

No error.

Actual output

NOTE: additional debugging line newTagItems t:

2018/05/01 12:29:58 parsing file "test.go" for inject tag comments
2018/05/01 12:29:58 parsed file "test.go", number of fields to inject custom tags: 1
2018/05/01 12:29:58 inject custom tag "valid:\"email, required\"" to expression "Email string `protobuf:\"bytes,1,opt,name=email\" json:\"email,omitempty\"`"
newTagItems t: protobuf:"bytes,1,opt,name=email"
newTagItems t: json:"email,omitempty"
newTagItems t: valid:"email,
newTagItems t: required"
panic: runtime error: slice bounds out of range

goroutine 1 [running]:
main.newTagItems(0xc420012580, 0x17, 0xc42005e300, 0x2, 0x2)
        /home/ms/Workspaces/myabuy/proofn/src/github.com/favadi/protoc-go-inject-tag/parse.go:58 +0x2fb
main.injectTag(0xc4200c2000, 0x92, 0x600, 0x49, 0x90, 0xc420014341, 0x38, 0xc420012580, 0x17, 0xc420012580, ...)
        /home/ms/Workspaces/myabuy/proofn/src/github.com/favadi/protoc-go-inject-tag/parse.go:69 +0x13e
main.writeFile(0x7ffe952d65f8, 0x7, 0xc42007e3c0, 0x1, 0x1, 0x0, 0x0)
        /home/ms/Workspaces/myabuy/proofn/src/github.com/favadi/protoc-go-inject-tag/file.go:103 +0x27c
main.main()
        /home/ms/Workspaces/myabuy/proofn/src/github.com/favadi/protoc-go-inject-tag/main.go:23 +0x14d

This was caused by,

splitted := strings.Split(tag, " ")

Solution

Remove space on tag value, e.g. from valid:"a, b" to valid:"a,b".

[1] https://golang.org/ref/spec#Tag
[2] https://golang.org/pkg/reflect/#StructTag

Is possible to integrate with buf.build?

I am trying to use this library, to integrate with modern build system called http://buf.build

plugins:
  - name: go
    out: pb
    opt: paths=source_relative
  - plugin: buf.build/grpc/go
    out: pb
    opt:
      - paths=source_relative
  - name: go-inject-tag
    out: .
    opt:
      - paths=source_relative
      - input: "*.pb.go"

But it throws an error with exit code 1

   **Failure: could not convert element map[string]interface {} to a string**

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.