mirror of
https://github.com/mikefarah/yq.git
synced 2024-12-19 20:19:04 +00:00
Split code
This commit is contained in:
parent
58bdb3ed21
commit
01845ea923
43
data_navigator.go
Normal file
43
data_navigator.go
Normal file
@ -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
|
||||||
|
}
|
37
yaml.go
37
yaml.go
@ -126,40 +126,3 @@ func readFile(filename string) []byte {
|
|||||||
}
|
}
|
||||||
return rawData
|
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
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user