Skip to content

Commit 01845ea

Browse files
committed
Split code
1 parent 58bdb3e commit 01845ea

File tree

3 files changed

+43
-37
lines changed

3 files changed

+43
-37
lines changed

data_navigator.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package main
2+
3+
import (
4+
"log"
5+
"strconv"
6+
)
7+
8+
func write(context map[interface{}]interface{}, head string, tail []string, value interface{}) {
9+
// e.g. if updating a.b.c, we need to get the 'b' map...
10+
toUpdate := readMap(context, head, tail[0:len(tail)-1]).(map[interface{}]interface{})
11+
// and then set the 'c' key.
12+
key := (tail[len(tail)-1])
13+
toUpdate[key] = value
14+
}
15+
16+
func readMap(context map[interface{}]interface{}, head string, tail []string) interface{} {
17+
value := context[head]
18+
if len(tail) > 0 {
19+
return recurse(value, tail[0], tail[1:len(tail)])
20+
}
21+
return value
22+
}
23+
24+
func recurse(value interface{}, head string, tail []string) interface{} {
25+
switch value.(type) {
26+
case []interface{}:
27+
index, err := strconv.ParseInt(head, 10, 64)
28+
if err != nil {
29+
log.Fatalf("Error accessing array: %v", err)
30+
}
31+
return readArray(value.([]interface{}), index, tail)
32+
default:
33+
return readMap(value.(map[interface{}]interface{}), head, tail)
34+
}
35+
}
36+
37+
func readArray(array []interface{}, head int64, tail []string) interface{} {
38+
value := array[head]
39+
if len(tail) > 0 {
40+
return recurse(value, tail[0], tail[1:len(tail)])
41+
}
42+
return value
43+
}
File renamed without changes.

yaml.go

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -126,40 +126,3 @@ func readFile(filename string) []byte {
126126
}
127127
return rawData
128128
}
129-
130-
func write(context map[interface{}]interface{}, head string, tail []string, value interface{}) {
131-
// e.g. if updating a.b.c, we need to get the 'b' map...
132-
toUpdate := readMap(context, head, tail[0:len(tail)-1]).(map[interface{}]interface{})
133-
// and then set the 'c' key.
134-
key := (tail[len(tail)-1])
135-
toUpdate[key] = value
136-
}
137-
138-
func readMap(context map[interface{}]interface{}, head string, tail []string) interface{} {
139-
value := context[head]
140-
if len(tail) > 0 {
141-
return recurse(value, tail[0], tail[1:len(tail)])
142-
}
143-
return value
144-
}
145-
146-
func recurse(value interface{}, head string, tail []string) interface{} {
147-
switch value.(type) {
148-
case []interface{}:
149-
index, err := strconv.ParseInt(head, 10, 64)
150-
if err != nil {
151-
log.Fatalf("Error accessing array: %v", err)
152-
}
153-
return readArray(value.([]interface{}), index, tail)
154-
default:
155-
return readMap(value.(map[interface{}]interface{}), head, tail)
156-
}
157-
}
158-
159-
func readArray(array []interface{}, head int64, tail []string) interface{} {
160-
value := array[head]
161-
if len(tail) > 0 {
162-
return recurse(value, tail[0], tail[1:len(tail)])
163-
}
164-
return value
165-
}

0 commit comments

Comments
 (0)