summaryrefslogtreecommitdiff
path: root/vendor/github.com/gin-gonic/gin/binding/form_mapping.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/gin-gonic/gin/binding/form_mapping.go')
-rw-r--r--vendor/github.com/gin-gonic/gin/binding/form_mapping.go28
1 files changed, 28 insertions, 0 deletions
diff --git a/vendor/github.com/gin-gonic/gin/binding/form_mapping.go b/vendor/github.com/gin-gonic/gin/binding/form_mapping.go
index 540bbbb84..db235e566 100644
--- a/vendor/github.com/gin-gonic/gin/binding/form_mapping.go
+++ b/vendor/github.com/gin-gonic/gin/binding/form_mapping.go
@@ -7,6 +7,7 @@ package binding
import (
"errors"
"fmt"
+ "mime/multipart"
"reflect"
"strconv"
"strings"
@@ -164,6 +165,23 @@ func tryToSetValue(value reflect.Value, field reflect.StructField, setter setter
return setter.TrySet(value, field, tagValue, setOpt)
}
+// BindUnmarshaler is the interface used to wrap the UnmarshalParam method.
+type BindUnmarshaler interface {
+ // UnmarshalParam decodes and assigns a value from an form or query param.
+ UnmarshalParam(param string) error
+}
+
+// trySetCustom tries to set a custom type value
+// If the value implements the BindUnmarshaler interface, it will be used to set the value, we will return `true`
+// to skip the default value setting.
+func trySetCustom(val string, value reflect.Value) (isSet bool, err error) {
+ switch v := value.Addr().Interface().(type) {
+ case BindUnmarshaler:
+ return true, v.UnmarshalParam(val)
+ }
+ return false, nil
+}
+
func setByForm(value reflect.Value, field reflect.StructField, form map[string][]string, tagValue string, opt setOptions) (isSet bool, err error) {
vs, ok := form[tagValue]
if !ok && !opt.isDefaultExists {
@@ -193,6 +211,9 @@ func setByForm(value reflect.Value, field reflect.StructField, form map[string][
if len(vs) > 0 {
val = vs[0]
}
+ if ok, err := trySetCustom(val, value); ok {
+ return ok, err
+ }
return true, setWithProperType(val, value, field)
}
}
@@ -235,10 +256,17 @@ func setWithProperType(val string, value reflect.Value, field reflect.StructFiel
switch value.Interface().(type) {
case time.Time:
return setTimeField(val, field, value)
+ case multipart.FileHeader:
+ return nil
}
return json.Unmarshal(bytesconv.StringToBytes(val), value.Addr().Interface())
case reflect.Map:
return json.Unmarshal(bytesconv.StringToBytes(val), value.Addr().Interface())
+ case reflect.Ptr:
+ if !value.Elem().IsValid() {
+ value.Set(reflect.New(value.Type().Elem()))
+ }
+ return setWithProperType(val, value.Elem(), field)
default:
return errUnknownType
}