Change template
This commit is contained in:
parent
eec6d38da6
commit
4d849550dc
@ -16,7 +16,6 @@
|
|||||||
// Given the name of a (signed or unsigned) integer type T that has constants
|
// Given the name of a (signed or unsigned) integer type T that has constants
|
||||||
// defined, jsonenums will create a new self-contained Go source file implementing
|
// defined, jsonenums will create a new self-contained Go source file implementing
|
||||||
//
|
//
|
||||||
// func (t T) String() string
|
|
||||||
// func (t T) MarshalJSON() ([]byte, error)
|
// func (t T) MarshalJSON() ([]byte, error)
|
||||||
// func (t *T) UnmarshalJSON([]byte) error
|
// func (t *T) UnmarshalJSON([]byte) error
|
||||||
//
|
//
|
||||||
@ -47,7 +46,6 @@
|
|||||||
// in the same directory will create the file pill_jsonenums.go, in package painkiller,
|
// in the same directory will create the file pill_jsonenums.go, in package painkiller,
|
||||||
// containing a definition of
|
// containing a definition of
|
||||||
//
|
//
|
||||||
// func (r Pill) String() string
|
|
||||||
// func (r Pill) MarshalJSON() ([]byte, error)
|
// func (r Pill) MarshalJSON() ([]byte, error)
|
||||||
// func (r *Pill) UnmarshalJSON([]byte) error
|
// func (r *Pill) UnmarshalJSON([]byte) error
|
||||||
//
|
//
|
||||||
|
76
template.go
76
template.go
@ -18,63 +18,61 @@ package main
|
|||||||
import "text/template"
|
import "text/template"
|
||||||
|
|
||||||
var generatedTmpl = template.Must(template.New("generated").Parse(`
|
var generatedTmpl = template.Must(template.New("generated").Parse(`
|
||||||
// generated by jsonenums {{.Command}}; DO NOT EDIT
|
// Code generated by "jsonenums {{.Command}}"; DO NOT EDIT.
|
||||||
|
|
||||||
package {{.PackageName}}
|
package {{.PackageName}}
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
)
|
)
|
||||||
|
|
||||||
{{range $typename, $values := .TypesAndValues}}
|
{{range $typename, $values := .TypesAndValues}}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
_{{$typename}}NameToValue = map[string]{{$typename}} {
|
_{{$typename}}NameToValue map[string]{{$typename}}
|
||||||
{{range $values}}"{{.}}": {{.}},
|
_{{$typename}}ValueToName map[{{$typename}}]string
|
||||||
{{end}}
|
|
||||||
}
|
|
||||||
|
|
||||||
_{{$typename}}ValueToName = map[{{$typename}}]string {
|
|
||||||
{{range $values}}{{.}}: "{{.}}",
|
|
||||||
{{end}}
|
|
||||||
}
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
var v {{$typename}}
|
_{{$typename}}NameToValue = map[string]{{$typename}} {
|
||||||
if _, ok := interface{}(v).(fmt.Stringer); ok {
|
{{range $values}}{{.}}.String(): {{.}},
|
||||||
_{{$typename}}NameToValue = map[string]{{$typename}} {
|
{{end}}
|
||||||
{{range $values}}interface{}({{.}}).(fmt.Stringer).String(): {{.}},
|
}
|
||||||
{{end}}
|
|
||||||
}
|
_{{$typename}}ValueToName = map[{{$typename}}]string {
|
||||||
}
|
{{range $values}}{{.}}: {{.}}.String(),
|
||||||
|
{{end}}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// MarshalJSON is generated so {{$typename}} satisfies json.Marshaler.
|
// MarshalJSON implements json.Marshaler
|
||||||
func (r {{$typename}}) MarshalJSON() ([]byte, error) {
|
func (r {{$typename}}) MarshalJSON() ([]byte, error) {
|
||||||
if s, ok := interface{}(r).(fmt.Stringer); ok {
|
if s, ok := interface{}(r).(fmt.Stringer); ok {
|
||||||
return json.Marshal(s.String())
|
return json.Marshal(s.String())
|
||||||
}
|
}
|
||||||
s, ok := _{{$typename}}ValueToName[r]
|
|
||||||
if !ok {
|
s, ok := _{{$typename}}ValueToName[r]
|
||||||
return nil, fmt.Errorf("invalid {{$typename}}: %d", r)
|
if !ok {
|
||||||
}
|
return nil, fmt.Errorf("invalid {{$typename}}: %d", r)
|
||||||
return json.Marshal(s)
|
}
|
||||||
|
return json.Marshal(s)
|
||||||
}
|
}
|
||||||
|
|
||||||
// UnmarshalJSON is generated so {{$typename}} satisfies json.Unmarshaler.
|
// UnmarshalJSON implements json.Unmarshaler
|
||||||
func (r *{{$typename}}) UnmarshalJSON(data []byte) error {
|
func (r *{{$typename}}) UnmarshalJSON(data []byte) error {
|
||||||
var s string
|
var s string
|
||||||
if err := json.Unmarshal(data, &s); err != nil {
|
if err := json.Unmarshal(data, &s); err != nil {
|
||||||
return fmt.Errorf("{{$typename}} should be a string, got %s", data)
|
return fmt.Errorf("{{$typename}} should be a string, got %s", data)
|
||||||
}
|
}
|
||||||
v, ok := _{{$typename}}NameToValue[s]
|
|
||||||
if !ok {
|
v, ok := _{{$typename}}NameToValue[s]
|
||||||
return fmt.Errorf("invalid {{$typename}} %q", s)
|
if !ok {
|
||||||
}
|
return fmt.Errorf("invalid value: %s", s)
|
||||||
*r = v
|
}
|
||||||
return nil
|
|
||||||
|
*r = v
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
{{end}}
|
{{end}}
|
||||||
|
Loading…
Reference in New Issue
Block a user