Skip to content

Commit f31e7ee

Browse files
committed
added when template function
1 parent d235ca2 commit f31e7ee

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
@@ -233,6 +233,7 @@ For example, this is a JSON version of an emitted RuntimeContainer struct:
233233
* *`whereNotExist $items $fieldPath`*: Like `where`, but returns only items where `$fieldPath` does not exist (is nil).
234234
* *`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.
235235
* *`whereAll $items $fieldPath $sep $values`*: Like `whereAny`, except all `$values` must exist in the `$fieldPath`.
236+
* *`when $condition $trueValue $falseValue`*: Returns the `$trueValue` when the `$condition` is `true` and the `$falseValue` otherwise
236237

237238
===
238239

template.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,15 @@ func trimSuffix(suffix, s string) string {
327327
return strings.TrimSuffix(s, suffix)
328328
}
329329

330+
// when returns the trueValue when the condition is true and the falseValue otherwise
331+
func when(condition bool, trueValue, falseValue interface{}) interface{} {
332+
if condition {
333+
return trueValue
334+
} else {
335+
return falseValue
336+
}
337+
}
338+
330339
func newTemplate(name string) *template.Template {
331340
tmpl := template.New(name).Funcs(template.FuncMap{
332341
"closest": arrayClosest,
@@ -357,6 +366,7 @@ func newTemplate(name string) *template.Template {
357366
"whereNotExist": whereNotExist,
358367
"whereAny": whereAny,
359368
"whereAll": whereAll,
369+
"when": when,
360370
})
361371
return tmpl
362372
}

template_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,3 +609,36 @@ func TestArrayClosestNoMatch(t *testing.T) {
609609
t.Fatal("Expected ''")
610610
}
611611
}
612+
613+
func TestWhen(t *testing.T) {
614+
context := struct {
615+
BoolValue bool
616+
StringValue string
617+
}{
618+
true,
619+
"foo",
620+
}
621+
622+
tests := templateTestList{
623+
{`{{ print (when .BoolValue "first" "second") }}`, context, `first`},
624+
{`{{ print (when (eq .StringValue "foo") "first" "second") }}`, context, `first`},
625+
626+
{`{{ when (not .BoolValue) "first" "second" | print }}`, context, `second`},
627+
{`{{ when (not (eq .StringValue "foo")) "first" "second" | print }}`, context, `second`},
628+
}
629+
630+
tests.run(t, "when")
631+
}
632+
633+
func TestWhenTrue(t *testing.T) {
634+
if when(true, "first", "second") != "first" {
635+
t.Fatal("Expected first value")
636+
637+
}
638+
}
639+
640+
func TestWhenFalse(t *testing.T) {
641+
if when(false, "first", "second") != "second" {
642+
t.Fatal("Expected second value")
643+
}
644+
}

0 commit comments

Comments
 (0)