ngerakines / protoc-gen-whatever

A protoc plugin use to generate arbitrary files from protocol buffer definitions.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

protoc-gen-whatever

This is a plugin that allows for protocol buffer definitions to be used as inputs to Golang's text template library to generate files.

Installation

Instlal with go get.

$ go get -u github.com/ngerakines/protoc-gen-whatever/cmd/protoc-gen-whatever

Plugin Configuration

This plugin is used when the --whatever_out and --whatever_opt arguments are provided to the base protoc command.

The --whatever_out argument is used to set the location of generated output files.

The --whatever_opt argument is used to set the input template and optional output file name. This argument is optional and defaults to --whatever_opt=whatever.tpl.

For example, with --whatever_out=. --whatever_opt=input.tpl the plugin will look for a file named input.tpl in the current working directory and generate a file namee output in the current working directory.

With --whatever_out=cmd --whatever_opt=templates/cobra.tpl,commands.go, the plugin will look for the templates/cobra.tpl file as the template and render the file cmd/commands.go.

Templates

The input to the template is the CodeGeneratorRequest structure as defined in google/protobuf/compiler/plugin.proto.

Usage

With the proto file simple.proto:

syntax = "proto3";

package test;

message Example {
    string label = 1;
}

service Foo {
    rpc GetFoo(GetFooReq) returns (GetFooRes);
}

message GetFooReq {

}

message GetFooRes {

}

And the template file simple.tpl:

{{.Name}}{{range .MessageType}}
*{{.Name}}{{end}}

Running the command:

$ protoc --plugin=./protoc-gen-whatever --whatever_out=. --whatever_opt=./test/simple.tpl,output.txt ./test/simple.proto

Yields the file output.txt:

simple.proto
*Example
*GetFooReq
*GetFooRes

Testing

$ protoc --go_out=plugins=grpc:. test/*.proto

Release

To release new versions, use mage.

$ go get github.com/goreleaser/goreleaser
$ go get -u -d github.com/magefile/mage
$ mage clean
$ TAG=0.2.0 mage release

Credit

This project was inspired by the work of David Muto on https://github.com/pseudomuto/protoc-gen-doc/.

License

Open source under the MIT license.

Copyright (c) 2018 Nick Gerakines (ngerakines)

About

A protoc plugin use to generate arbitrary files from protocol buffer definitions.

License:MIT License


Languages

Language:Go 99.6%Language:Smarty 0.4%