From d103ed6ca97ca5a2669f6cf5fe4b3d2a9c945f26 Mon Sep 17 00:00:00 2001 From: Christopher Speller Date: Wed, 17 May 2017 16:51:25 -0400 Subject: Upgrading server dependancies (#6431) --- vendor/github.com/pelletier/go-toml/marshal.go | 61 ++++++++++++++------------ 1 file changed, 33 insertions(+), 28 deletions(-) (limited to 'vendor/github.com/pelletier/go-toml/marshal.go') diff --git a/vendor/github.com/pelletier/go-toml/marshal.go b/vendor/github.com/pelletier/go-toml/marshal.go index a1d701046..358425aea 100644 --- a/vendor/github.com/pelletier/go-toml/marshal.go +++ b/vendor/github.com/pelletier/go-toml/marshal.go @@ -1,6 +1,7 @@ package toml import ( + "bytes" "errors" "fmt" "reflect" @@ -9,14 +10,14 @@ import ( ) /* -TomlTree structural types and corresponding marshal types +Tree structural types and corresponding marshal types ------------------------------------------------------------------------------- -*TomlTree (*)struct, (*)map[string]interface{} -[]*TomlTree (*)[](*)struct, (*)[](*)map[string]interface{} +*Tree (*)struct, (*)map[string]interface{} +[]*Tree (*)[](*)struct, (*)[](*)map[string]interface{} []interface{} (as interface{}) (*)[]primitive, (*)[]([]interface{}) interface{} (*)primitive -TomlTree primitive types and corresponding marshal types +Tree primitive types and corresponding marshal types ----------------------------------------------------------- uint64 uint, uint8-uint64, pointers to same int64 int, int8-uint64, pointers to same @@ -35,7 +36,7 @@ type tomlOpts struct { var timeType = reflect.TypeOf(time.Time{}) var marshalerType = reflect.TypeOf(new(Marshaler)).Elem() -// Check if the given marshall type maps to a TomlTree primitive +// Check if the given marshall type maps to a Tree primitive func isPrimitive(mtype reflect.Type) bool { switch mtype.Kind() { case reflect.Ptr: @@ -57,7 +58,7 @@ func isPrimitive(mtype reflect.Type) bool { } } -// Check if the given marshall type maps to a TomlTree slice +// Check if the given marshall type maps to a Tree slice func isTreeSlice(mtype reflect.Type) bool { switch mtype.Kind() { case reflect.Slice: @@ -67,7 +68,7 @@ func isTreeSlice(mtype reflect.Type) bool { } } -// Check if the given marshall type maps to a non-TomlTree slice +// Check if the given marshall type maps to a non-Tree slice func isOtherSlice(mtype reflect.Type) bool { switch mtype.Kind() { case reflect.Ptr: @@ -79,7 +80,7 @@ func isOtherSlice(mtype reflect.Type) bool { } } -// Check if the given marshall type maps to a TomlTree +// Check if the given marshall type maps to a Tree func isTree(mtype reflect.Type) bool { switch mtype.Kind() { case reflect.Map: @@ -133,11 +134,11 @@ func Marshal(v interface{}) ([]byte, error) { } // Convert given marshal struct or map value to toml tree -func valueToTree(mtype reflect.Type, mval reflect.Value) (*TomlTree, error) { +func valueToTree(mtype reflect.Type, mval reflect.Value) (*Tree, error) { if mtype.Kind() == reflect.Ptr { return valueToTree(mtype.Elem(), mval.Elem()) } - tval := newTomlTree() + tval := newTree() switch mtype.Kind() { case reflect.Struct: for i := 0; i < mtype.NumField(); i++ { @@ -165,8 +166,8 @@ func valueToTree(mtype reflect.Type, mval reflect.Value) (*TomlTree, error) { } // Convert given marshal slice to slice of Toml trees -func valueToTreeSlice(mtype reflect.Type, mval reflect.Value) ([]*TomlTree, error) { - tval := make([]*TomlTree, mval.Len(), mval.Len()) +func valueToTreeSlice(mtype reflect.Type, mval reflect.Value) ([]*Tree, error) { + tval := make([]*Tree, mval.Len(), mval.Len()) for i := 0; i < mval.Len(); i++ { val, err := valueToTree(mtype.Elem(), mval.Index(i)) if err != nil { @@ -224,34 +225,38 @@ func valueToToml(mtype reflect.Type, mval reflect.Value) (interface{}, error) { } } -/* -Unmarshal parses the TOML-encoded data and stores the result in the value -pointed to by v. Behavior is similar to the Go json encoder, except that there -is no concept of an Unmarshaler interface or UnmarshalTOML function for -sub-structs, and currently only definite types can be unmarshaled to (i.e. no -`interface{}`). -*/ -func Unmarshal(data []byte, v interface{}) error { +// Unmarshal attempts to unmarshal the Tree into a Go struct pointed by v. +// Neither Unmarshaler interfaces nor UnmarshalTOML functions are supported for +// sub-structs, and only definite types can be unmarshaled. +func (t *Tree) Unmarshal(v interface{}) error { mtype := reflect.TypeOf(v) if mtype.Kind() != reflect.Ptr || mtype.Elem().Kind() != reflect.Struct { return errors.New("Only a pointer to struct can be unmarshaled from TOML") } - t, err := Load(string(data)) + sval, err := valueFromTree(mtype.Elem(), t) if err != nil { return err } + reflect.ValueOf(v).Elem().Set(sval) + return nil +} - sval, err := valueFromTree(mtype.Elem(), t) +// Unmarshal parses the TOML-encoded data and stores the result in the value +// pointed to by v. Behavior is similar to the Go json encoder, except that there +// is no concept of an Unmarshaler interface or UnmarshalTOML function for +// sub-structs, and currently only definite types can be unmarshaled to (i.e. no +// `interface{}`). +func Unmarshal(data []byte, v interface{}) error { + t, err := LoadReader(bytes.NewReader(data)) if err != nil { return err } - reflect.ValueOf(v).Elem().Set(sval) - return nil + return t.Unmarshal(v) } // Convert toml tree to marshal struct or map, using marshal type -func valueFromTree(mtype reflect.Type, tval *TomlTree) (reflect.Value, error) { +func valueFromTree(mtype reflect.Type, tval *Tree) (reflect.Value, error) { if mtype.Kind() == reflect.Ptr { return unwrapPointer(mtype, tval) } @@ -290,7 +295,7 @@ func valueFromTree(mtype reflect.Type, tval *TomlTree) (reflect.Value, error) { } // Convert toml value to marshal struct/map slice, using marshal type -func valueFromTreeSlice(mtype reflect.Type, tval []*TomlTree) (reflect.Value, error) { +func valueFromTreeSlice(mtype reflect.Type, tval []*Tree) (reflect.Value, error) { mval := reflect.MakeSlice(mtype, len(tval), len(tval)) for i := 0; i < len(tval); i++ { val, err := valueFromTree(mtype.Elem(), tval[i]) @@ -322,9 +327,9 @@ func valueFromToml(mtype reflect.Type, tval interface{}) (reflect.Value, error) } switch { case isTree(mtype): - return valueFromTree(mtype, tval.(*TomlTree)) + return valueFromTree(mtype, tval.(*Tree)) case isTreeSlice(mtype): - return valueFromTreeSlice(mtype, tval.([]*TomlTree)) + return valueFromTreeSlice(mtype, tval.([]*Tree)) case isOtherSlice(mtype): return valueFromOtherSlice(mtype, tval.([]interface{})) default: -- cgit v1.2.3-1-g7c22