forked from goadesign/goa
/
finalizer.go
177 lines (166 loc) · 5.04 KB
/
finalizer.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package codegen
import (
"bytes"
"fmt"
"text/template"
"github.com/goadesign/goa/design"
)
// Finalizer is the code generator for the 'Finalize' type methods.
type Finalizer struct {
assignmentT *template.Template
arrayAssignmentT *template.Template
seen map[*design.AttributeDefinition]map[*design.AttributeDefinition]*bytes.Buffer
}
// NewFinalizer instantiates a finalize code generator.
func NewFinalizer() *Finalizer {
var (
f = &Finalizer{seen: make(map[*design.AttributeDefinition]map[*design.AttributeDefinition]*bytes.Buffer)}
err error
)
fm := template.FuncMap{
"tabs": Tabs,
"goify": Goify,
"gotyperef": GoTypeRef,
"add": Add,
"finalizeCode": f.Code,
}
f.assignmentT, err = template.New("assignment").Funcs(fm).Parse(assignmentTmpl)
if err != nil {
panic(err)
}
f.arrayAssignmentT, err = template.New("arrAssignment").Funcs(fm).Parse(arrayAssignmentTmpl)
if err != nil {
panic(err)
}
return f
}
// Code produces Go code that sets the default values for fields recursively for the given
// attribute.
func (f *Finalizer) Code(att *design.AttributeDefinition, target string, depth int) string {
buf := f.recurse(att, att, target, depth)
return buf.String()
}
func (f *Finalizer) recurse(root, att *design.AttributeDefinition, target string, depth int) *bytes.Buffer {
var (
buf = new(bytes.Buffer)
first = true
)
if s, ok := f.seen[root]; ok {
if buf, ok := s[att]; ok {
return buf
}
s[att] = buf
} else {
f.seen[root] = map[*design.AttributeDefinition]*bytes.Buffer{att: buf}
}
if o := att.Type.ToObject(); o != nil {
o.IterateAttributes(func(n string, catt *design.AttributeDefinition) error {
if att.HasDefaultValue(n) {
data := map[string]interface{}{
"target": target,
"field": n,
"catt": catt,
"depth": depth,
"isDatetime": catt.Type == design.DateTime,
"defaultVal": PrintVal(catt.Type, catt.DefaultValue),
}
if !first {
buf.WriteByte('\n')
} else {
first = false
}
buf.WriteString(RunTemplate(f.assignmentT, data))
}
a := f.recurse(root, catt, fmt.Sprintf("%s.%s", target, Goify(n, true)), depth+1).String()
if a != "" {
if catt.Type.IsObject() {
a = fmt.Sprintf("%sif %s.%s != nil {\n%s\n%s}",
Tabs(depth), target, Goify(n, true), a, Tabs(depth))
}
if !first {
buf.WriteByte('\n')
} else {
first = false
}
buf.WriteString(a)
}
return nil
})
} else if a := att.Type.ToArray(); a != nil {
data := map[string]interface{}{
"elemType": a.ElemType,
"target": target,
"depth": 1,
}
if as := RunTemplate(f.arrayAssignmentT, data); as != "" {
buf.WriteString(as)
}
}
return buf
}
// PrintVal prints the given value corresponding to the given data type.
// The value is already checked for the compatibility with the data type.
func PrintVal(t design.DataType, val interface{}) string {
switch {
case t.IsPrimitive():
// For primitive types, simply print the value
s := fmt.Sprintf("%#v", val)
switch t {
case design.Number:
v := val
if i, ok := val.(int); ok {
v = float64(i)
}
s = fmt.Sprintf("%f", v)
case design.DateTime:
s = fmt.Sprintf("time.Parse(time.RFC3339, %s)", s)
}
return s
case t.IsHash():
// The input is a hash
h := t.ToHash()
hval := val.(map[interface{}]interface{})
if len(hval) == 0 {
return fmt.Sprintf("%s{}", GoTypeName(t, nil, 0, false))
}
var buffer bytes.Buffer
buffer.WriteString(fmt.Sprintf("%s{", GoTypeName(t, nil, 0, false)))
for k, v := range hval {
buffer.WriteString(fmt.Sprintf("%s: %s, ", PrintVal(h.KeyType.Type, k), PrintVal(h.ElemType.Type, v)))
}
buffer.Truncate(buffer.Len() - 2) // remove ", "
buffer.WriteString("}")
return buffer.String()
case t.IsArray():
// Input is an array
a := t.ToArray()
aval := val.([]interface{})
if len(aval) == 0 {
return fmt.Sprintf("%s{}", GoTypeName(t, nil, 0, false))
}
var buffer bytes.Buffer
buffer.WriteString(fmt.Sprintf("%s{", GoTypeName(t, nil, 0, false)))
for _, e := range aval {
buffer.WriteString(fmt.Sprintf("%s, ", PrintVal(a.ElemType.Type, e)))
}
buffer.Truncate(buffer.Len() - 2) // remove ", "
buffer.WriteString("}")
return buffer.String()
default:
// shouldn't happen as the value's compatibility is already checked.
panic("unknown type")
}
}
const (
assignmentTmpl = `{{ if .catt.Type.IsPrimitive }}{{ $defaultName := (print "default" (goify .field true)) }}{{/*
*/}}{{ tabs .depth }}var {{ $defaultName }}{{if .isDatetime}}, _{{end}} = {{ .defaultVal }}
{{ tabs .depth }}if {{ .target }}.{{ goify .field true }} == nil {
{{ tabs .depth }} {{ .target }}.{{ goify .field true }} = &{{ $defaultName }}
}{{ else }}{{ tabs .depth }}if {{ .target }}.{{ goify .field true }} == nil {
{{ tabs .depth }} {{ .target }}.{{ goify .field true }} = {{ .defaultVal }}
}{{ end }}`
arrayAssignmentTmpl = `{{ $a := finalizeCode .elemType "e" (add .depth 1) }}{{/*
*/}}{{ if $a }}{{ tabs .depth }}for _, e := range {{ .target }} {
{{ $a }}
{{ tabs .depth }}}{{ end }}`
)