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

@ -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.

View File

@ -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) {