whatever

package module
v0.0.0-...-c8030ff Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 5, 2022 License: MIT Imports: 10 Imported by: 0

README

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)

Documentation

Index

Constants

View Source
const VERSION = "0.1.0"

VERSION is the version of the plugin.

Variables

This section is empty.

Functions

Types

type Flags

type Flags struct {
	// contains filtered or unexported fields
}

func ParseFlags

func ParseFlags(w io.Writer, args []string) *Flags

ParseFlags parses the supplied options are returns a `Flags` object to the caller.

Parameters:

  • `w` - the `io.Writer` to use for printing messages (help, version, etc.)
  • `args` - the set of args the program was invoked with (typically `os.Args`)

func (*Flags) Code

func (f *Flags) Code() int

Code returns the status code to exit with after handling the supplied flags

func (*Flags) HasMatch

func (f *Flags) HasMatch() bool

HasMatch returns whether or not the supplied args are matches. For example, passing `--help` will match, or some unknown parameter, but passing nothing will not.

func (*Flags) PrintHelp

func (f *Flags) PrintHelp()

PrintHelp prints the usage string including all flags to the `io.Writer` that was supplied to the `Flags` object.

func (*Flags) PrintVersion

func (f *Flags) PrintVersion()

PrintVersion prints the version string to the `io.Writer` that was supplied to the `Flags` object.

func (*Flags) ShowHelp

func (f *Flags) ShowHelp() bool

ShowHelp determines whether or not to show the help message

func (*Flags) ShowVersion

func (f *Flags) ShowVersion() bool

ShowVersion determines whether or not to show the version message

type PluginOptions

type PluginOptions struct {
	TemplateFile string
	Suffix       string
}

Directories

Path Synopsis
cmd
protoc-gen-whatever
protoc-gen-whatever is used to generate stuff from proto files.
protoc-gen-whatever is used to generate stuff from proto files.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL