-
Notifications
You must be signed in to change notification settings - Fork 116
/
data_source.go
963 lines (824 loc) · 23.8 KB
/
data_source.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
package validate
import (
"encoding/json"
"fmt"
"io"
"mime/multipart"
"net/http"
"net/url"
"reflect"
"strconv"
"strings"
"time"
"github.com/gookit/filter"
"github.com/gookit/goutil/maputil"
"github.com/gookit/goutil/reflects"
"github.com/gookit/goutil/strutil"
)
const (
// from user setting, unmarshal JSON
sourceMap uint8 = iota + 1
// from URL.Values, PostForm. contains Files data
sourceForm
// from user setting
sourceStruct
)
// 0: top level field
// 1: field at anonymous struct
// 2: field at non-anonymous struct
const (
fieldAtTopStruct int8 = iota
fieldAtAnonymous
fieldAtSubStruct
)
// data (Un)marshal func
var (
Marshal MarshalFunc = json.Marshal
Unmarshal UnmarshalFunc = json.Unmarshal
)
type (
// MarshalFunc define
MarshalFunc func(v any) ([]byte, error)
// UnmarshalFunc define
UnmarshalFunc func(data []byte, ptr any) error
)
// DataFace data source interface definition
//
// Current has three data source:
// - map
// - form
// - struct
type DataFace interface {
Type() uint8
Src() any
Get(key string) (val any, exist bool)
// TryGet value by key.
// if source data is struct, will return zero check
TryGet(key string) (val any, exist, zero bool)
Set(field string, val any) (any, error)
// Create validation instance create func
Create(err ...error) *Validation
Validation(err ...error) *Validation
}
/*************************************************************
* Map Data
*************************************************************/
// MapData definition
type MapData struct {
// Map the source map data
Map map[string]any
// from reflect Map
value reflect.Value
// bodyJSON from the original JSON bytes/string.
// available for FromJSONBytes(), FormJSON().
bodyJSON []byte
// TODO map field value cache by key path
// cache map[string]any
}
/*************************************************************
* Map data operate
*************************************************************/
// Src get
func (d *MapData) Src() any {
return d.Map
}
// Type get
func (d *MapData) Type() uint8 {
return sourceMap
}
// Set value by key
func (d *MapData) Set(field string, val any) (any, error) {
d.Map[field] = val
return val, nil
}
// Get value by key. support get value by path.
func (d *MapData) Get(field string) (any, bool) {
// if fv, ok := d.fields[field]; ok {
// return fv, true
// }
return maputil.GetByPath(field, d.Map)
}
// TryGet value by key
func (d *MapData) TryGet(field string) (val any, exist, zero bool) {
val, exist = maputil.GetByPath(field, d.Map)
return
}
// Create a Validation from data
func (d *MapData) Create(err ...error) *Validation {
return d.Validation(err...)
}
// Validation create from data
func (d *MapData) Validation(err ...error) *Validation {
if len(err) > 0 {
return NewValidation(d).WithError(err[0])
}
return NewValidation(d)
}
// BindJSON binds v to the JSON data in the request body.
// It calls json.Unmarshal and sets the value of v.
func (d *MapData) BindJSON(ptr any) error {
if len(d.bodyJSON) == 0 {
return nil
}
return Unmarshal(d.bodyJSON, ptr)
}
/*************************************************************
* Struct Data
*************************************************************/
// ConfigValidationFace definition. you can do something on create Validation.
type ConfigValidationFace interface {
ConfigValidation(v *Validation)
}
// FieldTranslatorFace definition. you can custom field translates.
// Usage:
//
// type User struct {
// Name string `json:"name" validate:"required|minLen:5"`
// }
//
// func (u *User) Translates() map[string]string {
// return MS{
// "Name": "Username",
// }
// }
type FieldTranslatorFace interface {
Translates() map[string]string
}
// CustomMessagesFace definition. you can custom validator error messages.
// Usage:
//
// type User struct {
// Name string `json:"name" validate:"required|minLen:5"`
// }
//
// func (u *User) Messages() map[string]string {
// return MS{
// "Name.required": "oh! User name is required",
// }
// }
type CustomMessagesFace interface {
Messages() map[string]string
}
// StructData definition.
//
// more struct tags define please see GlobalOption
type StructData struct {
// source struct data, from user setting
src any
// max depth for parse sub-struct. TODO WIP ...
// depth int
// reflect.Value of the source struct
value reflect.Value
// reflect.Type of the source struct
valueTyp reflect.Type
// field names in the src struct
// 0:common field 1:anonymous field 2:nonAnonymous field
fieldNames map[string]int8
// cache field reflect value info. key is path. eg: top.sub
fieldValues map[string]reflect.Value
// TODO field reflect values cache
// fieldRftValues map[string]any
// FilterTag name in the struct tags.
//
// see GlobalOption.FilterTag
FilterTag string
// ValidateTag name in the struct tags.
//
// see GlobalOption.ValidateTag
ValidateTag string
}
// StructOption definition
// type StructOption struct {
// // ValidateTag in the struct tags.
// ValidateTag string
// // MethodName string
// }
var (
cmFaceType = reflect.TypeOf(new(CustomMessagesFace)).Elem()
ftFaceType = reflect.TypeOf(new(FieldTranslatorFace)).Elem()
cvFaceType = reflect.TypeOf(new(ConfigValidationFace)).Elem()
timeType = reflect.TypeOf(time.Time{})
)
// Src get
func (d *StructData) Src() any {
return d.src
}
// Type get
func (d *StructData) Type() uint8 {
return sourceStruct
}
// Validation create a Validation from the StructData
func (d *StructData) Validation(err ...error) *Validation {
return d.Create(err...)
}
// Create from the StructData
//
//nolint:forcetypeassert
func (d *StructData) Create(err ...error) *Validation {
v := NewValidation(d)
if len(err) > 0 && err[0] != nil {
return v.WithError(err[0])
}
// collect field filter/validate rules from struct tags
d.parseRulesFromTag(v)
// has custom config func
if d.valueTyp.Implements(cvFaceType) {
fv := d.value.MethodByName("ConfigValidation")
fv.Call([]reflect.Value{reflect.ValueOf(v)})
}
// collect custom field translates config
if d.valueTyp.Implements(ftFaceType) {
fv := d.value.MethodByName("Translates")
vs := fv.Call(nil)
v.WithTranslates(vs[0].Interface().(map[string]string))
}
// collect custom error messages config
// if reflect.PtrTo(d.valueTyp).Implements(cmFaceType) {
if d.valueTyp.Implements(cmFaceType) {
fv := d.value.MethodByName("Messages")
vs := fv.Call(nil)
v.WithMessages(vs[0].Interface().(map[string]string))
}
// for struct, default update source value
v.UpdateSource = true
return v
}
// parse and collect rules from struct tags.
func (d *StructData) parseRulesFromTag(v *Validation) {
if d.ValidateTag == "" {
d.ValidateTag = gOpt.ValidateTag
}
if d.FilterTag == "" {
d.FilterTag = gOpt.FilterTag
}
fOutMap := make(map[string]string)
var recursiveFunc func(vv reflect.Value, vt reflect.Type, preStrName string, parentIsAnonymous bool)
vv := d.value
vt := d.valueTyp
// preStrName - the parent field name.
recursiveFunc = func(vv reflect.Value, vt reflect.Type, parentFName string, parentIsAnonymous bool) {
for i := 0; i < vt.NumField(); i++ {
fv := vt.Field(i)
// skip don't exported field
name := fv.Name
if name[0] >= 'a' && name[0] <= 'z' {
if !gOpt.ValidatePrivateFields {
continue
}
}
if parentFName == "" {
d.fieldNames[name] = fieldAtTopStruct
} else {
name = parentFName + "." + name
if parentIsAnonymous {
d.fieldNames[name] = fieldAtAnonymous
} else {
d.fieldNames[name] = fieldAtSubStruct
}
}
// validate rule
vRule := fv.Tag.Get(d.ValidateTag)
if vRule != "" {
v.StringRule(name, vRule)
}
// filter rule
fRule := fv.Tag.Get(d.FilterTag)
if fRule != "" {
v.FilterRule(name, fRule)
}
// load field output name by FieldTag. eg: `json:"user_name"`
outName := ""
if gOpt.FieldTag != "" {
outName = fv.Tag.Get(gOpt.FieldTag)
outName = strings.SplitN(outName, ",", 2)[0]
}
// add pre field display name to fName
if outName != "" {
if parentFName != "" {
if pOutName, ok := fOutMap[parentFName]; ok {
outName = pOutName + "." + outName
}
}
fOutMap[name] = outName
}
// load field translate name
// preferred to use label tag name. eg: `label:"display name"`
// and then use field output name. eg: `json:"user_name"`
if gOpt.LabelTag != "" {
v.trans.addLabelName(name, fv.Tag.Get(gOpt.LabelTag))
}
// load custom error messages.
// eg: `message:"required:name is required|minLen:name min len is %d"`
if gOpt.MessageTag != "" {
errMsg := fv.Tag.Get(gOpt.MessageTag)
if errMsg != "" {
d.loadMessagesFromTag(v.trans, name, vRule, errMsg)
}
}
ft := removeTypePtr(vt.Field(i).Type)
// collect rules from sub-struct and from arrays/slices elements
if ft != timeType && removeValuePtr(vv).IsValid() {
// feat: only collect sub-struct rule on current field has rule.
if vRule == "" && gOpt.CheckSubOnParentMarked {
continue
}
fValue := removeValuePtr(vv).Field(i)
switch ft.Kind() {
case reflect.Struct:
recursiveFunc(fValue, ft, name, fv.Anonymous)
case reflect.Array, reflect.Slice:
fValue = removeValuePtr(fValue)
// Check if the reflect.Value is valid and not a nil pointer
if !fValue.IsValid() || fValue.IsNil() {
continue
}
for j := 0; j < fValue.Len(); j++ {
elemValue := removeValuePtr(fValue.Index(j))
elemType := removeTypePtr(elemValue.Type())
arrayName := fmt.Sprintf("%s.%d", name, j)
if elemType.Kind() == reflect.Struct {
recursiveFunc(elemValue, elemType, arrayName, fv.Anonymous)
}
}
case reflect.Map:
fValue = removeValuePtr(fValue)
// Check if the reflect.Value is valid and not a nil pointer
if !fValue.IsValid() || fValue.IsNil() {
continue
}
for _, key := range fValue.MapKeys() {
key = removeValuePtr(key)
elemValue := removeValuePtr(fValue.MapIndex(key))
elemType := removeTypePtr(elemValue.Type())
format := "%s."
kind := key.Kind()
val := key.Interface()
switch {
case kind == reflect.String:
format += "%s"
val = strings.ReplaceAll(val.(string), "\"", "") //nolint:forcetypeassert
case kind >= reflect.Int && kind <= reflect.Uint64:
format += "%d"
case kind >= reflect.Float32 && kind <= reflect.Complex128:
format += "%f"
default:
format += "%#v"
}
arrayName := fmt.Sprintf(format, name, val)
if elemType.Kind() == reflect.Struct {
recursiveFunc(elemValue, elemType, arrayName, fv.Anonymous)
}
}
default:
// do nothing
}
}
}
}
recursiveFunc(removeValuePtr(vv), vt, "", false)
if len(fOutMap) > 0 {
v.Trans().AddFieldMap(fOutMap)
}
}
// eg: `message:"required:name is required|minLen:name min len is %d"`
func (d *StructData) loadMessagesFromTag(trans *Translator, field, vRule, vMsg string) {
var msgKey, vName string
var vNames []string
// only one message, use for first validator.
// eg: `message:"name is required"`
if !strings.ContainsRune(vMsg, '|') {
// eg: `message:"required:name is required"`
if strings.ContainsRune(vMsg, ':') {
nodes := strings.SplitN(vMsg, ":", 2)
vName = strings.TrimSpace(nodes[0])
vNames = []string{vName}
// first is validator name
vMsg = strings.TrimSpace(nodes[1])
}
if vName == "" {
// eg `validate:"required|date"`
vNames = []string{vRule}
if strings.ContainsRune(vRule, '|') {
vNames = strings.Split(vRule, "|")
}
for i, node := range vNames {
// has params for validator: "minLen:5"
if strings.ContainsRune(node, ':') {
tmp := strings.SplitN(node, ":", 2)
vNames[i] = tmp[0]
}
}
}
// if rName, has := validatorAliases[validator]; has {
// msgKey = field + "." + rName
// } else {
for _, name := range vNames {
msgKey = field + "." + name
trans.AddMessage(msgKey, vMsg)
}
return
}
// multi message for validators
// eg: `message:"required:name is required | minLen:name min len is %d"`
for _, validatorWithMsg := range strings.Split(vMsg, "|") {
// validatorWithMsg eg: "required:name is required"
nodes := strings.SplitN(validatorWithMsg, ":", 2)
validator := nodes[0]
msgKey = field + "." + validator
trans.AddMessage(msgKey, strings.TrimSpace(nodes[1]))
}
}
/*************************************************************
* Struct data operate
*************************************************************/
// Get value by field name. support get sub-value by path.
func (d *StructData) Get(field string) (val any, exist bool) {
val, exist, _ = d.TryGet(field)
return
}
// TryGet value by field name. support get sub-value by path.
func (d *StructData) TryGet(field string) (val any, exist, zero bool) {
field = strutil.UpperFirst(field)
// try read from cache
if fv, ok := d.fieldValues[field]; ok {
return fv.Interface(), true, fv.IsZero()
}
// var isPtr bool
var fv reflect.Value
// want to get sub struct field.
if strings.IndexByte(field, '.') > 0 {
fieldNodes := strings.Split(field, ".")
topLevelField, ok := d.valueTyp.FieldByName(fieldNodes[0])
if !ok {
return
}
kind := removeTypePtr(topLevelField.Type).Kind()
if kind != reflect.Struct && kind != reflect.Array && kind != reflect.Slice && kind != reflect.Map {
return
}
fv = removeValuePtr(d.value.FieldByName(fieldNodes[0]))
if !fv.IsValid() {
return
}
fieldNodes = fieldNodes[1:]
// lastIndex := len(fieldNodes) - 1
// last key is wildcard, return all sub-value
if len(fieldNodes) == 1 && fieldNodes[0] == maputil.Wildcard {
return fv.Interface(), true, fv.IsZero()
}
kind = fv.Type().Kind()
for _, fieldNode := range fieldNodes {
// fieldNode = strings.ReplaceAll(fieldNode, "\"", "") // for strings as keys
switch kind {
case reflect.Array, reflect.Slice:
index, _ := strconv.Atoi(fieldNode)
fv = fv.Index(index)
case reflect.Map:
fv = fv.MapIndex(reflect.ValueOf(fieldNode))
case reflect.Struct:
fv = fv.FieldByName(fieldNode)
default: // no sub-value, should never have happened
return
}
// isPtr = fv.Kind() == reflect.Pointer
fv = removeValuePtr(fv)
if !fv.IsValid() {
return
}
kind = fv.Type().Kind()
// if IsZero(fv) || (fv.Kind() == reflect.Ptr && fv.IsNil()) {
if fv.Kind() == reflect.Ptr && fv.IsNil() {
return
}
}
d.fieldNames[field] = fieldAtSubStruct
} else {
// field at top struct
fv = d.value.FieldByName(field)
if !fv.IsValid() { // field not exists
return
}
// is it a pointer
if fv.Kind() == reflect.Pointer {
if fv.IsNil() { // fix: top-field is nil
return
}
// fv = removeValuePtr(fv)
}
}
// check can interface
if fv.CanInterface() {
// TIP: if is zero value, as not exist.
// - bool as exists.
// if fv.Kind() != reflect.Bool && IsZero(fv) {
// return fv.Interface(), false
// // return nil, false
// }
// cache field value info
d.fieldValues[field] = fv
// isZero := fv.IsZero()
// if isPtr {
// isZero = fv.Elem().IsZero()
// }
return fv.Interface(), true, fv.IsZero()
}
return
}
// Set value by field name.
//
// Notice: `StructData.src` the incoming struct must be a pointer to set the value
func (d *StructData) Set(field string, val any) (newVal any, err error) {
field = strutil.UpperFirst(field)
if !d.HasField(field) { // field not found
return nil, ErrNoField
}
// find from cache
fv, ok := d.fieldValues[field]
if !ok {
f := d.fieldNames[field]
switch f {
case fieldAtTopStruct:
fv = d.value.FieldByName(field)
case fieldAtAnonymous:
case fieldAtSubStruct:
fieldNodes := strings.Split(field, ".")
if len(fieldNodes) < 2 {
return nil, ErrInvalidData
}
fv = d.value.FieldByName(fieldNodes[0])
fieldNodes = fieldNodes[1:]
for _, fieldNode := range fieldNodes {
switch fv.Type().Kind() {
case reflect.Array, reflect.Slice:
index, err := strconv.Atoi(fieldNode)
if err != nil {
return nil, ErrInvalidData
}
fv = fv.Index(index)
case reflect.Map:
fv = fv.MapIndex(reflect.ValueOf(fieldNode))
default:
fv = fv.FieldByName(fieldNode)
}
}
default:
return nil, ErrNoField
}
// add cache
d.fieldValues[field] = fv
}
// check whether the value of v can be changed.
fv = removeValuePtr(fv)
if !fv.CanSet() {
return nil, ErrUnaddressableField
}
// Notice: need convert value type
// - check whether you can direct convert type
rftVal := removeValuePtr(reflect.ValueOf(val))
if rftVal.Type().ConvertibleTo(fv.Type()) {
fv.Set(rftVal.Convert(fv.Type()))
return val, nil
}
// try manual convert type
newRv, err1 := reflects.ValueByKind(val, fv.Kind())
if err1 != nil {
return nil, err1
}
// update field value
fv.Set(newRv)
// fix: custom func return ptr type, fv.Kind() always is real type.
if rftVal.Kind() != fv.Kind() {
newVal = newRv.Interface()
}
return
}
// FuncValue get func value in the src struct
func (d *StructData) FuncValue(name string) (reflect.Value, bool) {
fv := d.value.MethodByName(filter.UpperFirst(name))
return fv, fv.IsValid()
}
// HasField in the src struct
func (d *StructData) HasField(field string) bool {
if _, ok := d.fieldNames[field]; ok {
return true
}
// has field, cache it
if _, ok := d.valueTyp.FieldByName(field); ok {
d.fieldNames[field] = fieldAtTopStruct
return true
}
return false
}
/*************************************************************
* Form Data
*************************************************************/
// FormData obtained from the request body or url query parameters or user custom setting.
type FormData struct {
// Form holds any basic key-value string data
// This includes all fields from urlencoded form,
// and the form fields only (not files) from a multipart form
Form url.Values
// Files holds files from a multipart form only.
// For any other type of request, it will always
// be empty. Files only supports one file per key,
// since this is by far the most common use. If you
// need to have more than one file per key, parse the
// files manually using r.MultipartForm.File.
Files map[string]*multipart.FileHeader
}
func newFormData() *FormData {
return &FormData{
Form: make(map[string][]string),
Files: make(map[string]*multipart.FileHeader),
}
}
/*************************************************************
* Form data operate
*************************************************************/
// Src data get
func (d *FormData) Src() any {
return d.Form
}
// Type get
func (d *FormData) Type() uint8 {
return sourceForm
}
// Create a Validation from data
func (d *FormData) Create(err ...error) *Validation {
return d.Validation(err...)
}
// Validation create from data
func (d *FormData) Validation(err ...error) *Validation {
if len(err) > 0 && err[0] != nil {
return NewValidation(d).WithError(err[0])
}
return NewValidation(d)
}
// Add adds the value to key. It appends to any existing values associated with key.
func (d *FormData) Add(key string, value string) {
d.Form.Add(key, value)
}
// AddValues to Data.Form
func (d *FormData) AddValues(values url.Values) {
for key, vals := range values {
for _, val := range vals {
d.Form.Add(key, val)
}
}
}
// AddFiles adds the multipart form files to data
func (d *FormData) AddFiles(filesMap map[string][]*multipart.FileHeader) {
for key, files := range filesMap {
if len(files) != 0 {
d.AddFile(key, files[0])
}
}
}
// AddFile adds the multipart form file to data with the given key.
func (d *FormData) AddFile(key string, file *multipart.FileHeader) {
d.Files[key] = file
}
// Del deletes the values associated with key.
func (d *FormData) Del(key string) {
d.Form.Del(key)
}
// DelFile deletes the file associated with key (if any).
// If there is no file associated with key, it does nothing.
func (d *FormData) DelFile(key string) {
delete(d.Files, key)
}
// Encode encodes the values into “URL encoded” form ("bar=baz&foo=quux") sorted by key.
// Any files in d will be ignored because there is no direct way to convert a file to a
// URL encoded value.
func (d *FormData) Encode() string {
return d.Form.Encode()
}
// Set sets the key to value. It replaces any existing values.
func (d *FormData) Set(field string, val any) (newVal any, err error) {
newVal = val
switch tpVal := val.(type) {
case string:
d.Form.Set(field, tpVal)
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64:
newVal = strutil.MustString(val)
d.Form.Set(field, newVal.(string))
default:
err = fmt.Errorf("set value failure for field: %s", field)
}
return
}
// TryGet value by key
func (d FormData) TryGet(key string) (val any, exist, zero bool) {
val, exist = d.Get(key)
return
}
// Get value by key
func (d FormData) Get(key string) (any, bool) {
// get form value
if vs, ok := d.Form[key]; ok && len(vs) > 0 {
return vs[0], true
}
// get uploaded file
if fh, ok := d.Files[key]; ok {
return fh, true
}
return nil, false
}
// String value get by key
func (d FormData) String(key string) string {
return d.Form.Get(key)
}
// Strings value get by key
func (d FormData) Strings(key string) []string {
return d.Form[key]
}
// GetFile returns the multipart form file associated with key, if any, as a *multipart.FileHeader.
// If there is no file associated with key, it returns nil. If you just want the body of the
// file, use GetFileBytes.
func (d FormData) GetFile(key string) *multipart.FileHeader {
return d.Files[key]
}
// Has key in the Data
func (d FormData) Has(key string) bool {
if vs, ok := d.Form[key]; ok && len(vs) > 0 {
return true
}
if _, ok := d.Files[key]; ok {
return true
}
return false
}
// HasField returns true iff data.Form[key] exists. When parsing a request body, the key
// is considered to be in existence if it was provided in the request body, even if its value
// is empty.
func (d FormData) HasField(key string) bool {
_, found := d.Form[key]
return found
}
// HasFile returns true iff data.Files[key] exists. When parsing a request body, the key
// is considered to be in existence if it was provided in the request body, even if the file
// is empty.
func (d FormData) HasFile(key string) bool {
_, found := d.Files[key]
return found
}
// Int returns the first element in data[key] converted to an int.
func (d FormData) Int(key string) int {
if val := d.String(key); val != "" {
iVal, _ := strconv.Atoi(val)
return iVal
}
return 0
}
// Int64 returns the first element in data[key] converted to an int64.
func (d FormData) Int64(key string) int64 {
if val := d.String(key); val != "" {
i64, _ := strconv.ParseInt(val, 10, 0)
return i64
}
return 0
}
// Float returns the first element in data[key] converted to a float.
func (d FormData) Float(key string) float64 {
if val := d.String(key); val != "" {
result, _ := strconv.ParseFloat(val, 64)
return result
}
return 0
}
// Bool returns the first element in data[key] converted to a bool.
func (d FormData) Bool(key string) bool {
if val := d.String(key); val != "" {
blVal, _ := filter.Bool(val)
return blVal
}
return false
}
// FileBytes returns the body of the file associated with key. If there is no
// file associated with key, it returns nil (not an error). It may return an error if
// there was a problem reading the file. If you need to know whether or not the file
// exists (i.e. whether it was provided in the request), use the FileExists method.
func (d FormData) FileBytes(field string) ([]byte, error) {
fh, found := d.Files[field]
if !found {
return nil, nil
}
file, err := fh.Open()
if err != nil {
return nil, err
}
return io.ReadAll(file)
}
// FileMimeType get File Mime Type name. eg "image/png"
func (d FormData) FileMimeType(field string) (mime string) {
fh, found := d.Files[field]
if !found {
return
}
if file, err := fh.Open(); err == nil {
var buf [sniffLen]byte
n, _ := io.ReadFull(file, buf[:])
mime = http.DetectContentType(buf[:n])
}
return
}