feature: adds no-stringer argument

This commit is contained in:
Yannick Briffa 2018-01-11 17:46:53 +01:00
parent dfc5db481e
commit c64b41d7d9
2 changed files with 11 additions and 5 deletions

View File

@ -91,6 +91,7 @@ var (
typeNames = flag.String("type", "", "comma-separated list of type names; must be set") 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") 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") 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") 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") outputSuffix = flag.String("suffix", "_jsonenums", "suffix to be added to the output file")
) )
@ -130,12 +131,14 @@ func main() {
TypesAndValues map[string][]string TypesAndValues map[string][]string
Lower bool Lower bool
Upper bool Upper bool
NoStringer bool
}{ }{
Command: strings.Join(os.Args[1:], " "), Command: strings.Join(os.Args[1:], " "),
PackageName: pkg.Name, PackageName: pkg.Name,
TypesAndValues: make(map[string][]string), TypesAndValues: make(map[string][]string),
Lower: *outputLower, Lower: *outputLower,
Upper: *outputUpper, Upper: *outputUpper,
NoStringer: *outputNoStringer,
} }
// Run generate for each type. // Run generate for each type.

View File

@ -35,6 +35,7 @@ import (
{{ $upper := .Upper}} {{ $upper := .Upper}}
{{ $lower := .Lower}} {{ $lower := .Lower}}
{{ $noStringer := .NoStringer }}
{{range $typename, $values := .TypesAndValues}} {{range $typename, $values := .TypesAndValues}}
var ( var (
@ -49,6 +50,7 @@ var (
} }
) )
{{ if not $noStringer }}
func init() { func init() {
var v {{$typename}} var v {{$typename}}
if _, ok := interface{}(v).(fmt.Stringer); ok { if _, ok := interface{}(v).(fmt.Stringer); ok {
@ -58,6 +60,7 @@ func init() {
} }
} }
} }
{{ end }}
// MarshalJSON is generated so {{$typename}} satisfies json.Marshaler. // MarshalJSON is generated so {{$typename}} satisfies json.Marshaler.
func (r {{$typename}}) MarshalJSON() ([]byte, error) { func (r {{$typename}}) MarshalJSON() ([]byte, error) {