Skip to content

Commit 81672d4

Browse files
author
mfarah
committed
Can now splat maps
1 parent 6a696c8 commit 81672d4

File tree

4 files changed

+73
-2
lines changed

4 files changed

+73
-2
lines changed

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,26 @@ cat sample.yaml | yaml - b.c
3333
```
3434
will output the value of '2'.
3535
36+
### Splat
37+
Given a sample.yaml file of:
38+
```yaml
39+
---
40+
bob:
41+
item1:
42+
cats: bananas
43+
item2:
44+
cats: apples
45+
```
46+
then
47+
```bash
48+
yaml sample.yaml bob.*.cats
49+
```
50+
will output
51+
```yaml
52+
- bananas
53+
- apples
54+
```
55+
3656
### Handling '.' in the yaml key
3757
Given a sample.yaml file of:
3858
```yaml
@@ -82,6 +102,8 @@ will output:
82102
- sam
83103
```
84104

105+
106+
85107
### Updating yaml
86108
Given a sample.yaml file of:
87109
```yaml

data_navigator.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,27 @@ func write(context map[interface{}]interface{}, head string, tail []string, valu
1919
}
2020

2121
func readMap(context map[interface{}]interface{}, head string, tail []string) interface{} {
22+
if head == "*" {
23+
return readMapSplat(context, tail)
24+
}
2225
value := context[head]
2326
return calculateValue(value, tail)
2427
}
2528

29+
func readMapSplat(context map[interface{}]interface{}, tail []string) interface{} {
30+
var newArray = make([]interface{}, len(context))
31+
var i = 0
32+
for _, value := range context {
33+
if len(tail) > 0 {
34+
newArray[i] = recurse(value, tail[0], tail[1:len(tail)])
35+
} else {
36+
newArray[i] = value
37+
}
38+
i++
39+
}
40+
return newArray
41+
}
42+
2643
func recurse(value interface{}, head string, tail []string) interface{} {
2744
switch value.(type) {
2845
case []interface{}:

data_navigator_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"gopkg.in/yaml.v2"
66
"os"
7+
"sort"
78
"testing"
89
)
910

@@ -26,6 +27,32 @@ b:
2627
assertResult(t, 2, readMap(data, "b", []string{"c"}))
2728
}
2829

30+
func TestReadMap_splat(t *testing.T) {
31+
var data = parseData(`
32+
---
33+
mapSplat:
34+
item1: things
35+
item2: whatever
36+
`)
37+
assertResult(t, "[things whatever]", fmt.Sprintf("%v", readMap(data, "mapSplat", []string{"*"})))
38+
}
39+
40+
func TestReadMap_deep_splat(t *testing.T) {
41+
var data = parseData(`
42+
---
43+
mapSplatDeep:
44+
item1:
45+
cats: bananas
46+
item2:
47+
cats: apples
48+
`)
49+
50+
var result = readMap(data, "mapSplatDeep", []string{"*", "cats"}).([]interface{})
51+
var actual = []string{result[0].(string), result[1].(string)}
52+
sort.Strings(actual)
53+
assertResult(t, "[apples bananas]", fmt.Sprintf("%v", actual))
54+
}
55+
2956
func TestReadMap_key_doesnt_exist(t *testing.T) {
3057
var data = parseData(`
3158
---

sample2.yaml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
a: Easy! as one two three
22
b:
3-
c: 3
4-
d: [3, 4]
3+
c: things
4+
d: whatever
5+
things:
6+
thing1:
7+
cat: 'fred'
8+
thing2:
9+
cat: 'sam'

0 commit comments

Comments
 (0)