This repository has been archived by the owner on May 29, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
render.go
202 lines (169 loc) · 3.78 KB
/
render.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package form
import (
"fmt"
"io"
"mime/multipart"
"reflect"
"time"
)
type renderValue struct {
form *form
name string
preferedName string
fieldsFns FieldFuncs
_type TypeCode
fls *FirstLayerStack
}
func (f *form) render(structPtr Interface, w io.Writer) {
var preStructPtr interface{} = structPtr
if v, ok := structPtr.(Hijacker); ok {
preStructPtr = v.CJStructPtr()
}
t := reflect.TypeOf(preStructPtr)
vc := reflect.ValueOf(preStructPtr)
switch {
case isStructPtr(t):
t = t.Elem()
vc = vc.Elem()
default:
panic(fmt.Errorf("form: '%p' is not a struct pointer", preStructPtr))
}
data := f.Data[structPtr]
fields := Fields{
map[string]FieldFuncs{},
nil,
nil,
[]*Field{},
}
structPtr.CJForm(&fields)
for _, afield := range fields.f {
name := afield.name
fieldFns := afield.funcs
_, exist := t.FieldByName(name)
if !exist {
panic(fmt.Errorf("form: '%s' field does not exist", name))
}
preferedName := name
field := vc.FieldByName(name)
if !field.CanSet() {
panic(fmt.Errorf("form: '%s' field cannot be set", name))
}
_type := Invalid
fieldFns.Call("init", map[string]interface{}{
"type": &_type,
})
fieldFns.Call("name", map[string]interface{}{
"name": &preferedName,
})
if _type <= Invalid || _type >= terminate {
continue
}
// First Layer
r := renderValue{f, name, preferedName, fieldFns, _type, &FirstLayerStack{}}
switch value := field.Interface().(type) {
case string:
r.str(value)
case []string:
r.strs(value)
case int64:
r.wnum(value)
case []int64:
r.wnums(value)
case float64:
r.fnum(value)
case []float64:
r.fnums(value)
case bool:
r.b(value)
case time.Time:
r.time(value)
case *multipart.FileHeader:
r.file()
default:
continue
}
// Second Layer
var err error
warning := ""
if data != nil {
err = data.shiftError(r.name)
warning = data.shiftWarning(r.name)
}
secondLayerData := RenderData{preferedName, f.rcount, _type, err, warning,
fieldFns, data != nil, *(r.fls)}
f.rcount++
f.R.Render(w, secondLayerData)
}
}
func (r renderValue) str(value string) {
switch r._type {
case InputText, InputSearch, InputPassword, InputHidden, InputUrl, InputTel:
r.strInputText(value)
case InputEmail:
r.strInputEmail(value)
case InputRadio:
r.strInputRadio(value)
case InputColor:
r.strInputColor(value)
case Textarea:
r.strTextarea(value)
case Select:
r.strSelect(value)
}
}
func (r renderValue) strs(values []string) {
switch r._type {
case Select:
r.strsSelect(values)
}
}
func (r renderValue) wnum(value int64) {
switch r._type {
case InputNumber, InputRange, InputHidden:
r.numInputNumber(value)
case InputRadio:
r.wnumInputRadio(value)
case Select:
r.wnumSelect(value)
}
}
func (r renderValue) wnums(values []int64) {
switch r._type {
case Select:
r.wnumsSelect(values)
}
}
func (r renderValue) fnum(value float64) {
switch r._type {
case InputNumber, InputRange, InputHidden:
r.numInputNumber(value)
case InputRadio:
r.fnumInputRadio(value)
case Select:
r.fnumSelect(value)
}
}
func (r renderValue) fnums(values []float64) {
switch r._type {
case Select:
r.fnumsSelect(values)
}
}
func (r renderValue) b(value bool) {
switch r._type {
case InputCheckbox, InputHidden:
r.bInputCheckbox(value)
}
}
func (r renderValue) time(value time.Time) {
switch r._type {
case InputDatetime, InputDatetimeLocal, InputTime, InputDate, InputMonth, InputWeek:
r.timeInputTime(value)
}
}
func (r renderValue) file() {
switch r._type {
case InputFile:
r.fileInputFile()
}
}