A command line tool for injecting custom struct tags into Protobuf's
Explore the docs »
Table of Contents
protoc-go-inject-tag is a command line tool for injecting custom struct tags into go-lang generated protobuf messages. I originally found a need for this when I was developing MTGJSON-API
and needed a way to insert bson
tags into the structs that protoc
generated. This feature is not supported out of the box using protoc
hence the need for this project to exist. Thsi was originally
created by Favadi and I encourage you to check out his original repo
What has changed?
- Fully featured Cobra CLI with Viper config management
- Recursively parse all files in a directory without needing to use patterns
- Proper verbosity and logging with
log/slog
- The ability to change the file extension to search for with
--file-ext
- Set your own comment prefix using
--comment-prefix
- Removed deprecation warning for @inject_tag
What will be included in the future?
- A refactored API to simplify the injection process
- Proper abstractions for TextArea and TagItems
- The ability to set default tags for all fields (i.e. bson=camelCase)
protoc-go-inject-tag originally created by Favadi, improved by Steven Zaluk
Realistically, nothing is needed to get started with this repo. All you need to do is pull the binary from the release's page and execute it to get started. However
if you do not have any protobuf messages generated then you are going to need to install both protoc
and the go lang support protoc-gen-go
You can see instructions for installing these in the "Prerequisites" section below this one
To start install protoc
using the following commands depending on your operating system
- MacOS
brew update && brew install protobuf
- Debian Based Linux:
apt update && apt install -y protobuf-compiler
You can validate that this is installed using the following command: protoc --version
Next install protoc-gen-go
: go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
- Clone the repo
git clone https://github.com/stevezaluk/mtgjson-api.git
- Install dependencies
go get .
- Build the project
go build
- View the help page
./protoc-go-inject-tag --help
Usage:
protoc-go-inject-tag inject [flags]
Flags:
--comment-prefix string The prefix of the comment that protoc-go-inject-tag should search for when looking for tags to inject. A @ will be prefixed to this value (default "gotags") -f, --file-ext string The file extensions that should be considered for injection (default ".pb.go")
-h, --help help for inject
-i, --input string The input path you want to search for protobufs with
--remove-comments Remove comments from generated protobufs
--tags strings Additional tags that should be applied to all fields (not implemented yet)
-v, --verbose Enable extended verbosity in logging
Global Flags:
--config string config file (default is $HOME/config/protoc-go-inject-tag/config.json)
Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.
If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star! Thanks again!
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature
) - Commit your Changes (
git commit -m 'Add some AmazingFeature'
) - Push to the Branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
Distributed under Apache License 2.0. See LICENSE.txt
for more information.
Steven A. Zaluk - @steve_zaluk
Project Link: https://github.com/stevezaluk/protoc-go-inject-tag\n