github.com/wangyougui/gf/v2@v2.6.5/net/ghttp/ghttp_request_param_query.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 "net/http" 11 12 "github.com/wangyougui/gf/v2/container/gvar" 13 "github.com/wangyougui/gf/v2/util/gconv" 14 ) 15 16 // SetQuery sets custom query value with key-value pairs. 17 func (r *Request) SetQuery(key string, value interface{}) { 18 r.parseQuery() 19 if r.queryMap == nil { 20 r.queryMap = make(map[string]interface{}) 21 } 22 r.queryMap[key] = value 23 } 24 25 // GetQuery retrieves and return parameter with the given name `key` from query string 26 // and request body. It returns `def` if `key` does not exist in the query and `def` is given, 27 // or else it returns nil. 28 // 29 // Note that if there are multiple parameters with the same name, the parameters are retrieved 30 // and overwrote in order of priority: query > body. 31 func (r *Request) GetQuery(key string, def ...interface{}) *gvar.Var { 32 r.parseQuery() 33 if len(r.queryMap) > 0 { 34 if value, ok := r.queryMap[key]; ok { 35 return gvar.New(value) 36 } 37 } 38 if r.Method == http.MethodGet { 39 r.parseBody() 40 } 41 if len(r.bodyMap) > 0 { 42 if v, ok := r.bodyMap[key]; ok { 43 return gvar.New(v) 44 } 45 } 46 if len(def) > 0 { 47 return gvar.New(def[0]) 48 } 49 return nil 50 } 51 52 // GetQueryMap retrieves and returns all parameters passed from the client using HTTP GET method 53 // as the map. The parameter `kvMap` specifies the keys retrieving from client parameters, 54 // the associated values are the default values if the client does not pass. 55 // 56 // Note that if there are multiple parameters with the same name, the parameters are retrieved and overwrote 57 // in order of priority: query > body. 58 func (r *Request) GetQueryMap(kvMap ...map[string]interface{}) map[string]interface{} { 59 r.parseQuery() 60 if r.Method == http.MethodGet { 61 r.parseBody() 62 } 63 var m map[string]interface{} 64 if len(kvMap) > 0 && kvMap[0] != nil { 65 if len(r.queryMap) == 0 && len(r.bodyMap) == 0 { 66 return kvMap[0] 67 } 68 m = make(map[string]interface{}, len(kvMap[0])) 69 if len(r.bodyMap) > 0 { 70 for k, v := range kvMap[0] { 71 if postValue, ok := r.bodyMap[k]; ok { 72 m[k] = postValue 73 } else { 74 m[k] = v 75 } 76 } 77 } 78 if len(r.queryMap) > 0 { 79 for k, v := range kvMap[0] { 80 if postValue, ok := r.queryMap[k]; ok { 81 m[k] = postValue 82 } else { 83 m[k] = v 84 } 85 } 86 } 87 } else { 88 m = make(map[string]interface{}, len(r.queryMap)+len(r.bodyMap)) 89 for k, v := range r.bodyMap { 90 m[k] = v 91 } 92 for k, v := range r.queryMap { 93 m[k] = v 94 } 95 } 96 return m 97 } 98 99 // GetQueryMapStrStr retrieves and returns all parameters passed from the client using the HTTP GET method as a 100 // 101 // map[string]string. The parameter `kvMap` specifies the keys 102 // 103 // retrieving from client parameters, the associated values are the default values if the client 104 // does not pass. 105 func (r *Request) GetQueryMapStrStr(kvMap ...map[string]interface{}) map[string]string { 106 queryMap := r.GetQueryMap(kvMap...) 107 if len(queryMap) > 0 { 108 m := make(map[string]string, len(queryMap)) 109 for k, v := range queryMap { 110 m[k] = gconv.String(v) 111 } 112 return m 113 } 114 return nil 115 } 116 117 // GetQueryMapStrVar retrieves and returns all parameters passed from the client using the HTTP GET method 118 // as map[string]*gvar.Var. The parameter `kvMap` specifies the keys 119 // retrieving from client parameters, the associated values are the default values if the client 120 // does not pass. 121 func (r *Request) GetQueryMapStrVar(kvMap ...map[string]interface{}) map[string]*gvar.Var { 122 queryMap := r.GetQueryMap(kvMap...) 123 if len(queryMap) > 0 { 124 m := make(map[string]*gvar.Var, len(queryMap)) 125 for k, v := range queryMap { 126 m[k] = gvar.New(v) 127 } 128 return m 129 } 130 return nil 131 } 132 133 // GetQueryStruct retrieves all parameters passed from the client using the HTTP GET method 134 // and converts them to a given struct object. Note that the parameter `pointer` is a pointer 135 // to the struct object. The optional parameter `mapping` is used to specify the key to 136 // attribute mapping. 137 func (r *Request) GetQueryStruct(pointer interface{}, mapping ...map[string]string) error { 138 _, err := r.doGetQueryStruct(pointer, mapping...) 139 return err 140 } 141 142 func (r *Request) doGetQueryStruct(pointer interface{}, mapping ...map[string]string) (data map[string]interface{}, err error) { 143 r.parseQuery() 144 data = r.GetQueryMap() 145 if data == nil { 146 data = map[string]interface{}{} 147 } 148 if err = r.mergeDefaultStructValue(data, pointer); err != nil { 149 return data, nil 150 } 151 return data, gconv.Struct(data, pointer, mapping...) 152 }