Return an error instead of panicking
This commit is contained in:
+11
-11
@@ -25,12 +25,12 @@ func TestCreateSubTree(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestSimpleKV(t *testing.T) {
|
func TestSimpleKV(t *testing.T) {
|
||||||
tree := Load("a = 42")
|
tree, _ := Load("a = 42")
|
||||||
assertTree(t, tree, map[string]interface{}{
|
assertTree(t, tree, map[string]interface{}{
|
||||||
"a": int64(42),
|
"a": int64(42),
|
||||||
})
|
})
|
||||||
|
|
||||||
tree = Load("a = 42\nb = 21")
|
tree, _ = Load("a = 42\nb = 21")
|
||||||
assertTree(t, tree, map[string]interface{}{
|
assertTree(t, tree, map[string]interface{}{
|
||||||
"a": int64(42),
|
"a": int64(42),
|
||||||
"b": int64(21),
|
"b": int64(21),
|
||||||
@@ -38,7 +38,7 @@ func TestSimpleKV(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestSimpleNumbers(t *testing.T) {
|
func TestSimpleNumbers(t *testing.T) {
|
||||||
tree := Load("a = +42\nb = -21\nc = +4.2\nd = -2.1")
|
tree, _ := Load("a = +42\nb = -21\nc = +4.2\nd = -2.1")
|
||||||
assertTree(t, tree, map[string]interface{}{
|
assertTree(t, tree, map[string]interface{}{
|
||||||
"a": int64(42),
|
"a": int64(42),
|
||||||
"b": int64(-21),
|
"b": int64(-21),
|
||||||
@@ -48,21 +48,21 @@ func TestSimpleNumbers(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestSimpleDate(t *testing.T) {
|
func TestSimpleDate(t *testing.T) {
|
||||||
tree := Load("a = 1979-05-27T07:32:00Z")
|
tree, _ := Load("a = 1979-05-27T07:32:00Z")
|
||||||
assertTree(t, tree, map[string]interface{}{
|
assertTree(t, tree, map[string]interface{}{
|
||||||
"a": time.Date(1979, time.May, 27, 7, 32, 0, 0, time.UTC),
|
"a": time.Date(1979, time.May, 27, 7, 32, 0, 0, time.UTC),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSimpleString(t *testing.T) {
|
func TestSimpleString(t *testing.T) {
|
||||||
tree := Load("a = \"hello world\"")
|
tree, _ := Load("a = \"hello world\"")
|
||||||
assertTree(t, tree, map[string]interface{}{
|
assertTree(t, tree, map[string]interface{}{
|
||||||
"a": "hello world",
|
"a": "hello world",
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBools(t *testing.T) {
|
func TestBools(t *testing.T) {
|
||||||
tree := Load("a = true\nb = false")
|
tree, _ := Load("a = true\nb = false")
|
||||||
assertTree(t, tree, map[string]interface{}{
|
assertTree(t, tree, map[string]interface{}{
|
||||||
"a": true,
|
"a": true,
|
||||||
"b": false,
|
"b": false,
|
||||||
@@ -70,33 +70,33 @@ func TestBools(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestNestedKeys(t *testing.T) {
|
func TestNestedKeys(t *testing.T) {
|
||||||
tree := Load("[a.b.c]\nd = 42")
|
tree, _ := Load("[a.b.c]\nd = 42")
|
||||||
assertTree(t, tree, map[string]interface{}{
|
assertTree(t, tree, map[string]interface{}{
|
||||||
"a.b.c.d": int64(42),
|
"a.b.c.d": int64(42),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestArraySimple(t *testing.T) {
|
func TestArraySimple(t *testing.T) {
|
||||||
tree := Load("a = [42, 21, 10]")
|
tree, _ := Load("a = [42, 21, 10]")
|
||||||
assertTree(t, tree, map[string]interface{}{
|
assertTree(t, tree, map[string]interface{}{
|
||||||
"a": []int64{int64(42), int64(21), int64(10)},
|
"a": []int64{int64(42), int64(21), int64(10)},
|
||||||
})
|
})
|
||||||
|
|
||||||
tree = Load("a = [42, 21, 10,]")
|
tree, _ = Load("a = [42, 21, 10,]")
|
||||||
assertTree(t, tree, map[string]interface{}{
|
assertTree(t, tree, map[string]interface{}{
|
||||||
"a": []int64{int64(42), int64(21), int64(10)},
|
"a": []int64{int64(42), int64(21), int64(10)},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestArrayMultiline(t *testing.T) {
|
func TestArrayMultiline(t *testing.T) {
|
||||||
tree := Load("a = [42,\n21, 10,]")
|
tree, _ := Load("a = [42,\n21, 10,]")
|
||||||
assertTree(t, tree, map[string]interface{}{
|
assertTree(t, tree, map[string]interface{}{
|
||||||
"a": []int64{int64(42), int64(21), int64(10)},
|
"a": []int64{int64(42), int64(21), int64(10)},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestArrayNested(t *testing.T) {
|
func TestArrayNested(t *testing.T) {
|
||||||
tree := Load("a = [[42, 21], [10]]")
|
tree, _ := Load("a = [[42, 21], [10]]")
|
||||||
assertTree(t, tree, map[string]interface{}{
|
assertTree(t, tree, map[string]interface{}{
|
||||||
"a": [][]int64{[]int64{int64(42), int64(21)}, []int64{int64(10)}},
|
"a": [][]int64{[]int64{int64(42), int64(21)}, []int64{int64(10)}},
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -5,6 +5,7 @@
|
|||||||
package toml
|
package toml
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -56,7 +57,7 @@ func (t *TomlTree) Set(key string, value interface{}) {
|
|||||||
(*subtree)[keys[len(keys)-1]] = value
|
(*subtree)[keys[len(keys)-1]] = value
|
||||||
}
|
}
|
||||||
|
|
||||||
// createSubTree takes a tree and a key andcreate the necessary intermediate
|
// createSubTree takes a tree and a key and create the necessary intermediate
|
||||||
// subtrees to create a subtree at that point. In-place.
|
// subtrees to create a subtree at that point. In-place.
|
||||||
//
|
//
|
||||||
// e.g. passing a.b.c will create (assuming tree is empty) tree[a], tree[a][b]
|
// e.g. passing a.b.c will create (assuming tree is empty) tree[a], tree[a][b]
|
||||||
@@ -74,7 +75,16 @@ func (t *TomlTree) createSubTree(key string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Create a TomlTree from a string.
|
// Create a TomlTree from a string.
|
||||||
func Load(content string) *TomlTree {
|
func Load(content string) (tree *TomlTree, err error) {
|
||||||
|
defer func() {
|
||||||
|
if r := recover(); r != nil {
|
||||||
|
if _, ok := r.(runtime.Error); ok {
|
||||||
|
panic(r)
|
||||||
|
}
|
||||||
|
err = r.(error)
|
||||||
|
}
|
||||||
|
}()
|
||||||
_, flow := lex(content)
|
_, flow := lex(content)
|
||||||
return parse(flow)
|
tree = parse(flow)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user