diff options
author | Jason Song <i@wolfogre.com> | 2023-01-11 13:31:16 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-01-11 13:31:16 +0800 |
commit | 477a1cc40ebd3ecb116c632b0717bba748e914d2 (patch) | |
tree | f8834d481acbf410d53828d21f5aa149c21a44bd /routers | |
parent | dc5f2cf5906ec2f87ad47ea4724cc245c401eef6 (diff) | |
download | gitea-477a1cc40ebd3ecb116c632b0717bba748e914d2.tar.gz gitea-477a1cc40ebd3ecb116c632b0717bba748e914d2.zip |
Improve utils of slices (#22379)
- Move the file `compare.go` and `slice.go` to `slice.go`.
- Fix `ExistsInSlice`, it's buggy
- It uses `sort.Search`, so it assumes that the input slice is sorted.
- It passes `func(i int) bool { return slice[i] == target })` to
`sort.Search`, that's incorrect, check the doc of `sort.Search`.
- Conbine `IsInt64InSlice(int64, []int64)` and `ExistsInSlice(string,
[]string)` to `SliceContains[T]([]T, T)`.
- Conbine `IsSliceInt64Eq([]int64, []int64)` and `IsEqualSlice([]string,
[]string)` to `SliceSortedEqual[T]([]T, T)`.
- Add `SliceEqual[T]([]T, T)` as a distinction from
`SliceSortedEqual[T]([]T, T)`.
- Redesign `RemoveIDFromList([]int64, int64) ([]int64, bool)` to
`SliceRemoveAll[T]([]T, T) []T`.
- Add `SliceContainsFunc[T]([]T, func(T) bool)` and
`SliceRemoveAllFunc[T]([]T, func(T) bool)` for general use.
- Add comments to explain why not `golang.org/x/exp/slices`.
- Add unit tests.
Diffstat (limited to 'routers')
-rw-r--r-- | routers/api/v1/utils/hook.go | 34 | ||||
-rw-r--r-- | routers/web/repo/issue.go | 4 | ||||
-rw-r--r-- | routers/web/repo/webhook.go | 2 | ||||
-rw-r--r-- | routers/web/user/notification.go | 2 | ||||
-rw-r--r-- | routers/web/user/setting/profile.go | 2 |
5 files changed, 22 insertions, 22 deletions
diff --git a/routers/api/v1/utils/hook.go b/routers/api/v1/utils/hook.go index 065b761adb..44fba22b5a 100644 --- a/routers/api/v1/utils/hook.go +++ b/routers/api/v1/utils/hook.go @@ -107,11 +107,11 @@ func toAPIHook(ctx *context.APIContext, repoLink string, hook *webhook.Webhook) } func issuesHook(events []string, event string) bool { - return util.IsStringInSlice(event, events, true) || util.IsStringInSlice(string(webhook_module.HookEventIssues), events, true) + return util.SliceContainsString(events, event, true) || util.SliceContainsString(events, string(webhook_module.HookEventIssues), true) } func pullHook(events []string, event string) bool { - return util.IsStringInSlice(event, events, true) || util.IsStringInSlice(string(webhook_module.HookEventPullRequest), events, true) + return util.SliceContainsString(events, event, true) || util.SliceContainsString(events, string(webhook_module.HookEventPullRequest), true) } // addHook add the hook specified by `form`, `orgID` and `repoID`. If there is @@ -130,15 +130,15 @@ func addHook(ctx *context.APIContext, form *api.CreateHookOption, orgID, repoID HookEvent: &webhook_module.HookEvent{ ChooseEvents: true, HookEvents: webhook_module.HookEvents{ - Create: util.IsStringInSlice(string(webhook_module.HookEventCreate), form.Events, true), - Delete: util.IsStringInSlice(string(webhook_module.HookEventDelete), form.Events, true), - Fork: util.IsStringInSlice(string(webhook_module.HookEventFork), form.Events, true), + Create: util.SliceContainsString(form.Events, string(webhook_module.HookEventCreate), true), + Delete: util.SliceContainsString(form.Events, string(webhook_module.HookEventDelete), true), + Fork: util.SliceContainsString(form.Events, string(webhook_module.HookEventFork), true), Issues: issuesHook(form.Events, "issues_only"), IssueAssign: issuesHook(form.Events, string(webhook_module.HookEventIssueAssign)), IssueLabel: issuesHook(form.Events, string(webhook_module.HookEventIssueLabel)), IssueMilestone: issuesHook(form.Events, string(webhook_module.HookEventIssueMilestone)), IssueComment: issuesHook(form.Events, string(webhook_module.HookEventIssueComment)), - Push: util.IsStringInSlice(string(webhook_module.HookEventPush), form.Events, true), + Push: util.SliceContainsString(form.Events, string(webhook_module.HookEventPush), true), PullRequest: pullHook(form.Events, "pull_request_only"), PullRequestAssign: pullHook(form.Events, string(webhook_module.HookEventPullRequestAssign)), PullRequestLabel: pullHook(form.Events, string(webhook_module.HookEventPullRequestLabel)), @@ -146,9 +146,9 @@ func addHook(ctx *context.APIContext, form *api.CreateHookOption, orgID, repoID PullRequestComment: pullHook(form.Events, string(webhook_module.HookEventPullRequestComment)), PullRequestReview: pullHook(form.Events, "pull_request_review"), PullRequestSync: pullHook(form.Events, string(webhook_module.HookEventPullRequestSync)), - Wiki: util.IsStringInSlice(string(webhook_module.HookEventWiki), form.Events, true), - Repository: util.IsStringInSlice(string(webhook_module.HookEventRepository), form.Events, true), - Release: util.IsStringInSlice(string(webhook_module.HookEventRelease), form.Events, true), + Wiki: util.SliceContainsString(form.Events, string(webhook_module.HookEventWiki), true), + Repository: util.SliceContainsString(form.Events, string(webhook_module.HookEventRepository), true), + Release: util.SliceContainsString(form.Events, string(webhook_module.HookEventRelease), true), }, BranchFilter: form.BranchFilter, }, @@ -277,14 +277,14 @@ func editHook(ctx *context.APIContext, form *api.EditHookOption, w *webhook.Webh w.PushOnly = false w.SendEverything = false w.ChooseEvents = true - w.Create = util.IsStringInSlice(string(webhook_module.HookEventCreate), form.Events, true) - w.Push = util.IsStringInSlice(string(webhook_module.HookEventPush), form.Events, true) - w.Create = util.IsStringInSlice(string(webhook_module.HookEventCreate), form.Events, true) - w.Delete = util.IsStringInSlice(string(webhook_module.HookEventDelete), form.Events, true) - w.Fork = util.IsStringInSlice(string(webhook_module.HookEventFork), form.Events, true) - w.Repository = util.IsStringInSlice(string(webhook_module.HookEventRepository), form.Events, true) - w.Wiki = util.IsStringInSlice(string(webhook_module.HookEventWiki), form.Events, true) - w.Release = util.IsStringInSlice(string(webhook_module.HookEventRelease), form.Events, true) + w.Create = util.SliceContainsString(form.Events, string(webhook_module.HookEventCreate), true) + w.Push = util.SliceContainsString(form.Events, string(webhook_module.HookEventPush), true) + w.Create = util.SliceContainsString(form.Events, string(webhook_module.HookEventCreate), true) + w.Delete = util.SliceContainsString(form.Events, string(webhook_module.HookEventDelete), true) + w.Fork = util.SliceContainsString(form.Events, string(webhook_module.HookEventFork), true) + w.Repository = util.SliceContainsString(form.Events, string(webhook_module.HookEventRepository), true) + w.Wiki = util.SliceContainsString(form.Events, string(webhook_module.HookEventWiki), true) + w.Release = util.SliceContainsString(form.Events, string(webhook_module.HookEventRelease), true) w.BranchFilter = form.BranchFilter err := w.SetHeaderAuthorization(form.AuthorizationHeader) diff --git a/routers/web/repo/issue.go b/routers/web/repo/issue.go index 07dd98f1a5..f037c771d4 100644 --- a/routers/web/repo/issue.go +++ b/routers/web/repo/issue.go @@ -139,7 +139,7 @@ func issues(ctx *context.Context, milestoneID, projectID int64, isPullOption uti viewType := ctx.FormString("type") sortType := ctx.FormString("sort") types := []string{"all", "your_repositories", "assigned", "created_by", "mentioned", "review_requested"} - if !util.IsStringInSlice(viewType, types, true) { + if !util.SliceContainsString(types, viewType, true) { viewType = "all" } @@ -3087,7 +3087,7 @@ func updateAttachments(ctx *context.Context, item interface{}, files []string) e return fmt.Errorf("unknown Type: %T", content) } for i := 0; i < len(attachments); i++ { - if util.IsStringInSlice(attachments[i].UUID, files) { + if util.SliceContainsString(files, attachments[i].UUID) { continue } if err := repo_model.DeleteAttachment(attachments[i], true); err != nil { diff --git a/routers/web/repo/webhook.go b/routers/web/repo/webhook.go index bf56e3d0bd..96261af674 100644 --- a/routers/web/repo/webhook.go +++ b/routers/web/repo/webhook.go @@ -110,7 +110,7 @@ func getOrgRepoCtx(ctx *context.Context) (*orgRepoCtx, error) { func checkHookType(ctx *context.Context) string { hookType := strings.ToLower(ctx.Params(":type")) - if !util.IsStringInSlice(hookType, setting.Webhook.Types, true) { + if !util.SliceContainsString(setting.Webhook.Types, hookType, true) { ctx.NotFound("checkHookType", nil) return "" } diff --git a/routers/web/user/notification.go b/routers/web/user/notification.go index b21d52bbfd..e96b3dd27a 100644 --- a/routers/web/user/notification.go +++ b/routers/web/user/notification.go @@ -214,7 +214,7 @@ func NotificationSubscriptions(ctx *context.Context) { ctx.Data["SortType"] = sortType state := ctx.FormString("state") - if !util.IsStringInSlice(state, []string{"all", "open", "closed"}, true) { + if !util.SliceContainsString([]string{"all", "open", "closed"}, state, true) { state = "all" } ctx.Data["State"] = state diff --git a/routers/web/user/setting/profile.go b/routers/web/user/setting/profile.go index ef45ad8a86..dac00850b1 100644 --- a/routers/web/user/setting/profile.go +++ b/routers/web/user/setting/profile.go @@ -413,7 +413,7 @@ func UpdateUserLang(ctx *context.Context) { ctx.Data["PageIsSettingsAppearance"] = true if len(form.Language) != 0 { - if !util.IsStringInSlice(form.Language, setting.Langs) { + if !util.SliceContainsString(setting.Langs, form.Language) { ctx.Flash.Error(ctx.Tr("settings.update_language_not_found", form.Language)) ctx.Redirect(setting.AppSubURL + "/user/settings/appearance") return |