Prevent mixed types in arrays
This commit is contained in:
@@ -4,6 +4,7 @@ package toml
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"reflect"
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@@ -146,6 +147,7 @@ func parseRvalue(p *parser) interface{} {
|
|||||||
|
|
||||||
func parseArray(p *parser) []interface{} {
|
func parseArray(p *parser) []interface{} {
|
||||||
array := make([]interface{}, 0)
|
array := make([]interface{}, 0)
|
||||||
|
arrayType := reflect.TypeOf(nil)
|
||||||
for {
|
for {
|
||||||
follow := p.peek()
|
follow := p.peek()
|
||||||
if follow == nil || follow.typ == tokenEOF {
|
if follow == nil || follow.typ == tokenEOF {
|
||||||
@@ -156,6 +158,12 @@ func parseArray(p *parser) []interface{} {
|
|||||||
return array
|
return array
|
||||||
}
|
}
|
||||||
val := parseRvalue(p)
|
val := parseRvalue(p)
|
||||||
|
if arrayType == nil {
|
||||||
|
arrayType = reflect.TypeOf(val)
|
||||||
|
}
|
||||||
|
if reflect.TypeOf(val) != arrayType {
|
||||||
|
panic("mixed types in array")
|
||||||
|
}
|
||||||
array = append(array, val)
|
array = append(array, val)
|
||||||
follow = p.peek()
|
follow = p.peek()
|
||||||
if follow == nil {
|
if follow == nil {
|
||||||
|
|||||||
@@ -142,6 +142,18 @@ func TestArrayNested(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestArrayMixedTypes(t *testing.T) {
|
||||||
|
_, err := Load("a = [42, 16.0]")
|
||||||
|
if err.Error() != "mixed types in array" {
|
||||||
|
t.Error("Bad error message:", err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = Load("a = [42, \"hello\"]")
|
||||||
|
if err.Error() != "mixed types in array" {
|
||||||
|
t.Error("Bad error message:", err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestArrayNestedStrings(t *testing.T) {
|
func TestArrayNestedStrings(t *testing.T) {
|
||||||
tree, err := Load("data = [ [\"gamma\", \"delta\"], [\"Foo\"] ]")
|
tree, err := Load("data = [ [\"gamma\", \"delta\"], [\"Foo\"] ]")
|
||||||
assertTree(t, tree, err, map[string]interface{}{
|
assertTree(t, tree, err, map[string]interface{}{
|
||||||
|
|||||||
Reference in New Issue
Block a user