From 7db31006da3dcf860aa4df2ad17fcb9c5379a027 Mon Sep 17 00:00:00 2001 From: mfarah Date: Mon, 5 Oct 2015 15:59:59 +1100 Subject: [PATCH] Fixed write bug: can now update yaml with single key in path --- data_navigator.go | 14 +++++++++----- data_navigator_test.go | 8 ++++++++ 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/data_navigator.go b/data_navigator.go index 1f1acacc..1dd172d8 100644 --- a/data_navigator.go +++ b/data_navigator.go @@ -7,11 +7,15 @@ import ( ) func write(context map[interface{}]interface{}, head string, tail []string, value interface{}) { - // e.g. if updating a.b.c, we need to get the 'b' map... - toUpdate := readMap(context, head, tail[0:len(tail)-1]).(map[interface{}]interface{}) - // and then set the 'c' key. - key := (tail[len(tail)-1]) - toUpdate[key] = value + if len(tail) == 0 { + context[head] = value + } else { + // e.g. if updating a.b.c, we need to get the 'b' map... + toUpdate := readMap(context, head, tail[0:len(tail)-1]).(map[interface{}]interface{}) + // and then set the 'c' key. + key := (tail[len(tail)-1]) + toUpdate[key] = value + } } func readMap(context map[interface{}]interface{}, head string, tail []string) interface{} { diff --git a/data_navigator_test.go b/data_navigator_test.go index 4f4de210..82980165 100644 --- a/data_navigator_test.go +++ b/data_navigator_test.go @@ -68,6 +68,14 @@ func TestWrite_simple(t *testing.T) { assertResult(t, "4", b["c"].(string)) } +func TestWrite_with_no_tail(t *testing.T) { + + write(parsedData, "b", []string{}, "4") + + b := parsedData["b"] + assertResult(t, "4", fmt.Sprintf("%v", b)) +} + func assertResult(t *testing.T, expectedValue interface{}, actualValue interface{}) { if expectedValue != actualValue { t.Error("Expected <", expectedValue, "> but got <", actualValue, ">", fmt.Sprintf("%T", actualValue))