GithubHelp home page GithubHelp logo

wesleywu / protoc-gen-gotag Goto Github PK

View Code? Open in Web Editor NEW

This project forked from srikrsna/protoc-gen-gotag

0.0 1.0 0.0 73 KB

Add custom struct tags to protobuf generated structs

License: MIT License

Go 95.05% Makefile 4.95%

protoc-gen-gotag's Introduction

protoc-gen-gotag (PGGT)

PGGT is a protoc plugin used to add/replace struct tags on generated protobuf messages. Get it using go get github.com/srikrsna/protoc-gen-gotag It supports the following features,

Add/Replace Tags

New tags like xml, sql, bson etc... can be added to struct messages of protobuf. Example

syntax = "proto3";

package example;

import "tagger/tagger.proto";

message Example {
    string with_new_tags = 1 [(tagger.tags) = "graphql:\"withNewTags,optional\"" ];
    string with_new_multiple = 2 [(tagger.tags) = "graphql:\"withNewTags,optional\" xml:\"multi,omitempty\"" ];
    
    string replace_default = 3 [(tagger.tags) = "json:\"replacePrevious\""] ; 

    oneof one_of {
        option (tagger.oneof_tags) = "graphql:\"withNewTags,optional\"";
        string a = 5 [(tagger.tags) = "json:\"A\""];
        int32 b_jk = 6 [(tagger.tags) = "json:\"b_Jk\""];
    }
}

message SecondMessage {
    string with_new_tags = 1 [(tagger.tags) = "graphql:\"withNewTags,optional\"" ];
    string with_new_multiple = 2 [(tagger.tags) = "graphql:\"withNewTags,optional\" xml:\"multi,omitempty\"" ];
    
    string replace_default = 3 [(tagger.tags) = "json:\"replacePrevious\""] ; 
}

Then struct tags can be added by running this command after the regular protobuf generation command.

    protoc -I /usr/local/include \
    	-I . \
    	--gotag_out=:. example/example.proto

In the above example tags like graphql and xml will be added whereas existing tags such as json are replaced with the supplied values.

Auto add tags on field

Automatically add custom tags to message field using provided transformer. It will compile the tag as tag:"snaked_key_name" by default if no transformer is being provide. To provide transformer, use: tagName-as-transformer instruction when running gotag

    protoc -I /usr/local/include \
    	-I . \
    	--gotag_out=auto="form+db-as-camel":. example/example.proto

The above command will add two addtional tags (form and db) for each field. The form tag will be lower_snake_case and db tag will be lowerCamelCase

Supported transformers:

Keys Action Ex
"lower_snake", "lower_snake_case", "snake", "snake_case" Make lower snake case key someKey -> some_key
"upper_snake", "upper_snake_case" Make upper snake case key someKey -> Some_key
"lower_camel", "lower_camel_case", "camel", "camel_case" Make lower camel case key someKey -> someKey
"upper_camel", "upper_camel_case" Make upper camel case key someKey -> SomeKey
"dot_notation", "dot", "lower_dot_notation", "lower_dot" Make lower cased dot notation key someKey -> some.key
"upper_dot", "upper_dot_notation" Make upper cased dot notation key someKey -> Some.Key

Add tags to XXX* fields

It is very useful to ignore XXX* fields in protobuf generated messages. The go protocol buffer compiler adds json:"-" tag to all XXX* fields. Additional tags can be added to these fields using the 'xxx' option of PGGT. It can be done like this. All '+' characters will be replaced with ':'.

    protoc -I /usr/local/include \
    	-I . \
    	--gotag_out=xxx="graphql+\"-\" bson+\"-\"":. example/example.proto

Output to a directory other than the current directory

When outputting to a directory other than the current directory, you will need to pass the output path twice using the "outdir" parameter. If you experience any no such file or directory, errors, try adding the "outdir" flag.

   protoc -I /usr/local/include \
       	-I ./pkg \
       	--gotag_out=outdir="./pkg":./pkg example/example.proto

Note

This should always run after protocol buffer compiler has run. The command such as the one below will fail/produce unexpected results.

   protoc -I /usr/local/include \
       	-I . \
       	--go_out=:. \
       	--gotag_out=xxx="graphql+\"-\" bson+\"-\"":. example/example.proto

protoc-gen-gotag's People

Contributors

srikrsna avatar sashayakovtseva avatar khorevaa avatar yinzara avatar titouanfreville avatar thefong avatar kostyay avatar qoke avatar

Watchers

 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.