mirror of
https://github.com/mikefarah/yq.git
synced 2024-11-12 05:38:04 +00:00
Can now splat maps
This commit is contained in:
parent
6a696c81db
commit
81672d4efe
22
README.md
22
README.md
@ -33,6 +33,26 @@ cat sample.yaml | yaml - b.c
|
|||||||
```
|
```
|
||||||
will output the value of '2'.
|
will output the value of '2'.
|
||||||
|
|
||||||
|
### Splat
|
||||||
|
Given a sample.yaml file of:
|
||||||
|
```yaml
|
||||||
|
---
|
||||||
|
bob:
|
||||||
|
item1:
|
||||||
|
cats: bananas
|
||||||
|
item2:
|
||||||
|
cats: apples
|
||||||
|
```
|
||||||
|
then
|
||||||
|
```bash
|
||||||
|
yaml sample.yaml bob.*.cats
|
||||||
|
```
|
||||||
|
will output
|
||||||
|
```yaml
|
||||||
|
- bananas
|
||||||
|
- apples
|
||||||
|
```
|
||||||
|
|
||||||
### Handling '.' in the yaml key
|
### Handling '.' in the yaml key
|
||||||
Given a sample.yaml file of:
|
Given a sample.yaml file of:
|
||||||
```yaml
|
```yaml
|
||||||
@ -82,6 +102,8 @@ will output:
|
|||||||
- sam
|
- sam
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### Updating yaml
|
### Updating yaml
|
||||||
Given a sample.yaml file of:
|
Given a sample.yaml file of:
|
||||||
```yaml
|
```yaml
|
||||||
|
@ -19,10 +19,27 @@ func write(context map[interface{}]interface{}, head string, tail []string, valu
|
|||||||
}
|
}
|
||||||
|
|
||||||
func readMap(context map[interface{}]interface{}, head string, tail []string) interface{} {
|
func readMap(context map[interface{}]interface{}, head string, tail []string) interface{} {
|
||||||
|
if head == "*" {
|
||||||
|
return readMapSplat(context, tail)
|
||||||
|
}
|
||||||
value := context[head]
|
value := context[head]
|
||||||
return calculateValue(value, tail)
|
return calculateValue(value, tail)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func readMapSplat(context map[interface{}]interface{}, tail []string) interface{} {
|
||||||
|
var newArray = make([]interface{}, len(context))
|
||||||
|
var i = 0
|
||||||
|
for _, value := range context {
|
||||||
|
if len(tail) > 0 {
|
||||||
|
newArray[i] = recurse(value, tail[0], tail[1:len(tail)])
|
||||||
|
} else {
|
||||||
|
newArray[i] = value
|
||||||
|
}
|
||||||
|
i++
|
||||||
|
}
|
||||||
|
return newArray
|
||||||
|
}
|
||||||
|
|
||||||
func recurse(value interface{}, head string, tail []string) interface{} {
|
func recurse(value interface{}, head string, tail []string) interface{} {
|
||||||
switch value.(type) {
|
switch value.(type) {
|
||||||
case []interface{}:
|
case []interface{}:
|
||||||
|
@ -4,6 +4,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"gopkg.in/yaml.v2"
|
"gopkg.in/yaml.v2"
|
||||||
"os"
|
"os"
|
||||||
|
"sort"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -26,6 +27,32 @@ b:
|
|||||||
assertResult(t, 2, readMap(data, "b", []string{"c"}))
|
assertResult(t, 2, readMap(data, "b", []string{"c"}))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestReadMap_splat(t *testing.T) {
|
||||||
|
var data = parseData(`
|
||||||
|
---
|
||||||
|
mapSplat:
|
||||||
|
item1: things
|
||||||
|
item2: whatever
|
||||||
|
`)
|
||||||
|
assertResult(t, "[things whatever]", fmt.Sprintf("%v", readMap(data, "mapSplat", []string{"*"})))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestReadMap_deep_splat(t *testing.T) {
|
||||||
|
var data = parseData(`
|
||||||
|
---
|
||||||
|
mapSplatDeep:
|
||||||
|
item1:
|
||||||
|
cats: bananas
|
||||||
|
item2:
|
||||||
|
cats: apples
|
||||||
|
`)
|
||||||
|
|
||||||
|
var result = readMap(data, "mapSplatDeep", []string{"*", "cats"}).([]interface{})
|
||||||
|
var actual = []string{result[0].(string), result[1].(string)}
|
||||||
|
sort.Strings(actual)
|
||||||
|
assertResult(t, "[apples bananas]", fmt.Sprintf("%v", actual))
|
||||||
|
}
|
||||||
|
|
||||||
func TestReadMap_key_doesnt_exist(t *testing.T) {
|
func TestReadMap_key_doesnt_exist(t *testing.T) {
|
||||||
var data = parseData(`
|
var data = parseData(`
|
||||||
---
|
---
|
||||||
|
@ -1,4 +1,9 @@
|
|||||||
a: Easy! as one two three
|
a: Easy! as one two three
|
||||||
b:
|
b:
|
||||||
c: 3
|
c: things
|
||||||
d: [3, 4]
|
d: whatever
|
||||||
|
things:
|
||||||
|
thing1:
|
||||||
|
cat: 'fred'
|
||||||
|
thing2:
|
||||||
|
cat: 'sam'
|
||||||
|
Loading…
Reference in New Issue
Block a user