protoc-gen-tpl
is a plugin for protobuf compiler (protoc
) which allows to generate files using custom templates and rules.
See in checklist implemented and planned features.
You need protobuf compiler. See instructions on Protocol Buffers site.
Install on Linux
using apt
apt install protobuf-compiler
Install on macOS
using Homebrew
brew install protobuf
Install protoc-gen-tpl
plugin using go
go get github.com/apoprotsky/protoc-gen-tpl
Example how to generate go code from proto files
mkdir examples/out
protoc \
--tpl_out=examples/out \
--tpl_opt=prefix=github.com/apoprotsky/protoc-gen-tpl/examples/ \
--tpl_opt=lang=go \
--tpl_opt=lang=ts \
--tpl_opt=lang=php \
examples/proto/*.proto
Option prefix
has same behaviour as module=$PREFIX
for go plugin.
Option lang
designates which languages to use for output files. This option can be specified multiple times.
- Generate
go
files fromproto
files - Generate structs types from messages
- Generate struct fields tags
- Generate json tags
- Generate custom tags
- Generate types from enumerations
- Generate constants from enumerations
- Supported struct fields types:
- Scalar types (string, numbers)
- Arrays
- Message type
- Enumeration type
- Generate
ts
files fromproto
files - Generate interfaces from messages
- Generate enumerations
- Supported interface fields types:
- Scalar types (string, boolean, number, bigint)
- Arrays
- Message type
- Enumeration type
- Generate
php
files fromproto
files - Generate classes from messages
- Generate constants from enumerations