summaryrefslogtreecommitdiffstats
path: root/tpl/compare
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2022-12-21 13:11:08 +0100
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2022-12-21 15:33:02 +0100
commitcd1ed563a82f8a9ebdd3109230e34e74bf5ec6eb (patch)
treed766a7d88e59d20be9380aaf3d45b799737a288e /tpl/compare
parentaa2c724195ae53e698dfaa7f9bea63e5cecea391 (diff)
tpl: Improve template funcs GoDoc
Diffstat (limited to 'tpl/compare')
-rw-r--r--tpl/compare/compare.go33
1 files changed, 17 insertions, 16 deletions
diff --git a/tpl/compare/compare.go b/tpl/compare/compare.go
index 0b2d065ab..47f77cc99 100644
--- a/tpl/compare/compare.go
+++ b/tpl/compare/compare.go
@@ -40,25 +40,25 @@ type Namespace struct {
caseInsensitive bool
}
-// Default checks whether a given value is set and returns a default value if it
+// Default checks whether a givenv is set and returns the default value defaultv if it
// is not. "Set" in this context means non-zero for numeric types and times;
// non-zero length for strings, arrays, slices, and maps;
// any boolean or struct value; or non-nil for any other types.
-func (*Namespace) Default(dflt any, given ...any) (any, error) {
+func (*Namespace) Default(defaultv any, givenv ...any) (any, error) {
// given is variadic because the following construct will not pass a piped
// argument when the key is missing: {{ index . "key" | default "foo" }}
// The Go template will complain that we got 1 argument when we expected 2.
- if len(given) == 0 {
- return dflt, nil
+ if len(givenv) == 0 {
+ return defaultv, nil
}
- if len(given) != 1 {
- return nil, fmt.Errorf("wrong number of args for default: want 2 got %d", len(given)+1)
+ if len(givenv) != 1 {
+ return nil, fmt.Errorf("wrong number of args for default: want 2 got %d", len(givenv)+1)
}
- g := reflect.ValueOf(given[0])
+ g := reflect.ValueOf(givenv[0])
if !g.IsValid() {
- return dflt, nil
+ return defaultv, nil
}
set := false
@@ -77,7 +77,7 @@ func (*Namespace) Default(dflt any, given ...any) (any, error) {
case reflect.Complex64, reflect.Complex128:
set = g.Complex() != 0
case reflect.Struct:
- switch actual := given[0].(type) {
+ switch actual := givenv[0].(type) {
case time.Time:
set = !actual.IsZero()
default:
@@ -88,10 +88,10 @@ func (*Namespace) Default(dflt any, given ...any) (any, error) {
}
if set {
- return given[0], nil
+ return givenv[0], nil
}
- return dflt, nil
+ return defaultv, nil
}
// Eq returns the boolean truth of arg1 == arg2 || arg1 == arg3 || arg1 == arg4.
@@ -223,12 +223,13 @@ func (n *Namespace) checkComparisonArgCount(min int, others ...any) bool {
}
// Conditional can be used as a ternary operator.
-// It returns a if condition, else b.
-func (n *Namespace) Conditional(condition bool, a, b any) any {
- if condition {
- return a
+//
+// It returns v1 if cond is true, else v2.
+func (n *Namespace) Conditional(cond bool, v1, v2 any) any {
+ if cond {
+ return v1
}
- return b
+ return v2
}
func (ns *Namespace) compareGet(a any, b any) (float64, float64) {