mirror of
https://github.com/go-gitea/gitea.git
synced 2026-10-26 06:16:56 +09:00
refactor: clean up pull request comment related code and template dict (#39789)
1. drop comment.commit_id 2. refactor templates, remove `dict "." .` from templates
This commit is contained in:
1 parent
1c117a9352
commit
222136ee71
28 files changed
+321
-266
No files matched your search
@@ -15,23 +15,7 @@ import (
|
||||
"gitea.dev/modules/setting"
|
||||
)
|
||||
|
||||
func dictMerge(base map[string]any, arg any) bool {
|
||||
if arg == nil {
|
||||
return true
|
||||
}
|
||||
rv := reflect.ValueOf(arg)
|
||||
if rv.Kind() == reflect.Map {
|
||||
for _, k := range rv.MapKeys() {
|
||||
base[k.String()] = rv.MapIndex(k).Interface()
|
||||
}
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// dict is a helper function for creating a map[string]any from a list of key-value pairs.
|
||||
// If the key is dot ".", the value is merged into the base map, just like Golang template's dot syntax: dot means current
|
||||
// The dot syntax is highly discouraged because it might cause unclear key conflicts. It's always good to use explicit keys.
|
||||
func dict(args ...any) (map[string]any, error) {
|
||||
if len(args)%2 != 0 {
|
||||
return nil, errors.New("invalid dict constructor syntax: must have key-value pairs")
|
||||
@@ -42,13 +26,7 @@ func dict(args ...any) (map[string]any, error) {
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("invalid dict constructor syntax: unable to merge args[%d]", i)
|
||||
}
|
||||
if key == "." {
|
||||
if ok = dictMerge(m, args[i+1]); !ok {
|
||||
return nil, fmt.Errorf("invalid dict constructor syntax: dot arg[%d] must be followed by a dict", i)
|
||||
}
|
||||
} else {
|
||||
m[key] = args[i+1]
|
||||
}
|
||||
m[key] = args[i+1]
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
@@ -18,18 +18,17 @@ func TestDict(t *testing.T) {
|
||||
args []any
|
||||
want map[string]any
|
||||
}{
|
||||
{nil, M{}},
|
||||
{[]any{"a", nil}, M{"a": nil}},
|
||||
{[]any{"a", 1, "b", 2}, M{"a": 1, "b": 2}},
|
||||
{[]any{".", M{"base": 1}, "b", 2}, M{"base": 1, "b": 2}},
|
||||
{[]any{"a", 1, ".", M{"extra": 2}}, M{"a": 1, "extra": 2}},
|
||||
{[]any{"a", 1, ".", map[string]int{"int": 2}}, M{"a": 1, "int": 2}},
|
||||
{[]any{".", nil, "b", 2}, M{"b": 2}},
|
||||
{[]any{"a", 1, "a", 2}, M{"a": 2}},
|
||||
{[]any{"nested", map[string]int{"int": 2}}, M{"nested": map[string]int{"int": 2}}},
|
||||
}
|
||||
|
||||
for _, c := range cases {
|
||||
got, err := dict(c.args...)
|
||||
if assert.NoError(t, err) {
|
||||
assert.Equal(t, c.want, got)
|
||||
}
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, c.want, got)
|
||||
}
|
||||
|
||||
bads := []struct {
|
||||
@@ -38,6 +37,7 @@ func TestDict(t *testing.T) {
|
||||
{[]any{"a", 1, "b"}},
|
||||
{[]any{1}},
|
||||
{[]any{struct{}{}}},
|
||||
{[]any{1, "value"}},
|
||||
}
|
||||
for _, c := range bads {
|
||||
_, err := dict(c.args...)
|
||||
|
||||
Reference in new issue
Block a user