github.com/wangyougui/gf/v2@v2.6.5/net/ghttp/ghttp_request_param_form.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/wangyougui/gf. 6 7 package ghttp 8 9 import ( 10 "github.com/wangyougui/gf/v2/container/gvar" 11 "github.com/wangyougui/gf/v2/util/gconv" 12 ) 13 14 // SetForm sets custom form value with key-value pairs. 15 func (r *Request) SetForm(key string, value interface{}) { 16 r.parseForm() 17 if r.formMap == nil { 18 r.formMap = make(map[string]interface{}) 19 } 20 r.formMap[key] = value 21 } 22 23 // GetForm retrieves and returns parameter `key` from form. 24 // It returns `def` if `key` does not exist in the form and `def` is given, or else it returns nil. 25 func (r *Request) GetForm(key string, def ...interface{}) *gvar.Var { 26 r.parseForm() 27 if len(r.formMap) > 0 { 28 if value, ok := r.formMap[key]; ok { 29 return gvar.New(value) 30 } 31 } 32 if len(def) > 0 { 33 return gvar.New(def[0]) 34 } 35 return nil 36 } 37 38 // GetFormMap retrieves and returns all form parameters passed from client as map. 39 // The parameter `kvMap` specifies the keys retrieving from client parameters, 40 // the associated values are the default values if the client does not pass. 41 func (r *Request) GetFormMap(kvMap ...map[string]interface{}) map[string]interface{} { 42 r.parseForm() 43 if len(kvMap) > 0 && kvMap[0] != nil { 44 if len(r.formMap) == 0 { 45 return kvMap[0] 46 } 47 m := make(map[string]interface{}, len(kvMap[0])) 48 for k, defValue := range kvMap[0] { 49 if postValue, ok := r.formMap[k]; ok { 50 m[k] = postValue 51 } else { 52 m[k] = defValue 53 } 54 } 55 return m 56 } else { 57 return r.formMap 58 } 59 } 60 61 // GetFormMapStrStr retrieves and returns all form parameters passed from client as map[string]string. 62 // The parameter `kvMap` specifies the keys retrieving from client parameters, the associated values 63 // are the default values if the client does not pass. 64 func (r *Request) GetFormMapStrStr(kvMap ...map[string]interface{}) map[string]string { 65 formMap := r.GetFormMap(kvMap...) 66 if len(formMap) > 0 { 67 m := make(map[string]string, len(formMap)) 68 for k, v := range formMap { 69 m[k] = gconv.String(v) 70 } 71 return m 72 } 73 return nil 74 } 75 76 // GetFormMapStrVar retrieves and returns all form parameters passed from client as map[string]*gvar.Var. 77 // The parameter `kvMap` specifies the keys retrieving from client parameters, the associated values 78 // are the default values if the client does not pass. 79 func (r *Request) GetFormMapStrVar(kvMap ...map[string]interface{}) map[string]*gvar.Var { 80 formMap := r.GetFormMap(kvMap...) 81 if len(formMap) > 0 { 82 m := make(map[string]*gvar.Var, len(formMap)) 83 for k, v := range formMap { 84 m[k] = gvar.New(v) 85 } 86 return m 87 } 88 return nil 89 } 90 91 // GetFormStruct retrieves all form parameters passed from client and converts them to 92 // given struct object. Note that the parameter `pointer` is a pointer to the struct object. 93 // The optional parameter `mapping` is used to specify the key to attribute mapping. 94 func (r *Request) GetFormStruct(pointer interface{}, mapping ...map[string]string) error { 95 _, err := r.doGetFormStruct(pointer, mapping...) 96 return err 97 } 98 99 func (r *Request) doGetFormStruct(pointer interface{}, mapping ...map[string]string) (data map[string]interface{}, err error) { 100 r.parseForm() 101 data = r.formMap 102 if data == nil { 103 data = map[string]interface{}{} 104 } 105 if err = r.mergeDefaultStructValue(data, pointer); err != nil { 106 return data, nil 107 } 108 return data, gconv.Struct(data, pointer, mapping...) 109 }