From f4dc3d3fd977c8326a39d2fc11aa85882ee9c50e Mon Sep 17 00:00:00 2001 From: daheige Date: Sat, 5 Jun 2021 09:51:26 +0800 Subject: [PATCH] code style adjust --- binding/form_mapping.go | 2 +- binding/json.go | 25 +++++++++++++++---------- binding/multipart_form_mapping.go | 6 +++--- 3 files changed, 19 insertions(+), 14 deletions(-) diff --git a/binding/form_mapping.go b/binding/form_mapping.go index 421c0f71..4932c395 100644 --- a/binding/form_mapping.go +++ b/binding/form_mapping.go @@ -160,7 +160,7 @@ func tryToSetValue(value reflect.Value, field reflect.StructField, setter setter return setter.TrySet(value, field, tagValue, setOpt) } -func setByForm(value reflect.Value, field reflect.StructField, form map[string][]string, tagValue string, opt setOptions) (isSetted bool, err error) { +func setByForm(value reflect.Value, field reflect.StructField, form map[string][]string, tagValue string, opt setOptions) (bool, error) { vs, ok := form[tagValue] if !ok && !opt.isDefaultExists { return false, nil diff --git a/binding/json.go b/binding/json.go index 45aaa494..c1bc5f79 100644 --- a/binding/json.go +++ b/binding/json.go @@ -13,16 +13,21 @@ import ( "github.com/gin-gonic/gin/internal/json" ) -// EnableDecoderUseNumber is used to call the UseNumber method on the JSON -// Decoder instance. UseNumber causes the Decoder to unmarshal a number into an -// interface{} as a Number instead of as a float64. -var EnableDecoderUseNumber = false +var ( + // EnableDecoderUseNumber is used to call the UseNumber method on the JSON + // Decoder instance. UseNumber causes the Decoder to unmarshal a number into an + // interface{} as a Number instead of as a float64. + EnableDecoderUseNumber = false -// EnableDecoderDisallowUnknownFields is used to call the DisallowUnknownFields method -// on the JSON Decoder instance. DisallowUnknownFields causes the Decoder to -// return an error when the destination is a struct and the input contains object -// keys which do not match any non-ignored, exported fields in the destination. -var EnableDecoderDisallowUnknownFields = false + // EnableDecoderDisallowUnknownFields is used to call the DisallowUnknownFields method + // on the JSON Decoder instance. DisallowUnknownFields causes the Decoder to + // return an error when the destination is a struct and the input contains object + // keys which do not match any non-ignored, exported fields in the destination. + EnableDecoderDisallowUnknownFields = false + + // ErrRequestJsonInvalid request json invalid + ErrRequestJsonInvalid = errors.New("invalid request") +) type jsonBinding struct{} @@ -32,7 +37,7 @@ func (jsonBinding) Name() string { func (jsonBinding) Bind(req *http.Request, obj interface{}) error { if req == nil || req.Body == nil { - return errors.New("invalid request") + return ErrRequestJsonInvalid } return decodeJSON(req.Body, obj) } diff --git a/binding/multipart_form_mapping.go b/binding/multipart_form_mapping.go index 69c0a544..b8887105 100644 --- a/binding/multipart_form_mapping.go +++ b/binding/multipart_form_mapping.go @@ -32,7 +32,7 @@ func (r *multipartRequest) TrySet(value reflect.Value, field reflect.StructField return setByForm(value, field, r.MultipartForm.Value, key, opt) } -func setByMultipartFormFile(value reflect.Value, field reflect.StructField, files []*multipart.FileHeader) (isSetted bool, err error) { +func setByMultipartFormFile(value reflect.Value, field reflect.StructField, files []*multipart.FileHeader) (bool, error) { switch value.Kind() { case reflect.Ptr: switch value.Interface().(type) { @@ -48,7 +48,7 @@ func setByMultipartFormFile(value reflect.Value, field reflect.StructField, file } case reflect.Slice: slice := reflect.MakeSlice(value.Type(), len(files), len(files)) - isSetted, err = setArrayOfMultipartFormFiles(slice, field, files) + isSetted, err := setArrayOfMultipartFormFiles(slice, field, files) if err != nil || !isSetted { return isSetted, err } @@ -60,7 +60,7 @@ func setByMultipartFormFile(value reflect.Value, field reflect.StructField, file return false, ErrMultiFileHeader } -func setArrayOfMultipartFormFiles(value reflect.Value, field reflect.StructField, files []*multipart.FileHeader) (isSetted bool, err error) { +func setArrayOfMultipartFormFiles(value reflect.Value, field reflect.StructField, files []*multipart.FileHeader) (bool, error) { if value.Len() != len(files) { return false, ErrMultiFileHeaderLenInvalid }