forked from campoy/jsonenums
/
template.go
123 lines (103 loc) · 3.16 KB
/
template.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Copyright 2017 Google Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to writing, software distributed
// under the License is distributed on a "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied.
//
// See the License for the specific language governing permissions and
// limitations under the License.
// 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(`
// Code generated by jsonenums {{.Command}}; DO NOT EDIT.
package {{.PackageName}}
import (
"database/sql/driver"
"encoding/json"
"fmt"
)
{{range $typename, $values := .TypesAndValues}}
var (
_{{$typename}}NameToValue = map[string]{{$typename}} {
{{range $values}}"{{.}}": {{.}},
{{end}}
}
_{{$typename}}ValueToName = map[{{$typename}}]string {
{{range $values}}{{.}}: "{{.}}",
{{end}}
}
)
func init() {
var v {{$typename}}
if _, ok := interface{}(v).(fmt.Stringer); ok {
_{{$typename}}NameToValue = map[string]{{$typename}} {
{{range $values}}interface{}({{.}}).(fmt.Stringer).String(): {{.}},
{{end}}
}
}
}
func (r {{$typename}}) toString() (string, error) {
s, ok := _{{$typename}}ValueToName[r]
if !ok {
return "", fmt.Errorf("invalid {{$typename}}: %d", r)
}
return s, nil
}
func (r *{{$typename}}) setValue(str string) error {
v, ok := _{{$typename}}NameToValue[str]
if !ok {
return fmt.Errorf("string %s value is not type of {{$typename}}", str)
}
*r = v
return nil
}
// Scan - Implement the database/sql scanner interface
func (r *{{$typename}}) Scan(value interface{}) error {
if value == nil {
return nil
}
switch data := value.(type) {
case string:
return r.setValue(data)
case []byte:
return r.setValue(string(data[:]))
default:
return fmt.Errorf("can't scan %T into type %T", value, r)
}
return nil
}
// Value - Implementation of valuer for database/sql
func (r {{$typename}}) Value() (driver.Value, error) {
return r.toString()
}
// MarshalJSON is generated so {{$typename}} satisfies json.Marshaler.
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)
}
// UnmarshalJSON is generated so {{$typename}} satisfies json.Unmarshaler.
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)
}
v, ok := _{{$typename}}NameToValue[s]
if !ok {
return fmt.Errorf("invalid {{$typename}} %q", s)
}
*r = v
return nil
}
{{end}}
`))