From 45b527a6237e758faa022b9e5f9b02a806ea9b2e Mon Sep 17 00:00:00 2001 From: Dave Henderson Date: Sat, 17 Nov 2018 12:22:15 -0500 Subject: New test.Ternary function Signed-off-by: Dave Henderson --- docs-src/content/functions/test.yml | 29 +++++++++++++++++++++++++++ docs/content/functions/test.md | 40 +++++++++++++++++++++++++++++++++++++ funcs/test.go | 9 +++++++++ funcs/test_test.go | 15 ++++++++++++++ 4 files changed, 93 insertions(+) diff --git a/docs-src/content/functions/test.yml b/docs-src/content/functions/test.yml index 9f638c88..a2763d12 100644 --- a/docs-src/content/functions/test.yml +++ b/docs-src/content/functions/test.yml @@ -76,3 +76,32 @@ funcs: template: :1:25: executing "" at : error calling required: The `config` datasource must have a value defined for `empty` $ gomplate -d config=config.yaml -i '{{ (ds "config").bogus | required "The `config` datasource must have a value defined for `bogus`" }}' template: :1:7: executing "" at <"config">: map has no entry for key "bogus" + - name: test.Ternary + alias: ternary + description: | + Returns one of two values depending on whether the third is true. Note that the third value does not have to be a boolean - it is converted first by the [`conv.ToBool`](../conv/#conv-tobool) function (values like `true`, `1`, `"true"`, `"Yes"`, etc... are considered true). + + This is effectively a short-form of the following template: + + ``` + {{ if conv.ToBool $condition }}{{ $truevalue }}{{ else }}{{ $falsevalue }}{{ end }} + ``` + + Keep in mind that using an explicit `if`/`else` block is often easier to understand than ternary expressions! + pipeline: true + arguments: + - name: truevalue + required: true + description: the value to return if `condition` is true + - name: falsevalue + required: true + description: the value to return if `condition` is false + - name: condition + required: true + description: the value to evaluate for truthiness + examples: + - | + $ gomplate -i '{{ ternary "FOO" "BAR" false }}' + BAR + $ gomplate -i '{{ ternary "FOO" "BAR" "yes" }}' + FOO diff --git a/docs/content/functions/test.md b/docs/content/functions/test.md index c6d24008..dfb0b0ae 100644 --- a/docs/content/functions/test.md +++ b/docs/content/functions/test.md @@ -122,3 +122,43 @@ template: :1:25: executing "" at : error call $ gomplate -d config=config.yaml -i '{{ (ds "config").bogus | required "The `config` datasource must have a value defined for `bogus`" }}' template: :1:7: executing "" at <"config">: map has no entry for key "bogus" ``` + +## `test.Ternary` + +**Alias:** `ternary` + +Returns one of two values depending on whether the third is true. Note that the third value does not have to be a boolean - it is converted first by the [`conv.ToBool`](../conv/#conv-tobool) function (values like `true`, `1`, `"true"`, `"Yes"`, etc... are considered true). + +This is effectively a short-form of the following template: + +``` +{{ if conv.ToBool $condition }}{{ $truevalue }}{{ else }}{{ $falsevalue }}{{ end }} +``` + +Keep in mind that using an explicit `if`/`else` block is often easier to understand than ternary expressions! + +### Usage +```go +test.Ternary truevalue falsevalue condition +``` + +```go +condition | test.Ternary truevalue falsevalue +``` + +### Arguments + +| name | description | +|------|-------------| +| `truevalue` | _(required)_ the value to return if `condition` is true | +| `falsevalue` | _(required)_ the value to return if `condition` is false | +| `condition` | _(required)_ the value to evaluate for truthiness | + +### Examples + +```console +$ gomplate -i '{{ ternary "FOO" "BAR" false }}' +BAR +$ gomplate -i '{{ ternary "FOO" "BAR" "yes" }}' +FOO +``` diff --git a/funcs/test.go b/funcs/test.go index 4def26f1..ed80e6dc 100644 --- a/funcs/test.go +++ b/funcs/test.go @@ -27,6 +27,7 @@ func AddTestFuncs(f map[string]interface{}) { f["assert"] = TestNS().Assert f["fail"] = TestNS().Fail f["required"] = TestNS().Required + f["ternary"] = TestNS().Ternary } // TestFuncs - @@ -76,3 +77,11 @@ func (f *TestFuncs) Required(args ...interface{}) (interface{}, error) { return nil, errors.Errorf("wrong number of args: want 1 or 2, got %d", len(args)) } } + +// Ternary - +func (f *TestFuncs) Ternary(tval, fval, b interface{}) interface{} { + if conv.ToBool(b) { + return tval + } + return fval +} diff --git a/funcs/test_test.go b/funcs/test_test.go index 7b7adfca..1a6868d3 100644 --- a/funcs/test_test.go +++ b/funcs/test_test.go @@ -63,3 +63,18 @@ func TestRequired(t *testing.T) { assert.NoError(t, err) assert.Equal(t, v, "foo") } + +func TestTernary(t *testing.T) { + f := TestNS() + testdata := []struct { + tval, fval, b interface{} + expected interface{} + }{ + {"foo", 42, false, 42}, + {"foo", 42, "yes", "foo"}, + {false, true, true, false}, + } + for _, d := range testdata { + assert.Equal(t, d.expected, f.Ternary(d.tval, d.fval, d.b)) + } +} -- cgit v1.2.3