Skip to content

Commit

Permalink
Merge pull request Masterminds#268 from phuslu/allany
Browse files Browse the repository at this point in the history
add all/any for evaluating multiple conditions
  • Loading branch information
mattfarina authored Dec 3, 2020
2 parents 18b20c1 + a60fe27 commit a81d0aa
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 0 deletions.
22 changes: 22 additions & 0 deletions defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,28 @@ func coalesce(v ...interface{}) interface{} {
return nil
}

// all returns true if empty(x) is false for all values x in the list.
// If the list is empty, return true.
func all(v ...interface{}) bool {
for _, val := range v {
if empty(val) {
return false
}
}
return true
}

// any returns true if empty(x) is false for any x in the list.
// If the list is empty, return false.
func any(v ...interface{}) bool {
for _, val := range v {
if !empty(val) {
return true
}
}
return false
}

// fromJson decodes JSON into a structured value, ignoring errors.
func fromJson(v string) interface{} {
output, _ := mustFromJson(v)
Expand Down
41 changes: 41 additions & 0 deletions defaults_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func TestEmpty(t *testing.T) {
t.Error(err)
}
}

func TestCoalesce(t *testing.T) {
tests := map[string]string{
`{{ coalesce 1 }}`: "1",
Expand All @@ -83,6 +84,46 @@ func TestCoalesce(t *testing.T) {
}
}

func TestAll(t *testing.T) {
tests := map[string]string{
`{{ all 1 }}`: "true",
`{{ all "" 0 nil 2 }}`: "false",
`{{ $two := 2 }}{{ all "" 0 nil $two }}`: "false",
`{{ $two := 2 }}{{ all "" $two 0 0 0 }}`: "false",
`{{ $two := 2 }}{{ all "" $two 3 4 5 }}`: "false",
`{{ all }}`: "true",
}
for tpl, expect := range tests {
assert.NoError(t, runt(tpl, expect))
}

dict := map[string]interface{}{"top": map[string]interface{}{}}
tpl := `{{ all .top.NoSuchThing .bottom .bottom.dollar "airplane"}}`
if err := runtv(tpl, "false", dict); err != nil {
t.Error(err)
}
}

func TestAny(t *testing.T) {
tests := map[string]string{
`{{ any 1 }}`: "true",
`{{ any "" 0 nil 2 }}`: "true",
`{{ $two := 2 }}{{ any "" 0 nil $two }}`: "true",
`{{ $two := 2 }}{{ any "" $two 3 4 5 }}`: "true",
`{{ $zero := 0 }}{{ any "" $zero 0 0 0 }}`: "false",
`{{ any }}`: "false",
}
for tpl, expect := range tests {
assert.NoError(t, runt(tpl, expect))
}

dict := map[string]interface{}{"top": map[string]interface{}{}}
tpl := `{{ any .top.NoSuchThing .bottom .bottom.dollar "airplane"}}`
if err := runtv(tpl, "true", dict); err != nil {
t.Error(err)
}
}

func TestFromJson(t *testing.T) {
dict := map[string]interface{}{"Input": `{"foo": 55}`}

Expand Down
36 changes: 36 additions & 0 deletions docs/defaults.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,42 @@ The above will first check to see if `.name` is empty. If it is not, it will ret
that value. If it _is_ empty, `coalesce` will evaluate `.parent.name` for emptiness.
Finally, if both `.name` and `.parent.name` are empty, it will return `Matt`.

## all

The `all` function takes a list of values and returns true if all values are non-empty.

```
all 0 1 2
```

The above returns `false`.

This function is useful for evaluating multiple conditions of variables or values:

```
all (eq .Request.TLS.Version 0x0304) (.Request.ProtoAtLeast 2 0) (eq .Request.Method "POST")
```

The above will check http.Request is POST with tls 1.3 and http/2.

## any

The `any` function takes a list of values and returns true if any value is non-empty.

```
any 0 1 2
```

The above returns `true`.

This function is useful for evaluating multiple conditions of variables or values:

```
any (eq .Request.Method "GET") (eq .Request.Method "POST") (eq .Request.Method "OPTIONS")
```

The above will check http.Request method is one of GET/POST/OPTIONS.

## fromJson, mustFromJson

`fromJson` decodes a JSON document into a structure. If the input cannot be decoded as JSON the function will return an empty string.
Expand Down
2 changes: 2 additions & 0 deletions functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,8 @@ var genericMap = map[string]interface{}{
"default": dfault,
"empty": empty,
"coalesce": coalesce,
"all": all,
"any": any,
"compact": compact,
"mustCompact": mustCompact,
"fromJson": fromJson,
Expand Down

0 comments on commit a81d0aa

Please sign in to comment.