@@ -2,21 +2,34 @@ package main
22
33import (
44 // "fmt"
5+ "github.com/mikefarah/yaml/Godeps/_workspace/src/gopkg.in/yaml.v2"
56 "strconv"
67)
78
8- func write (context map [interface {}]interface {}, head string , tail []string , value interface {}) {
9+ func entryInSlice (context yaml.MapSlice , key interface {}) * yaml.MapItem {
10+ for idx := range context {
11+ var entry = & context [idx ]
12+ if entry .Key == key {
13+ return entry
14+ }
15+ }
16+ return nil
17+ }
18+
19+ func write (context yaml.MapSlice , head string , tail []string , value interface {}) {
920 if len (tail ) == 0 {
10- context [head ] = value
21+ var entry = entryInSlice (context , head )
22+ entry .Value = value
1123 } else {
1224 // e.g. if updating a.b.c, we need to get the 'b', this could be a map or an array
1325 var parent = readMap (context , head , tail [0 :len (tail )- 1 ])
1426 switch parent .(type ) {
15- case map [ interface {}] interface {} :
16- toUpdate := parent .(map [ interface {}] interface {} )
27+ case yaml. MapSlice :
28+ toUpdate := parent .(yaml. MapSlice )
1729 // b is a map, update the key 'c' to the supplied value
1830 key := (tail [len (tail )- 1 ])
19- toUpdate [key ] = value
31+ toUpdateEntry := entryInSlice (toUpdate , key )
32+ toUpdateEntry .Value = value
2033 case []interface {}:
2134 toUpdate := parent .([]interface {})
2235 // b is an array, update it at index 'c' to the supplied value
@@ -31,22 +44,26 @@ func write(context map[interface{}]interface{}, head string, tail []string, valu
3144 }
3245}
3346
34- func readMap (context map [ interface {}] interface {} , head string , tail []string ) interface {} {
47+ func readMap (context yaml. MapSlice , head string , tail []string ) interface {} {
3548 if head == "*" {
3649 return readMapSplat (context , tail )
3750 }
38- value := context [head ]
51+ entry := entryInSlice (context , head )
52+ var value interface {}
53+ if entry != nil {
54+ value = entry .Value
55+ }
3956 return calculateValue (value , tail )
4057}
4158
42- func readMapSplat (context map [ interface {}] interface {} , tail []string ) interface {} {
59+ func readMapSplat (context yaml. MapSlice , tail []string ) interface {} {
4360 var newArray = make ([]interface {}, len (context ))
4461 var i = 0
45- for _ , value := range context {
62+ for _ , entry := range context {
4663 if len (tail ) > 0 {
47- newArray [i ] = recurse (value , tail [0 ], tail [1 :len (tail )])
64+ newArray [i ] = recurse (entry . Value , tail [0 ], tail [1 :len (tail )])
4865 } else {
49- newArray [i ] = value
66+ newArray [i ] = entry . Value
5067 }
5168 i ++
5269 }
@@ -64,8 +81,8 @@ func recurse(value interface{}, head string, tail []string) interface{} {
6481 die ("Error accessing array: %v" , err )
6582 }
6683 return readArray (value .([]interface {}), index , tail )
67- case map [ interface {}] interface {} :
68- return readMap (value .(map [ interface {}] interface {} ), head , tail )
84+ case yaml. MapSlice :
85+ return readMap (value .(yaml. MapSlice ), head , tail )
6986 default :
7087 return nil
7188 }
0 commit comments