Skip to content

Commit edec740

Browse files
committed
Merge branch 'webner-when-function'
2 parents 9aa1cda + 108e442 commit edec740

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ For example, this is a JSON version of an emitted RuntimeContainer struct:
235235
* *`whereNotExist $items $fieldPath`*: Like `where`, but returns only items where `$fieldPath` does not exist (is nil).
236236
* *`whereAny $items $fieldPath $sep $values`*: Like `where`, but the string value specified by `$fieldPath` is first split by `$sep` into a list of strings. The comparison value is a string slice with possible matches. Returns items which OR intersect these values.
237237
* *`whereAll $items $fieldPath $sep $values`*: Like `whereAny`, except all `$values` must exist in the `$fieldPath`.
238+
* *`when $condition $trueValue $falseValue`*: Returns the `$trueValue` when the `$condition` is `true` and the `$falseValue` otherwise
238239

239240
===
240241

template.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,15 @@ func trim(s string) string {
332332
return strings.TrimSpace(s)
333333
}
334334

335+
// when returns the trueValue when the condition is true and the falseValue otherwise
336+
func when(condition bool, trueValue, falseValue interface{}) interface{} {
337+
if condition {
338+
return trueValue
339+
} else {
340+
return falseValue
341+
}
342+
}
343+
335344
func newTemplate(name string) *template.Template {
336345
tmpl := template.New(name).Funcs(template.FuncMap{
337346
"closest": arrayClosest,
@@ -363,6 +372,7 @@ func newTemplate(name string) *template.Template {
363372
"whereNotExist": whereNotExist,
364373
"whereAny": whereAny,
365374
"whereAll": whereAll,
375+
"when": when,
366376
})
367377
return tmpl
368378
}

template_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,3 +618,36 @@ func TestArrayClosestNoMatch(t *testing.T) {
618618
t.Fatal("Expected ''")
619619
}
620620
}
621+
622+
func TestWhen(t *testing.T) {
623+
context := struct {
624+
BoolValue bool
625+
StringValue string
626+
}{
627+
true,
628+
"foo",
629+
}
630+
631+
tests := templateTestList{
632+
{`{{ print (when .BoolValue "first" "second") }}`, context, `first`},
633+
{`{{ print (when (eq .StringValue "foo") "first" "second") }}`, context, `first`},
634+
635+
{`{{ when (not .BoolValue) "first" "second" | print }}`, context, `second`},
636+
{`{{ when (not (eq .StringValue "foo")) "first" "second" | print }}`, context, `second`},
637+
}
638+
639+
tests.run(t, "when")
640+
}
641+
642+
func TestWhenTrue(t *testing.T) {
643+
if when(true, "first", "second") != "first" {
644+
t.Fatal("Expected first value")
645+
646+
}
647+
}
648+
649+
func TestWhenFalse(t *testing.T) {
650+
if when(false, "first", "second") != "second" {
651+
t.Fatal("Expected second value")
652+
}
653+
}

0 commit comments

Comments
 (0)