From c64b41d7d9a28e16a6fed5cfbc0471cf9844754a Mon Sep 17 00:00:00 2001 From: Yannick Briffa Date: Thu, 11 Jan 2018 17:46:53 +0100 Subject: [PATCH] feature: adds no-stringer argument --- jsonenums.go | 13 ++++++++----- template.go | 3 +++ 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/jsonenums.go b/jsonenums.go index 343f3d2..480f350 100644 --- a/jsonenums.go +++ b/jsonenums.go @@ -88,11 +88,12 @@ import ( ) var ( - typeNames = flag.String("type", "", "comma-separated list of type names; must be set") - outputLower = flag.Bool("lower", false, "set the json enum values to lower case") - outputUpper = flag.Bool("upper", false, "set the json enum values to upper case") - outputPrefix = flag.String("prefix", "", "prefix to be added to the output file") - outputSuffix = flag.String("suffix", "_jsonenums", "suffix to be added to the output file") + typeNames = flag.String("type", "", "comma-separated list of type names; must be set") + outputLower = flag.Bool("lower", false, "set the json enum values to lower case") + outputUpper = flag.Bool("upper", false, "set the json enum values to upper case") + outputNoStringer = flag.Bool("no-stringer", false, "disable the usage of stringer if exists") + outputPrefix = flag.String("prefix", "", "prefix to be added to the output file") + outputSuffix = flag.String("suffix", "_jsonenums", "suffix to be added to the output file") ) func main() { @@ -130,12 +131,14 @@ func main() { TypesAndValues map[string][]string Lower bool Upper bool + NoStringer bool }{ Command: strings.Join(os.Args[1:], " "), PackageName: pkg.Name, TypesAndValues: make(map[string][]string), Lower: *outputLower, Upper: *outputUpper, + NoStringer: *outputNoStringer, } // Run generate for each type. diff --git a/template.go b/template.go index ca1ac10..764dd48 100644 --- a/template.go +++ b/template.go @@ -35,6 +35,7 @@ import ( {{ $upper := .Upper}} {{ $lower := .Lower}} +{{ $noStringer := .NoStringer }} {{range $typename, $values := .TypesAndValues}} var ( @@ -49,6 +50,7 @@ var ( } ) +{{ if not $noStringer }} func init() { var v {{$typename}} if _, ok := interface{}(v).(fmt.Stringer); ok { @@ -58,6 +60,7 @@ func init() { } } } +{{ end }} // MarshalJSON is generated so {{$typename}} satisfies json.Marshaler. func (r {{$typename}}) MarshalJSON() ([]byte, error) {