-
Notifications
You must be signed in to change notification settings - Fork 8.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor(form_mapping.go): mapping multipart request #1829
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
cdc534f
refactor(form_mapping.go): mapping multipart request
vkd adff101
add checkers for a types to match with the setter interface
vkd 6c5e1da
form_mapping.go: rename method name on setter interface, add comments
vkd fe96cf6
fix style of comments
vkd 0235b7c
Merge branch 'master' into refactor_mapping_multipart
thinkerou File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,6 @@ package binding | |
import ( | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
"reflect" | ||
"strconv" | ||
"strings" | ||
|
@@ -16,34 +15,6 @@ import ( | |
"github.com/gin-gonic/gin/internal/json" | ||
) | ||
|
||
func mapFiles(ptr interface{}, req *http.Request) error { | ||
typ := reflect.TypeOf(ptr).Elem() | ||
val := reflect.ValueOf(ptr).Elem() | ||
for i := 0; i < typ.NumField(); i++ { | ||
typeField := typ.Field(i) | ||
structField := val.Field(i) | ||
|
||
t := fmt.Sprintf("%s", typeField.Type) | ||
if string(t) != "*multipart.FileHeader" { | ||
continue | ||
} | ||
|
||
inputFieldName := typeField.Tag.Get("form") | ||
if inputFieldName == "" { | ||
inputFieldName = typeField.Name | ||
} | ||
|
||
_, fileHeader, err := req.FormFile(inputFieldName) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
structField.Set(reflect.ValueOf(fileHeader)) | ||
|
||
} | ||
return nil | ||
} | ||
|
||
var errUnknownType = errors.New("Unknown type") | ||
|
||
func mapUri(ptr interface{}, m map[string][]string) error { | ||
|
@@ -57,11 +28,29 @@ func mapForm(ptr interface{}, form map[string][]string) error { | |
var emptyField = reflect.StructField{} | ||
|
||
func mapFormByTag(ptr interface{}, form map[string][]string, tag string) error { | ||
_, err := mapping(reflect.ValueOf(ptr), emptyField, form, tag) | ||
return mappingByPtr(ptr, formSource(form), tag) | ||
} | ||
|
||
// setter tries to set value on a walking by fields of a struct | ||
type setter interface { | ||
TrySet(value reflect.Value, field reflect.StructField, key string, opt setOptions) (isSetted bool, err error) | ||
} | ||
|
||
type formSource map[string][]string | ||
|
||
var _ setter = formSource(nil) | ||
|
||
// TrySet tries to set a value by request's form source (like map[string][]string) | ||
func (form formSource) TrySet(value reflect.Value, field reflect.StructField, tagValue string, opt setOptions) (isSetted bool, err error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the same above, thanks! |
||
return setByForm(value, field, form, tagValue, opt) | ||
} | ||
|
||
func mappingByPtr(ptr interface{}, setter setter, tag string) error { | ||
_, err := mapping(reflect.ValueOf(ptr), emptyField, setter, tag) | ||
return err | ||
} | ||
|
||
func mapping(value reflect.Value, field reflect.StructField, form map[string][]string, tag string) (bool, error) { | ||
func mapping(value reflect.Value, field reflect.StructField, setter setter, tag string) (bool, error) { | ||
var vKind = value.Kind() | ||
|
||
if vKind == reflect.Ptr { | ||
|
@@ -71,7 +60,7 @@ func mapping(value reflect.Value, field reflect.StructField, form map[string][]s | |
isNew = true | ||
vPtr = reflect.New(value.Type().Elem()) | ||
} | ||
isSetted, err := mapping(vPtr.Elem(), field, form, tag) | ||
isSetted, err := mapping(vPtr.Elem(), field, setter, tag) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
@@ -81,7 +70,7 @@ func mapping(value reflect.Value, field reflect.StructField, form map[string][]s | |
return isSetted, nil | ||
} | ||
|
||
ok, err := tryToSetValue(value, field, form, tag) | ||
ok, err := tryToSetValue(value, field, setter, tag) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
@@ -97,7 +86,7 @@ func mapping(value reflect.Value, field reflect.StructField, form map[string][]s | |
if !value.Field(i).CanSet() { | ||
continue | ||
} | ||
ok, err := mapping(value.Field(i), tValue.Field(i), form, tag) | ||
ok, err := mapping(value.Field(i), tValue.Field(i), setter, tag) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
@@ -108,9 +97,14 @@ func mapping(value reflect.Value, field reflect.StructField, form map[string][]s | |
return false, nil | ||
} | ||
|
||
func tryToSetValue(value reflect.Value, field reflect.StructField, form map[string][]string, tag string) (bool, error) { | ||
var tagValue, defaultValue string | ||
var isDefaultExists bool | ||
type setOptions struct { | ||
isDefaultExists bool | ||
defaultValue string | ||
} | ||
|
||
func tryToSetValue(value reflect.Value, field reflect.StructField, setter setter, tag string) (bool, error) { | ||
var tagValue string | ||
var setOpt setOptions | ||
|
||
tagValue = field.Tag.Get(tag) | ||
tagValue, opts := head(tagValue, ",") | ||
|
@@ -132,25 +126,29 @@ func tryToSetValue(value reflect.Value, field reflect.StructField, form map[stri | |
k, v := head(opt, "=") | ||
switch k { | ||
case "default": | ||
isDefaultExists = true | ||
defaultValue = v | ||
setOpt.isDefaultExists = true | ||
setOpt.defaultValue = v | ||
} | ||
} | ||
|
||
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) { | ||
vs, ok := form[tagValue] | ||
if !ok && !isDefaultExists { | ||
if !ok && !opt.isDefaultExists { | ||
return false, nil | ||
} | ||
|
||
switch value.Kind() { | ||
case reflect.Slice: | ||
if !ok { | ||
vs = []string{defaultValue} | ||
vs = []string{opt.defaultValue} | ||
} | ||
return true, setSlice(vs, value, field) | ||
case reflect.Array: | ||
if !ok { | ||
vs = []string{defaultValue} | ||
vs = []string{opt.defaultValue} | ||
} | ||
if len(vs) != value.Len() { | ||
return false, fmt.Errorf("%q is not valid value for %s", vs, value.Type().String()) | ||
|
@@ -159,7 +157,7 @@ func tryToSetValue(value reflect.Value, field reflect.StructField, form map[stri | |
default: | ||
var val string | ||
if !ok { | ||
val = defaultValue | ||
val = opt.defaultValue | ||
} | ||
|
||
if len(vs) > 0 { | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add some notes?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please not use
// setter -
style, thanks!