Remove AddParamIfExist(AddParam) (#29841)

Follow #29834

Remove AddParamIfExist, use "AddParamString" instead, it should clearly
know what is being added into the parameters.
This commit is contained in:
wxiaoguang 2024-03-16 20:07:56 +08:00 committed by GitHub
parent 66902d89e5
commit a889381664
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
17 changed files with 55 additions and 58 deletions

View File

@ -4,6 +4,7 @@
package admin package admin
import ( import (
"fmt"
"net/http" "net/http"
"net/url" "net/url"
"strings" "strings"
@ -84,7 +85,7 @@ func UnadoptedRepos(ctx *context.Context) {
if !doSearch { if !doSearch {
pager := context.NewPagination(0, opts.PageSize, opts.Page, 5) pager := context.NewPagination(0, opts.PageSize, opts.Page, 5)
pager.SetDefaultParams(ctx) pager.SetDefaultParams(ctx)
pager.AddParamIfExist("search", ctx.Data["search"]) pager.AddParamString("search", fmt.Sprint(doSearch))
ctx.Data["Page"] = pager ctx.Data["Page"] = pager
ctx.HTML(http.StatusOK, tplUnadoptedRepos) ctx.HTML(http.StatusOK, tplUnadoptedRepos)
return return
@ -98,7 +99,7 @@ func UnadoptedRepos(ctx *context.Context) {
ctx.Data["Dirs"] = repoNames ctx.Data["Dirs"] = repoNames
pager := context.NewPagination(count, opts.PageSize, opts.Page, 5) pager := context.NewPagination(count, opts.PageSize, opts.Page, 5)
pager.SetDefaultParams(ctx) pager.SetDefaultParams(ctx)
pager.AddParamIfExist("search", ctx.Data["search"]) pager.AddParamString("search", fmt.Sprint(doSearch))
ctx.Data["Page"] = pager ctx.Data["Page"] = pager
ctx.HTML(http.StatusOK, tplUnadoptedRepos) ctx.HTML(http.StatusOK, tplUnadoptedRepos)
} }

View File

@ -137,7 +137,7 @@ func Code(ctx *context.Context) {
pager := context.NewPagination(total, setting.UI.RepoSearchPagingNum, page, 5) pager := context.NewPagination(total, setting.UI.RepoSearchPagingNum, page, 5)
pager.SetDefaultParams(ctx) pager.SetDefaultParams(ctx)
pager.AddParamIfExist("l", ctx.Data["Language"]) pager.AddParamString("l", language)
ctx.Data["Page"] = pager ctx.Data["Page"] = pager
ctx.HTML(http.StatusOK, tplExploreCode) ctx.HTML(http.StatusOK, tplExploreCode)

View File

@ -169,8 +169,8 @@ func RenderRepoSearch(ctx *context.Context, opts *RepoSearchOptions) {
pager := context.NewPagination(int(count), opts.PageSize, page, 5) pager := context.NewPagination(int(count), opts.PageSize, page, 5)
pager.SetDefaultParams(ctx) pager.SetDefaultParams(ctx)
pager.AddParamIfExist("topic", ctx.Data["TopicOnly"]) pager.AddParamString("topic", fmt.Sprint(topicOnly))
pager.AddParamIfExist("language", ctx.Data["Language"]) pager.AddParamString("language", language)
pager.AddParamString(relevantReposOnlyParam, fmt.Sprint(opts.OnlyShowRelevant)) pager.AddParamString(relevantReposOnlyParam, fmt.Sprint(opts.OnlyShowRelevant))
ctx.Data["Page"] = pager ctx.Data["Page"] = pager

View File

@ -154,7 +154,7 @@ func Home(ctx *context.Context) {
pager := context.NewPagination(int(count), setting.UI.User.RepoPagingNum, page, 5) pager := context.NewPagination(int(count), setting.UI.User.RepoPagingNum, page, 5)
pager.SetDefaultParams(ctx) pager.SetDefaultParams(ctx)
pager.AddParamIfExist("language", ctx.Data["Language"]) pager.AddParamString("language", language)
ctx.Data["Page"] = pager ctx.Data["Page"] = pager
ctx.Data["ShowMemberAndTeamTab"] = ctx.Org.IsMember || len(members) > 0 ctx.Data["ShowMemberAndTeamTab"] = ctx.Org.IsMember || len(members) > 0

View File

@ -120,7 +120,7 @@ func Projects(ctx *context.Context) {
} }
pager := context.NewPagination(int(total), setting.UI.IssuePagingNum, page, numPages) pager := context.NewPagination(int(total), setting.UI.IssuePagingNum, page, numPages)
pager.AddParamIfExist("state", ctx.Data["State"]) pager.AddParamString("state", fmt.Sprint(ctx.Data["State"]))
ctx.Data["Page"] = pager ctx.Data["Page"] = pager
ctx.Data["CanWriteProjects"] = canWriteProjects(ctx) ctx.Data["CanWriteProjects"] = canWriteProjects(ctx)

View File

@ -163,8 +163,8 @@ func Graph(ctx *context.Context) {
ctx.Data["CommitCount"] = commitsCount ctx.Data["CommitCount"] = commitsCount
paginator := context.NewPagination(int(graphCommitsCount), setting.UI.GraphMaxCommitNum, page, 5) paginator := context.NewPagination(int(graphCommitsCount), setting.UI.GraphMaxCommitNum, page, 5)
paginator.AddParamIfExist("mode", ctx.Data["Mode"]) paginator.AddParamString("mode", mode)
paginator.AddParamIfExist("hide-pr-refs", ctx.Data["HidePRRefs"]) paginator.AddParamString("hide-pr-refs", fmt.Sprint(hidePRRefs))
for _, branch := range branches { for _, branch := range branches {
paginator.AddParamString("branch", branch) paginator.AddParamString("branch", branch)
} }

View File

@ -472,16 +472,16 @@ func issues(ctx *context.Context, milestoneID, projectID int64, isPullOption opt
} }
ctx.Data["ShowArchivedLabels"] = archived ctx.Data["ShowArchivedLabels"] = archived
pager.AddParamIfExist("q", ctx.Data["Keyword"]) pager.AddParamString("q", keyword)
pager.AddParamIfExist("type", ctx.Data["ViewType"]) pager.AddParamString("type", viewType)
pager.AddParamIfExist("sort", ctx.Data["SortType"]) pager.AddParamString("sort", sortType)
pager.AddParamIfExist("state", ctx.Data["State"]) pager.AddParamString("state", fmt.Sprint(ctx.Data["State"]))
pager.AddParamIfExist("labels", ctx.Data["SelectLabels"]) pager.AddParamString("labels", fmt.Sprint(selectLabels))
pager.AddParamIfExist("milestone", ctx.Data["MilestoneID"]) pager.AddParamString("milestone", fmt.Sprint(milestoneID))
pager.AddParamIfExist("project", ctx.Data["ProjectID"]) pager.AddParamString("project", fmt.Sprint(projectID))
pager.AddParamIfExist("assignee", ctx.Data["AssigneeID"]) pager.AddParamString("assignee", fmt.Sprint(assigneeID))
pager.AddParamIfExist("poster", ctx.Data["PosterID"]) pager.AddParamString("poster", fmt.Sprint(posterID))
pager.AddParamIfExist("archived", ctx.Data["ShowArchivedLabels"]) pager.AddParamString("archived", fmt.Sprint(archived))
ctx.Data["Page"] = pager ctx.Data["Page"] = pager
} }

View File

@ -106,8 +106,8 @@ func Milestones(ctx *context.Context) {
ctx.Data["IsShowClosed"] = isShowClosed ctx.Data["IsShowClosed"] = isShowClosed
pager := context.NewPagination(int(total), setting.UI.IssuePagingNum, page, 5) pager := context.NewPagination(int(total), setting.UI.IssuePagingNum, page, 5)
pager.AddParamIfExist("state", ctx.Data["State"]) pager.AddParamString("state", fmt.Sprint(ctx.Data["State"]))
pager.AddParamIfExist("q", ctx.Data["Keyword"]) pager.AddParamString("q", keyword)
ctx.Data["Page"] = pager ctx.Data["Page"] = pager
ctx.HTML(http.StatusOK, tplMilestone) ctx.HTML(http.StatusOK, tplMilestone)

View File

@ -70,8 +70,8 @@ func Packages(ctx *context.Context) {
ctx.Data["RepositoryAccessMap"] = map[int64]bool{ctx.Repo.Repository.ID: true} // There is only the current repository ctx.Data["RepositoryAccessMap"] = map[int64]bool{ctx.Repo.Repository.ID: true} // There is only the current repository
pager := context.NewPagination(int(total), setting.UI.PackagesPagingNum, page, 5) pager := context.NewPagination(int(total), setting.UI.PackagesPagingNum, page, 5)
pager.AddParamIfExist("q", ctx.Data["Query"]) pager.AddParamString("q", query)
pager.AddParamIfExist("type", ctx.Data["PackageType"]) pager.AddParamString("type", packageType)
ctx.Data["Page"] = pager ctx.Data["Page"] = pager
ctx.HTML(http.StatusOK, tplPackagesList) ctx.HTML(http.StatusOK, tplPackagesList)

View File

@ -118,7 +118,7 @@ func Projects(ctx *context.Context) {
} }
pager := context.NewPagination(total, setting.UI.IssuePagingNum, page, numPages) pager := context.NewPagination(total, setting.UI.IssuePagingNum, page, numPages)
pager.AddParamIfExist("state", ctx.Data["State"]) pager.AddParamString("state", fmt.Sprint(ctx.Data["State"]))
ctx.Data["Page"] = pager ctx.Data["Page"] = pager
ctx.Data["CanWriteProjects"] = ctx.Repo.Permission.CanWrite(unit.TypeProjects) ctx.Data["CanWriteProjects"] = ctx.Repo.Permission.CanWrite(unit.TypeProjects)

View File

@ -68,7 +68,7 @@ func Search(ctx *context.Context) {
pager := context.NewPagination(total, setting.UI.RepoSearchPagingNum, page, 5) pager := context.NewPagination(total, setting.UI.RepoSearchPagingNum, page, 5)
pager.SetDefaultParams(ctx) pager.SetDefaultParams(ctx)
pager.AddParamIfExist("l", ctx.Data["Language"]) pager.AddParamString("l", language)
ctx.Data["Page"] = pager ctx.Data["Page"] = pager
ctx.HTML(http.StatusOK, tplSearch) ctx.HTML(http.StatusOK, tplSearch)

View File

@ -122,7 +122,7 @@ func CodeSearch(ctx *context.Context) {
pager := context.NewPagination(total, setting.UI.RepoSearchPagingNum, page, 5) pager := context.NewPagination(total, setting.UI.RepoSearchPagingNum, page, 5)
pager.SetDefaultParams(ctx) pager.SetDefaultParams(ctx)
pager.AddParamIfExist("l", ctx.Data["Language"]) pager.AddParamString("l", language)
ctx.Data["Page"] = pager ctx.Data["Page"] = pager
ctx.HTML(http.StatusOK, tplUserCode) ctx.HTML(http.StatusOK, tplUserCode)

View File

@ -133,7 +133,7 @@ func Dashboard(ctx *context.Context) {
ctx.Data["Feeds"] = feeds ctx.Data["Feeds"] = feeds
pager := context.NewPagination(int(count), setting.UI.FeedPagingNum, page, 5) pager := context.NewPagination(int(count), setting.UI.FeedPagingNum, page, 5)
pager.AddParamIfExist("date", ctx.Data["Date"]) pager.AddParamString("date", date)
ctx.Data["Page"] = pager ctx.Data["Page"] = pager
ctx.HTML(http.StatusOK, tplDashboard) ctx.HTML(http.StatusOK, tplDashboard)
@ -329,10 +329,10 @@ func Milestones(ctx *context.Context) {
ctx.Data["IsShowClosed"] = isShowClosed ctx.Data["IsShowClosed"] = isShowClosed
pager := context.NewPagination(pagerCount, setting.UI.IssuePagingNum, page, 5) pager := context.NewPagination(pagerCount, setting.UI.IssuePagingNum, page, 5)
pager.AddParamIfExist("q", ctx.Data["Keyword"]) pager.AddParamString("q", keyword)
pager.AddParamIfExist("repos", ctx.Data["RepoIDs"]) pager.AddParamString("repos", reposQuery)
pager.AddParamIfExist("sort", ctx.Data["SortType"]) pager.AddParamString("sort", sortType)
pager.AddParamIfExist("state", ctx.Data["State"]) pager.AddParamString("state", fmt.Sprint(ctx.Data["State"]))
ctx.Data["Page"] = pager ctx.Data["Page"] = pager
ctx.HTML(http.StatusOK, tplMilestones) ctx.HTML(http.StatusOK, tplMilestones)
@ -632,13 +632,11 @@ func buildIssueOverview(ctx *context.Context, unitType unit.Type) {
} }
pager := context.NewPagination(shownIssues, setting.UI.IssuePagingNum, page, 5) pager := context.NewPagination(shownIssues, setting.UI.IssuePagingNum, page, 5)
pager.AddParamIfExist("q", ctx.Data["Keyword"]) pager.AddParamString("q", keyword)
pager.AddParamIfExist("type", ctx.Data["ViewType"]) pager.AddParamString("type", viewType)
pager.AddParamIfExist("sort", ctx.Data["SortType"]) pager.AddParamString("sort", sortType)
pager.AddParamIfExist("state", ctx.Data["State"]) pager.AddParamString("state", fmt.Sprint(ctx.Data["State"]))
pager.AddParamIfExist("labels", ctx.Data["SelectLabels"]) pager.AddParamString("labels", selectedLabels)
pager.AddParamIfExist("milestone", ctx.Data["MilestoneID"])
pager.AddParamIfExist("assignee", ctx.Data["AssigneeID"])
ctx.Data["Page"] = pager ctx.Data["Page"] = pager
ctx.HTML(http.StatusOK, tplIssues) ctx.HTML(http.StatusOK, tplIssues)

View File

@ -344,8 +344,8 @@ func NotificationSubscriptions(ctx *context.Context) {
ctx.Redirect(fmt.Sprintf("/notifications/subscriptions?page=%d", pager.Paginater.Current())) ctx.Redirect(fmt.Sprintf("/notifications/subscriptions?page=%d", pager.Paginater.Current()))
return return
} }
pager.AddParamIfExist("sort", ctx.Data["SortType"]) pager.AddParamString("sort", sortType)
pager.AddParamIfExist("state", ctx.Data["State"]) pager.AddParamString("state", state)
ctx.Data["Page"] = pager ctx.Data["Page"] = pager
ctx.HTML(http.StatusOK, tplNotificationSubscriptions) ctx.HTML(http.StatusOK, tplNotificationSubscriptions)

View File

@ -125,8 +125,8 @@ func ListPackages(ctx *context.Context) {
} }
pager := context.NewPagination(int(total), setting.UI.PackagesPagingNum, page, 5) pager := context.NewPagination(int(total), setting.UI.PackagesPagingNum, page, 5)
pager.AddParamIfExist("q", ctx.Data["Query"]) pager.AddParamString("q", query)
pager.AddParamIfExist("type", ctx.Data["PackageType"]) pager.AddParamString("type", packageType)
ctx.Data["Page"] = pager ctx.Data["Page"] = pager
ctx.HTML(http.StatusOK, tplPackagesList) ctx.HTML(http.StatusOK, tplPackagesList)

View File

@ -324,12 +324,14 @@ func prepareUserProfileTabData(ctx *context.Context, showPrivate bool, profileDb
pager := context.NewPagination(total, pagingNum, page, 5) pager := context.NewPagination(total, pagingNum, page, 5)
pager.SetDefaultParams(ctx) pager.SetDefaultParams(ctx)
pager.AddParamIfExist("tab", ctx.Data["TabName"]) pager.AddParamString("tab", tab)
if tab != "followers" && tab != "following" && tab != "activity" && tab != "projects" { if tab != "followers" && tab != "following" && tab != "activity" && tab != "projects" {
pager.AddParamIfExist("language", ctx.Data["Language"]) pager.AddParamString("language", language)
} }
if tab == "activity" { if tab == "activity" {
pager.AddParamIfExist("date", ctx.Data["Date"]) if ctx.Data["Date"] != nil {
pager.AddParamString("date", fmt.Sprint(ctx.Data["Date"]))
}
} }
ctx.Data["Page"] = pager ctx.Data["Page"] = pager
} }

View File

@ -26,16 +26,6 @@ func NewPagination(total, pagingNum, current, numPages int) *Pagination {
return p return p
} }
// AddParamIfExist adds a value to the query parameters if the value is not nil
func (p *Pagination) AddParamIfExist(key string, val any) {
if val == nil {
return
}
paramData := fmt.Sprint(val)
urlParam := fmt.Sprintf("%s=%v", url.QueryEscape(key), url.QueryEscape(paramData))
p.urlParams = append(p.urlParams, urlParam)
}
// AddParamString adds a string parameter directly // AddParamString adds a string parameter directly
func (p *Pagination) AddParamString(key, value string) { func (p *Pagination) AddParamString(key, value string) {
urlParam := fmt.Sprintf("%s=%v", url.QueryEscape(key), url.QueryEscape(value)) urlParam := fmt.Sprintf("%s=%v", url.QueryEscape(key), url.QueryEscape(value))
@ -49,8 +39,14 @@ func (p *Pagination) GetParams() template.URL {
// SetDefaultParams sets common pagination params that are often used // SetDefaultParams sets common pagination params that are often used
func (p *Pagination) SetDefaultParams(ctx *Context) { func (p *Pagination) SetDefaultParams(ctx *Context) {
p.AddParamIfExist("sort", ctx.Data["SortType"]) if v, ok := ctx.Data["SortType"].(string); ok {
p.AddParamIfExist("q", ctx.Data["Keyword"]) p.AddParamString("sort", v)
}
if v, ok := ctx.Data["Keyword"].(string); ok {
p.AddParamString("q", v)
}
if v, ok := ctx.Data["IsFuzzy"].(bool); ok {
p.AddParamString("fuzzy", fmt.Sprint(v))
}
// do not add any more uncommon params here! // do not add any more uncommon params here!
p.AddParamIfExist("fuzzy", ctx.Data["IsFuzzy"])
} }