github.com/gogf/gf@v1.16.9/net/ghttp/ghttp_request_param_request.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/gogf/gf.
     6  
     7  package ghttp
     8  
     9  import (
    10  	"github.com/gogf/gf/container/gvar"
    11  	"github.com/gogf/gf/internal/empty"
    12  	"github.com/gogf/gf/internal/structs"
    13  	"github.com/gogf/gf/util/gconv"
    14  	"github.com/gogf/gf/util/gutil"
    15  )
    16  
    17  // GetRequest retrieves and returns the parameter named <key> passed from client and
    18  // custom params as interface{}, no matter what HTTP method the client is using. The
    19  // parameter <def> specifies the default value if the <key> does not exist.
    20  //
    21  // GetRequest is one of the most commonly used functions for retrieving parameters.
    22  //
    23  // Note that if there're multiple parameters with the same name, the parameters are
    24  // retrieved and overwrote in order of priority: router < query < body < form < custom.
    25  func (r *Request) GetRequest(key string, def ...interface{}) interface{} {
    26  	value := r.GetParam(key)
    27  	if value == nil {
    28  		value = r.GetForm(key)
    29  	}
    30  	if value == nil {
    31  		r.parseBody()
    32  		if len(r.bodyMap) > 0 {
    33  			value = r.bodyMap[key]
    34  		}
    35  	}
    36  	if value == nil {
    37  		value = r.GetQuery(key)
    38  	}
    39  	if value == nil {
    40  		value = r.GetRouterValue(key)
    41  	}
    42  	if value != nil {
    43  		return value
    44  	}
    45  	if len(def) > 0 {
    46  		return def[0]
    47  	}
    48  	return value
    49  }
    50  
    51  // GetRequestVar retrieves and returns the parameter named <key> passed from client and
    52  // custom params as gvar.Var, no matter what HTTP method the client is using. The parameter
    53  // <def> specifies the default value if the <key> does not exist.
    54  func (r *Request) GetRequestVar(key string, def ...interface{}) *gvar.Var {
    55  	return gvar.New(r.GetRequest(key, def...))
    56  }
    57  
    58  // GetRequestString retrieves and returns the parameter named <key> passed from client and
    59  // custom params as string, no matter what HTTP method the client is using. The parameter
    60  // <def> specifies the default value if the <key> does not exist.
    61  func (r *Request) GetRequestString(key string, def ...interface{}) string {
    62  	return r.GetRequestVar(key, def...).String()
    63  }
    64  
    65  // GetRequestBool retrieves and returns the parameter named <key> passed from client and
    66  // custom params as bool, no matter what HTTP method the client is using. The parameter
    67  // <def> specifies the default value if the <key> does not exist.
    68  func (r *Request) GetRequestBool(key string, def ...interface{}) bool {
    69  	return r.GetRequestVar(key, def...).Bool()
    70  }
    71  
    72  // GetRequestInt retrieves and returns the parameter named <key> passed from client and
    73  // custom params as int, no matter what HTTP method the client is using. The parameter
    74  // <def> specifies the default value if the <key> does not exist.
    75  func (r *Request) GetRequestInt(key string, def ...interface{}) int {
    76  	return r.GetRequestVar(key, def...).Int()
    77  }
    78  
    79  // GetRequestInt32 retrieves and returns the parameter named <key> passed from client and
    80  // custom params as int32, no matter what HTTP method the client is using. The parameter
    81  // <def> specifies the default value if the <key> does not exist.
    82  func (r *Request) GetRequestInt32(key string, def ...interface{}) int32 {
    83  	return r.GetRequestVar(key, def...).Int32()
    84  }
    85  
    86  // GetRequestInt64 retrieves and returns the parameter named <key> passed from client and
    87  // custom params as int64, no matter what HTTP method the client is using. The parameter
    88  // <def> specifies the default value if the <key> does not exist.
    89  func (r *Request) GetRequestInt64(key string, def ...interface{}) int64 {
    90  	return r.GetRequestVar(key, def...).Int64()
    91  }
    92  
    93  // GetRequestInts retrieves and returns the parameter named <key> passed from client and
    94  // custom params as []int, no matter what HTTP method the client is using. The parameter
    95  // <def> specifies the default value if the <key> does not exist.
    96  func (r *Request) GetRequestInts(key string, def ...interface{}) []int {
    97  	return r.GetRequestVar(key, def...).Ints()
    98  }
    99  
   100  // GetRequestUint retrieves and returns the parameter named <key> passed from client and
   101  // custom params as uint, no matter what HTTP method the client is using. The parameter
   102  // <def> specifies the default value if the <key> does not exist.
   103  func (r *Request) GetRequestUint(key string, def ...interface{}) uint {
   104  	return r.GetRequestVar(key, def...).Uint()
   105  }
   106  
   107  // GetRequestUint32 retrieves and returns the parameter named <key> passed from client and
   108  // custom params as uint32, no matter what HTTP method the client is using. The parameter
   109  // <def> specifies the default value if the <key> does not exist.
   110  func (r *Request) GetRequestUint32(key string, def ...interface{}) uint32 {
   111  	return r.GetRequestVar(key, def...).Uint32()
   112  }
   113  
   114  // GetRequestUint64 retrieves and returns the parameter named <key> passed from client and
   115  // custom params as uint64, no matter what HTTP method the client is using. The parameter
   116  // <def> specifies the default value if the <key> does not exist.
   117  func (r *Request) GetRequestUint64(key string, def ...interface{}) uint64 {
   118  	return r.GetRequestVar(key, def...).Uint64()
   119  }
   120  
   121  // GetRequestFloat32 retrieves and returns the parameter named <key> passed from client and
   122  // custom params as float32, no matter what HTTP method the client is using. The parameter
   123  // <def> specifies the default value if the <key> does not exist.
   124  func (r *Request) GetRequestFloat32(key string, def ...interface{}) float32 {
   125  	return r.GetRequestVar(key, def...).Float32()
   126  }
   127  
   128  // GetRequestFloat64 retrieves and returns the parameter named <key> passed from client and
   129  // custom params as float64, no matter what HTTP method the client is using. The parameter
   130  // <def> specifies the default value if the <key> does not exist.
   131  func (r *Request) GetRequestFloat64(key string, def ...interface{}) float64 {
   132  	return r.GetRequestVar(key, def...).Float64()
   133  }
   134  
   135  // GetRequestFloats retrieves and returns the parameter named <key> passed from client and
   136  // custom params as []float64, no matter what HTTP method the client is using. The parameter
   137  // <def> specifies the default value if the <key> does not exist.
   138  func (r *Request) GetRequestFloats(key string, def ...interface{}) []float64 {
   139  	return r.GetRequestVar(key, def...).Floats()
   140  }
   141  
   142  // GetRequestArray retrieves and returns the parameter named <key> passed from client and
   143  // custom params as []string, no matter what HTTP method the client is using. The parameter
   144  // <def> specifies the default value if the <key> does not exist.
   145  func (r *Request) GetRequestArray(key string, def ...interface{}) []string {
   146  	return r.GetRequestVar(key, def...).Strings()
   147  }
   148  
   149  // GetRequestStrings retrieves and returns the parameter named <key> passed from client and
   150  // custom params as []string, no matter what HTTP method the client is using. The parameter
   151  // <def> specifies the default value if the <key> does not exist.
   152  func (r *Request) GetRequestStrings(key string, def ...interface{}) []string {
   153  	return r.GetRequestVar(key, def...).Strings()
   154  }
   155  
   156  // GetRequestInterfaces retrieves and returns the parameter named <key> passed from client
   157  // and custom params as []interface{}, no matter what HTTP method the client is using. The
   158  // parameter <def> specifies the default value if the <key> does not exist.
   159  func (r *Request) GetRequestInterfaces(key string, def ...interface{}) []interface{} {
   160  	return r.GetRequestVar(key, def...).Interfaces()
   161  }
   162  
   163  // GetRequestMap retrieves and returns all parameters passed from client and custom params
   164  // as map, no matter what HTTP method the client is using. The parameter <kvMap> specifies
   165  // the keys retrieving from client parameters, the associated values are the default values
   166  // if the client does not pass the according keys.
   167  //
   168  // GetRequestMap is one of the most commonly used functions for retrieving parameters.
   169  //
   170  // Note that if there're multiple parameters with the same name, the parameters are retrieved
   171  // and overwrote in order of priority: router < query < body < form < custom.
   172  func (r *Request) GetRequestMap(kvMap ...map[string]interface{}) map[string]interface{} {
   173  	r.parseQuery()
   174  	r.parseForm()
   175  	r.parseBody()
   176  	var ok, filter bool
   177  	var length int
   178  	if len(kvMap) > 0 && kvMap[0] != nil {
   179  		length = len(kvMap[0])
   180  		filter = true
   181  	} else {
   182  		length = len(r.routerMap) + len(r.queryMap) + len(r.formMap) + len(r.bodyMap) + len(r.paramsMap)
   183  	}
   184  	m := make(map[string]interface{}, length)
   185  	for k, v := range r.routerMap {
   186  		if filter {
   187  			if _, ok = kvMap[0][k]; !ok {
   188  				continue
   189  			}
   190  		}
   191  		m[k] = v
   192  	}
   193  	for k, v := range r.queryMap {
   194  		if filter {
   195  			if _, ok = kvMap[0][k]; !ok {
   196  				continue
   197  			}
   198  		}
   199  		m[k] = v
   200  	}
   201  	for k, v := range r.formMap {
   202  		if filter {
   203  			if _, ok = kvMap[0][k]; !ok {
   204  				continue
   205  			}
   206  		}
   207  		m[k] = v
   208  	}
   209  	for k, v := range r.bodyMap {
   210  		if filter {
   211  			if _, ok = kvMap[0][k]; !ok {
   212  				continue
   213  			}
   214  		}
   215  		m[k] = v
   216  	}
   217  	for k, v := range r.paramsMap {
   218  		if filter {
   219  			if _, ok = kvMap[0][k]; !ok {
   220  				continue
   221  			}
   222  		}
   223  		m[k] = v
   224  	}
   225  	// Check none exist parameters and assign it with default value.
   226  	if filter {
   227  		for k, v := range kvMap[0] {
   228  			if _, ok = m[k]; !ok {
   229  				m[k] = v
   230  			}
   231  		}
   232  	}
   233  	return m
   234  }
   235  
   236  // GetRequestMapStrStr retrieves and returns all parameters passed from client and custom
   237  // params as map[string]string, no matter what HTTP method the client is using. The parameter
   238  // <kvMap> specifies the keys retrieving from client parameters, the associated values are the
   239  // default values if the client does not pass.
   240  func (r *Request) GetRequestMapStrStr(kvMap ...map[string]interface{}) map[string]string {
   241  	requestMap := r.GetRequestMap(kvMap...)
   242  	if len(requestMap) > 0 {
   243  		m := make(map[string]string, len(requestMap))
   244  		for k, v := range requestMap {
   245  			m[k] = gconv.String(v)
   246  		}
   247  		return m
   248  	}
   249  	return nil
   250  }
   251  
   252  // GetRequestMapStrVar retrieves and returns all parameters passed from client and custom
   253  // params as map[string]*gvar.Var, no matter what HTTP method the client is using. The parameter
   254  // <kvMap> specifies the keys retrieving from client parameters, the associated values are the
   255  // default values if the client does not pass.
   256  func (r *Request) GetRequestMapStrVar(kvMap ...map[string]interface{}) map[string]*gvar.Var {
   257  	requestMap := r.GetRequestMap(kvMap...)
   258  	if len(requestMap) > 0 {
   259  		m := make(map[string]*gvar.Var, len(requestMap))
   260  		for k, v := range requestMap {
   261  			m[k] = gvar.New(v)
   262  		}
   263  		return m
   264  	}
   265  	return nil
   266  }
   267  
   268  // GetRequestStruct retrieves all parameters passed from client and custom params no matter
   269  // what HTTP method the client is using, and converts them to given struct object. Note that
   270  // the parameter <pointer> is a pointer to the struct object.
   271  // The optional parameter <mapping> is used to specify the key to attribute mapping.
   272  func (r *Request) GetRequestStruct(pointer interface{}, mapping ...map[string]string) error {
   273  	_, err := r.doGetRequestStruct(pointer, mapping...)
   274  	return err
   275  }
   276  
   277  func (r *Request) doGetRequestStruct(pointer interface{}, mapping ...map[string]string) (data map[string]interface{}, err error) {
   278  	data = r.GetRequestMap()
   279  	if data == nil {
   280  		data = map[string]interface{}{}
   281  	}
   282  	if err := r.mergeDefaultStructValue(data, pointer); err != nil {
   283  		return data, nil
   284  	}
   285  	return data, gconv.Struct(data, pointer, mapping...)
   286  }
   287  
   288  // mergeDefaultStructValue merges the request parameters with default values from struct tag definition.
   289  func (r *Request) mergeDefaultStructValue(data map[string]interface{}, pointer interface{}) error {
   290  	tagFields, err := structs.TagFields(pointer, defaultValueTags)
   291  	if err != nil {
   292  		return err
   293  	}
   294  	if len(tagFields) > 0 {
   295  		var (
   296  			foundKey   string
   297  			foundValue interface{}
   298  		)
   299  		for _, field := range tagFields {
   300  			foundKey, foundValue = gutil.MapPossibleItemByKey(data, field.Name())
   301  			if foundKey == "" {
   302  				data[field.Name()] = field.TagValue
   303  			} else {
   304  				if empty.IsEmpty(foundValue) {
   305  					data[foundKey] = field.TagValue
   306  				}
   307  			}
   308  		}
   309  	}
   310  	return nil
   311  }