Skip to content

Commit 7db3100

Browse files
author
mfarah
committed
Fixed write bug: can now update yaml with single key in path
1 parent 8409be1 commit 7db3100

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

data_navigator.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,15 @@ import (
77
)
88

99
func write(context map[interface{}]interface{}, head string, tail []string, value interface{}) {
10-
// e.g. if updating a.b.c, we need to get the 'b' map...
11-
toUpdate := readMap(context, head, tail[0:len(tail)-1]).(map[interface{}]interface{})
12-
// and then set the 'c' key.
13-
key := (tail[len(tail)-1])
14-
toUpdate[key] = value
10+
if len(tail) == 0 {
11+
context[head] = value
12+
} else {
13+
// e.g. if updating a.b.c, we need to get the 'b' map...
14+
toUpdate := readMap(context, head, tail[0:len(tail)-1]).(map[interface{}]interface{})
15+
// and then set the 'c' key.
16+
key := (tail[len(tail)-1])
17+
toUpdate[key] = value
18+
}
1519
}
1620

1721
func readMap(context map[interface{}]interface{}, head string, tail []string) interface{} {

data_navigator_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,14 @@ func TestWrite_simple(t *testing.T) {
6868
assertResult(t, "4", b["c"].(string))
6969
}
7070

71+
func TestWrite_with_no_tail(t *testing.T) {
72+
73+
write(parsedData, "b", []string{}, "4")
74+
75+
b := parsedData["b"]
76+
assertResult(t, "4", fmt.Sprintf("%v", b))
77+
}
78+
7179
func assertResult(t *testing.T, expectedValue interface{}, actualValue interface{}) {
7280
if expectedValue != actualValue {
7381
t.Error("Expected <", expectedValue, "> but got <", actualValue, ">", fmt.Sprintf("%T", actualValue))

0 commit comments

Comments
 (0)