-
Notifications
You must be signed in to change notification settings - Fork 0
/
m2s.go
187 lines (165 loc) · 4.75 KB
/
m2s.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
package m2s
import (
"cmp"
"encoding"
"encoding/json"
"mime/multipart"
"reflect"
"strconv"
"unsafe"
)
type fieldType uint
const (
value fieldType = iota
file
files
)
func Convert(mpf *multipart.Form, v any) error {
rv := reflect.ValueOf(v)
err := validate(rv)
if err != nil {
return err
}
rv = rv.Elem()
for i := range rv.NumField() {
fieldType := rv.Type().Field(i)
fieldValue := rv.Field(i)
tag := fieldType.Tag.Get("form")
if !fieldType.IsExported() || tag == "-" {
continue // Skip if struct field is unexported or ignored (-)
}
fieldName := cmp.Or(tag, fieldType.Name)
ft := determineFieldType(fieldType.Type)
if ft == file || ft == files {
formFiles, ok := mpf.File[fieldName]
if !ok || len(formFiles) == 0 {
continue
}
// if single file
if ft == file {
setFile(fieldType.Type, fieldValue, formFiles[0])
continue
}
// if multiple files
setFiles(formFiles, fieldType.Type, fieldValue)
continue
}
formValues, ok := mpf.Value[fieldName]
if !ok || len(formValues) == 0 {
continue
}
// if value
err = convertValue(fieldType.Type, fieldValue, fieldType.Name, cmp.Or(formValues...))
if err != nil {
return err
}
}
return nil
}
func setFile(fieldType reflect.Type, fieldValue reflect.Value, formFile *multipart.FileHeader) {
if fieldType.Kind() != reflect.Pointer {
fieldValue.Set(reflect.ValueOf(formFile).Elem())
return
}
fieldValue.Set(reflect.ValueOf(formFile))
}
func setFiles(formFiles []*multipart.FileHeader, fieldType reflect.Type, fieldValue reflect.Value) {
list := reflect.MakeSlice(fieldType, 0, len(formFiles))
for i := range formFiles {
v := reflect.New(reflect.TypeFor[multipart.FileHeader]())
setFile(v.Type().Elem(), v.Elem(), formFiles[i])
if fieldType.Elem().Kind() == reflect.Pointer {
list = reflect.Append(list, v)
} else {
list = reflect.Append(list, v.Elem())
}
}
fieldValue.Set(list)
}
func convertValue(fieldType reflect.Type, fieldValue reflect.Value, fieldName, formValue string) error {
// if implements encoding.TextUnmarshaler
if reflect.PointerTo(fieldType).Implements(reflect.TypeFor[encoding.TextUnmarshaler]()) {
ptrVal := reflect.New(fieldType)
result := ptrVal.MethodByName("UnmarshalText").
Call([]reflect.Value{reflect.ValueOf(string2ByteSlice(formValue))})
if !result[0].IsNil() {
return ErrParseFailed{fieldName, result[0].Interface().(error)}
}
fieldValue.Set(ptrVal.Elem())
return nil
}
switch fieldType.Kind() {
case reflect.Pointer:
v := reflect.New(fieldType.Elem())
err := convertValue(fieldType.Elem(), v.Elem(), fieldName, formValue)
if err != nil {
return err
}
fieldValue.Set(v)
case reflect.String:
fieldValue.SetString(formValue)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
v, err := strconv.ParseInt(formValue, 10, 64)
if err != nil {
return ErrParseFailed{Field: fieldName, Err: err}
}
fieldValue.SetInt(v)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
v, err := strconv.ParseUint(formValue, 10, 64)
if err != nil {
return ErrParseFailed{Field: fieldName, Err: err}
}
fieldValue.SetUint(v)
case reflect.Float32, reflect.Float64:
v, err := strconv.ParseFloat(formValue, 64)
if err != nil {
return ErrParseFailed{Field: fieldName, Err: err}
}
fieldValue.SetFloat(v)
case reflect.Bool:
v, err := strconv.ParseBool(formValue)
if err != nil {
return ErrParseFailed{Field: fieldName, Err: err}
}
fieldValue.SetBool(v)
case reflect.Complex64, reflect.Complex128:
v, err := strconv.ParseComplex(formValue, 64)
if err != nil {
return ErrParseFailed{Field: fieldName, Err: err}
}
fieldValue.SetComplex(v)
case reflect.Struct, reflect.Slice, reflect.Map:
err := json.Unmarshal(string2ByteSlice(formValue), fieldValue.Addr().Interface())
if err != nil {
return ErrParseFailed{Field: fieldName, Err: err}
}
default:
return ErrInvalidFieldType
}
return nil
}
func validate(rv reflect.Value) error {
if rv.Kind() != reflect.Ptr {
return ErrValueMustBePointer
}
if rv.IsNil() {
return ErrValueCannotBeNil
}
if rv.Elem().Kind() != reflect.Struct {
return ErrValueMustBeStruct
}
return nil
}
func determineFieldType(rt reflect.Type) fieldType {
if rt.Kind() == reflect.Pointer && rt.Elem() == reflect.TypeFor[multipart.FileHeader]() ||
rt == reflect.TypeFor[multipart.FileHeader]() {
return file
} else if rt.Kind() == reflect.Slice && rt.Elem().Kind() == reflect.Pointer && rt.Elem().Elem() == reflect.TypeFor[multipart.FileHeader]() || // []*multipart.File
rt.Kind() == reflect.Slice && rt.Elem() == reflect.TypeFor[multipart.FileHeader]() {
return files
}
return value
}
func string2ByteSlice(s string) []byte {
return unsafe.Slice(unsafe.StringData(s), len(s))
}