Skip to content

Commit be88baf

Browse files
committed
add some comments
1 parent fcc9d89 commit be88baf

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

modules/templates/eval/eval.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -207,11 +207,7 @@ func (e *eval) applyOp() {
207207
}
208208
}
209209

210-
// Exec evaluates the given expression tokens and returns the result.
211-
// If no error occurs, the result is either an int64 or a float64.
212-
// If all numbers are integer, the result is an int64, otherwise if there is any float number, the result is a float64.
213-
// Golang's template syntax supports comparable int types: {{if lt $i32 $i64}} is right.
214-
func (e *eval) Exec(tokens ...any) (ret Num, err error) {
210+
func (e *eval) exec(tokens ...any) (ret Num, err error) {
215211
defer func() {
216212
if r := recover(); r != nil {
217213
rErr, ok := r.(error)
@@ -336,8 +332,13 @@ func fnSum(nums []Num) Num {
336332
return Num{sum}
337333
}
338334

335+
// Expr evaluates the given expression tokens and returns the result.
336+
// It supports the following operators: +, -, *, /, and, or, not, ==, !=, >, >=, <, <=.
337+
// Non-zero values are treated as true, zero values are treated as false.
338+
// If no error occurs, the result is either an int64 or a float64.
339+
// If all numbers are integer, the result is an int64, otherwise if there is any float number, the result is a float64.
339340
func Expr(tokens ...any) (Num, error) {
340341
e := newEval()
341342
e.funcMap = map[string]func([]Num) Num{"sum": fnSum}
342-
return e.Exec(tokens...)
343+
return e.exec(tokens...)
343344
}

modules/templates/helper.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -985,6 +985,13 @@ func LocaleNumber(v interface{}) template.HTML {
985985
return template.HTML(fmt.Sprintf(`<gitea-locale-number data-number="%d">%d</gitea-locale-number>`, num, num))
986986
}
987987

988+
// Eval the expression and return the result, see the comment of eval.Expr for details.
989+
// To use this helper function in templates, pass each token as a separate parameter.
990+
//
991+
// {{ $int64 := Eval $var "+" 1 }}
992+
// {{ $float64 := Eval $var "+" 1.0 }}
993+
//
994+
// Golang's template supports comparable int types, so the int64 result can be used in later statements like {{if lt $int64 10}}
988995
func Eval(tokens ...any) (any, error) {
989996
n, err := eval.Expr(tokens...)
990997
return n.Value, err

0 commit comments

Comments
 (0)