Skip to content

Commit 4bf848a

Browse files
authored
Make template Iif exactly match if (#31322)
1 parent 61c97fd commit 4bf848a

File tree

2 files changed

+47
-21
lines changed

2 files changed

+47
-21
lines changed

modules/templates/helper.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -239,15 +239,15 @@ func DotEscape(raw string) string {
239239
// Iif is an "inline-if", similar util.Iif[T] but templates need the non-generic version,
240240
// and it could be simply used as "{{Iif expr trueVal}}" (omit the falseVal).
241241
func Iif(condition any, vals ...any) any {
242-
if IsTruthy(condition) {
242+
if isTemplateTruthy(condition) {
243243
return vals[0]
244244
} else if len(vals) > 1 {
245245
return vals[1]
246246
}
247247
return nil
248248
}
249249

250-
func IsTruthy(v any) bool {
250+
func isTemplateTruthy(v any) bool {
251251
if v == nil {
252252
return false
253253
}
@@ -256,20 +256,20 @@ func IsTruthy(v any) bool {
256256
switch rv.Kind() {
257257
case reflect.Bool:
258258
return rv.Bool()
259-
case reflect.String:
260-
return rv.String() != ""
261259
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
262260
return rv.Int() != 0
263261
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
264262
return rv.Uint() != 0
265263
case reflect.Float32, reflect.Float64:
266264
return rv.Float() != 0
267-
case reflect.Slice, reflect.Array, reflect.Map:
265+
case reflect.Complex64, reflect.Complex128:
266+
return rv.Complex() != 0
267+
case reflect.String, reflect.Slice, reflect.Array, reflect.Map:
268268
return rv.Len() > 0
269-
case reflect.Ptr:
270-
return !rv.IsNil() && IsTruthy(reflect.Indirect(rv).Interface())
269+
case reflect.Struct:
270+
return true
271271
default:
272-
return rv.Kind() == reflect.Struct && !rv.IsNil()
272+
return !rv.IsNil()
273273
}
274274
}
275275

modules/templates/helper_test.go

Lines changed: 39 additions & 13 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

@@ -66,17 +69,40 @@ 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
}
6871

69-
func TestIsTruthy(t *testing.T) {
70-
var test any
71-
assert.Equal(t, false, IsTruthy(test))
72-
assert.Equal(t, false, IsTruthy(nil))
73-
assert.Equal(t, false, IsTruthy(""))
74-
assert.Equal(t, true, IsTruthy("non-empty"))
75-
assert.Equal(t, true, IsTruthy(-1))
76-
assert.Equal(t, false, IsTruthy(0))
77-
assert.Equal(t, true, IsTruthy(42))
78-
assert.Equal(t, false, IsTruthy(0.0))
79-
assert.Equal(t, true, IsTruthy(3.14))
80-
assert.Equal(t, false, IsTruthy([]int{}))
81-
assert.Equal(t, true, IsTruthy([]int{1}))
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))
82108
}

0 commit comments

Comments
 (0)