From 01845ea923ac3943867bbc01e758d9f170f6a331 Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Fri, 2 Oct 2015 09:05:13 +1000 Subject: [PATCH] Split code --- data_navigator.go | 43 ++++++++++++++++++++++++++ yaml_test.go => data_navigator_test.go | 0 yaml.go | 37 ---------------------- 3 files changed, 43 insertions(+), 37 deletions(-) create mode 100644 data_navigator.go rename yaml_test.go => data_navigator_test.go (100%) diff --git a/data_navigator.go b/data_navigator.go new file mode 100644 index 0000000..efe8a4a --- /dev/null +++ b/data_navigator.go @@ -0,0 +1,43 @@ +package main + +import ( + "log" + "strconv" +) + +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 +} + +func readMap(context map[interface{}]interface{}, head string, tail []string) interface{} { + value := context[head] + if len(tail) > 0 { + return recurse(value, tail[0], tail[1:len(tail)]) + } + return value +} + +func recurse(value interface{}, head string, tail []string) interface{} { + switch value.(type) { + case []interface{}: + index, err := strconv.ParseInt(head, 10, 64) + if err != nil { + log.Fatalf("Error accessing array: %v", err) + } + return readArray(value.([]interface{}), index, tail) + default: + return readMap(value.(map[interface{}]interface{}), head, tail) + } +} + +func readArray(array []interface{}, head int64, tail []string) interface{} { + value := array[head] + if len(tail) > 0 { + return recurse(value, tail[0], tail[1:len(tail)]) + } + return value +} diff --git a/yaml_test.go b/data_navigator_test.go similarity index 100% rename from yaml_test.go rename to data_navigator_test.go diff --git a/yaml.go b/yaml.go index 6c6004d..9b985d4 100644 --- a/yaml.go +++ b/yaml.go @@ -126,40 +126,3 @@ func readFile(filename string) []byte { } return rawData } - -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 -} - -func readMap(context map[interface{}]interface{}, head string, tail []string) interface{} { - value := context[head] - if len(tail) > 0 { - return recurse(value, tail[0], tail[1:len(tail)]) - } - return value -} - -func recurse(value interface{}, head string, tail []string) interface{} { - switch value.(type) { - case []interface{}: - index, err := strconv.ParseInt(head, 10, 64) - if err != nil { - log.Fatalf("Error accessing array: %v", err) - } - return readArray(value.([]interface{}), index, tail) - default: - return readMap(value.(map[interface{}]interface{}), head, tail) - } -} - -func readArray(array []interface{}, head int64, tail []string) interface{} { - value := array[head] - if len(tail) > 0 { - return recurse(value, tail[0], tail[1:len(tail)]) - } - return value -}