From 820e4302b7c85f0cab4dfe491d6a1d303c334a7a Mon Sep 17 00:00:00 2001 From: Jack Date: Thu, 13 Aug 2020 18:51:59 +0800 Subject: [PATCH] improve function *Struct for ghttp.Request --- net/ghttp/ghttp_request_param_form.go | 3 +++ net/ghttp/ghttp_request_param_query.go | 6 +++++- net/ghttp/ghttp_request_param_request.go | 6 +++++- 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/net/ghttp/ghttp_request_param_form.go b/net/ghttp/ghttp_request_param_form.go index c100160ff..7c4a5d150 100644 --- a/net/ghttp/ghttp_request_param_form.go +++ b/net/ghttp/ghttp_request_param_form.go @@ -189,6 +189,9 @@ func (r *Request) GetFormMapStrVar(kvMap ...map[string]interface{}) map[string]* // The optional parameter is used to specify the key to attribute mapping. func (r *Request) GetFormStruct(pointer interface{}, mapping ...map[string]string) error { r.parseForm() + if len(r.formMap) == 0 { + return nil + } return gconv.StructDeep(r.formMap, pointer, mapping...) } diff --git a/net/ghttp/ghttp_request_param_query.go b/net/ghttp/ghttp_request_param_query.go index b8250961d..042da32f9 100644 --- a/net/ghttp/ghttp_request_param_query.go +++ b/net/ghttp/ghttp_request_param_query.go @@ -193,7 +193,11 @@ func (r *Request) GetQueryMapStrVar(kvMap ...map[string]interface{}) map[string] // attribute mapping. func (r *Request) GetQueryStruct(pointer interface{}, mapping ...map[string]string) error { r.parseQuery() - return gconv.StructDeep(r.GetQueryMap(), pointer, mapping...) + m := r.GetQueryMap() + if len(m) == 0 { + return nil + } + return gconv.StructDeep(m, pointer, mapping...) } // GetQueryToStruct is alias of GetQueryStruct. See GetQueryStruct. diff --git a/net/ghttp/ghttp_request_param_request.go b/net/ghttp/ghttp_request_param_request.go index be998f6fb..cb973e511 100644 --- a/net/ghttp/ghttp_request_param_request.go +++ b/net/ghttp/ghttp_request_param_request.go @@ -267,7 +267,11 @@ func (r *Request) GetRequestMapStrVar(kvMap ...map[string]interface{}) map[strin // the parameter is a pointer to the struct object. // The optional parameter is used to specify the key to attribute mapping. func (r *Request) GetRequestStruct(pointer interface{}, mapping ...map[string]string) error { - return gconv.StructDeep(r.GetRequestMap(), pointer, mapping...) + m := r.GetRequestMap() + if len(m) == 0 { + return nil + } + return gconv.StructDeep(m, pointer, mapping...) } // GetRequestToStruct is alias of GetRequestStruct. See GetRequestStruct.