barometer: update DMA's vendoring packages
[barometer.git] / src / dma / vendor / github.com / labstack / echo / bind.go
index 38e0715..4998e25 100644 (file)
@@ -31,9 +31,9 @@ type (
 func (b *DefaultBinder) Bind(i interface{}, c Context) (err error) {
        req := c.Request()
        if req.ContentLength == 0 {
-               if req.Method == GET || req.Method == DELETE {
+               if req.Method == http.MethodGet || req.Method == http.MethodDelete {
                        if err = b.bindData(i, c.QueryParams(), "query"); err != nil {
-                               return NewHTTPError(http.StatusBadRequest, err.Error())
+                               return NewHTTPError(http.StatusBadRequest, err.Error()).SetInternal(err)
                        }
                        return
                }
@@ -44,30 +44,32 @@ func (b *DefaultBinder) Bind(i interface{}, c Context) (err error) {
        case strings.HasPrefix(ctype, MIMEApplicationJSON):
                if err = json.NewDecoder(req.Body).Decode(i); err != nil {
                        if ute, ok := err.(*json.UnmarshalTypeError); ok {
-                               return NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Unmarshal type error: expected=%v, got=%v, offset=%v", ute.Type, ute.Value, ute.Offset))
+                               return NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Unmarshal type error: expected=%v, got=%v, field=%v, offset=%v", ute.Type, ute.Value, ute.Field, ute.Offset)).SetInternal(err)
                        } else if se, ok := err.(*json.SyntaxError); ok {
-                               return NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Syntax error: offset=%v, error=%v", se.Offset, se.Error()))
+                               return NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Syntax error: offset=%v, error=%v", se.Offset, se.Error())).SetInternal(err)
                        } else {
-                               return NewHTTPError(http.StatusBadRequest, err.Error())
+                               return NewHTTPError(http.StatusBadRequest, err.Error()).SetInternal(err)
                        }
+                       return NewHTTPError(http.StatusBadRequest, err.Error())
                }
        case strings.HasPrefix(ctype, MIMEApplicationXML), strings.HasPrefix(ctype, MIMETextXML):
                if err = xml.NewDecoder(req.Body).Decode(i); err != nil {
                        if ute, ok := err.(*xml.UnsupportedTypeError); ok {
-                               return NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Unsupported type error: type=%v, error=%v", ute.Type, ute.Error()))
+                               return NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Unsupported type error: type=%v, error=%v", ute.Type, ute.Error())).SetInternal(err)
                        } else if se, ok := err.(*xml.SyntaxError); ok {
-                               return NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Syntax error: line=%v, error=%v", se.Line, se.Error()))
+                               return NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Syntax error: line=%v, error=%v", se.Line, se.Error())).SetInternal(err)
                        } else {
-                               return NewHTTPError(http.StatusBadRequest, err.Error())
+                               return NewHTTPError(http.StatusBadRequest, err.Error()).SetInternal(err)
                        }
+                       return NewHTTPError(http.StatusBadRequest, err.Error())
                }
        case strings.HasPrefix(ctype, MIMEApplicationForm), strings.HasPrefix(ctype, MIMEMultipartForm):
                params, err := c.FormParams()
                if err != nil {
-                       return NewHTTPError(http.StatusBadRequest, err.Error())
+                       return NewHTTPError(http.StatusBadRequest, err.Error()).SetInternal(err)
                }
                if err = b.bindData(i, params, "form"); err != nil {
-                       return NewHTTPError(http.StatusBadRequest, err.Error())
+                       return NewHTTPError(http.StatusBadRequest, err.Error()).SetInternal(err)
                }
        default:
                return ErrUnsupportedMediaType
@@ -96,14 +98,29 @@ func (b *DefaultBinder) bindData(ptr interface{}, data map[string][]string, tag
                        inputFieldName = typeField.Name
                        // If tag is nil, we inspect if the field is a struct.
                        if _, ok := bindUnmarshaler(structField); !ok && structFieldKind == reflect.Struct {
-                               err := b.bindData(structField.Addr().Interface(), data, tag)
-                               if err != nil {
+                               if err := b.bindData(structField.Addr().Interface(), data, tag); err != nil {
                                        return err
                                }
                                continue
                        }
                }
+
                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.
+                       inputFieldName = strings.ToLower(inputFieldName)
+                       for k, v := range data {
+                               if strings.ToLower(k) == inputFieldName {
+                                       inputValue = v
+                                       exists = true
+                                       break
+                               }
+                       }
+               }
+
                if !exists {
                        continue
                }
@@ -126,10 +143,9 @@ func (b *DefaultBinder) bindData(ptr interface{}, data map[string][]string, tag
                                }
                        }
                        val.Field(i).Set(slice)
-               } else {
-                       if err := setWithProperType(typeField.Type.Kind(), inputValue[0], structField); err != nil {
-                               return err
-                       }
+               } else if err := setWithProperType(typeField.Type.Kind(), inputValue[0], structField); err != nil {
+                       return err
+
                }
        }
        return nil