2015-01-29 11:35:15 +01:00
|
|
|
// Copyright 2014 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// Added as a .go file to avoid embedding issues of the template.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import "text/template"
|
|
|
|
|
|
|
|
var generatedTmpl = template.Must(template.New("generated").Parse(`
|
|
|
|
// generated by jsonenums {{.Command}}; DO NOT EDIT
|
|
|
|
|
|
|
|
package {{.PackageName}}
|
|
|
|
|
|
|
|
import (
|
2016-07-29 21:29:41 +02:00
|
|
|
"database/sql/driver"
|
2015-01-29 11:35:15 +01:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2016-07-29 21:29:41 +02:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2015-01-29 11:35:15 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
{{range $typename, $values := .TypesAndValues}}
|
|
|
|
|
2015-02-04 14:38:28 +01:00
|
|
|
var (
|
|
|
|
_{{$typename}}NameToValue = map[string]{{$typename}} {
|
2016-07-29 21:29:41 +02:00
|
|
|
{{range $values}}"{{.SnakeRep}}": {{.CammelRep}},
|
2015-02-04 14:38:28 +01:00
|
|
|
{{end}}
|
2015-01-31 11:09:02 +01:00
|
|
|
}
|
|
|
|
|
2015-02-04 14:38:28 +01:00
|
|
|
_{{$typename}}ValueToName = map[{{$typename}}]string {
|
2016-07-29 21:29:41 +02:00
|
|
|
{{range $values}}{{.CammelRep}}: "{{.SnakeRep}}",
|
2015-02-04 14:38:28 +01:00
|
|
|
{{end}}
|
|
|
|
}
|
|
|
|
)
|
2015-01-29 11:35:15 +01:00
|
|
|
|
2015-01-31 12:21:55 +01:00
|
|
|
func init() {
|
|
|
|
var v {{$typename}}
|
|
|
|
if _, ok := interface{}(v).(fmt.Stringer); ok {
|
|
|
|
_{{$typename}}NameToValue = map[string]{{$typename}} {
|
2016-07-29 21:29:41 +02:00
|
|
|
{{range $values}}interface{}({{.CammelRep}}).(fmt.Stringer).String(): {{.CammelRep}},
|
2015-02-04 14:38:28 +01:00
|
|
|
{{end}}
|
2015-01-31 12:21:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-23 22:35:24 +02:00
|
|
|
// MarshalJSON is generated so {{$typename}} satisfies json.Marshaler.
|
2015-02-04 14:38:28 +01:00
|
|
|
func (r {{$typename}}) MarshalJSON() ([]byte, error) {
|
|
|
|
if s, ok := interface{}(r).(fmt.Stringer); ok {
|
|
|
|
return json.Marshal(s.String())
|
|
|
|
}
|
|
|
|
s, ok := _{{$typename}}ValueToName[r]
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("invalid {{$typename}}: %d", r)
|
|
|
|
}
|
|
|
|
return json.Marshal(s)
|
|
|
|
}
|
|
|
|
|
2015-04-23 22:35:24 +02:00
|
|
|
// UnmarshalJSON is generated so {{$typename}} satisfies json.Unmarshaler.
|
2015-01-29 11:35:15 +01:00
|
|
|
func (r *{{$typename}}) UnmarshalJSON(data []byte) error {
|
|
|
|
var s string
|
|
|
|
if err := json.Unmarshal(data, &s); err != nil {
|
|
|
|
return fmt.Errorf("{{$typename}} should be a string, got %s", data)
|
|
|
|
}
|
2015-01-31 11:09:02 +01:00
|
|
|
v, ok := _{{$typename}}NameToValue[s]
|
|
|
|
if !ok {
|
2015-01-29 11:35:15 +01:00
|
|
|
return fmt.Errorf("invalid {{$typename}} %q", s)
|
|
|
|
}
|
2015-01-31 11:09:02 +01:00
|
|
|
*r = v
|
2015-01-29 11:35:15 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-07-29 21:29:41 +02:00
|
|
|
//Scan an input string into this structure for use with GORP
|
|
|
|
func (r *{{$typename}}) Scan(i interface{}) error {
|
|
|
|
switch i.(type) {
|
|
|
|
case string:
|
|
|
|
r.UnmarshalJSON([]byte(i.(string)))
|
|
|
|
default:
|
|
|
|
return errors.Errorf("Can't scan %T into type %T", i, r)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r {{$typename}}) Value() (driver.Value, error) {
|
|
|
|
bytes, err := r.MarshalJSON()
|
|
|
|
return string(bytes), err
|
|
|
|
}
|
|
|
|
|
2015-01-29 11:35:15 +01:00
|
|
|
{{end}}
|
|
|
|
`))
|