Monkey-Pro / protoc-go-inject-tag

Inject custom tags to protobuf golang struct

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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.

To enable verbose logging, use -verbose

标签类型说明

  1. 作用域优先级: 结构体注解上的标签 < 字段注解上的标签 即:当字段注解上没有改类型的标签说明,则适用结构体注解上的标签规则;当字段注解和结构体注解上都有标签的说明,只使用字段注解上的标签规则。
  2. 忽略以XXX_打头的字段名转换
  3. 标签值规则, 如下表格:
标签值 说明 示例
"-" 忽略标签值 json:"-"
"#toSnake" 根据字段名转蛇形 json:"field_name"
"#toCamel" 根据字段名转驼峰 json:"FieldName"
"#toCamel2" 根据字段名转驼峰并首字母小写 json:"fieldName"
自定义标签 直接使用该标签 selfTag:"self-tag-value"

About

Inject custom tags to protobuf golang struct

License:BSD 2-Clause "Simplified" License


Languages

Language:Go 99.7%Language:Shell 0.3%