2016-05-01 21:38:51 +02:00
|
|
|
package echo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"encoding/xml"
|
|
|
|
"errors"
|
2016-08-01 22:27:00 +02:00
|
|
|
"fmt"
|
2016-05-01 21:38:51 +02:00
|
|
|
"net/http"
|
|
|
|
"reflect"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
// Binder is the interface that wraps the Bind method.
|
|
|
|
Binder interface {
|
2016-12-09 20:24:14 +02:00
|
|
|
Bind(i interface{}, c Context) error
|
2016-05-01 21:38:51 +02:00
|
|
|
}
|
|
|
|
|
2016-12-10 18:38:27 +02:00
|
|
|
// DefaultBinder is the default implementation of the Binder interface.
|
|
|
|
DefaultBinder struct{}
|
2016-12-15 20:23:14 +02:00
|
|
|
|
|
|
|
// BindUnmarshaler is the interface used to wrap the UnmarshalParam method.
|
|
|
|
BindUnmarshaler interface {
|
2016-12-15 21:08:56 +02:00
|
|
|
// UnmarshalParam decodes and assigns a value from an form or query param.
|
2016-12-21 09:00:40 +02:00
|
|
|
UnmarshalParam(param string) error
|
2016-12-15 20:23:14 +02:00
|
|
|
}
|
2016-05-01 21:38:51 +02:00
|
|
|
)
|
|
|
|
|
2016-12-10 18:38:27 +02:00
|
|
|
// Bind implements the `Binder#Bind` function.
|
|
|
|
func (b *DefaultBinder) Bind(i interface{}, c Context) (err error) {
|
2016-05-01 21:38:51 +02:00
|
|
|
req := c.Request()
|
2016-09-23 07:53:44 +02:00
|
|
|
if req.Method == GET {
|
2016-12-10 18:38:27 +02:00
|
|
|
if err = b.bindData(i, c.QueryParams(), "query"); err != nil {
|
2016-09-23 07:53:44 +02:00
|
|
|
return NewHTTPError(http.StatusBadRequest, err.Error())
|
2016-06-24 04:43:05 +02:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2016-09-23 07:53:44 +02:00
|
|
|
ctype := req.Header.Get(HeaderContentType)
|
|
|
|
if req.ContentLength == 0 {
|
2017-01-03 06:12:06 +02:00
|
|
|
return NewHTTPError(http.StatusBadRequest, "Request body can't be empty")
|
2016-05-01 21:38:51 +02:00
|
|
|
}
|
|
|
|
switch {
|
|
|
|
case strings.HasPrefix(ctype, MIMEApplicationJSON):
|
2016-09-23 07:53:44 +02:00
|
|
|
if err = json.NewDecoder(req.Body).Decode(i); err != nil {
|
2016-08-01 22:27:00 +02:00
|
|
|
if ute, ok := err.(*json.UnmarshalTypeError); ok {
|
2017-01-03 06:12:06 +02:00
|
|
|
return NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Unmarshal type error: expected=%v, got=%v, offset=%v", ute.Type, ute.Value, ute.Offset))
|
2016-08-01 22:27:00 +02:00
|
|
|
} else if se, ok := err.(*json.SyntaxError); ok {
|
2017-01-03 06:12:06 +02:00
|
|
|
return NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Syntax error: offset=%v, error=%v", se.Offset, se.Error()))
|
2016-08-01 22:27:00 +02:00
|
|
|
} else {
|
2016-09-23 07:53:44 +02:00
|
|
|
return NewHTTPError(http.StatusBadRequest, err.Error())
|
2016-08-01 22:27:00 +02:00
|
|
|
}
|
2016-05-01 21:38:51 +02:00
|
|
|
}
|
|
|
|
case strings.HasPrefix(ctype, MIMEApplicationXML):
|
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 {
|
2017-01-03 06:12:06 +02:00
|
|
|
return NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Unsupported type error: type=%v, error=%v", ute.Type, ute.Error()))
|
2016-08-01 22:27:00 +02:00
|
|
|
} else if se, ok := err.(*xml.SyntaxError); ok {
|
2017-01-03 06:12:06 +02:00
|
|
|
return NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Syntax error: line=%v, error=%v", se.Line, se.Error()))
|
2016-08-01 22:27:00 +02:00
|
|
|
} else {
|
2016-09-23 07:53:44 +02:00
|
|
|
return NewHTTPError(http.StatusBadRequest, err.Error())
|
2016-08-01 22:27:00 +02:00
|
|
|
}
|
2016-05-01 21:38:51 +02:00
|
|
|
}
|
|
|
|
case strings.HasPrefix(ctype, MIMEApplicationForm), strings.HasPrefix(ctype, MIMEMultipartForm):
|
2016-09-23 07:53:44 +02:00
|
|
|
params, err := c.FormParams()
|
|
|
|
if err != nil {
|
|
|
|
return NewHTTPError(http.StatusBadRequest, err.Error())
|
|
|
|
}
|
2016-12-10 18:38:27 +02:00
|
|
|
if err = b.bindData(i, params, "form"); err != nil {
|
2016-09-23 07:53:44 +02:00
|
|
|
return NewHTTPError(http.StatusBadRequest, err.Error())
|
2016-05-01 21:38:51 +02:00
|
|
|
}
|
2016-09-23 07:53:44 +02:00
|
|
|
default:
|
|
|
|
return ErrUnsupportedMediaType
|
2016-05-01 21:38:51 +02:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-12-10 18:38:27 +02:00
|
|
|
func (b *DefaultBinder) bindData(ptr interface{}, data map[string][]string, tag string) error {
|
2016-05-01 21:38:51 +02:00
|
|
|
typ := reflect.TypeOf(ptr).Elem()
|
|
|
|
val := reflect.ValueOf(ptr).Elem()
|
|
|
|
|
2016-06-23 16:40:14 +02:00
|
|
|
if typ.Kind() != reflect.Struct {
|
2017-01-03 06:12:06 +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)
|
|
|
|
if !structField.CanSet() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
structFieldKind := structField.Kind()
|
2016-12-10 18:38:27 +02:00
|
|
|
inputFieldName := typeField.Tag.Get(tag)
|
2016-05-01 21:38:51 +02:00
|
|
|
|
|
|
|
if inputFieldName == "" {
|
|
|
|
inputFieldName = typeField.Name
|
2016-12-10 18:38:27 +02:00
|
|
|
// If tag is nil, we inspect if the field is a struct.
|
2016-05-01 21:38:51 +02:00
|
|
|
if structFieldKind == reflect.Struct {
|
2016-12-10 18:38:27 +02:00
|
|
|
err := b.bindData(structField.Addr().Interface(), data, tag)
|
2016-05-01 21:38:51 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2016-06-24 04:43:05 +02:00
|
|
|
inputValue, exists := data[inputFieldName]
|
2016-05-01 21:38:51 +02:00
|
|
|
if !exists {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-12-23 20:01:42 +02:00
|
|
|
// Call this first, in case we're dealing with an alias to an array type
|
|
|
|
if ok, err := unmarshalField(typeField.Type.Kind(), inputValue[0], structField); ok {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-05-01 21:38:51 +02:00
|
|
|
numElems := len(inputValue)
|
|
|
|
if structFieldKind == reflect.Slice && numElems > 0 {
|
|
|
|
sliceOf := structField.Type().Elem().Kind()
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
val.Field(i).Set(slice)
|
|
|
|
} else {
|
|
|
|
if err := setWithProperType(typeField.Type.Kind(), inputValue[0], structField); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
|
|
if ok, err := unmarshalField(valueKind, val, structField); ok {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-05-01 21:38:51 +02:00
|
|
|
switch valueKind {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2016-12-23 20:01:42 +02:00
|
|
|
func unmarshalField(valueKind reflect.Kind, val string, field reflect.Value) (bool, error) {
|
|
|
|
switch valueKind {
|
|
|
|
case reflect.Ptr:
|
|
|
|
return unmarshalFieldPtr(val, field)
|
|
|
|
default:
|
|
|
|
return unmarshalFieldNonPtr(val, field)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func unmarshalFieldNonPtr(value string, field reflect.Value) (bool, error) {
|
2016-12-15 20:23:14 +02:00
|
|
|
ptr := reflect.New(field.Type())
|
|
|
|
if ptr.CanInterface() {
|
|
|
|
iface := ptr.Interface()
|
|
|
|
if unmarshaler, ok := iface.(BindUnmarshaler); ok {
|
|
|
|
err := unmarshaler.UnmarshalParam(value)
|
|
|
|
field.Set(ptr.Elem())
|
2016-12-23 20:01:42 +02:00
|
|
|
return true, err
|
2016-12-15 20:23:14 +02:00
|
|
|
}
|
|
|
|
}
|
2016-12-23 20:01:42 +02:00
|
|
|
return false, nil
|
2016-12-15 20:23:14 +02:00
|
|
|
}
|
|
|
|
|
2016-12-23 20:01:42 +02:00
|
|
|
func unmarshalFieldPtr(value string, field reflect.Value) (bool, error) {
|
2016-12-15 20:23:14 +02:00
|
|
|
if field.IsNil() {
|
|
|
|
// Initialize the pointer to a nil value
|
|
|
|
field.Set(reflect.New(field.Type().Elem()))
|
|
|
|
}
|
2016-12-23 20:01:42 +02:00
|
|
|
return unmarshalFieldNonPtr(value, field.Elem())
|
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
|
|
|
|
}
|