+18
-18
@@ -10,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
|
||||
@@ -36,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:
|
||||
@@ -58,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:
|
||||
@@ -68,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:
|
||||
@@ -80,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:
|
||||
@@ -134,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++ {
|
||||
@@ -166,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 {
|
||||
@@ -225,10 +225,10 @@ func valueToToml(mtype reflect.Type, mval reflect.Value) (interface{}, error) {
|
||||
}
|
||||
}
|
||||
|
||||
// Unmarshal attempts to unmarshal the TomlTree into a Go struct pointed by v.
|
||||
// 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 *TomlTree) Unmarshal(v interface{}) error {
|
||||
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")
|
||||
@@ -256,7 +256,7 @@ func Unmarshal(data []byte, v interface{}) error {
|
||||
}
|
||||
|
||||
// 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)
|
||||
}
|
||||
@@ -295,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])
|
||||
@@ -327,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:
|
||||
|
||||
Reference in New Issue
Block a user