Merge pull request #644 from chrisport/develop
config: fix error when json config starts with an array
Showing
2 changed files
with
53 additions
and
1 deletions
| ... | @@ -35,7 +35,12 @@ func (js *JsonConfig) Parse(filename string) (ConfigContainer, error) { | ... | @@ -35,7 +35,12 @@ func (js *JsonConfig) Parse(filename string) (ConfigContainer, error) { |
| 35 | } | 35 | } |
| 36 | err = json.Unmarshal(content, &x.data) | 36 | err = json.Unmarshal(content, &x.data) |
| 37 | if err != nil { | 37 | if err != nil { |
| 38 | return nil, err | 38 | var wrappingArray []interface{} |
| 39 | err2 := json.Unmarshal(content, &wrappingArray) | ||
| 40 | if err2 != nil { | ||
| 41 | return nil, err | ||
| 42 | } | ||
| 43 | x.data["rootArray"] = wrappingArray | ||
| 39 | } | 44 | } |
| 40 | return x, nil | 45 | return x, nil |
| 41 | } | 46 | } | ... | ... |
| ... | @@ -33,6 +33,53 @@ var jsoncontext = `{ | ... | @@ -33,6 +33,53 @@ var jsoncontext = `{ |
| 33 | } | 33 | } |
| 34 | }` | 34 | }` |
| 35 | 35 | ||
| 36 | var jsoncontextwitharray = `[ | ||
| 37 | { | ||
| 38 | "url": "user", | ||
| 39 | "serviceAPI": "http://www.test.com/user" | ||
| 40 | }, | ||
| 41 | { | ||
| 42 | "url": "employee", | ||
| 43 | "serviceAPI": "http://www.test.com/employee" | ||
| 44 | } | ||
| 45 | ]` | ||
| 46 | |||
| 47 | func TestJsonStartsWithArray(t *testing.T) { | ||
| 48 | f, err := os.Create("testjsonWithArray.conf") | ||
| 49 | if err != nil { | ||
| 50 | t.Fatal(err) | ||
| 51 | } | ||
| 52 | _, err = f.WriteString(jsoncontextwitharray) | ||
| 53 | if err != nil { | ||
| 54 | f.Close() | ||
| 55 | t.Fatal(err) | ||
| 56 | } | ||
| 57 | f.Close() | ||
| 58 | defer os.Remove("testjsonWithArray.conf") | ||
| 59 | jsonconf, err := NewConfig("json", "testjsonWithArray.conf") | ||
| 60 | if err != nil { | ||
| 61 | t.Fatal(err) | ||
| 62 | } | ||
| 63 | rootArray, err := jsonconf.DIY("rootArray") | ||
| 64 | if (err != nil) { | ||
| 65 | t.Error("array does not exist as element") | ||
| 66 | } | ||
| 67 | rootArrayCasted := rootArray.([]interface{}) | ||
| 68 | if (rootArrayCasted == nil) { | ||
| 69 | t.Error("array from root is nil") | ||
| 70 | }else { | ||
| 71 | elem := rootArrayCasted[0].(map[string]interface{}) | ||
| 72 | if elem["url"] != "user" || elem["serviceAPI"] != "http://www.test.com/user" { | ||
| 73 | t.Error("array[0] values are not valid") | ||
| 74 | } | ||
| 75 | |||
| 76 | elem2 := rootArrayCasted[1].(map[string]interface{}) | ||
| 77 | if elem2["url"] != "employee" || elem2["serviceAPI"] != "http://www.test.com/employee" { | ||
| 78 | t.Error("array[1] values are not valid") | ||
| 79 | } | ||
| 80 | } | ||
| 81 | } | ||
| 82 | |||
| 36 | func TestJson(t *testing.T) { | 83 | func TestJson(t *testing.T) { |
| 37 | f, err := os.Create("testjson.conf") | 84 | f, err := os.Create("testjson.conf") |
| 38 | if err != nil { | 85 | if err != nil { | ... | ... |
-
Please register or sign in to post a comment