Skip to content

Commit

Permalink
stub with string list
Browse files Browse the repository at this point in the history
  • Loading branch information
mandelsoft committed Feb 23, 2019
1 parent 0535053 commit 2ca1e0a
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 5 deletions.
81 changes: 79 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -955,6 +955,79 @@ foo:
- 4
```

### `<<: (( merge none ))`

If the reference of an redirecting merge is set to the constant `none`,
no merge is done at all.

e.g.: for

**template.yml**
```yaml
map:
<<: (( merge none ))
value: notmerged
```

**values.yml**
```yaml
map:
value: merged
```

`spiff merge template.yml values.yml` yields:

```yaml
map:
value: notmerged
```

This can be used for explicit field merging using the `stub` function
to access dedicated parts of upstream stubs.

e.g.:

**template.yml**
```yaml
map:
<<: (( merge none ))
value: (( "alice" "+" stub(map.value) ))
```

**values.yml**
```yaml
map:
value: bob
```

`spiff merge template.yml values.yml` yields:

```yaml
test:
value: alice+bob
```

This also works for dedicated fields:

**template.yml**
```yaml
map:
value: (( merge none // "alice" "+" stub(map.value) ))
```

**values.yml**
```yaml
map:
value: bob
```

`spiff merge template.yml values.yml` yields:

```yaml
test:
value: alice+bob
```

## `(( a || b ))`

Uses a, or b if a cannot be resolved.
Expand Down Expand Up @@ -1622,7 +1695,8 @@ alice: default

### `(( stub(foo.bar) ))`

The function `stub` yields the value of a dedicated field found in the first upstream stub defining it.
The function `stub` yields the value of a dedicated field found in the first
upstream stub defining it.

e.g.:

Expand All @@ -1644,7 +1718,10 @@ evaluates to
value: foobar
```

The argument passed to this function must either be a reference literal or an expression evaluating to a string denoting a reference. If no argument is given, the actual field path is used.
The argument passed to this function must either be a reference literal or
an expression evaluating to either a string denoting a reference or a string
list denoting the list of path elements for the reference.
If no argument is given, the actual field path is used.

Alternatively the `merge` operation could be used, for example `merge foo.bar`. The difference is that `stub` does not merge, therefore the field will still be merged (with the original path in the document).

Expand Down
16 changes: 13 additions & 3 deletions dynaml/stub.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dynaml

import (
"github.com/mandelsoft/spiff/yaml"
"strings"
)

Expand All @@ -24,11 +25,20 @@ func (e CallExpr) stub(binding Binding) (interface{}, EvaluationInfo, bool) {
if !ok {
return val, info, ok
} else {
str, ok := val.(string)
if !ok {
switch v := val.(type) {
case string:
arg = strings.Split(v, ".")
case []yaml.Node:
for _, n := range v {
str, ok := n.Value().(string)
if !ok {
return info.Error("stub() requires a string entries in list")
}
arg = append(arg, str)
}
default:
return info.Error("stub() requires a string or reference argument")
}
arg = strings.Split(str, ".")
}
} else {
arg = ref.Path
Expand Down
18 changes: 18 additions & 0 deletions flow/flow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3010,6 +3010,24 @@ age: 24
Expect(source).To(FlowAs(resolved, stub))
})

It("handles string list arg", func() {
source := parseYAML(`
---
age: (( stub(["data","alice"]) ))
`)
stub := parseYAML(`
---
data:
alice: "24"
`)

resolved := parseYAML(`
---
age: "24"
`)
Expect(source).To(FlowAs(resolved, stub))
})

It("fails on missing stub", func() {
source := parseYAML(`
---
Expand Down

0 comments on commit 2ca1e0a

Please sign in to comment.