From 274c0aea2e88db9bc41690c90e13e8aedf6193d4 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Wed, 28 Feb 2024 06:39:12 +0100 Subject: [PATCH] Let ctx.FormOptionalBool() return optional.Option[bool] (#29461) just some refactoring bits towards replacing **util.OptionalBool** with **optional.Option[bool]** --- models/repo/release.go | 21 +++++++++++---------- routers/web/repo/release.go | 3 ++- routers/web/user/search.go | 3 ++- services/context/base.go | 12 ++++++------ services/context/repo.go | 2 +- 5 files changed, 22 insertions(+), 19 deletions(-) diff --git a/models/repo/release.go b/models/repo/release.go index 1f37f11b2e38..9287931dd5a2 100644 --- a/models/repo/release.go +++ b/models/repo/release.go @@ -15,6 +15,7 @@ import ( "code.gitea.io/gitea/models/db" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/container" + "code.gitea.io/gitea/modules/optional" "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/timeutil" "code.gitea.io/gitea/modules/util" @@ -228,10 +229,10 @@ type FindReleasesOptions struct { RepoID int64 IncludeDrafts bool IncludeTags bool - IsPreRelease util.OptionalBool - IsDraft util.OptionalBool + IsPreRelease optional.Option[bool] + IsDraft optional.Option[bool] TagNames []string - HasSha1 util.OptionalBool // useful to find draft releases which are created with existing tags + HasSha1 optional.Option[bool] // useful to find draft releases which are created with existing tags } func (opts FindReleasesOptions) ToConds() builder.Cond { @@ -246,14 +247,14 @@ func (opts FindReleasesOptions) ToConds() builder.Cond { if len(opts.TagNames) > 0 { cond = cond.And(builder.In("tag_name", opts.TagNames)) } - if !opts.IsPreRelease.IsNone() { - cond = cond.And(builder.Eq{"is_prerelease": opts.IsPreRelease.IsTrue()}) + if opts.IsPreRelease.Has() { + cond = cond.And(builder.Eq{"is_prerelease": opts.IsPreRelease.Value()}) } - if !opts.IsDraft.IsNone() { - cond = cond.And(builder.Eq{"is_draft": opts.IsDraft.IsTrue()}) + if opts.IsDraft.Has() { + cond = cond.And(builder.Eq{"is_draft": opts.IsDraft.Value()}) } - if !opts.HasSha1.IsNone() { - if opts.HasSha1.IsTrue() { + if opts.HasSha1.Has() { + if opts.HasSha1.Value() { cond = cond.And(builder.Neq{"sha1": ""}) } else { cond = cond.And(builder.Eq{"sha1": ""}) @@ -275,7 +276,7 @@ func GetTagNamesByRepoID(ctx context.Context, repoID int64) ([]string, error) { ListOptions: listOptions, IncludeDrafts: true, IncludeTags: true, - HasSha1: util.OptionalBoolTrue, + HasSha1: optional.Some(true), RepoID: repoID, } diff --git a/routers/web/repo/release.go b/routers/web/repo/release.go index f9ab956d4cff..a730c2d3b786 100644 --- a/routers/web/repo/release.go +++ b/routers/web/repo/release.go @@ -21,6 +21,7 @@ import ( "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/markup" "code.gitea.io/gitea/modules/markup/markdown" + "code.gitea.io/gitea/modules/optional" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/modules/web" @@ -223,7 +224,7 @@ func TagsList(ctx *context.Context) { // the drafts should also be included because a real tag might be used as a draft. IncludeDrafts: true, IncludeTags: true, - HasSha1: util.OptionalBoolTrue, + HasSha1: optional.Some(true), RepoID: ctx.Repo.Repository.ID, } diff --git a/routers/web/user/search.go b/routers/web/user/search.go index fb7729bbe141..5ef61c88d45e 100644 --- a/routers/web/user/search.go +++ b/routers/web/user/search.go @@ -8,6 +8,7 @@ import ( "code.gitea.io/gitea/models/db" user_model "code.gitea.io/gitea/models/user" + "code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/services/context" "code.gitea.io/gitea/services/convert" ) @@ -24,7 +25,7 @@ func Search(ctx *context.Context) { Keyword: ctx.FormTrim("q"), UID: ctx.FormInt64("uid"), Type: user_model.UserTypeIndividual, - IsActive: ctx.FormOptionalBool("active"), + IsActive: util.OptionalBoolFromGeneric(ctx.FormOptionalBool("active")), ListOptions: listOptions, }) if err != nil { diff --git a/services/context/base.go b/services/context/base.go index ddd04f476745..c4aa467ff425 100644 --- a/services/context/base.go +++ b/services/context/base.go @@ -17,8 +17,8 @@ import ( "code.gitea.io/gitea/modules/httplib" "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/log" + "code.gitea.io/gitea/modules/optional" "code.gitea.io/gitea/modules/translation" - "code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/modules/web/middleware" "github.com/go-chi/chi/v5" @@ -207,17 +207,17 @@ func (b *Base) FormBool(key string) bool { return v } -// FormOptionalBool returns an OptionalBoolTrue or OptionalBoolFalse if the value -// for the provided key exists in the form else it returns OptionalBoolNone -func (b *Base) FormOptionalBool(key string) util.OptionalBool { +// FormOptionalBool returns an optional.Some(true) or optional.Some(false) if the value +// for the provided key exists in the form else it returns optional.None[bool]() +func (b *Base) FormOptionalBool(key string) optional.Option[bool] { value := b.Req.FormValue(key) if len(value) == 0 { - return util.OptionalBoolNone + return optional.None[bool]() } s := b.Req.FormValue(key) v, _ := strconv.ParseBool(s) v = v || strings.EqualFold(s, "on") - return util.OptionalBoolOf(v) + return optional.Some(v) } func (b *Base) SetFormString(key, value string) { diff --git a/services/context/repo.go b/services/context/repo.go index a73d09ee21b8..d6a68c0c1a0e 100644 --- a/services/context/repo.go +++ b/services/context/repo.go @@ -546,7 +546,7 @@ func RepoAssignment(ctx *Context) context.CancelFunc { ctx.Data["NumTags"], err = db.Count[repo_model.Release](ctx, repo_model.FindReleasesOptions{ IncludeDrafts: true, IncludeTags: true, - HasSha1: util.OptionalBoolTrue, // only draft releases which are created with existing tags + HasSha1: optional.Some(true), // only draft releases which are created with existing tags RepoID: ctx.Repo.Repository.ID, }) if err != nil {