Generates constants for json tags in go structs.
Code is based on golang.org/x/tools/cmd/stringer and works similarly.
Manual
Usage of gojsontagstringer:
gojsontagstringer [flags] -type T [package]
gojsontagstringer [flags] -type T [directory]
gojsontagstringer [flags] -type T files... # Must be a single package
For more information, see:
https://pkg.go.dev/github.com/sgtsquiggs/gojsontagstringer
Flags:
-output string
output file name; default srcdir/<type>_string.go
-tags string
comma-separated list of build tags to apply
-type string
comma-separated list of type names; must be set
Examples
Local Files
Given file car.go
:
package car
type Car struct {
Make string `json:"make"`
Model string `json:"model"`
}
Then executing command:
$ gojsontagstringer -type Car car.go
Resulting with file car_jsontags.go
:
// Code generated by "gojsontagstringer -type Car car.go"; DO NOT EDIT.
package car
const (
CarMakeJson = "make"
CarModelJson = "model"
)
3rd Party Library
Assumes that the 3rd party package is available to the module in the current working directory.
Executing command:
$ gojsontagstringer -type BoolValue google.golang.org/protobuf/types/known/wrapperspb
Resulting with file boolvalue_jsontags.go
:
// Code generated by "gojsontagstringer -type BoolValue google.golang.org/protobuf/types/known/wrapperspb"; DO NOT EDIT.
package local_package
const (
BoolValueValueJson = "value"
)