-
Notifications
You must be signed in to change notification settings - Fork 11
/
binder_toform.go
270 lines (260 loc) · 7.03 KB
/
binder_toform.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package echo
import (
"fmt"
"reflect"
"strconv"
"strings"
"time"
"github.com/webx-top/echo/engine"
"github.com/webx-top/echo/param"
"github.com/webx-top/tagfast"
)
func SetFormValue(f engine.URLValuer, fName string, index int, value interface{}) {
if index == 0 {
f.Set(fName, fmt.Sprint(value))
} else {
f.Add(fName, fmt.Sprint(value))
}
}
func SetFormValues(f engine.URLValuer, fName string, values []string) {
for index, value := range values {
if index == 0 {
f.Set(fName, fmt.Sprint(value))
} else {
f.Add(fName, fmt.Sprint(value))
}
}
}
// FlatStructToForm 映射struct到form
func FlatStructToForm(ctx Context, m interface{}, fieldNameFormatter FieldNameFormatter, formatters ...param.StringerMap) {
StructToForm(ctx, m, ``, fieldNameFormatter, formatters...)
}
func (e *Echo) binderValueEncode(name string, typev reflect.Type, tv reflect.Value) ([]string, error) {
f, _ := typev.FieldByName(name)
encoder := tagfast.Value(typev, f, `form_encoder`)
if len(encoder) == 0 {
return nil, ErrNotImplemented
}
parts := strings.SplitN(encoder, `:`, 2)
encoder = parts[0]
var params string
if len(parts) == 2 {
params = parts[1]
}
// ErrNotImplemented
return e.CallBinderValueEncoder(encoder, name, tv.Interface(), params)
}
// StructToForm 映射struct到form
func StructToForm(ctx Context, m interface{}, topName string, fieldNameFormatter FieldNameFormatter, formatters ...param.StringerMap) {
var stringers param.StringerMap // 这里的 key 为表单字段 name 属性值
if len(formatters) > 0 {
stringers = formatters[0]
}
if fieldNameFormatter == nil {
if g, y := m.(FormNameFormatterGetter); y {
fieldNameFormatter = g.FormNameFormatter(ctx)
}
if fieldNameFormatter == nil {
fieldNameFormatter = DefaultFieldNameFormatter
}
}
var valueEncoders BinderValueCustomEncoders
if g, y := m.(ValueEncodersGetter); y {
valueEncoders = g.ValueEncoders(ctx)
}
if valueEncoders == nil {
valueEncoders = BinderValueCustomEncoders{}
}
if stringers == nil {
if g, y := m.(ValueStringersGetter); y {
stringers = g.ValueStringers(ctx)
}
}
for k, f := range stringers {
valueEncoders[k] = FormStringer(f)
}
structToForm(ctx, m, topName, fieldNameFormatter, valueEncoders)
}
func structToForm(ctx Context, m interface{}, topName string, fieldNameFormatter FieldNameFormatter, valueEncoders BinderValueCustomEncoders) {
vc := reflect.ValueOf(m)
tc := reflect.TypeOf(m)
if tc.Kind() == reflect.Ptr {
tc = tc.Elem()
if vc.IsNil() {
return
}
vc = vc.Elem()
}
switch tc.Kind() {
case reflect.Struct:
case reflect.Map:
for _, srcKey := range vc.MapKeys() {
srcVal := vc.MapIndex(srcKey)
if !srcVal.CanInterface() || srcVal.Interface() == nil {
continue
}
key := fieldNameFormatter(topName, srcKey.String())
structToForm(ctx, srcVal.Interface(), key, fieldNameFormatter, valueEncoders)
}
return
case reflect.Array, reflect.Slice:
elems := vc.Len()
for i := 0; i < elems; i++ {
srcVal := vc.Index(i)
if !srcVal.CanInterface() || srcVal.Interface() == nil {
continue
}
iStr := strconv.Itoa(i)
key := fieldNameFormatter(topName, iStr)
structToForm(ctx, srcVal.Interface(), key, fieldNameFormatter, valueEncoders)
}
case reflect.Bool,
reflect.Float32, reflect.Float64,
reflect.Uint, reflect.Uint16, reflect.Uint32, reflect.Uint64,
reflect.Int, reflect.Int16, reflect.Int32, reflect.Int64, reflect.String:
if vc.CanInterface() && vc.Interface() != nil {
ctx.Request().Form().Set(topName, fmt.Sprint(vc.Interface()))
}
return
default:
//fieldToForm(ctx, tc, reflect.StructField{}, vc, topName, fieldNameFormatter, formatter)
//fmt.Printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~>%T\n", m)
return
}
for i, l := 0, tc.NumField(); i < l; i++ {
fVal := vc.Field(i)
fStruct := tc.Field(i)
if fStruct.Anonymous {
if fVal.CanInterface() {
structToForm(ctx, fVal.Interface(), topName, fieldNameFormatter, valueEncoders)
}
continue
}
err := fieldToForm(ctx, tc, fStruct, fVal, topName, fieldNameFormatter, valueEncoders)
if err != nil {
fpath := fStruct.Name
if len(topName) > 0 {
fpath = topName + `.` + fpath
}
ctx.Logger().Warnf(`[StructToForm] %s: %v`, fpath, err)
}
}
}
func fieldToForm(ctx Context, parentTyp reflect.Type, fStruct reflect.StructField, fVal reflect.Value, topName string, fieldNameFormatter FieldNameFormatter, valueEncoders BinderValueCustomEncoders) error {
f := ctx.Request().Form()
fName := fieldNameFormatter(topName, fStruct.Name)
if !fVal.CanInterface() || len(fName) == 0 {
return nil
}
if valueEncoders != nil {
encoder, ok := valueEncoders[fName]
if ok {
values := encoder(fVal.Interface())
if len(values) == 0 {
return nil
}
SetFormValues(f, fName, values)
return nil
}
}
if parentTyp.Kind() == reflect.Struct {
values, err := ctx.Echo().binderValueEncode(fStruct.Name, parentTyp, fVal)
if err != nil {
if err != ErrNotImplemented {
return err
}
} else {
SetFormValues(f, fName, values)
return nil
}
}
switch fVal.Type().String() {
case `time.Time`:
if t, y := fVal.Interface().(time.Time); y {
if t.IsZero() {
f.Set(fName, ``)
} else {
dateformat := tagfast.Value(parentTyp, fStruct, `form_format`)
if len(dateformat) > 0 {
f.Set(fName, t.Format(dateformat))
} else {
f.Set(fName, t.Format(`2006-01-02 15:04:05`))
}
}
}
case `time.Duration`:
if t, y := fVal.Interface().(time.Duration); y {
f.Set(fName, t.String())
}
default:
switch fVal.Type().Kind() {
case reflect.Struct:
structToForm(ctx, fVal.Interface(), fName, fieldNameFormatter, valueEncoders)
case reflect.Ptr:
structToForm(ctx, fVal.Interface(), fName, fieldNameFormatter, valueEncoders)
case reflect.Slice:
switch sl := fVal.Interface().(type) {
case []uint:
for k, v := range sl {
SetFormValue(f, fName, k, v)
}
case []uint16:
for k, v := range sl {
SetFormValue(f, fName, k, v)
}
case []uint32:
for k, v := range sl {
SetFormValue(f, fName, k, v)
}
case []uint64:
for k, v := range sl {
SetFormValue(f, fName, k, v)
}
case []int:
for k, v := range sl {
SetFormValue(f, fName, k, v)
}
case []int16:
for k, v := range sl {
SetFormValue(f, fName, k, v)
}
case []int32:
for k, v := range sl {
SetFormValue(f, fName, k, v)
}
case []int64:
for k, v := range sl {
SetFormValue(f, fName, k, v)
}
case []float32:
for k, v := range sl {
SetFormValue(f, fName, k, v)
}
case []float64:
for k, v := range sl {
SetFormValue(f, fName, k, v)
}
case []string:
for k, v := range sl {
SetFormValue(f, fName, k, v)
}
case []interface{}:
for k, v := range sl {
SetFormValue(f, fName, k, v)
}
default:
// ignore
}
case reflect.Map:
structToForm(ctx, fVal.Interface(), fName, fieldNameFormatter, valueEncoders)
default:
switch v := fVal.Interface().(type) {
case ToConversion:
f.Set(fName, v.ToString())
default:
f.Set(fName, fmt.Sprint(v))
}
}
}
return nil
}