2024-03-09 11:21:24 +02:00
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
// SPDX-FileCopyrightText: © 2015 LabStack LLC and Echo contributors
|
|
|
|
|
2016-05-01 21:38:51 +02:00
|
|
|
package echo
|
|
|
|
|
|
|
|
import (
|
2019-06-09 18:39:54 +02:00
|
|
|
"encoding"
|
2016-05-01 21:38:51 +02:00
|
|
|
"encoding/xml"
|
|
|
|
"errors"
|
2016-08-01 22:27:00 +02:00
|
|
|
"fmt"
|
2024-10-06 16:42:24 +02:00
|
|
|
"mime/multipart"
|
2016-05-01 21:38:51 +02:00
|
|
|
"net/http"
|
|
|
|
"reflect"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2024-03-09 17:53:07 +02:00
|
|
|
// Binder is the interface that wraps the Bind method.
|
|
|
|
type Binder interface {
|
|
|
|
Bind(i interface{}, c Context) error
|
|
|
|
}
|
2016-05-01 21:38:51 +02:00
|
|
|
|
2024-03-09 17:53:07 +02:00
|
|
|
// DefaultBinder is the default implementation of the Binder interface.
|
|
|
|
type DefaultBinder struct{}
|
2016-12-15 20:23:14 +02:00
|
|
|
|
2024-03-09 17:53:07 +02:00
|
|
|
// BindUnmarshaler is the interface used to wrap the UnmarshalParam method.
|
|
|
|
// Types that don't implement this, but do implement encoding.TextUnmarshaler
|
|
|
|
// will use that interface instead.
|
|
|
|
type BindUnmarshaler interface {
|
|
|
|
// UnmarshalParam decodes and assigns a value from an form or query param.
|
|
|
|
UnmarshalParam(param string) error
|
|
|
|
}
|
2016-05-01 21:38:51 +02:00
|
|
|
|
2024-03-11 22:49:58 +02:00
|
|
|
// bindMultipleUnmarshaler is used by binder to unmarshal multiple values from request at once to
|
|
|
|
// type implementing this interface. For example request could have multiple query fields `?a=1&a=2&b=test` in that case
|
|
|
|
// for `a` following slice `["1", "2"] will be passed to unmarshaller.
|
|
|
|
type bindMultipleUnmarshaler interface {
|
|
|
|
UnmarshalParams(params []string) error
|
|
|
|
}
|
|
|
|
|
2020-11-12 12:28:45 +02:00
|
|
|
// BindPathParams binds path params to bindable object
|
|
|
|
func (b *DefaultBinder) BindPathParams(c Context, i interface{}) error {
|
2019-06-27 19:52:17 +02:00
|
|
|
names := c.ParamNames()
|
|
|
|
values := c.ParamValues()
|
|
|
|
params := map[string][]string{}
|
|
|
|
for i, name := range names {
|
|
|
|
params[name] = []string{values[i]}
|
2019-06-21 15:12:55 +02:00
|
|
|
}
|
|
|
|
if err := b.bindData(i, params, "param"); err != nil {
|
|
|
|
return NewHTTPError(http.StatusBadRequest, err.Error()).SetInternal(err)
|
|
|
|
}
|
2020-11-12 12:28:45 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// BindQueryParams binds query params to bindable object
|
|
|
|
func (b *DefaultBinder) BindQueryParams(c Context, i interface{}) error {
|
|
|
|
if err := b.bindData(i, c.QueryParams(), "query"); err != nil {
|
2019-09-30 19:56:32 +02:00
|
|
|
return NewHTTPError(http.StatusBadRequest, err.Error()).SetInternal(err)
|
|
|
|
}
|
2020-11-12 12:28:45 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// BindBody binds request body contents to bindable object
|
|
|
|
// NB: then binding forms take note that this implementation uses standard library form parsing
|
|
|
|
// which parses form data from BOTH URL and BODY if content type is not MIMEMultipartForm
|
|
|
|
// See non-MIMEMultipartForm: https://golang.org/pkg/net/http/#Request.ParseForm
|
|
|
|
// See MIMEMultipartForm: https://golang.org/pkg/net/http/#Request.ParseMultipartForm
|
|
|
|
func (b *DefaultBinder) BindBody(c Context, i interface{}) (err error) {
|
|
|
|
req := c.Request()
|
2016-09-23 07:53:44 +02:00
|
|
|
if req.ContentLength == 0 {
|
2019-09-30 19:56:32 +02:00
|
|
|
return
|
2016-05-01 21:38:51 +02:00
|
|
|
}
|
2020-11-12 12:28:45 +02:00
|
|
|
|
2017-03-12 08:42:24 +02:00
|
|
|
ctype := req.Header.Get(HeaderContentType)
|
2016-05-01 21:38:51 +02:00
|
|
|
switch {
|
|
|
|
case strings.HasPrefix(ctype, MIMEApplicationJSON):
|
2021-07-05 21:33:19 +02:00
|
|
|
if err = c.Echo().JSONSerializer.Deserialize(c, i); err != nil {
|
|
|
|
switch err.(type) {
|
|
|
|
case *HTTPError:
|
|
|
|
return err
|
|
|
|
default:
|
|
|
|
return NewHTTPError(http.StatusBadRequest, err.Error()).SetInternal(err)
|
2016-08-01 22:27:00 +02:00
|
|
|
}
|
2016-05-01 21:38:51 +02:00
|
|
|
}
|
2017-02-28 22:04:29 +02:00
|
|
|
case strings.HasPrefix(ctype, MIMEApplicationXML), strings.HasPrefix(ctype, MIMETextXML):
|
2016-09-23 07:53:44 +02:00
|
|
|
if err = xml.NewDecoder(req.Body).Decode(i); err != nil {
|
2016-08-01 22:27:00 +02:00
|
|
|
if ute, ok := err.(*xml.UnsupportedTypeError); ok {
|
2018-09-28 19:41:13 +02:00
|
|
|
return NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Unsupported type error: type=%v, error=%v", ute.Type, ute.Error())).SetInternal(err)
|
2016-08-01 22:27:00 +02:00
|
|
|
} else if se, ok := err.(*xml.SyntaxError); ok {
|
2018-09-28 19:41:13 +02:00
|
|
|
return NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Syntax error: line=%v, error=%v", se.Line, se.Error())).SetInternal(err)
|
2016-08-01 22:27:00 +02:00
|
|
|
}
|
2019-01-28 17:59:36 +02:00
|
|
|
return NewHTTPError(http.StatusBadRequest, err.Error()).SetInternal(err)
|
2016-05-01 21:38:51 +02:00
|
|
|
}
|
2024-10-06 16:42:24 +02:00
|
|
|
case strings.HasPrefix(ctype, MIMEApplicationForm):
|
2016-09-23 07:53:44 +02:00
|
|
|
params, err := c.FormParams()
|
|
|
|
if err != nil {
|
2018-09-28 19:41:13 +02:00
|
|
|
return NewHTTPError(http.StatusBadRequest, err.Error()).SetInternal(err)
|
2016-09-23 07:53:44 +02:00
|
|
|
}
|
2016-12-10 18:38:27 +02:00
|
|
|
if err = b.bindData(i, params, "form"); err != nil {
|
2018-09-28 19:41:13 +02:00
|
|
|
return NewHTTPError(http.StatusBadRequest, err.Error()).SetInternal(err)
|
2016-05-01 21:38:51 +02:00
|
|
|
}
|
2024-10-06 16:42:24 +02:00
|
|
|
case strings.HasPrefix(ctype, MIMEMultipartForm):
|
|
|
|
params, err := c.MultipartForm()
|
|
|
|
if err != nil {
|
|
|
|
return NewHTTPError(http.StatusBadRequest, err.Error()).SetInternal(err)
|
|
|
|
}
|
|
|
|
if err = b.bindData(i, params.Value, "form", params.File); err != nil {
|
|
|
|
return NewHTTPError(http.StatusBadRequest, err.Error()).SetInternal(err)
|
|
|
|
}
|
2016-09-23 07:53:44 +02:00
|
|
|
default:
|
|
|
|
return ErrUnsupportedMediaType
|
2016-05-01 21:38:51 +02:00
|
|
|
}
|
2020-11-12 12:28:45 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-25 14:50:49 +02:00
|
|
|
// BindHeaders binds HTTP headers to a bindable object
|
|
|
|
func (b *DefaultBinder) BindHeaders(c Context, i interface{}) error {
|
|
|
|
if err := b.bindData(i, c.Request().Header, "header"); err != nil {
|
|
|
|
return NewHTTPError(http.StatusBadRequest, err.Error()).SetInternal(err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-12 12:28:45 +02:00
|
|
|
// Bind implements the `Binder#Bind` function.
|
2020-12-20 11:05:42 +02:00
|
|
|
// Binding is done in following order: 1) path params; 2) query params; 3) request body. Each step COULD override previous
|
|
|
|
// step binded values. For single source binding use their own methods BindBody, BindQueryParams, BindPathParams.
|
2020-11-12 12:28:45 +02:00
|
|
|
func (b *DefaultBinder) Bind(i interface{}, c Context) (err error) {
|
|
|
|
if err := b.BindPathParams(c, i); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-01-24 11:28:48 +02:00
|
|
|
// Only bind query parameters for GET/DELETE/HEAD to avoid unexpected behavior with destination struct binding from body.
|
|
|
|
// For example a request URL `&id=1&lang=en` with body `{"id":100,"lang":"de"}` would lead to precedence issues.
|
|
|
|
// The HTTP method check restores pre-v4.1.11 behavior to avoid these problems (see issue #1670)
|
2023-05-12 19:14:59 +02:00
|
|
|
method := c.Request().Method
|
2022-01-24 11:28:48 +02:00
|
|
|
if method == http.MethodGet || method == http.MethodDelete || method == http.MethodHead {
|
2020-12-20 11:05:42 +02:00
|
|
|
if err = b.BindQueryParams(c, i); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-11-12 12:28:45 +02:00
|
|
|
}
|
|
|
|
return b.BindBody(c, i)
|
2016-05-01 21:38:51 +02:00
|
|
|
}
|
|
|
|
|
2021-01-05 12:04:24 +02:00
|
|
|
// bindData will bind data ONLY fields in destination struct that have EXPLICIT tag
|
2024-10-06 16:42:24 +02:00
|
|
|
func (b *DefaultBinder) bindData(destination interface{}, data map[string][]string, tag string, files ...map[string][]*multipart.FileHeader) error {
|
|
|
|
if destination == nil || (len(data) == 0 && len(files) == 0) {
|
2019-06-27 19:52:17 +02:00
|
|
|
return nil
|
|
|
|
}
|
2021-01-05 12:04:24 +02:00
|
|
|
typ := reflect.TypeOf(destination).Elem()
|
|
|
|
val := reflect.ValueOf(destination).Elem()
|
2016-05-01 21:38:51 +02:00
|
|
|
|
2023-12-20 15:32:51 +02:00
|
|
|
// Support binding to limited Map destinations:
|
|
|
|
// - map[string][]string,
|
|
|
|
// - map[string]string <-- (binds first value from data slice)
|
|
|
|
// - map[string]interface{}
|
|
|
|
// You are better off binding to struct but there are user who want this map feature. Source of data for these cases are:
|
|
|
|
// params,query,header,form as these sources produce string values, most of the time slice of strings, actually.
|
|
|
|
if typ.Kind() == reflect.Map && typ.Key().Kind() == reflect.String {
|
|
|
|
k := typ.Elem().Kind()
|
|
|
|
isElemInterface := k == reflect.Interface
|
|
|
|
isElemString := k == reflect.String
|
|
|
|
isElemSliceOfStrings := k == reflect.Slice && typ.Elem().Elem().Kind() == reflect.String
|
|
|
|
if !(isElemSliceOfStrings || isElemString || isElemInterface) {
|
|
|
|
return nil
|
|
|
|
}
|
2024-02-06 17:18:12 +02:00
|
|
|
if val.IsNil() {
|
|
|
|
val.Set(reflect.MakeMap(typ))
|
|
|
|
}
|
2019-07-15 06:35:59 +02:00
|
|
|
for k, v := range data {
|
2023-12-20 15:32:51 +02:00
|
|
|
if isElemString {
|
|
|
|
val.SetMapIndex(reflect.ValueOf(k), reflect.ValueOf(v[0]))
|
2024-07-22 06:33:01 +02:00
|
|
|
} else if isElemInterface {
|
|
|
|
// To maintain backward compatibility, we always bind to the first string value
|
|
|
|
// and not the slice of strings when dealing with map[string]interface{}{}
|
|
|
|
val.SetMapIndex(reflect.ValueOf(k), reflect.ValueOf(v[0]))
|
2023-12-20 15:32:51 +02:00
|
|
|
} else {
|
|
|
|
val.SetMapIndex(reflect.ValueOf(k), reflect.ValueOf(v))
|
|
|
|
}
|
2019-07-15 06:35:59 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-10-24 23:59:52 +02:00
|
|
|
// !struct
|
2016-06-23 16:40:14 +02:00
|
|
|
if typ.Kind() != reflect.Struct {
|
2021-05-25 14:50:49 +02:00
|
|
|
if tag == "param" || tag == "query" || tag == "header" {
|
2021-04-06 09:05:33 +02:00
|
|
|
// incompatible type, data is probably to be found in the body
|
|
|
|
return nil
|
|
|
|
}
|
2018-02-21 20:44:17 +02:00
|
|
|
return errors.New("binding element must be a struct")
|
2016-06-23 16:40:14 +02:00
|
|
|
}
|
|
|
|
|
2016-05-01 21:38:51 +02:00
|
|
|
for i := 0; i < typ.NumField(); i++ {
|
|
|
|
typeField := typ.Field(i)
|
|
|
|
structField := val.Field(i)
|
2021-04-25 03:50:14 +02:00
|
|
|
if typeField.Anonymous {
|
2021-04-29 03:22:01 +02:00
|
|
|
if structField.Kind() == reflect.Ptr {
|
2021-04-25 03:50:14 +02:00
|
|
|
structField = structField.Elem()
|
|
|
|
}
|
|
|
|
}
|
2016-05-01 21:38:51 +02:00
|
|
|
if !structField.CanSet() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
structFieldKind := structField.Kind()
|
2016-12-10 18:38:27 +02:00
|
|
|
inputFieldName := typeField.Tag.Get(tag)
|
2024-03-13 22:07:08 +02:00
|
|
|
if typeField.Anonymous && structFieldKind == reflect.Struct && inputFieldName != "" {
|
2021-04-29 03:22:01 +02:00
|
|
|
// if anonymous struct with query/param/form tags, report an error
|
|
|
|
return errors.New("query/param/form tags are not allowed with anonymous struct field")
|
2021-04-25 03:50:14 +02:00
|
|
|
}
|
2016-05-01 21:38:51 +02:00
|
|
|
|
|
|
|
if inputFieldName == "" {
|
2021-01-05 12:04:24 +02:00
|
|
|
// If tag is nil, we inspect if the field is a not BindUnmarshaler struct and try to bind data into it (might contains fields with tags).
|
2024-03-13 22:07:08 +02:00
|
|
|
// structs that implement BindUnmarshaler are bound only when they have explicit tag
|
2020-01-08 23:40:52 +02:00
|
|
|
if _, ok := structField.Addr().Interface().(BindUnmarshaler); !ok && structFieldKind == reflect.Struct {
|
2018-09-07 16:58:04 +02:00
|
|
|
if err := b.bindData(structField.Addr().Interface(), data, tag); err != nil {
|
2016-05-01 21:38:51 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2021-01-05 12:04:24 +02:00
|
|
|
// does not have explicit tag and is not an ordinary struct - so move to next field
|
|
|
|
continue
|
2016-05-01 21:38:51 +02:00
|
|
|
}
|
2018-05-30 23:50:24 +02:00
|
|
|
|
2024-10-06 16:42:24 +02:00
|
|
|
// Handle multiple file uploads ([]*multipart.FileHeader, *multipart.FileHeader, []multipart.FileHeader)
|
|
|
|
if len(files) > 0 {
|
|
|
|
for _, fileMap := range files {
|
|
|
|
fileHeaders, exists := fileMap[inputFieldName]
|
|
|
|
if exists {
|
|
|
|
if structField.Type() == reflect.TypeOf([]*multipart.FileHeader(nil)) {
|
|
|
|
structField.Set(reflect.ValueOf(fileHeaders))
|
|
|
|
continue
|
|
|
|
} else if structField.Type() == reflect.TypeOf([]multipart.FileHeader(nil)) {
|
|
|
|
var headers []multipart.FileHeader
|
|
|
|
for _, fileHeader := range fileHeaders {
|
|
|
|
headers = append(headers, *fileHeader)
|
|
|
|
}
|
|
|
|
structField.Set(reflect.ValueOf(headers))
|
|
|
|
continue
|
|
|
|
} else if structField.Type() == reflect.TypeOf(&multipart.FileHeader{}) {
|
|
|
|
|
|
|
|
if len(fileHeaders) > 0 {
|
|
|
|
structField.Set(reflect.ValueOf(fileHeaders[0]))
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
} else if structField.Type() == reflect.TypeOf(multipart.FileHeader{}) {
|
|
|
|
if len(fileHeaders) > 0 {
|
|
|
|
structField.Set(reflect.ValueOf(*fileHeaders[0]))
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-31 00:09:39 +02:00
|
|
|
inputValue, exists := data[inputFieldName]
|
|
|
|
if !exists {
|
|
|
|
// Go json.Unmarshal supports case insensitive binding. However the
|
|
|
|
// url params are bound case sensitive which is inconsistent. To
|
|
|
|
// fix this we must check all of the map values in a
|
|
|
|
// case-insensitive search.
|
|
|
|
for k, v := range data {
|
2020-01-08 23:40:52 +02:00
|
|
|
if strings.EqualFold(k, inputFieldName) {
|
2018-05-31 00:09:39 +02:00
|
|
|
inputValue = v
|
|
|
|
exists = true
|
|
|
|
break
|
|
|
|
}
|
2018-05-30 23:50:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-01 21:38:51 +02:00
|
|
|
if !exists {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-03-13 22:07:08 +02:00
|
|
|
// NOTE: algorithm here is not particularly sophisticated. It probably does not work with absurd types like `**[]*int`
|
|
|
|
// but it is smart enough to handle niche cases like `*int`,`*[]string`,`[]*int` .
|
|
|
|
|
|
|
|
// try unmarshalling first, in case we're dealing with an alias to an array type
|
2024-03-11 22:49:58 +02:00
|
|
|
if ok, err := unmarshalInputsToField(typeField.Type.Kind(), inputValue, structField); ok {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if ok, err := unmarshalInputToField(typeField.Type.Kind(), inputValue[0], structField); ok {
|
2016-12-23 20:01:42 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-03-13 22:07:08 +02:00
|
|
|
// we could be dealing with pointer to slice `*[]string` so dereference it. There are wierd OpenAPI generators
|
|
|
|
// that could create struct fields like that.
|
|
|
|
if structFieldKind == reflect.Pointer {
|
|
|
|
structFieldKind = structField.Elem().Kind()
|
|
|
|
structField = structField.Elem()
|
|
|
|
}
|
|
|
|
|
|
|
|
if structFieldKind == reflect.Slice {
|
2016-05-01 21:38:51 +02:00
|
|
|
sliceOf := structField.Type().Elem().Kind()
|
2024-03-13 22:07:08 +02:00
|
|
|
numElems := len(inputValue)
|
2016-05-01 21:38:51 +02:00
|
|
|
slice := reflect.MakeSlice(structField.Type(), numElems, numElems)
|
2016-09-23 07:53:44 +02:00
|
|
|
for j := 0; j < numElems; j++ {
|
|
|
|
if err := setWithProperType(sliceOf, inputValue[j], slice.Index(j)); err != nil {
|
2016-05-01 21:38:51 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2024-03-13 22:07:08 +02:00
|
|
|
structField.Set(slice)
|
|
|
|
continue
|
|
|
|
}
|
2018-09-07 16:58:04 +02:00
|
|
|
|
2024-03-13 22:07:08 +02:00
|
|
|
if err := setWithProperType(structFieldKind, inputValue[0], structField); err != nil {
|
|
|
|
return err
|
2016-05-01 21:38:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func setWithProperType(valueKind reflect.Kind, val string, structField reflect.Value) error {
|
2016-12-23 20:01:42 +02:00
|
|
|
// But also call it here, in case we're dealing with an array of BindUnmarshalers
|
2024-03-11 22:49:58 +02:00
|
|
|
if ok, err := unmarshalInputToField(valueKind, val, structField); ok {
|
2016-12-23 20:01:42 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-05-01 21:38:51 +02:00
|
|
|
switch valueKind {
|
2017-04-28 20:07:20 +02:00
|
|
|
case reflect.Ptr:
|
|
|
|
return setWithProperType(structField.Elem().Kind(), val, structField.Elem())
|
2016-05-01 21:38:51 +02:00
|
|
|
case reflect.Int:
|
|
|
|
return setIntField(val, 0, structField)
|
|
|
|
case reflect.Int8:
|
|
|
|
return setIntField(val, 8, structField)
|
|
|
|
case reflect.Int16:
|
|
|
|
return setIntField(val, 16, structField)
|
|
|
|
case reflect.Int32:
|
|
|
|
return setIntField(val, 32, structField)
|
|
|
|
case reflect.Int64:
|
|
|
|
return setIntField(val, 64, structField)
|
|
|
|
case reflect.Uint:
|
|
|
|
return setUintField(val, 0, structField)
|
|
|
|
case reflect.Uint8:
|
|
|
|
return setUintField(val, 8, structField)
|
|
|
|
case reflect.Uint16:
|
|
|
|
return setUintField(val, 16, structField)
|
|
|
|
case reflect.Uint32:
|
|
|
|
return setUintField(val, 32, structField)
|
|
|
|
case reflect.Uint64:
|
|
|
|
return setUintField(val, 64, structField)
|
|
|
|
case reflect.Bool:
|
|
|
|
return setBoolField(val, structField)
|
|
|
|
case reflect.Float32:
|
|
|
|
return setFloatField(val, 32, structField)
|
|
|
|
case reflect.Float64:
|
|
|
|
return setFloatField(val, 64, structField)
|
|
|
|
case reflect.String:
|
|
|
|
structField.SetString(val)
|
|
|
|
default:
|
2016-12-23 20:01:42 +02:00
|
|
|
return errors.New("unknown type")
|
2016-05-01 21:38:51 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-03-11 22:49:58 +02:00
|
|
|
func unmarshalInputsToField(valueKind reflect.Kind, values []string, field reflect.Value) (bool, error) {
|
|
|
|
if valueKind == reflect.Ptr {
|
|
|
|
if field.IsNil() {
|
|
|
|
field.Set(reflect.New(field.Type().Elem()))
|
|
|
|
}
|
|
|
|
field = field.Elem()
|
2016-12-23 20:01:42 +02:00
|
|
|
}
|
|
|
|
|
2020-01-08 23:40:52 +02:00
|
|
|
fieldIValue := field.Addr().Interface()
|
2024-03-11 22:49:58 +02:00
|
|
|
unmarshaler, ok := fieldIValue.(bindMultipleUnmarshaler)
|
|
|
|
if !ok {
|
|
|
|
return false, nil
|
2019-06-09 18:39:54 +02:00
|
|
|
}
|
2024-03-11 22:49:58 +02:00
|
|
|
return true, unmarshaler.UnmarshalParams(values)
|
2016-12-15 20:23:14 +02:00
|
|
|
}
|
|
|
|
|
2024-03-11 22:49:58 +02:00
|
|
|
func unmarshalInputToField(valueKind reflect.Kind, val string, field reflect.Value) (bool, error) {
|
|
|
|
if valueKind == reflect.Ptr {
|
|
|
|
if field.IsNil() {
|
|
|
|
field.Set(reflect.New(field.Type().Elem()))
|
|
|
|
}
|
|
|
|
field = field.Elem()
|
2016-12-15 20:23:14 +02:00
|
|
|
}
|
2024-03-11 22:49:58 +02:00
|
|
|
|
|
|
|
fieldIValue := field.Addr().Interface()
|
|
|
|
switch unmarshaler := fieldIValue.(type) {
|
|
|
|
case BindUnmarshaler:
|
|
|
|
return true, unmarshaler.UnmarshalParam(val)
|
|
|
|
case encoding.TextUnmarshaler:
|
|
|
|
return true, unmarshaler.UnmarshalText([]byte(val))
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, nil
|
2016-12-15 20:23:14 +02:00
|
|
|
}
|
|
|
|
|
2016-05-01 21:38:51 +02:00
|
|
|
func setIntField(value string, bitSize int, field reflect.Value) error {
|
|
|
|
if value == "" {
|
|
|
|
value = "0"
|
|
|
|
}
|
|
|
|
intVal, err := strconv.ParseInt(value, 10, bitSize)
|
|
|
|
if err == nil {
|
|
|
|
field.SetInt(intVal)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func setUintField(value string, bitSize int, field reflect.Value) error {
|
|
|
|
if value == "" {
|
|
|
|
value = "0"
|
|
|
|
}
|
|
|
|
uintVal, err := strconv.ParseUint(value, 10, bitSize)
|
|
|
|
if err == nil {
|
|
|
|
field.SetUint(uintVal)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func setBoolField(value string, field reflect.Value) error {
|
|
|
|
if value == "" {
|
|
|
|
value = "false"
|
|
|
|
}
|
|
|
|
boolVal, err := strconv.ParseBool(value)
|
|
|
|
if err == nil {
|
|
|
|
field.SetBool(boolVal)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func setFloatField(value string, bitSize int, field reflect.Value) error {
|
|
|
|
if value == "" {
|
|
|
|
value = "0.0"
|
|
|
|
}
|
|
|
|
floatVal, err := strconv.ParseFloat(value, bitSize)
|
|
|
|
if err == nil {
|
|
|
|
field.SetFloat(floatVal)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|