Skip to content

Commit cdd057c

Browse files
authored
Backport Iif (#31353)
1 parent 6d0d464 commit cdd057c

File tree

2 files changed

+70
-2
lines changed

2 files changed

+70
-2
lines changed

modules/templates/helper.go

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"html"
1010
"html/template"
1111
"net/url"
12+
"reflect"
1213
"slices"
1314
"strings"
1415
"time"
@@ -237,15 +238,41 @@ func DotEscape(raw string) string {
237238

238239
// Iif is an "inline-if", similar util.Iif[T] but templates need the non-generic version,
239240
// and it could be simply used as "{{Iif expr trueVal}}" (omit the falseVal).
240-
func Iif(condition bool, vals ...any) any {
241-
if condition {
241+
func Iif(condition any, vals ...any) any {
242+
if isTemplateTruthy(condition) {
242243
return vals[0]
243244
} else if len(vals) > 1 {
244245
return vals[1]
245246
}
246247
return nil
247248
}
248249

250+
func isTemplateTruthy(v any) bool {
251+
if v == nil {
252+
return false
253+
}
254+
255+
rv := reflect.ValueOf(v)
256+
switch rv.Kind() {
257+
case reflect.Bool:
258+
return rv.Bool()
259+
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
260+
return rv.Int() != 0
261+
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
262+
return rv.Uint() != 0
263+
case reflect.Float32, reflect.Float64:
264+
return rv.Float() != 0
265+
case reflect.Complex64, reflect.Complex128:
266+
return rv.Complex() != 0
267+
case reflect.String, reflect.Slice, reflect.Array, reflect.Map:
268+
return rv.Len() > 0
269+
case reflect.Struct:
270+
return true
271+
default:
272+
return !rv.IsNil()
273+
}
274+
}
275+
249276
// Eval the expression and return the result, see the comment of eval.Expr for details.
250277
// To use this helper function in templates, pass each token as a separate parameter.
251278
//

modules/templates/helper_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@ package templates
55

66
import (
77
"html/template"
8+
"strings"
89
"testing"
910

11+
"code.gitea.io/gitea/modules/util"
12+
1013
"github.com/stretchr/testify/assert"
1114
)
1215

@@ -65,3 +68,41 @@ func TestHTMLFormat(t *testing.T) {
6568
func TestSanitizeHTML(t *testing.T) {
6669
assert.Equal(t, template.HTML(`<a href="/" rel="nofollow">link</a> xss <div>inline</div>`), SanitizeHTML(`<a href="/">link</a> <a href="javascript:">xss</a> <div style="dangerous">inline</div>`))
6770
}
71+
72+
func TestTemplateTruthy(t *testing.T) {
73+
tmpl := template.New("test")
74+
tmpl.Funcs(template.FuncMap{"Iif": Iif})
75+
template.Must(tmpl.Parse(`{{if .Value}}true{{else}}false{{end}}:{{Iif .Value "true" "false"}}`))
76+
77+
cases := []any{
78+
nil, false, true, "", "string", 0, 1,
79+
byte(0), byte(1), int64(0), int64(1), float64(0), float64(1),
80+
complex(0, 0), complex(1, 0),
81+
(chan int)(nil), make(chan int),
82+
(func())(nil), func() {},
83+
util.ToPointer(0), util.ToPointer(util.ToPointer(0)),
84+
util.ToPointer(1), util.ToPointer(util.ToPointer(1)),
85+
[0]int{},
86+
[1]int{0},
87+
[]int(nil),
88+
[]int{},
89+
[]int{0},
90+
map[any]any(nil),
91+
map[any]any{},
92+
map[any]any{"k": "v"},
93+
(*struct{})(nil),
94+
struct{}{},
95+
util.ToPointer(struct{}{}),
96+
}
97+
w := &strings.Builder{}
98+
truthyCount := 0
99+
for i, v := range cases {
100+
w.Reset()
101+
assert.NoError(t, tmpl.Execute(w, struct{ Value any }{v}), "case %d (%T) %#v fails", i, v, v)
102+
out := w.String()
103+
truthyCount += util.Iif(out == "true:true", 1, 0)
104+
truthyMatches := out == "true:true" || out == "false:false"
105+
assert.True(t, truthyMatches, "case %d (%T) %#v fail: %s", i, v, v, out)
106+
}
107+
assert.True(t, truthyCount != 0 && truthyCount != len(cases))
108+
}

0 commit comments

Comments
 (0)