@@ -57,6 +57,19 @@ func simpleValueCoercion(object interface{}) (interface{}, error) {
|
||||
return float64(original), nil
|
||||
case fmt.Stringer:
|
||||
return original.String(), nil
|
||||
case []interface{}:
|
||||
value := reflect.ValueOf(original)
|
||||
length := value.Len()
|
||||
arrayValue := reflect.MakeSlice(value.Type(), 0, length)
|
||||
for i := 0; i < length; i++ {
|
||||
val := value.Index(i).Interface()
|
||||
simpleValue, err := simpleValueCoercion(val)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
arrayValue = reflect.Append(arrayValue, reflect.ValueOf(simpleValue))
|
||||
}
|
||||
return arrayValue.Interface(), nil
|
||||
default:
|
||||
return nil, fmt.Errorf("cannot convert type %T to Tree", object)
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package toml
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"strconv"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -124,3 +125,119 @@ func TestRoundTripArrayOfTables(t *testing.T) {
|
||||
t.Errorf("want:\n%s\ngot:\n%s", want, got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTomlSliceOfSlice(t *testing.T) {
|
||||
tree, err := Load(` hosts=[["10.1.0.107:9092","10.1.0.107:9093", "192.168.0.40:9094"] ] `)
|
||||
m := tree.ToMap()
|
||||
tree, err = TreeFromMap(m)
|
||||
if err != nil {
|
||||
t.Error("should not error", err)
|
||||
}
|
||||
type Struct struct {
|
||||
Hosts [][]string
|
||||
}
|
||||
var actual Struct
|
||||
tree.Unmarshal(&actual)
|
||||
|
||||
expected := Struct{Hosts: [][]string{[]string{"10.1.0.107:9092", "10.1.0.107:9093", "192.168.0.40:9094"}}}
|
||||
|
||||
if !reflect.DeepEqual(actual, expected) {
|
||||
t.Errorf("Bad unmarshal: expected %+v, got %+v", expected, actual)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTomlSliceOfSliceOfSlice(t *testing.T) {
|
||||
tree, err := Load(` hosts=[[["10.1.0.107:9092","10.1.0.107:9093", "192.168.0.40:9094"] ]] `)
|
||||
m := tree.ToMap()
|
||||
tree, err = TreeFromMap(m)
|
||||
if err != nil {
|
||||
t.Error("should not error", err)
|
||||
}
|
||||
type Struct struct {
|
||||
Hosts [][][]string
|
||||
}
|
||||
var actual Struct
|
||||
tree.Unmarshal(&actual)
|
||||
|
||||
expected := Struct{Hosts: [][][]string{[][]string{[]string{"10.1.0.107:9092", "10.1.0.107:9093", "192.168.0.40:9094"}}}}
|
||||
|
||||
if !reflect.DeepEqual(actual, expected) {
|
||||
t.Errorf("Bad unmarshal: expected %+v, got %+v", expected, actual)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTomlSliceOfSliceInt(t *testing.T) {
|
||||
tree, err := Load(` hosts=[[1,2,3],[4,5,6] ] `)
|
||||
m := tree.ToMap()
|
||||
tree, err = TreeFromMap(m)
|
||||
if err != nil {
|
||||
t.Error("should not error", err)
|
||||
}
|
||||
type Struct struct {
|
||||
Hosts [][]int
|
||||
}
|
||||
var actual Struct
|
||||
err = tree.Unmarshal(&actual)
|
||||
if err != nil {
|
||||
t.Error("should not error", err)
|
||||
}
|
||||
|
||||
expected := Struct{Hosts: [][]int{[]int{1, 2, 3}, []int{4, 5, 6}}}
|
||||
|
||||
if !reflect.DeepEqual(actual, expected) {
|
||||
t.Errorf("Bad unmarshal: expected %+v, got %+v", expected, actual)
|
||||
}
|
||||
|
||||
}
|
||||
func TestTomlSliceOfSliceInt64(t *testing.T) {
|
||||
tree, err := Load(` hosts=[[1,2,3],[4,5,6] ] `)
|
||||
m := tree.ToMap()
|
||||
tree, err = TreeFromMap(m)
|
||||
if err != nil {
|
||||
t.Error("should not error", err)
|
||||
}
|
||||
type Struct struct {
|
||||
Hosts [][]int64
|
||||
}
|
||||
var actual Struct
|
||||
err = tree.Unmarshal(&actual)
|
||||
if err != nil {
|
||||
t.Error("should not error", err)
|
||||
}
|
||||
|
||||
expected := Struct{Hosts: [][]int64{[]int64{1, 2, 3}, []int64{4, 5, 6}}}
|
||||
|
||||
if !reflect.DeepEqual(actual, expected) {
|
||||
t.Errorf("Bad unmarshal: expected %+v, got %+v", expected, actual)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestTomlSliceOfSliceInt64FromMap(t *testing.T) {
|
||||
tree, err := TreeFromMap(map[string]interface{}{"hosts": [][]interface{}{[]interface{}{int32(1), int8(2), 3}}})
|
||||
if err != nil {
|
||||
t.Error("should not error", err)
|
||||
}
|
||||
type Struct struct {
|
||||
Hosts [][]int64
|
||||
}
|
||||
var actual Struct
|
||||
err = tree.Unmarshal(&actual)
|
||||
if err != nil {
|
||||
t.Error("should not error", err)
|
||||
}
|
||||
|
||||
expected := Struct{Hosts: [][]int64{[]int64{1, 2, 3}}}
|
||||
|
||||
if !reflect.DeepEqual(actual, expected) {
|
||||
t.Errorf("Bad unmarshal: expected %+v, got %+v", expected, actual)
|
||||
}
|
||||
|
||||
}
|
||||
func TestTomlSliceOfSliceError(t *testing.T) { // make Codecov happy
|
||||
_, err := TreeFromMap(map[string]interface{}{"hosts": [][]interface{}{[]interface{}{1, 2, []struct{}{}}}})
|
||||
expected := "cannot convert type []struct {} to Tree"
|
||||
if err.Error() != expected {
|
||||
t.Fatalf("unexpected error: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user