From 183bbe2aa9389638c0c786028b8412fba75e54b0 Mon Sep 17 00:00:00 2001 From: Thomas Pelletier Date: Fri, 22 Mar 2013 15:03:51 +0100 Subject: [PATCH] Return an error instead of panicking --- parser_test.go | 22 +++++++++++----------- toml.go | 16 +++++++++++++--- 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/parser_test.go b/parser_test.go index fd271ce..1531a1f 100644 --- a/parser_test.go +++ b/parser_test.go @@ -25,12 +25,12 @@ func TestCreateSubTree(t *testing.T) { } func TestSimpleKV(t *testing.T) { - tree := Load("a = 42") + tree, _ := Load("a = 42") assertTree(t, tree, map[string]interface{}{ "a": int64(42), }) - tree = Load("a = 42\nb = 21") + tree, _ = Load("a = 42\nb = 21") assertTree(t, tree, map[string]interface{}{ "a": int64(42), "b": int64(21), @@ -38,7 +38,7 @@ func TestSimpleKV(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{}{ "a": int64(42), "b": int64(-21), @@ -48,21 +48,21 @@ func TestSimpleNumbers(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{}{ "a": time.Date(1979, time.May, 27, 7, 32, 0, 0, time.UTC), }) } func TestSimpleString(t *testing.T) { - tree := Load("a = \"hello world\"") + tree, _ := Load("a = \"hello world\"") assertTree(t, tree, map[string]interface{}{ "a": "hello world", }) } func TestBools(t *testing.T) { - tree := Load("a = true\nb = false") + tree, _ := Load("a = true\nb = false") assertTree(t, tree, map[string]interface{}{ "a": true, "b": false, @@ -70,33 +70,33 @@ func TestBools(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{}{ "a.b.c.d": int64(42), }) } func TestArraySimple(t *testing.T) { - tree := Load("a = [42, 21, 10]") + tree, _ := Load("a = [42, 21, 10]") assertTree(t, tree, map[string]interface{}{ "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{}{ "a": []int64{int64(42), int64(21), int64(10)}, }) } func TestArrayMultiline(t *testing.T) { - tree := Load("a = [42,\n21, 10,]") + tree, _ := Load("a = [42,\n21, 10,]") assertTree(t, tree, map[string]interface{}{ "a": []int64{int64(42), int64(21), int64(10)}, }) } func TestArrayNested(t *testing.T) { - tree := Load("a = [[42, 21], [10]]") + tree, _ := Load("a = [[42, 21], [10]]") assertTree(t, tree, map[string]interface{}{ "a": [][]int64{[]int64{int64(42), int64(21)}, []int64{int64(10)}}, }) diff --git a/toml.go b/toml.go index e8f5556..f27548a 100644 --- a/toml.go +++ b/toml.go @@ -5,6 +5,7 @@ package toml import ( + "runtime" "strings" ) @@ -56,7 +57,7 @@ func (t *TomlTree) Set(key string, value interface{}) { (*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. // // 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. -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) - return parse(flow) + tree = parse(flow) + return }