-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsoner.go
85 lines (69 loc) · 1.72 KB
/
jsoner.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
package main
import "fmt"
const errStrToValueMap = `func %[1]sString(s string) (%[1]s, error) {
if val, ok := _%[1]sNameToValue_map[s]; ok {
return val, nil
}
return 0, fmt.Errorf("%%s is not the name of type %[1]s")
}
`
// adapted from github.com/alvaroloes/enumer
func (g *Generator) buildErrStrToValueMap(runs [][]Value, typeName string) {
var n int
var runID string
// called after Stringer and Error are in the buffer
g.Printf("\nvar _%sNameToValue_map = map[string]%s{\n", typeName, typeName)
hasRuns := len(runs) > 1 && len(runs) <= 10
for i, values := range runs {
if hasRuns {
runID = fmt.Sprintf("_%d", i)
n = 0
} else {
runID = ""
}
for _, value := range values {
g.Printf("\t_%s_name%s[%d:%d]: %s,\n", typeName, runID, n, n+len(value.name), &value)
n += len(value.name)
}
}
g.Printf("}\n\n")
// now build our function
g.Printf(errStrToValueMap, typeName)
}
// Arguments:
// [1]: type name
const jsonMethods = `
func (i %[1]s) MarshalJSON() ([]byte, error) {
b := new(bytes.Buffer)
msg, err := json.Marshal(i.Error())
if err != nil {
return b.Bytes(), err
}
name, err := json.Marshal(i.String())
if err != nil {
return b.Bytes(), err
}
json := fmt.Sprintf("{\"type\":%%s,\"message\":%%s}", name, msg)
b.WriteString(json)
return b.Bytes(), nil
}
type errStruct struct {
name string
message string
}
func (i *%[1]s) UnmarshalJSON(data []byte) error {
var errData errStruct
if err := json.Unmarshal(data, &errData); err != nil {
return fmt.Errorf("Expecting a string, got %%s", data)
}
val, err := %[1]sString(errData.name)
if err != nil {
return err
}
*i = val
return nil
}
`
func (g *Generator) buildJsonMethods(typeName string) {
g.Printf(jsonMethods, typeName)
}