summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/gorilla/schema/decoder.go
diff options
context:
space:
mode:
authorChristopher Speller <crspeller@gmail.com>2018-04-16 05:37:14 -0700
committerJoram Wilander <jwawilander@gmail.com>2018-04-16 08:37:14 -0400
commit6e2cb00008cbf09e556b00f87603797fcaa47e09 (patch)
tree3c0eb55ff4226a3f024aad373140d1fb860a6404 /vendor/github.com/gorilla/schema/decoder.go
parentbf24f51c4e1cc6286885460672f7f449e8c6f5ef (diff)
downloadchat-6e2cb00008cbf09e556b00f87603797fcaa47e09.tar.gz
chat-6e2cb00008cbf09e556b00f87603797fcaa47e09.tar.bz2
chat-6e2cb00008cbf09e556b00f87603797fcaa47e09.zip
Depenancy upgrades and movign to dep. (#8630)
Diffstat (limited to 'vendor/github.com/gorilla/schema/decoder.go')
-rw-r--r--vendor/github.com/gorilla/schema/decoder.go89
1 files changed, 58 insertions, 31 deletions
diff --git a/vendor/github.com/gorilla/schema/decoder.go b/vendor/github.com/gorilla/schema/decoder.go
index e49b53c87..5352a908b 100644
--- a/vendor/github.com/gorilla/schema/decoder.go
+++ b/vendor/github.com/gorilla/schema/decoder.go
@@ -106,7 +106,7 @@ func (d *Decoder) checkRequired(t reflect.Type, src map[string][]string, prefix
if f.typ.Kind() == reflect.Struct {
err := d.checkRequired(f.typ, src, prefix+f.alias+".")
if err != nil {
- if !f.anon {
+ if !f.isAnonymous {
return err
}
// check embedded parent field.
@@ -116,7 +116,7 @@ func (d *Decoder) checkRequired(t reflect.Type, src map[string][]string, prefix
}
}
}
- if f.required {
+ if f.isRequired {
key := f.alias
if prefix != "" {
key = prefix + key
@@ -185,7 +185,7 @@ func (d *Decoder) decode(v reflect.Value, path string, parts []pathPart, values
// Get the converter early in case there is one for a slice type.
conv := d.cache.converter(t)
m := isTextUnmarshaler(v)
- if conv == nil && t.Kind() == reflect.Slice && m.IsSlice {
+ if conv == nil && t.Kind() == reflect.Slice && m.IsSliceElement {
var items []reflect.Value
elemT := t.Elem()
isPtrElem := elemT.Kind() == reflect.Ptr
@@ -211,7 +211,7 @@ func (d *Decoder) decode(v reflect.Value, path string, parts []pathPart, values
}
} else if m.IsValid {
u := reflect.New(elemT)
- if m.IsPtr {
+ if m.IsSliceElementPtr {
u = reflect.New(reflect.PtrTo(elemT).Elem())
}
if err := u.Interface().(encoding.TextUnmarshaler).UnmarshalText([]byte(value)); err != nil {
@@ -222,7 +222,7 @@ func (d *Decoder) decode(v reflect.Value, path string, parts []pathPart, values
Err: err,
}
}
- if m.IsPtr {
+ if m.IsSliceElementPtr {
items = append(items, u.Elem().Addr())
} else if u.Kind() == reflect.Ptr {
items = append(items, u.Elem())
@@ -283,11 +283,7 @@ func (d *Decoder) decode(v reflect.Value, path string, parts []pathPart, values
val = values[len(values)-1]
}
- if val == "" {
- if d.zeroEmpty {
- v.Set(reflect.Zero(t))
- }
- } else if conv != nil {
+ if conv != nil {
if value := conv(val); value.IsValid() {
v.Set(value.Convert(t))
} else {
@@ -298,15 +294,32 @@ func (d *Decoder) decode(v reflect.Value, path string, parts []pathPart, values
}
}
} else if m.IsValid {
- // If the value implements the encoding.TextUnmarshaler interface
- // apply UnmarshalText as the converter
- if err := m.Unmarshaler.UnmarshalText([]byte(val)); err != nil {
- return ConversionError{
- Key: path,
- Type: t,
- Index: -1,
- Err: err,
+ if m.IsPtr {
+ u := reflect.New(v.Type())
+ if err := u.Interface().(encoding.TextUnmarshaler).UnmarshalText([]byte(val)); err != nil {
+ return ConversionError{
+ Key: path,
+ Type: t,
+ Index: -1,
+ Err: err,
+ }
}
+ v.Set(reflect.Indirect(u))
+ } else {
+ // If the value implements the encoding.TextUnmarshaler interface
+ // apply UnmarshalText as the converter
+ if err := m.Unmarshaler.UnmarshalText([]byte(val)); err != nil {
+ return ConversionError{
+ Key: path,
+ Type: t,
+ Index: -1,
+ Err: err,
+ }
+ }
+ }
+ } else if val == "" {
+ if d.zeroEmpty {
+ v.Set(reflect.Zero(t))
}
} else if conv := builtinConverters[t.Kind()]; conv != nil {
if value := conv(val); value.IsValid() {
@@ -326,18 +339,18 @@ func (d *Decoder) decode(v reflect.Value, path string, parts []pathPart, values
}
func isTextUnmarshaler(v reflect.Value) unmarshaler {
-
// Create a new unmarshaller instance
m := unmarshaler{}
-
- // As the UnmarshalText function should be applied
- // to the pointer of the type, we convert the value to pointer.
- if v.CanAddr() {
- v = v.Addr()
- }
if m.Unmarshaler, m.IsValid = v.Interface().(encoding.TextUnmarshaler); m.IsValid {
return m
}
+ // As the UnmarshalText function should be applied to the pointer of the
+ // type, we check that type to see if it implements the necessary
+ // method.
+ if m.Unmarshaler, m.IsValid = reflect.New(v.Type()).Interface().(encoding.TextUnmarshaler); m.IsValid {
+ m.IsPtr = true
+ return m
+ }
// if v is []T or *[]T create new T
t := v.Type()
@@ -345,12 +358,17 @@ func isTextUnmarshaler(v reflect.Value) unmarshaler {
t = t.Elem()
}
if t.Kind() == reflect.Slice {
- // if t is a pointer slice, check if it implements encoding.TextUnmarshaler
- m.IsSlice = true
+ // Check if the slice implements encoding.TextUnmarshaller
+ if m.Unmarshaler, m.IsValid = v.Interface().(encoding.TextUnmarshaler); m.IsValid {
+ return m
+ }
+ // If t is a pointer slice, check if its elements implement
+ // encoding.TextUnmarshaler
+ m.IsSliceElement = true
if t = t.Elem(); t.Kind() == reflect.Ptr {
t = reflect.PtrTo(t.Elem())
v = reflect.Zero(t)
- m.IsPtr = true
+ m.IsSliceElementPtr = true
m.Unmarshaler, m.IsValid = v.Interface().(encoding.TextUnmarshaler)
return m
}
@@ -365,9 +383,18 @@ func isTextUnmarshaler(v reflect.Value) unmarshaler {
// unmarshaller contains information about a TextUnmarshaler type
type unmarshaler struct {
Unmarshaler encoding.TextUnmarshaler
- IsSlice bool
- IsPtr bool
- IsValid bool
+ // IsValid indicates whether the resolved type indicated by the other
+ // flags implements the encoding.TextUnmarshaler interface.
+ IsValid bool
+ // IsPtr indicates that the resolved type is the pointer of the original
+ // type.
+ IsPtr bool
+ // IsSliceElement indicates that the resolved type is a slice element of
+ // the original type.
+ IsSliceElement bool
+ // IsSliceElementPtr indicates that the resolved type is a pointer to a
+ // slice element of the original type.
+ IsSliceElementPtr bool
}
// Errors ---------------------------------------------------------------------