Skip to content
Myr

Optimize code adjust (#2700)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* setFormMap error of result * adjust code for TrySet * error export for type multipart.FileHeader * code style adjust * reflect code maping optimize * Update form_mapping.go

authored by heige · committed by GitHub · parent 6703dea · Browse files · 4 files+29 added−11 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Co-authored-by: Bo-Yi Wu <appleboy.tw@gmail.com>
  • Co-authored-by: thinkerou <thinkerou@gmail.com>

Diff stats

4 files+29 added−11 removed

  • other:+29 added−11 removed

4 files+29 added−11 removed

modified:binding/form_mapping.go+12 added-4 removed
14 unmodified lines
15 "github.com/gin-gonic/gin/internal/bytesconv"16 "github.com/gin-gonic/gin/internal/json"17)1819var (19var errUnknownType = errors.New("unknown type")20 errUnknownType = errors.New("unknown type")2122 // ErrConvertMapStringSlice can not covert to map[string][]string23 ErrConvertMapStringSlice = errors.New("can not convert to map slices of strings")2425 // ErrConvertToMapString can not convert to map[string]string26 ErrConvertToMapString = errors.New("can not convert to map of strings")27)2829func mapUri(ptr interface{}, m map[string][]string) error {30 return mapFormByTag(ptr, m, "uri")31}32
83 unmodified lines
116 sf := tValue.Field(i)117 if sf.PkgPath != "" && !sf.Anonymous { // unexported118 continue119 }112 ok, err := mapping(value.Field(i), tValue.Field(i), setter, tag)120 ok, err := mapping(value.Field(i), sf, setter, tag)121 if err != nil {122 return false, err123 }124 isSetted = isSetted || ok
253 unmodified lines
378379 if el.Kind() == reflect.Slice {380 ptrMap, ok := ptr.(map[string][]string)381 if !ok {374 return errors.New("cannot convert to map slices of strings")382 return ErrConvertMapStringSlice383 }384 for k, v := range form {385 ptrMap[k] = v386 }
2 unmodified lines
389 }390391 ptrMap, ok := ptr.(map[string]string)392 if !ok {385 return errors.New("cannot convert to map of strings")393 return ErrConvertToMapString394 }395 for k, v := range form {396 ptrMap[k] = v[len(v)-1] // pick last397 }
3 unmodified lines
modified:binding/header.go+1 added-1 removed
27 unmodified lines
28type headerSource map[string][]string2930var _ setter = headerSource(nil)3132func (hs headerSource) TrySet(value reflect.Value, field reflect.StructField, tagValue string, opt setOptions) (isSetted bool, err error) {32func (hs headerSource) TrySet(value reflect.Value, field reflect.StructField, tagValue string, opt setOptions) (bool, error) {33 return setByForm(value, field, hs, textproto.CanonicalMIMEHeaderKey(tagValue), opt)34}
modified:binding/multipart_form_mapping.go+11 added-3 removed
13 unmodified lines
14type multipartRequest http.Request1516var _ setter = (*multipartRequest)(nil)1718var (19 // ErrMultiFileHeader multipart.FileHeader invalid20 ErrMultiFileHeader = errors.New("unsupported field type for multipart.FileHeader")2122 // ErrMultiFileHeaderLenInvalid array for []*multipart.FileHeader len invalid23 ErrMultiFileHeaderLenInvalid = errors.New("unsupported len of array for []*multipart.FileHeader")24)2526// TrySet tries to set a value by the multipart request with the binding a form file19func (r *multipartRequest) TrySet(value reflect.Value, field reflect.StructField, key string, opt setOptions) (isSetted bool, err error) {27func (r *multipartRequest) TrySet(value reflect.Value, field reflect.StructField, key string, opt setOptions) (bool, error) {28 if files := r.MultipartForm.File[key]; len(files) != 0 {29 return setByMultipartFormFile(value, field, files)30 }31
24 unmodified lines
56 return true, nil57 case reflect.Array:58 return setArrayOfMultipartFormFiles(value, field, files)59 }52 return false, errors.New("unsupported field type for multipart.FileHeader")60 return false, ErrMultiFileHeader61}6263func setArrayOfMultipartFormFiles(value reflect.Value, field reflect.StructField, files []*multipart.FileHeader) (isSetted bool, err error) {64 if value.Len() != len(files) {57 return false, errors.New("unsupported len of array for []*multipart.FileHeader")65 return false, ErrMultiFileHeaderLenInvalid66 }67 for i := range files {68 setted, err := setByMultipartFormFile(value.Index(i), field, files[i:i+1])69 if err != nil || !setted {
5 unmodified lines
modified:render/json.go+5 added-3 removed
44 unmodified lines
45type PureJSON struct {46 Data interface{}47}4849var (49var jsonContentType = []string{"application/json; charset=utf-8"}50var jsonpContentType = []string{"application/javascript; charset=utf-8"}51var jsonAsciiContentType = []string{"application/json"}50 jsonContentType = []string{"application/json; charset=utf-8"}51 jsonpContentType = []string{"application/javascript; charset=utf-8"}52 jsonAsciiContentType = []string{"application/json"}53)5455// Render (JSON) writes data with custom ContentType.56func (r JSON) Render(w http.ResponseWriter) (err error) {57 if err = WriteJSON(w, r.Data); err != nil {
138 unmodified lines