From 0ccc3ae85b352b4d067796a48f6f1b7b7a084b54 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Wed, 5 Apr 2023 11:24:08 +0800 Subject: [PATCH 1/3] fix --- modules/templates/helper.go | 85 ++----------------- modules/templates/util.go | 47 ++++++++++ modules/templates/util_test.go | 43 ++++++++++ modules/util/util.go | 24 ------ templates/org/team/teams.tmpl | 2 +- templates/repo/diff/comments.tmpl | 8 +- templates/repo/diff/section_split.tmpl | 12 +-- templates/repo/diff/section_unified.tmpl | 2 +- templates/repo/issue/list.tmpl | 2 +- templates/repo/issue/milestone_issues.tmpl | 2 +- templates/repo/issue/new_form.tmpl | 12 +-- templates/repo/issue/view_content.tmpl | 8 +- .../repo/issue/view_content/comments.tmpl | 80 ++++++++--------- .../repo/issue/view_content/reactions.tmpl | 2 +- templates/user/dashboard/issues.tmpl | 2 +- .../notification_subscriptions.tmpl | 2 +- 16 files changed, 162 insertions(+), 171 deletions(-) create mode 100644 modules/templates/util.go create mode 100644 modules/templates/util_test.go diff --git a/modules/templates/helper.go b/modules/templates/helper.go index 54c85863bd827..fad181d47f410 100644 --- a/modules/templates/helper.go +++ b/modules/templates/helper.go @@ -8,7 +8,6 @@ import ( "bytes" "context" "encoding/hex" - "errors" "fmt" "html" "html/template" @@ -265,20 +264,6 @@ func NewFuncMap() []template.FuncMap { "DisableImportLocal": func() bool { return !setting.ImportLocalPaths }, - "Dict": func(values ...interface{}) (map[string]interface{}, error) { - if len(values)%2 != 0 { - return nil, errors.New("invalid dict call") - } - dict := make(map[string]interface{}, len(values)/2) - for i := 0; i < len(values); i += 2 { - key, ok := values[i].(string) - if !ok { - return nil, errors.New("dict keys must be strings") - } - dict[key] = values[i+1] - } - return dict, nil - }, "Printf": fmt.Sprintf, "Escape": Escape, "Sec2Time": util.SecToTime, @@ -288,28 +273,7 @@ func NewFuncMap() []template.FuncMap { "DefaultTheme": func() string { return setting.UI.DefaultTheme }, - // pass key-value pairs to a partial template which receives them as a dict - "dict": func(values ...interface{}) (map[string]interface{}, error) { - if len(values) == 0 { - return nil, errors.New("invalid dict call") - } - - dict := make(map[string]interface{}) - return util.MergeInto(dict, values...) - }, - /* like dict but merge key-value pairs into the first dict and return it */ - "mergeinto": func(root map[string]interface{}, values ...interface{}) (map[string]interface{}, error) { - if len(values) == 0 { - return nil, errors.New("invalid mergeinto call") - } - - dict := make(map[string]interface{}) - for key, value := range root { - dict[key] = value - } - - return util.MergeInto(dict, values...) - }, + "dict": dict, "percentage": func(n int, values ...int) float32 { sum := 0 for i := 0; i < len(values); i++ { @@ -483,52 +447,13 @@ func NewTextFuncMap() []texttmpl.FuncMap { }, "EllipsisString": base.EllipsisString, "URLJoin": util.URLJoin, - "Dict": func(values ...interface{}) (map[string]interface{}, error) { - if len(values)%2 != 0 { - return nil, errors.New("invalid dict call") - } - dict := make(map[string]interface{}, len(values)/2) - for i := 0; i < len(values); i += 2 { - key, ok := values[i].(string) - if !ok { - return nil, errors.New("dict keys must be strings") - } - dict[key] = values[i+1] - } - return dict, nil - }, - "Printf": fmt.Sprintf, - "Escape": Escape, - "Sec2Time": util.SecToTime, + "Printf": fmt.Sprintf, + "Escape": Escape, + "Sec2Time": util.SecToTime, "ParseDeadline": func(deadline string) []string { return strings.Split(deadline, "|") }, - "dict": func(values ...interface{}) (map[string]interface{}, error) { - if len(values) == 0 { - return nil, errors.New("invalid dict call") - } - - dict := make(map[string]interface{}) - - for i := 0; i < len(values); i++ { - switch key := values[i].(type) { - case string: - i++ - if i == len(values) { - return nil, errors.New("specify the key for non array values") - } - dict[key] = values[i] - case map[string]interface{}: - m := values[i].(map[string]interface{}) - for i, v := range m { - dict[i] = v - } - default: - return nil, errors.New("dict values must be maps") - } - } - return dict, nil - }, + "dict": dict, "percentage": func(n int, values ...int) float32 { sum := 0 for i := 0; i < len(values); i++ { diff --git a/modules/templates/util.go b/modules/templates/util.go new file mode 100644 index 0000000000000..9fd8f42ce4a7e --- /dev/null +++ b/modules/templates/util.go @@ -0,0 +1,47 @@ +// Copyright 2023 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package templates + +import ( + "fmt" + "reflect" +) + +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 case unclear key conflicts. It's always good use explicit keys. +func dict(args ...any) (map[string]any, error) { + if len(args)%2 != 0 { + return nil, fmt.Errorf("invalid dict constructor syntax: must have key-value pairs") + } + m := make(map[string]any, len(args)/2) + for i := 0; i < len(args); i += 2 { + key, ok := args[i].(string) + 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] + } + } + return m, nil +} diff --git a/modules/templates/util_test.go b/modules/templates/util_test.go new file mode 100644 index 0000000000000..dfa691c5e2d6a --- /dev/null +++ b/modules/templates/util_test.go @@ -0,0 +1,43 @@ +// Copyright 2023 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package templates + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestDict(t *testing.T) { + type M map[string]any + cases := []struct { + args []any + want map[string]any + }{ + {[]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}}, + } + + for _, c := range cases { + got, err := dict(c.args...) + if assert.NoError(t, err) { + assert.EqualValues(t, c.want, got) + } + } + + bads := []struct { + args []any + }{ + {[]any{"a", 1, "b"}}, + {[]any{1}}, + {[]any{struct{}{}}}, + } + for _, c := range bads { + _, err := dict(c.args...) + assert.Error(t, err) + } +} diff --git a/modules/util/util.go b/modules/util/util.go index e9ea007ccb8d6..5ff4af28877d9 100644 --- a/modules/util/util.go +++ b/modules/util/util.go @@ -6,7 +6,6 @@ package util import ( "bytes" "crypto/rand" - "errors" "fmt" "math/big" "os" @@ -118,29 +117,6 @@ func NormalizeEOL(input []byte) []byte { return tmp[:pos] } -// MergeInto merges pairs of values into a "dict" -func MergeInto(dict map[string]interface{}, values ...interface{}) (map[string]interface{}, error) { - for i := 0; i < len(values); i++ { - switch key := values[i].(type) { - case string: - i++ - if i == len(values) { - return nil, errors.New("specify the key for non array values") - } - dict[key] = values[i] - case map[string]interface{}: - m := values[i].(map[string]interface{}) - for i, v := range m { - dict[i] = v - } - default: - return nil, errors.New("dict values must be maps") - } - } - - return dict, nil -} - // CryptoRandomInt returns a crypto random integer between 0 and limit, inclusive func CryptoRandomInt(limit int64) (int64, error) { rInt, err := rand.Int(rand.Reader, big.NewInt(limit)) diff --git a/templates/org/team/teams.tmpl b/templates/org/team/teams.tmpl index df0620af49032..27bbe80a3c535 100644 --- a/templates/org/team/teams.tmpl +++ b/templates/org/team/teams.tmpl @@ -32,7 +32,7 @@
{{range .Members}} - {{template "shared/user/avatarlink" Dict "Context" $.Context "user" .}} + {{template "shared/user/avatarlink" dict "Context" $.Context "user" .}} {{end}}
diff --git a/templates/repo/diff/comments.tmpl b/templates/repo/diff/comments.tmpl index c14b145a379b4..5c26e7815ff14 100644 --- a/templates/repo/diff/comments.tmpl +++ b/templates/repo/diff/comments.tmpl @@ -5,7 +5,7 @@ {{if .OriginalAuthor}} {{else}} - {{template "shared/user/avatarlink" Dict "Context" $.root.Context "user" .Poster}} + {{template "shared/user/avatarlink" dict "Context" $.root.Context "user" .Poster}} {{end}}
@@ -42,8 +42,8 @@
{{end}} {{end}} - {{template "repo/issue/view_content/add_reaction" Dict "ctxData" $.root "ActionURL" (Printf "%s/comments/%d/reactions" $.root.RepoLink .ID)}} - {{template "repo/issue/view_content/context_menu" Dict "ctxData" $.root "item" . "delete" true "issue" false "diff" true "IsCommentPoster" (and $.root.IsSigned (eq $.root.SignedUserID .PosterID))}} + {{template "repo/issue/view_content/add_reaction" dict "ctxData" $.root "ActionURL" (Printf "%s/comments/%d/reactions" $.root.RepoLink .ID)}} + {{template "repo/issue/view_content/context_menu" dict "ctxData" $.root "item" . "delete" true "issue" false "diff" true "IsCommentPoster" (and $.root.IsSigned (eq $.root.SignedUserID .PosterID))}}
@@ -60,7 +60,7 @@ {{$reactions := .Reactions.GroupByType}} {{if $reactions}}
- {{template "repo/issue/view_content/reactions" Dict "ctxData" $.root "ActionURL" (Printf "%s/comments/%d/reactions" $.root.RepoLink .ID) "Reactions" $reactions}} + {{template "repo/issue/view_content/reactions" dict "ctxData" $.root "ActionURL" (Printf "%s/comments/%d/reactions" $.root.RepoLink .ID) "Reactions" $reactions}}
{{end}}
diff --git a/templates/repo/diff/section_split.tmpl b/templates/repo/diff/section_split.tmpl index 4fa81f4e620ea..e44c2a0f73569 100644 --- a/templates/repo/diff/section_split.tmpl +++ b/templates/repo/diff/section_split.tmpl @@ -111,22 +111,22 @@ {{if gt (len $line.Comments) 0}} {{if eq $line.GetCommentSide "previous"}} - {{template "repo/diff/conversation" mergeinto $.root "comments" $line.Comments}} + {{template "repo/diff/conversation" dict "." $.root "comments" $line.Comments}} {{end}} {{end}} {{if gt (len $match.Comments) 0}} {{if eq $match.GetCommentSide "previous"}} - {{template "repo/diff/conversation" mergeinto $.root "comments" $match.Comments}} + {{template "repo/diff/conversation" dict "." $.root "comments" $match.Comments}} {{end}} {{end}} {{if eq $line.GetCommentSide "proposed"}} - {{template "repo/diff/conversation" mergeinto $.root "comments" $line.Comments}} + {{template "repo/diff/conversation" dict "." $.root "comments" $line.Comments}} {{end}} {{if gt (len $match.Comments) 0}} {{if eq $match.GetCommentSide "proposed"}} - {{template "repo/diff/conversation" mergeinto $.root "comments" $match.Comments}} + {{template "repo/diff/conversation" dict "." $.root "comments" $match.Comments}} {{end}} {{end}} @@ -137,13 +137,13 @@ {{if gt (len $line.Comments) 0}} {{if eq $line.GetCommentSide "previous"}} - {{template "repo/diff/conversation" mergeinto $.root "comments" $line.Comments}} + {{template "repo/diff/conversation" dict "." $.root "comments" $line.Comments}} {{end}} {{end}} {{if eq $line.GetCommentSide "proposed"}} - {{template "repo/diff/conversation" mergeinto $.root "comments" $line.Comments}} + {{template "repo/diff/conversation" dict "." $.root "comments" $line.Comments}} {{end}} diff --git a/templates/repo/diff/section_unified.tmpl b/templates/repo/diff/section_unified.tmpl index ba4ad5b51393c..cdd2c26f81c13 100644 --- a/templates/repo/diff/section_unified.tmpl +++ b/templates/repo/diff/section_unified.tmpl @@ -57,7 +57,7 @@ {{if gt (len $line.Comments) 0}} - {{template "repo/diff/conversation" mergeinto $.root "comments" $line.Comments}} + {{template "repo/diff/conversation" dict "." $.root "comments" $line.Comments}} {{end}} diff --git a/templates/repo/issue/list.tmpl b/templates/repo/issue/list.tmpl index cfc9a930484f1..004e0d4c55158 100644 --- a/templates/repo/issue/list.tmpl +++ b/templates/repo/issue/list.tmpl @@ -308,7 +308,7 @@ - {{template "shared/issuelist" mergeinto . "listType" "repo"}} + {{template "shared/issuelist" dict "." . "listType" "repo"}} {{template "base/footer" .}} diff --git a/templates/repo/issue/milestone_issues.tmpl b/templates/repo/issue/milestone_issues.tmpl index 2ca2fa2bce70f..8f6deba200bea 100644 --- a/templates/repo/issue/milestone_issues.tmpl +++ b/templates/repo/issue/milestone_issues.tmpl @@ -199,7 +199,7 @@ - {{template "shared/issuelist" mergeinto . "listType" "milestone"}} + {{template "shared/issuelist" dict "." . "listType" "milestone"}} {{template "base/footer" .}} diff --git a/templates/repo/issue/new_form.tmpl b/templates/repo/issue/new_form.tmpl index 006568d9bf262..d00a4813d250f 100644 --- a/templates/repo/issue/new_form.tmpl +++ b/templates/repo/issue/new_form.tmpl @@ -8,7 +8,7 @@
- {{template "shared/user/avatarlink" Dict "Context" $.Context "user" .SignedUser}} + {{template "shared/user/avatarlink" dict "Context" $.Context "user" .SignedUser}}
@@ -20,15 +20,15 @@ {{range .Fields}} {{if eq .Type "input"}} - {{template "repo/issue/fields/input" Dict "Context" $.Context "item" .}} + {{template "repo/issue/fields/input" dict "Context" $.Context "item" .}} {{else if eq .Type "markdown"}} - {{template "repo/issue/fields/markdown" Dict "Context" $.Context "item" .}} + {{template "repo/issue/fields/markdown" dict "Context" $.Context "item" .}} {{else if eq .Type "textarea"}} - {{template "repo/issue/fields/textarea" Dict "Context" $.Context "item" .}} + {{template "repo/issue/fields/textarea" dict "Context" $.Context "item" .}} {{else if eq .Type "dropdown"}} - {{template "repo/issue/fields/dropdown" Dict "Context" $.Context "item" .}} + {{template "repo/issue/fields/dropdown" dict "Context" $.Context "item" .}} {{else if eq .Type "checkboxes"}} - {{template "repo/issue/fields/checkboxes" Dict "Context" $.Context "item" .}} + {{template "repo/issue/fields/checkboxes" dict "Context" $.Context "item" .}} {{end}} {{end}} {{if .IsAttachmentEnabled}} diff --git a/templates/repo/issue/view_content.tmpl b/templates/repo/issue/view_content.tmpl index 2781db432906a..7c94d00fe4116 100644 --- a/templates/repo/issue/view_content.tmpl +++ b/templates/repo/issue/view_content.tmpl @@ -64,8 +64,8 @@ {{end}} {{end}} {{if not $.Repository.IsArchived}} - {{template "repo/issue/view_content/add_reaction" Dict "ctxData" $ "ActionURL" (Printf "%s/issues/%d/reactions" $.RepoLink .Issue.Index)}} - {{template "repo/issue/view_content/context_menu" Dict "ctxData" $ "item" .Issue "delete" false "issue" true "diff" false "IsCommentPoster" $.IsIssuePoster}} + {{template "repo/issue/view_content/add_reaction" dict "ctxData" $ "ActionURL" (Printf "%s/issues/%d/reactions" $.RepoLink .Issue.Index)}} + {{template "repo/issue/view_content/context_menu" dict "ctxData" $ "item" .Issue "delete" false "issue" true "diff" false "IsCommentPoster" $.IsIssuePoster}} {{end}}
@@ -80,13 +80,13 @@
{{.Issue.Content}}
{{if .Issue.Attachments}} - {{template "repo/issue/view_content/attachments" Dict "ctxData" $ "Attachments" .Issue.Attachments "Content" .Issue.RenderedContent}} + {{template "repo/issue/view_content/attachments" dict "ctxData" $ "Attachments" .Issue.Attachments "Content" .Issue.RenderedContent}} {{end}}
{{$reactions := .Issue.Reactions.GroupByType}} {{if $reactions}}
- {{template "repo/issue/view_content/reactions" Dict "ctxData" $ "ActionURL" (Printf "%s/issues/%d/reactions" $.RepoLink .Issue.Index) "Reactions" $reactions}} + {{template "repo/issue/view_content/reactions" dict "ctxData" $ "ActionURL" (Printf "%s/issues/%d/reactions" $.RepoLink .Issue.Index) "Reactions" $reactions}}
{{end}}
diff --git a/templates/repo/issue/view_content/comments.tmpl b/templates/repo/issue/view_content/comments.tmpl index 9f2b7ec2d0cd1..52b453d0dd00c 100644 --- a/templates/repo/issue/view_content/comments.tmpl +++ b/templates/repo/issue/view_content/comments.tmpl @@ -64,8 +64,8 @@
{{end}} {{if not $.Repository.IsArchived}} - {{template "repo/issue/view_content/add_reaction" Dict "ctxData" $ "ActionURL" (Printf "%s/comments/%d/reactions" $.RepoLink .ID)}} - {{template "repo/issue/view_content/context_menu" Dict "ctxData" $ "item" . "delete" true "issue" true "diff" false "IsCommentPoster" (and $.IsSigned (eq $.SignedUserID .PosterID))}} + {{template "repo/issue/view_content/add_reaction" dict "ctxData" $ "ActionURL" (Printf "%s/comments/%d/reactions" $.RepoLink .ID)}} + {{template "repo/issue/view_content/context_menu" dict "ctxData" $ "item" . "delete" true "issue" true "diff" false "IsCommentPoster" (and $.IsSigned (eq $.SignedUserID .PosterID))}} {{end}} @@ -80,13 +80,13 @@
{{.Content}}
{{if .Attachments}} - {{template "repo/issue/view_content/attachments" Dict "ctxData" $ "Attachments" .Attachments "Content" .RenderedContent}} + {{template "repo/issue/view_content/attachments" dict "ctxData" $ "Attachments" .Attachments "Content" .RenderedContent}} {{end}} {{$reactions := .Reactions.GroupByType}} {{if $reactions}}
- {{template "repo/issue/view_content/reactions" Dict "ctxData" $ "ActionURL" (Printf "%s/comments/%d/reactions" $.RepoLink .ID) "Reactions" $reactions}} + {{template "repo/issue/view_content/reactions" dict "ctxData" $ "ActionURL" (Printf "%s/comments/%d/reactions" $.RepoLink .ID) "Reactions" $reactions}}
{{end}} @@ -94,7 +94,7 @@ {{else if eq .Type 1}}
{{svg "octicon-dot-fill"}} - {{template "shared/user/avatarlink" Dict "Context" $.Context "user" .Poster}} + {{template "shared/user/avatarlink" dict "Context" $.Context "user" .Poster}} {{template "shared/user/authorlink" .Poster}} {{if .Issue.IsPull}} @@ -107,7 +107,7 @@ {{else if eq .Type 2}}
{{svg "octicon-circle-slash"}} - {{template "shared/user/avatarlink" Dict "Context" $.Context "user" .Poster}} + {{template "shared/user/avatarlink" dict "Context" $.Context "user" .Poster}} {{template "shared/user/authorlink" .Poster}} {{if .Issue.IsPull}} @@ -120,7 +120,7 @@ {{else if eq .Type 28}}
{{svg "octicon-git-merge"}} - {{template "shared/user/avatarlink" Dict "Context" $.Context "user" .Poster}} + {{template "shared/user/avatarlink" dict "Context" $.Context "user" .Poster}} {{template "shared/user/authorlink" .Poster}} {{$link := printf "%s/commit/%s" $.Repository.Link ($.Issue.PullRequest.MergedCommitID|PathEscape)}} @@ -147,7 +147,7 @@ {{$createdStr:= TimeSinceUnix .CreatedUnix $.locale}}
{{svg "octicon-bookmark"}} - {{template "shared/user/avatarlink" Dict "Context" $.Context "user" .Poster}} + {{template "shared/user/avatarlink" dict "Context" $.Context "user" .Poster}} {{if eq .RefAction 3}}{{end}} {{template "shared/user/authorlink" .Poster}} @@ -162,7 +162,7 @@ {{else if eq .Type 4}}
{{svg "octicon-bookmark"}} - {{template "shared/user/avatarlink" Dict "Context" $.Context "user" .Poster}} + {{template "shared/user/avatarlink" dict "Context" $.Context "user" .Poster}} {{template "shared/user/authorlink" .Poster}} {{$.locale.Tr "repo.issues.commit_ref_at" .EventTag $createdStr | Safe}} @@ -176,7 +176,7 @@ {{if or .AddedLabels .RemovedLabels}}
{{svg "octicon-tag"}} - {{template "shared/user/avatarlink" Dict "Context" $.Context "user" .Poster}} + {{template "shared/user/avatarlink" dict "Context" $.Context "user" .Poster}} {{template "shared/user/authorlink" .Poster}} {{if and .AddedLabels (not .RemovedLabels)}} @@ -192,7 +192,7 @@ {{else if eq .Type 8}}
{{svg "octicon-milestone"}} - {{template "shared/user/avatarlink" Dict "Context" $.Context "user" .Poster}} + {{template "shared/user/avatarlink" dict "Context" $.Context "user" .Poster}} {{template "shared/user/authorlink" .Poster}} {{if gt .OldMilestoneID 0}}{{if gt .MilestoneID 0}}{{$.locale.Tr "repo.issues.change_milestone_at" (.OldMilestone.Name|Escape) (.Milestone.Name|Escape) $createdStr | Safe}}{{else}}{{$.locale.Tr "repo.issues.remove_milestone_at" (.OldMilestone.Name|Escape) $createdStr | Safe}}{{end}}{{else if gt .MilestoneID 0}}{{$.locale.Tr "repo.issues.add_milestone_at" (.Milestone.Name|Escape) $createdStr | Safe}}{{end}} @@ -203,7 +203,7 @@ {{svg "octicon-person"}} {{if gt .AssigneeID 0}} {{if .RemovedAssignee}} - {{template "shared/user/avatarlink" Dict "Context" $.Context "user" .Assignee}} + {{template "shared/user/avatarlink" dict "Context" $.Context "user" .Assignee}} {{template "shared/user/authorlink" .Assignee}} {{if eq .Poster.ID .Assignee.ID}} @@ -213,7 +213,7 @@ {{end}} {{else}} - {{template "shared/user/avatarlink" Dict "Context" $.Context "user" .Assignee}} + {{template "shared/user/avatarlink" dict "Context" $.Context "user" .Assignee}} {{template "shared/user/authorlink" .Assignee}} {{if eq .Poster.ID .AssigneeID}} @@ -228,7 +228,7 @@ {{else if eq .Type 10}}
{{svg "octicon-pencil"}} - {{template "shared/user/avatarlink" Dict "Context" $.Context "user" .Poster}} + {{template "shared/user/avatarlink" dict "Context" $.Context "user" .Poster}} {{template "shared/user/authorlink" .Poster}} {{$.locale.Tr "repo.issues.change_title_at" (.OldTitle|RenderEmoji $.Context) (.NewTitle|RenderEmoji $.Context) $createdStr | Safe}} @@ -237,7 +237,7 @@ {{else if eq .Type 11}}
{{svg "octicon-git-branch"}} - {{template "shared/user/avatarlink" Dict "Context" $.Context "user" .Poster}} + {{template "shared/user/avatarlink" dict "Context" $.Context "user" .Poster}} {{template "shared/user/authorlink" .Poster}} {{$.locale.Tr "repo.issues.delete_branch_at" (.OldRef|Escape) $createdStr | Safe}} @@ -246,7 +246,7 @@ {{else if eq .Type 12}}
{{svg "octicon-clock"}} - {{template "shared/user/avatarlink" Dict "Context" $.Context "user" .Poster}} + {{template "shared/user/avatarlink" dict "Context" $.Context "user" .Poster}} {{template "shared/user/authorlink" .Poster}} {{$.locale.Tr "repo.issues.start_tracking_history" $createdStr | Safe}} @@ -255,12 +255,12 @@ {{else if eq .Type 13}}
{{svg "octicon-clock"}} - {{template "shared/user/avatarlink" Dict "Context" $.Context "user" .Poster}} + {{template "shared/user/avatarlink" dict "Context" $.Context "user" .Poster}} {{template "shared/user/authorlink" .Poster}} {{$.locale.Tr "repo.issues.stop_tracking_history" $createdStr | Safe}} - {{template "repo/issue/view_content/comments_delete_time" Dict "ctxData" $ "comment" .}} + {{template "repo/issue/view_content/comments_delete_time" dict "ctxData" $ "comment" .}}
{{svg "octicon-clock"}} {{.Content}} @@ -269,12 +269,12 @@ {{else if eq .Type 14}}
{{svg "octicon-clock"}} - {{template "shared/user/avatarlink" Dict "Context" $.Context "user" .Poster}} + {{template "shared/user/avatarlink" dict "Context" $.Context "user" .Poster}} {{template "shared/user/authorlink" .Poster}} {{$.locale.Tr "repo.issues.add_time_history" $createdStr | Safe}} - {{template "repo/issue/view_content/comments_delete_time" Dict "ctxData" $ "comment" .}} + {{template "repo/issue/view_content/comments_delete_time" dict "ctxData" $ "comment" .}}
{{svg "octicon-clock"}} {{.Content}} @@ -283,7 +283,7 @@ {{else if eq .Type 15}}
{{svg "octicon-clock"}} - {{template "shared/user/avatarlink" Dict "Context" $.Context "user" .Poster}} + {{template "shared/user/avatarlink" dict "Context" $.Context "user" .Poster}} {{template "shared/user/authorlink" .Poster}} {{$.locale.Tr "repo.issues.cancel_tracking_history" $createdStr | Safe}} @@ -292,7 +292,7 @@ {{else if eq .Type 16}}
{{svg "octicon-clock"}} - {{template "shared/user/avatarlink" Dict "Context" $.Context "user" .Poster}} + {{template "shared/user/avatarlink" dict "Context" $.Context "user" .Poster}} {{template "shared/user/authorlink" .Poster}} {{$.locale.Tr "repo.issues.due_date_added" .Content $createdStr | Safe}} @@ -301,7 +301,7 @@ {{else if eq .Type 17}}
{{svg "octicon-clock"}} - {{template "shared/user/avatarlink" Dict "Context" $.Context "user" .Poster}} + {{template "shared/user/avatarlink" dict "Context" $.Context "user" .Poster}} {{template "shared/user/authorlink" .Poster}} {{$parsedDeadline := .Content | ParseDeadline}} @@ -311,7 +311,7 @@ {{else if eq .Type 18}}
{{svg "octicon-clock"}} - {{template "shared/user/avatarlink" Dict "Context" $.Context "user" .Poster}} + {{template "shared/user/avatarlink" dict "Context" $.Context "user" .Poster}} {{template "shared/user/authorlink" .Poster}} {{$.locale.Tr "repo.issues.due_date_remove" .Content $createdStr | Safe}} @@ -320,7 +320,7 @@ {{else if eq .Type 19}}
{{svg "octicon-package-dependents"}} - {{template "shared/user/avatarlink" Dict "Context" $.Context "user" .Poster}} + {{template "shared/user/avatarlink" dict "Context" $.Context "user" .Poster}} {{template "shared/user/authorlink" .Poster}} {{$.locale.Tr "repo.issues.dependency.added_dependency" $createdStr | Safe}} @@ -343,7 +343,7 @@ {{else if eq .Type 20}}
{{svg "octicon-package-dependents"}} - {{template "shared/user/avatarlink" Dict "Context" $.Context "user" .Poster}} + {{template "shared/user/avatarlink" dict "Context" $.Context "user" .Poster}} {{template "shared/user/authorlink" .Poster}} {{$.locale.Tr "repo.issues.dependency.removed_dependency" $createdStr | Safe}} @@ -436,8 +436,8 @@
{{end}} {{if not $.Repository.IsArchived}} - {{template "repo/issue/view_content/add_reaction" Dict "ctxData" $ "ActionURL" (Printf "%s/comments/%d/reactions" $.RepoLink .ID)}} - {{template "repo/issue/view_content/context_menu" Dict "ctxData" $ "item" . "delete" false "issue" true "diff" false "IsCommentPoster" (and $.IsSigned (eq $.SignedUserID .PosterID))}} + {{template "repo/issue/view_content/add_reaction" dict "ctxData" $ "ActionURL" (Printf "%s/comments/%d/reactions" $.RepoLink .ID)}} + {{template "repo/issue/view_content/context_menu" dict "ctxData" $ "item" . "delete" false "issue" true "diff" false "IsCommentPoster" (and $.IsSigned (eq $.SignedUserID .PosterID))}} {{end}}
@@ -452,13 +452,13 @@
{{.Content}}
{{if .Attachments}} - {{template "repo/issue/view_content/attachments" Dict "ctxData" $ "Attachments" .Attachments "Content" .RenderedContent}} + {{template "repo/issue/view_content/attachments" dict "ctxData" $ "Attachments" .Attachments "Content" .RenderedContent}} {{end}}
{{$reactions := .Reactions.GroupByType}} {{if $reactions}}
- {{template "repo/issue/view_content/reactions" Dict "ctxData" $ "ActionURL" (Printf "%s/comments/%d/reactions" $.RepoLink .ID) "Reactions" $reactions}} + {{template "repo/issue/view_content/reactions" dict "ctxData" $ "ActionURL" (Printf "%s/comments/%d/reactions" $.RepoLink .ID) "Reactions" $reactions}}
{{end}}
@@ -563,8 +563,8 @@
{{end}} {{if not $.Repository.IsArchived}} - {{template "repo/issue/view_content/add_reaction" Dict "ctxData" $ "ActionURL" (Printf "%s/comments/%d/reactions" $.RepoLink .ID)}} - {{template "repo/issue/view_content/context_menu" Dict "ctxData" $ "item" . "delete" true "issue" true "diff" true "IsCommentPoster" (and $.IsSigned (eq $.SignedUserID .PosterID))}} + {{template "repo/issue/view_content/add_reaction" dict "ctxData" $ "ActionURL" (Printf "%s/comments/%d/reactions" $.RepoLink .ID)}} + {{template "repo/issue/view_content/context_menu" dict "ctxData" $ "item" . "delete" true "issue" true "diff" true "IsCommentPoster" (and $.IsSigned (eq $.SignedUserID .PosterID))}} {{end}}
@@ -582,7 +582,7 @@ {{$reactions := .Reactions.GroupByType}} {{if $reactions}}
- {{template "repo/issue/view_content/reactions" Dict "ctxData" $ "ActionURL" (Printf "%s/comments/%d/reactions" $.RepoLink .ID) "Reactions" $reactions}} + {{template "repo/issue/view_content/reactions" dict "ctxData" $ "ActionURL" (Printf "%s/comments/%d/reactions" $.RepoLink .ID) "Reactions" $reactions}}
{{end}}
@@ -626,7 +626,7 @@ {{else if eq .Type 23}}
{{svg "octicon-lock"}} - {{template "shared/user/avatarlink" Dict "Context" $.Context "user" .Poster}} + {{template "shared/user/avatarlink" dict "Context" $.Context "user" .Poster}} {{if .Content}} {{template "shared/user/authorlink" .Poster}} @@ -642,7 +642,7 @@ {{else if eq .Type 24}}
{{svg "octicon-key"}} - {{template "shared/user/avatarlink" Dict "Context" $.Context "user" .Poster}} + {{template "shared/user/avatarlink" dict "Context" $.Context "user" .Poster}} {{template "shared/user/authorlink" .Poster}} {{$.locale.Tr "repo.issues.unlock_comment" $createdStr | Safe}} @@ -651,7 +651,7 @@ {{else if eq .Type 25}}
{{svg "octicon-git-branch"}} - {{template "shared/user/avatarlink" Dict "Context" $.Context "user" .Poster}} + {{template "shared/user/avatarlink" dict "Context" $.Context "user" .Poster}} {{.Poster.Name}} {{$.locale.Tr "repo.pulls.change_target_branch_at" (.OldRef|Escape) (.NewRef|Escape) $createdStr | Safe}} @@ -660,7 +660,7 @@ {{else if eq .Type 26}}
{{svg "octicon-clock"}} - {{template "shared/user/avatarlink" Dict "Context" $.Context "user" .Poster}} + {{template "shared/user/avatarlink" dict "Context" $.Context "user" .Poster}} {{template "shared/user/authorlink" .Poster}} @@ -674,7 +674,7 @@ {{else if eq .Type 27}}
{{svg "octicon-eye"}} - {{template "shared/user/avatarlink" Dict "Context" $.Context "user" .Poster}} + {{template "shared/user/avatarlink" dict "Context" $.Context "user" .Poster}} {{template "shared/user/authorlink" .Poster}} {{if (gt .AssigneeID 0)}} @@ -724,7 +724,7 @@ {{if not $.UnitProjectsGlobalDisabled}}
{{svg "octicon-project"}} - {{template "shared/user/avatarlink" Dict "Context" $.Context "user" .Poster}} + {{template "shared/user/avatarlink" dict "Context" $.Context "user" .Poster}} {{template "shared/user/authorlink" .Poster}} {{if gt .OldProjectID 0}} @@ -781,7 +781,7 @@ {{else if eq .Type 33}}
{{svg "octicon-git-branch"}} - {{template "shared/user/avatarlink" Dict "Context" $.Context "user" .Poster}} + {{template "shared/user/avatarlink" dict "Context" $.Context "user" .Poster}} {{template "shared/user/authorlink" .Poster}} {{if and .OldRef .NewRef}} diff --git a/templates/repo/issue/view_content/reactions.tmpl b/templates/repo/issue/view_content/reactions.tmpl index b36fd4272fc5c..3c957d91c7318 100644 --- a/templates/repo/issue/view_content/reactions.tmpl +++ b/templates/repo/issue/view_content/reactions.tmpl @@ -5,5 +5,5 @@ {{end}} {{if AllowedReactions}} - {{template "repo/issue/view_content/add_reaction" Dict "ctxData" $.ctxData "ActionURL" .ActionURL}} + {{template "repo/issue/view_content/add_reaction" dict "ctxData" $.ctxData "ActionURL" .ActionURL}} {{end}} diff --git a/templates/user/dashboard/issues.tmpl b/templates/user/dashboard/issues.tmpl index 88ab1f9b7371f..6b9bfe4ebad45 100644 --- a/templates/user/dashboard/issues.tmpl +++ b/templates/user/dashboard/issues.tmpl @@ -112,7 +112,7 @@ {{end}}
- {{template "shared/issuelist" mergeinto . "listType" "dashboard"}} + {{template "shared/issuelist" dict "." . "listType" "dashboard"}}
diff --git a/templates/user/notification/notification_subscriptions.tmpl b/templates/user/notification/notification_subscriptions.tmpl index 5aff7ef9d6d71..26a33bf9baed6 100644 --- a/templates/user/notification/notification_subscriptions.tmpl +++ b/templates/user/notification/notification_subscriptions.tmpl @@ -66,7 +66,7 @@
{{.locale.Tr "notification.no_subscriptions"}} {{else}} - {{template "shared/issuelist" mergeinto . "listType" "dashboard"}} + {{template "shared/issuelist" dict "." . "listType" "dashboard"}} {{end}} {{else}} {{template "explore/repo_search" .}} From e64f3438944d816015da36b26d706c710bfdabf2 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Thu, 6 Apr 2023 00:18:56 +0800 Subject: [PATCH 2/3] Update modules/templates/util.go Co-authored-by: techknowlogick --- modules/templates/util.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/templates/util.go b/modules/templates/util.go index 9fd8f42ce4a7e..13f3a56808a2e 100644 --- a/modules/templates/util.go +++ b/modules/templates/util.go @@ -24,7 +24,7 @@ func dictMerge(base map[string]any, arg any) bool { // 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 case unclear key conflicts. It's always good use explicit keys. +// 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, fmt.Errorf("invalid dict constructor syntax: must have key-value pairs") From bd37277fb9580673b470a14df2012a1f080e9671 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Fri, 7 Apr 2023 21:45:22 +0800 Subject: [PATCH 3/3] fix merge --- modules/templates/helper.go | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/modules/templates/helper.go b/modules/templates/helper.go index 8d893c0263c07..407b2c64f5353 100644 --- a/modules/templates/helper.go +++ b/modules/templates/helper.go @@ -227,14 +227,7 @@ func NewFuncMap() []template.FuncMap { "DefaultTheme": func() string { return setting.UI.DefaultTheme }, - "dict": dict, - "percentage": func(n int, values ...int) float32 { - sum := 0 - for i := 0; i < len(values); i++ { - sum += values[i] - } - return float32(n) * 100 / float32(sum) - }, + "dict": dict, "CommentMustAsDiff": gitdiff.CommentMustAsDiff, "MirrorRemoteAddress": mirrorRemoteAddress, "NotificationSettings": func() map[string]interface{} { @@ -383,8 +376,7 @@ func NewTextFuncMap() []texttmpl.FuncMap { "ParseDeadline": func(deadline string) []string { return strings.Split(deadline, "|") }, - "dict": dict, - }, + "dict": dict, "QueryEscape": url.QueryEscape, "Eval": Eval, }}