2016-03-23 20:53:09 +01:00
|
|
|
// Copyright 2014 Alvaro J. Genial. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package form
|
|
|
|
|
|
|
|
import (
|
2016-05-04 17:48:12 +02:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/url"
|
|
|
|
"reflect"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
2016-03-23 20:53:09 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// NewDecoder returns a new form decoder.
|
|
|
|
func NewDecoder(r io.Reader) *decoder {
|
2016-07-21 21:10:33 +02:00
|
|
|
return &decoder{r, defaultDelimiter, defaultEscape, false, false}
|
2016-03-23 20:53:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// decoder decodes data from a form (application/x-www-form-urlencoded).
|
|
|
|
type decoder struct {
|
2016-07-21 21:10:33 +02:00
|
|
|
r io.Reader
|
|
|
|
d rune
|
|
|
|
e rune
|
|
|
|
ignoreUnknown bool
|
|
|
|
ignoreCase bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// DelimitWith sets r as the delimiter used for composite keys by decoder d and returns the latter; it is '.' by default.
|
|
|
|
func (d *decoder) DelimitWith(r rune) *decoder {
|
|
|
|
d.d = r
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
|
|
|
// EscapeWith sets r as the escape used for delimiters (and to escape itself) by decoder d and returns the latter; it is '\\' by default.
|
|
|
|
func (d *decoder) EscapeWith(r rune) *decoder {
|
|
|
|
d.e = r
|
|
|
|
return d
|
2016-03-23 20:53:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Decode reads in and decodes form-encoded data into dst.
|
|
|
|
func (d decoder) Decode(dst interface{}) error {
|
2016-05-04 17:48:12 +02:00
|
|
|
bs, err := ioutil.ReadAll(d.r)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
vs, err := url.ParseQuery(string(bs))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
v := reflect.ValueOf(dst)
|
2016-07-21 21:10:33 +02:00
|
|
|
return d.decodeNode(v, parseValues(d.d, d.e, vs, canIndexOrdinally(v)))
|
|
|
|
}
|
|
|
|
|
|
|
|
// IgnoreUnknownKeys if set to true it will make the decoder ignore values
|
|
|
|
// that are not found in the destination object instead of returning an error.
|
|
|
|
func (d *decoder) IgnoreUnknownKeys(ignoreUnknown bool) {
|
|
|
|
d.ignoreUnknown = ignoreUnknown
|
|
|
|
}
|
|
|
|
|
|
|
|
// IgnoreCase if set to true it will make the decoder try to set values in the
|
|
|
|
// destination object even if the case does not match.
|
|
|
|
func (d *decoder) IgnoreCase(ignoreCase bool) {
|
|
|
|
d.ignoreCase = ignoreCase
|
2016-03-23 20:53:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// DecodeString decodes src into dst.
|
2016-07-21 21:10:33 +02:00
|
|
|
func (d decoder) DecodeString(dst interface{}, src string) error {
|
2016-05-04 17:48:12 +02:00
|
|
|
vs, err := url.ParseQuery(src)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
v := reflect.ValueOf(dst)
|
2016-07-21 21:10:33 +02:00
|
|
|
return d.decodeNode(v, parseValues(d.d, d.e, vs, canIndexOrdinally(v)))
|
2016-03-23 20:53:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// DecodeValues decodes vs into dst.
|
2016-07-21 21:10:33 +02:00
|
|
|
func (d decoder) DecodeValues(dst interface{}, vs url.Values) error {
|
2016-05-04 17:48:12 +02:00
|
|
|
v := reflect.ValueOf(dst)
|
2016-07-21 21:10:33 +02:00
|
|
|
return d.decodeNode(v, parseValues(d.d, d.e, vs, canIndexOrdinally(v)))
|
2016-03-23 20:53:09 +01:00
|
|
|
}
|
|
|
|
|
2016-07-21 21:10:33 +02:00
|
|
|
// DecodeString decodes src into dst.
|
|
|
|
func DecodeString(dst interface{}, src string) error {
|
|
|
|
return NewDecoder(nil).DecodeString(dst, src)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DecodeValues decodes vs into dst.
|
|
|
|
func DecodeValues(dst interface{}, vs url.Values) error {
|
|
|
|
return NewDecoder(nil).DecodeValues(dst, vs)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d decoder) decodeNode(v reflect.Value, n node) (err error) {
|
2016-05-04 17:48:12 +02:00
|
|
|
defer func() {
|
|
|
|
if e := recover(); e != nil {
|
|
|
|
err = fmt.Errorf("%v", e)
|
|
|
|
}
|
|
|
|
}()
|
2016-03-23 20:53:09 +01:00
|
|
|
|
2016-05-04 17:48:12 +02:00
|
|
|
if v.Kind() == reflect.Slice {
|
|
|
|
return fmt.Errorf("could not decode directly into slice; use pointer to slice")
|
|
|
|
}
|
2016-07-21 21:10:33 +02:00
|
|
|
d.decodeValue(v, n)
|
2016-05-04 17:48:12 +02:00
|
|
|
return nil
|
2016-03-23 20:53:09 +01:00
|
|
|
}
|
|
|
|
|
2016-07-21 21:10:33 +02:00
|
|
|
func (d decoder) decodeValue(v reflect.Value, x interface{}) {
|
2016-05-04 17:48:12 +02:00
|
|
|
t := v.Type()
|
|
|
|
k := v.Kind()
|
2016-03-23 20:53:09 +01:00
|
|
|
|
2016-05-04 17:48:12 +02:00
|
|
|
if k == reflect.Ptr && v.IsNil() {
|
|
|
|
v.Set(reflect.New(t.Elem()))
|
|
|
|
}
|
2016-03-23 20:53:09 +01:00
|
|
|
|
2016-05-04 17:48:12 +02:00
|
|
|
if unmarshalValue(v, x) {
|
|
|
|
return
|
|
|
|
}
|
2016-03-23 20:53:09 +01:00
|
|
|
|
2016-05-04 17:48:12 +02:00
|
|
|
empty := isEmpty(x)
|
2016-03-23 20:53:09 +01:00
|
|
|
|
2016-05-04 17:48:12 +02:00
|
|
|
switch k {
|
|
|
|
case reflect.Ptr:
|
2016-07-21 21:10:33 +02:00
|
|
|
d.decodeValue(v.Elem(), x)
|
2016-05-04 17:48:12 +02:00
|
|
|
return
|
|
|
|
case reflect.Interface:
|
|
|
|
if !v.IsNil() {
|
2016-07-21 21:10:33 +02:00
|
|
|
d.decodeValue(v.Elem(), x)
|
2016-05-04 17:48:12 +02:00
|
|
|
return
|
2016-03-23 20:53:09 +01:00
|
|
|
|
2016-05-04 17:48:12 +02:00
|
|
|
} else if empty {
|
|
|
|
return // Allow nil interfaces only if empty.
|
|
|
|
} else {
|
|
|
|
panic("form: cannot decode non-empty value into into nil interface")
|
|
|
|
}
|
|
|
|
}
|
2016-03-23 20:53:09 +01:00
|
|
|
|
2016-05-04 17:48:12 +02:00
|
|
|
if empty {
|
|
|
|
v.Set(reflect.Zero(t)) // Treat the empty string as the zero value.
|
|
|
|
return
|
|
|
|
}
|
2016-03-23 20:53:09 +01:00
|
|
|
|
2016-05-04 17:48:12 +02:00
|
|
|
switch k {
|
|
|
|
case reflect.Struct:
|
|
|
|
if t.ConvertibleTo(timeType) {
|
2016-07-21 21:10:33 +02:00
|
|
|
d.decodeTime(v, x)
|
2016-05-04 17:48:12 +02:00
|
|
|
} else if t.ConvertibleTo(urlType) {
|
2016-07-21 21:10:33 +02:00
|
|
|
d.decodeURL(v, x)
|
2016-05-04 17:48:12 +02:00
|
|
|
} else {
|
2016-07-21 21:10:33 +02:00
|
|
|
d.decodeStruct(v, x)
|
2016-05-04 17:48:12 +02:00
|
|
|
}
|
|
|
|
case reflect.Slice:
|
2016-07-21 21:10:33 +02:00
|
|
|
d.decodeSlice(v, x)
|
2016-05-04 17:48:12 +02:00
|
|
|
case reflect.Array:
|
2016-07-21 21:10:33 +02:00
|
|
|
d.decodeArray(v, x)
|
2016-05-04 17:48:12 +02:00
|
|
|
case reflect.Map:
|
2016-07-21 21:10:33 +02:00
|
|
|
d.decodeMap(v, x)
|
2016-05-04 17:48:12 +02:00
|
|
|
case reflect.Invalid, reflect.Uintptr, reflect.UnsafePointer, reflect.Chan, reflect.Func:
|
|
|
|
panic(t.String() + " has unsupported kind " + k.String())
|
|
|
|
default:
|
2016-07-21 21:10:33 +02:00
|
|
|
d.decodeBasic(v, x)
|
2016-05-04 17:48:12 +02:00
|
|
|
}
|
2016-03-23 20:53:09 +01:00
|
|
|
}
|
|
|
|
|
2016-07-21 21:10:33 +02:00
|
|
|
func (d decoder) decodeStruct(v reflect.Value, x interface{}) {
|
2016-05-04 17:48:12 +02:00
|
|
|
t := v.Type()
|
|
|
|
for k, c := range getNode(x) {
|
2016-07-21 21:10:33 +02:00
|
|
|
if f, ok := findField(v, k, d.ignoreCase); !ok && k == "" {
|
2016-05-04 17:48:12 +02:00
|
|
|
panic(getString(x) + " cannot be decoded as " + t.String())
|
|
|
|
} else if !ok {
|
2016-07-21 21:10:33 +02:00
|
|
|
if !d.ignoreUnknown {
|
|
|
|
panic(k + " doesn't exist in " + t.String())
|
|
|
|
}
|
2016-05-04 17:48:12 +02:00
|
|
|
} else if !f.CanSet() {
|
|
|
|
panic(k + " cannot be set in " + t.String())
|
|
|
|
} else {
|
2016-07-21 21:10:33 +02:00
|
|
|
d.decodeValue(f, c)
|
2016-05-04 17:48:12 +02:00
|
|
|
}
|
|
|
|
}
|
2016-03-23 20:53:09 +01:00
|
|
|
}
|
|
|
|
|
2016-07-21 21:10:33 +02:00
|
|
|
func (d decoder) decodeMap(v reflect.Value, x interface{}) {
|
2016-05-04 17:48:12 +02:00
|
|
|
t := v.Type()
|
|
|
|
if v.IsNil() {
|
|
|
|
v.Set(reflect.MakeMap(t))
|
|
|
|
}
|
|
|
|
for k, c := range getNode(x) {
|
|
|
|
i := reflect.New(t.Key()).Elem()
|
2016-07-21 21:10:33 +02:00
|
|
|
d.decodeValue(i, k)
|
2016-03-23 20:53:09 +01:00
|
|
|
|
2016-05-04 17:48:12 +02:00
|
|
|
w := v.MapIndex(i)
|
|
|
|
if w.IsValid() { // We have an actual element value to decode into.
|
|
|
|
if w.Kind() == reflect.Interface {
|
|
|
|
w = w.Elem()
|
|
|
|
}
|
|
|
|
w = reflect.New(w.Type()).Elem()
|
|
|
|
} else if t.Elem().Kind() != reflect.Interface { // The map's element type is concrete.
|
|
|
|
w = reflect.New(t.Elem()).Elem()
|
|
|
|
} else {
|
|
|
|
// The best we can do here is to decode as either a string (for scalars) or a map[string]interface {} (for the rest).
|
|
|
|
// We could try to guess the type based on the string (e.g. true/false => bool) but that'll get ugly fast,
|
|
|
|
// especially if we have to guess the kind (slice vs. array vs. map) and index type (e.g. string, int, etc.)
|
|
|
|
switch c.(type) {
|
|
|
|
case node:
|
|
|
|
w = reflect.MakeMap(stringMapType)
|
|
|
|
case string:
|
|
|
|
w = reflect.New(stringType).Elem()
|
|
|
|
default:
|
|
|
|
panic("value is neither node nor string")
|
|
|
|
}
|
|
|
|
}
|
2016-03-23 20:53:09 +01:00
|
|
|
|
2016-07-21 21:10:33 +02:00
|
|
|
d.decodeValue(w, c)
|
2016-05-04 17:48:12 +02:00
|
|
|
v.SetMapIndex(i, w)
|
|
|
|
}
|
2016-03-23 20:53:09 +01:00
|
|
|
}
|
|
|
|
|
2016-07-21 21:10:33 +02:00
|
|
|
func (d decoder) decodeArray(v reflect.Value, x interface{}) {
|
2016-05-04 17:48:12 +02:00
|
|
|
t := v.Type()
|
|
|
|
for k, c := range getNode(x) {
|
|
|
|
i, err := strconv.Atoi(k)
|
|
|
|
if err != nil {
|
|
|
|
panic(k + " is not a valid index for type " + t.String())
|
|
|
|
}
|
|
|
|
if l := v.Len(); i >= l {
|
|
|
|
panic("index is above array size")
|
|
|
|
}
|
2016-07-21 21:10:33 +02:00
|
|
|
d.decodeValue(v.Index(i), c)
|
2016-05-04 17:48:12 +02:00
|
|
|
}
|
2016-03-23 20:53:09 +01:00
|
|
|
}
|
|
|
|
|
2016-07-21 21:10:33 +02:00
|
|
|
func (d decoder) decodeSlice(v reflect.Value, x interface{}) {
|
2016-05-04 17:48:12 +02:00
|
|
|
t := v.Type()
|
|
|
|
if t.Elem().Kind() == reflect.Uint8 {
|
|
|
|
// Allow, but don't require, byte slices to be encoded as a single string.
|
|
|
|
if s, ok := x.(string); ok {
|
|
|
|
v.SetBytes([]byte(s))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2016-03-23 20:53:09 +01:00
|
|
|
|
2016-05-04 17:48:12 +02:00
|
|
|
// NOTE: Implicit indexing is currently done at the parseValues level,
|
|
|
|
// so if if an implicitKey reaches here it will always replace the last.
|
|
|
|
implicit := 0
|
|
|
|
for k, c := range getNode(x) {
|
|
|
|
var i int
|
|
|
|
if k == implicitKey {
|
|
|
|
i = implicit
|
|
|
|
implicit++
|
|
|
|
} else {
|
|
|
|
explicit, err := strconv.Atoi(k)
|
|
|
|
if err != nil {
|
|
|
|
panic(k + " is not a valid index for type " + t.String())
|
|
|
|
}
|
|
|
|
i = explicit
|
|
|
|
implicit = explicit + 1
|
|
|
|
}
|
|
|
|
// "Extend" the slice if it's too short.
|
|
|
|
if l := v.Len(); i >= l {
|
|
|
|
delta := i - l + 1
|
|
|
|
v.Set(reflect.AppendSlice(v, reflect.MakeSlice(t, delta, delta)))
|
|
|
|
}
|
2016-07-21 21:10:33 +02:00
|
|
|
d.decodeValue(v.Index(i), c)
|
2016-05-04 17:48:12 +02:00
|
|
|
}
|
2016-03-23 20:53:09 +01:00
|
|
|
}
|
|
|
|
|
2016-07-21 21:10:33 +02:00
|
|
|
func (d decoder) decodeBasic(v reflect.Value, x interface{}) {
|
2016-05-04 17:48:12 +02:00
|
|
|
t := v.Type()
|
|
|
|
switch k, s := t.Kind(), getString(x); k {
|
|
|
|
case reflect.Bool:
|
|
|
|
if b, e := strconv.ParseBool(s); e == nil {
|
|
|
|
v.SetBool(b)
|
|
|
|
} else {
|
|
|
|
panic("could not parse bool from " + strconv.Quote(s))
|
|
|
|
}
|
|
|
|
case reflect.Int,
|
|
|
|
reflect.Int8,
|
|
|
|
reflect.Int16,
|
|
|
|
reflect.Int32,
|
|
|
|
reflect.Int64:
|
|
|
|
if i, e := strconv.ParseInt(s, 10, 64); e == nil {
|
|
|
|
v.SetInt(i)
|
|
|
|
} else {
|
|
|
|
panic("could not parse int from " + strconv.Quote(s))
|
|
|
|
}
|
|
|
|
case reflect.Uint,
|
|
|
|
reflect.Uint8,
|
|
|
|
reflect.Uint16,
|
|
|
|
reflect.Uint32,
|
|
|
|
reflect.Uint64:
|
|
|
|
if u, e := strconv.ParseUint(s, 10, 64); e == nil {
|
|
|
|
v.SetUint(u)
|
|
|
|
} else {
|
|
|
|
panic("could not parse uint from " + strconv.Quote(s))
|
|
|
|
}
|
|
|
|
case reflect.Float32,
|
|
|
|
reflect.Float64:
|
|
|
|
if f, e := strconv.ParseFloat(s, 64); e == nil {
|
|
|
|
v.SetFloat(f)
|
|
|
|
} else {
|
|
|
|
panic("could not parse float from " + strconv.Quote(s))
|
|
|
|
}
|
|
|
|
case reflect.Complex64,
|
|
|
|
reflect.Complex128:
|
|
|
|
var c complex128
|
|
|
|
if n, err := fmt.Sscanf(s, "%g", &c); n == 1 && err == nil {
|
|
|
|
v.SetComplex(c)
|
|
|
|
} else {
|
|
|
|
panic("could not parse complex from " + strconv.Quote(s))
|
|
|
|
}
|
|
|
|
case reflect.String:
|
|
|
|
v.SetString(s)
|
|
|
|
default:
|
|
|
|
panic(t.String() + " has unsupported kind " + k.String())
|
|
|
|
}
|
2016-03-23 20:53:09 +01:00
|
|
|
}
|
|
|
|
|
2016-07-21 21:10:33 +02:00
|
|
|
func (d decoder) decodeTime(v reflect.Value, x interface{}) {
|
2016-05-04 17:48:12 +02:00
|
|
|
t := v.Type()
|
|
|
|
s := getString(x)
|
|
|
|
// TODO: Find a more efficient way to do this.
|
|
|
|
for _, f := range allowedTimeFormats {
|
|
|
|
if p, err := time.Parse(f, s); err == nil {
|
|
|
|
v.Set(reflect.ValueOf(p).Convert(v.Type()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
panic("cannot decode string `" + s + "` as " + t.String())
|
2016-03-23 20:53:09 +01:00
|
|
|
}
|
|
|
|
|
2016-07-21 21:10:33 +02:00
|
|
|
func (d decoder) decodeURL(v reflect.Value, x interface{}) {
|
2016-05-04 17:48:12 +02:00
|
|
|
t := v.Type()
|
|
|
|
s := getString(x)
|
|
|
|
if u, err := url.Parse(s); err == nil {
|
|
|
|
v.Set(reflect.ValueOf(*u).Convert(v.Type()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
panic("cannot decode string `" + s + "` as " + t.String())
|
2016-03-23 20:53:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var allowedTimeFormats = []string{
|
2016-05-04 17:48:12 +02:00
|
|
|
"2006-01-02T15:04:05.999999999Z07:00",
|
|
|
|
"2006-01-02T15:04:05.999999999Z07",
|
|
|
|
"2006-01-02T15:04:05.999999999Z",
|
|
|
|
"2006-01-02T15:04:05.999999999",
|
|
|
|
"2006-01-02T15:04:05Z07:00",
|
|
|
|
"2006-01-02T15:04:05Z07",
|
|
|
|
"2006-01-02T15:04:05Z",
|
|
|
|
"2006-01-02T15:04:05",
|
|
|
|
"2006-01-02T15:04Z",
|
|
|
|
"2006-01-02T15:04",
|
|
|
|
"2006-01-02T15Z",
|
|
|
|
"2006-01-02T15",
|
|
|
|
"2006-01-02",
|
|
|
|
"2006-01",
|
|
|
|
"2006",
|
|
|
|
"15:04:05.999999999Z07:00",
|
|
|
|
"15:04:05.999999999Z07",
|
|
|
|
"15:04:05.999999999Z",
|
|
|
|
"15:04:05.999999999",
|
|
|
|
"15:04:05Z07:00",
|
|
|
|
"15:04:05Z07",
|
|
|
|
"15:04:05Z",
|
|
|
|
"15:04:05",
|
|
|
|
"15:04Z",
|
|
|
|
"15:04",
|
|
|
|
"15Z",
|
|
|
|
"15",
|
2016-03-23 20:53:09 +01:00
|
|
|
}
|