summaryrefslogtreecommitdiffstats
path: root/modules
diff options
context:
space:
mode:
authorwxiaoguang <wxiaoguang@gmail.com>2023-04-23 02:16:22 +0800
committerGitHub <noreply@github.com>2023-04-22 14:16:22 -0400
commit88201914767e15a51c910d295647d97973fa09bf (patch)
tree460ac508dc67089db99877d5394db98844283f27 /modules
parentc0d105609f120839c91679b0548b9438155c4bba (diff)
downloadgitea-88201914767e15a51c910d295647d97973fa09bf.tar.gz
gitea-88201914767e15a51c910d295647d97973fa09bf.zip
Improve template helper functions: string/slice (#24266)
Follow #23328 The improvements: 1. The `contains` functions are covered by tests 2. The inconsistent behavior of `containGeneric` is replaced by `StringUtils.Contains` and `SliceUtils.Contains` 3. In the future we can move more help functions into XxxUtils to simplify the `helper.go` and reduce unnecessary global functions. FAQ: 1. Why it's called `StringUtils.Contains` but not `strings.Contains` like Golang? Because our `StringUtils` is not Golang's `strings` package. There will be our own string functions. --------- Co-authored-by: silverwind <me@silverwind.io>
Diffstat (limited to 'modules')
-rw-r--r--modules/templates/helper.go36
-rw-r--r--modules/templates/util_dict.go (renamed from modules/templates/util.go)0
-rw-r--r--modules/templates/util_slice.go35
-rw-r--r--modules/templates/util_string.go20
-rw-r--r--modules/templates/util_test.go36
5 files changed, 96 insertions, 31 deletions
diff --git a/modules/templates/helper.go b/modules/templates/helper.go
index 42680827eb..c5b77989be 100644
--- a/modules/templates/helper.go
+++ b/modules/templates/helper.go
@@ -15,7 +15,6 @@ import (
"mime"
"net/url"
"path/filepath"
- "reflect"
"regexp"
"strings"
"time"
@@ -68,11 +67,15 @@ func NewFuncMap() []template.FuncMap {
"PathEscape": url.PathEscape,
"PathEscapeSegments": util.PathEscapeSegments,
+ // utils
+ "StringUtils": NewStringUtils,
+ "SliceUtils": NewSliceUtils,
+
// -----------------------------------------------------------------
// string / json
+ // TODO: move string helper functions to StringUtils
"Join": strings.Join,
"DotEscape": DotEscape,
- "HasPrefix": strings.HasPrefix,
"EllipsisString": base.EllipsisString,
"DumpVar": dumpVar,
@@ -145,35 +148,6 @@ func NewFuncMap() []template.FuncMap {
},
// -----------------------------------------------------------------
- // slice
- "containGeneric": func(arr, v interface{}) bool {
- arrV := reflect.ValueOf(arr)
- if arrV.Kind() == reflect.String && reflect.ValueOf(v).Kind() == reflect.String {
- return strings.Contains(arr.(string), v.(string))
- }
- if arrV.Kind() == reflect.Slice {
- for i := 0; i < arrV.Len(); i++ {
- iV := arrV.Index(i)
- if !iV.CanInterface() {
- continue
- }
- if iV.Interface() == v {
- return true
- }
- }
- }
- return false
- },
- "contain": func(s []int64, id int64) bool {
- for i := 0; i < len(s); i++ {
- if s[i] == id {
- return true
- }
- }
- return false
- },
-
- // -----------------------------------------------------------------
// setting
"AppName": func() string {
return setting.AppName
diff --git a/modules/templates/util.go b/modules/templates/util_dict.go
index c83f22449c..c83f22449c 100644
--- a/modules/templates/util.go
+++ b/modules/templates/util_dict.go
diff --git a/modules/templates/util_slice.go b/modules/templates/util_slice.go
new file mode 100644
index 0000000000..a3318cc11a
--- /dev/null
+++ b/modules/templates/util_slice.go
@@ -0,0 +1,35 @@
+// Copyright 2023 The Gitea Authors. All rights reserved.
+// SPDX-License-Identifier: MIT
+
+package templates
+
+import (
+ "fmt"
+ "reflect"
+)
+
+type SliceUtils struct{}
+
+func NewSliceUtils() *SliceUtils {
+ return &SliceUtils{}
+}
+
+func (su *SliceUtils) Contains(s, v any) bool {
+ if s == nil {
+ return false
+ }
+ sv := reflect.ValueOf(s)
+ if sv.Kind() != reflect.Slice && sv.Kind() != reflect.Array {
+ panic(fmt.Sprintf("invalid type, expected slice or array, but got: %T", s))
+ }
+ for i := 0; i < sv.Len(); i++ {
+ it := sv.Index(i)
+ if !it.CanInterface() {
+ continue
+ }
+ if it.Interface() == v {
+ return true
+ }
+ }
+ return false
+}
diff --git a/modules/templates/util_string.go b/modules/templates/util_string.go
new file mode 100644
index 0000000000..e86bbe9e70
--- /dev/null
+++ b/modules/templates/util_string.go
@@ -0,0 +1,20 @@
+// Copyright 2023 The Gitea Authors. All rights reserved.
+// SPDX-License-Identifier: MIT
+
+package templates
+
+import "strings"
+
+type StringUtils struct{}
+
+func NewStringUtils() *StringUtils {
+ return &StringUtils{}
+}
+
+func (su *StringUtils) HasPrefix(s, prefix string) bool {
+ return strings.HasPrefix(s, prefix)
+}
+
+func (su *StringUtils) Contains(s, substr string) bool {
+ return strings.Contains(s, substr)
+}
diff --git a/modules/templates/util_test.go b/modules/templates/util_test.go
index dfa691c5e2..febaf7fa88 100644
--- a/modules/templates/util_test.go
+++ b/modules/templates/util_test.go
@@ -4,6 +4,9 @@
package templates
import (
+ "html/template"
+ "io"
+ "strings"
"testing"
"github.com/stretchr/testify/assert"
@@ -41,3 +44,36 @@ func TestDict(t *testing.T) {
assert.Error(t, err)
}
}
+
+func TestUtils(t *testing.T) {
+ execTmpl := func(code string, data any) string {
+ tmpl := template.New("test")
+ tmpl.Funcs(template.FuncMap{"SliceUtils": NewSliceUtils, "StringUtils": NewStringUtils})
+ template.Must(tmpl.Parse(code))
+ w := &strings.Builder{}
+ assert.NoError(t, tmpl.Execute(w, data))
+ return w.String()
+ }
+
+ actual := execTmpl("{{SliceUtils.Contains .Slice .Value}}", map[string]any{"Slice": []string{"a", "b"}, "Value": "a"})
+ assert.Equal(t, "true", actual)
+
+ actual = execTmpl("{{SliceUtils.Contains .Slice .Value}}", map[string]any{"Slice": []string{"a", "b"}, "Value": "x"})
+ assert.Equal(t, "false", actual)
+
+ actual = execTmpl("{{SliceUtils.Contains .Slice .Value}}", map[string]any{"Slice": []int64{1, 2}, "Value": int64(2)})
+ assert.Equal(t, "true", actual)
+
+ actual = execTmpl("{{StringUtils.Contains .String .Value}}", map[string]any{"String": "abc", "Value": "b"})
+ assert.Equal(t, "true", actual)
+
+ actual = execTmpl("{{StringUtils.Contains .String .Value}}", map[string]any{"String": "abc", "Value": "x"})
+ assert.Equal(t, "false", actual)
+
+ tmpl := template.New("test")
+ tmpl.Funcs(template.FuncMap{"SliceUtils": NewSliceUtils, "StringUtils": NewStringUtils})
+ template.Must(tmpl.Parse("{{SliceUtils.Contains .Slice .Value}}"))
+ // error is like this: `template: test:1:12: executing "test" at <SliceUtils.Contains>: error calling Contains: ...`
+ err := tmpl.Execute(io.Discard, map[string]any{"Slice": struct{}{}})
+ assert.ErrorContains(t, err, "invalid type, expected slice or array")
+}