Update template to include DB functions and correct json format as per

snake case names
This commit is contained in:
BenTrapani 2016-07-29 15:29:41 -04:00
parent 67c61e436f
commit 8ec93328be

View File

@ -14,20 +14,23 @@ var generatedTmpl = template.Must(template.New("generated").Parse(`
package {{.PackageName}} package {{.PackageName}}
import ( import (
"database/sql/driver"
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/pkg/errors"
) )
{{range $typename, $values := .TypesAndValues}} {{range $typename, $values := .TypesAndValues}}
var ( var (
_{{$typename}}NameToValue = map[string]{{$typename}} { _{{$typename}}NameToValue = map[string]{{$typename}} {
{{range $values}}"{{.}}": {{.}}, {{range $values}}"{{.SnakeRep}}": {{.CammelRep}},
{{end}} {{end}}
} }
_{{$typename}}ValueToName = map[{{$typename}}]string { _{{$typename}}ValueToName = map[{{$typename}}]string {
{{range $values}}{{.}}: "{{.}}", {{range $values}}{{.CammelRep}}: "{{.SnakeRep}}",
{{end}} {{end}}
} }
) )
@ -36,7 +39,7 @@ func init() {
var v {{$typename}} var v {{$typename}}
if _, ok := interface{}(v).(fmt.Stringer); ok { if _, ok := interface{}(v).(fmt.Stringer); ok {
_{{$typename}}NameToValue = map[string]{{$typename}} { _{{$typename}}NameToValue = map[string]{{$typename}} {
{{range $values}}interface{}({{.}}).(fmt.Stringer).String(): {{.}}, {{range $values}}interface{}({{.CammelRep}}).(fmt.Stringer).String(): {{.CammelRep}},
{{end}} {{end}}
} }
} }
@ -68,5 +71,21 @@ func (r *{{$typename}}) UnmarshalJSON(data []byte) error {
return nil return nil
} }
//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
}
{{end}} {{end}}
`)) `))