Skip to content

Commit 274c0ae

Browse files
authored
Let ctx.FormOptionalBool() return optional.Option[bool] (#29461)
just some refactoring bits towards replacing **util.OptionalBool** with **optional.Option[bool]**
1 parent db545b2 commit 274c0ae

File tree

5 files changed

+22
-19
lines changed

5 files changed

+22
-19
lines changed

models/repo/release.go

+11-10
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"code.gitea.io/gitea/models/db"
1616
user_model "code.gitea.io/gitea/models/user"
1717
"code.gitea.io/gitea/modules/container"
18+
"code.gitea.io/gitea/modules/optional"
1819
"code.gitea.io/gitea/modules/structs"
1920
"code.gitea.io/gitea/modules/timeutil"
2021
"code.gitea.io/gitea/modules/util"
@@ -228,10 +229,10 @@ type FindReleasesOptions struct {
228229
RepoID int64
229230
IncludeDrafts bool
230231
IncludeTags bool
231-
IsPreRelease util.OptionalBool
232-
IsDraft util.OptionalBool
232+
IsPreRelease optional.Option[bool]
233+
IsDraft optional.Option[bool]
233234
TagNames []string
234-
HasSha1 util.OptionalBool // useful to find draft releases which are created with existing tags
235+
HasSha1 optional.Option[bool] // useful to find draft releases which are created with existing tags
235236
}
236237

237238
func (opts FindReleasesOptions) ToConds() builder.Cond {
@@ -246,14 +247,14 @@ func (opts FindReleasesOptions) ToConds() builder.Cond {
246247
if len(opts.TagNames) > 0 {
247248
cond = cond.And(builder.In("tag_name", opts.TagNames))
248249
}
249-
if !opts.IsPreRelease.IsNone() {
250-
cond = cond.And(builder.Eq{"is_prerelease": opts.IsPreRelease.IsTrue()})
250+
if opts.IsPreRelease.Has() {
251+
cond = cond.And(builder.Eq{"is_prerelease": opts.IsPreRelease.Value()})
251252
}
252-
if !opts.IsDraft.IsNone() {
253-
cond = cond.And(builder.Eq{"is_draft": opts.IsDraft.IsTrue()})
253+
if opts.IsDraft.Has() {
254+
cond = cond.And(builder.Eq{"is_draft": opts.IsDraft.Value()})
254255
}
255-
if !opts.HasSha1.IsNone() {
256-
if opts.HasSha1.IsTrue() {
256+
if opts.HasSha1.Has() {
257+
if opts.HasSha1.Value() {
257258
cond = cond.And(builder.Neq{"sha1": ""})
258259
} else {
259260
cond = cond.And(builder.Eq{"sha1": ""})
@@ -275,7 +276,7 @@ func GetTagNamesByRepoID(ctx context.Context, repoID int64) ([]string, error) {
275276
ListOptions: listOptions,
276277
IncludeDrafts: true,
277278
IncludeTags: true,
278-
HasSha1: util.OptionalBoolTrue,
279+
HasSha1: optional.Some(true),
279280
RepoID: repoID,
280281
}
281282

routers/web/repo/release.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"code.gitea.io/gitea/modules/log"
2222
"code.gitea.io/gitea/modules/markup"
2323
"code.gitea.io/gitea/modules/markup/markdown"
24+
"code.gitea.io/gitea/modules/optional"
2425
"code.gitea.io/gitea/modules/setting"
2526
"code.gitea.io/gitea/modules/util"
2627
"code.gitea.io/gitea/modules/web"
@@ -223,7 +224,7 @@ func TagsList(ctx *context.Context) {
223224
// the drafts should also be included because a real tag might be used as a draft.
224225
IncludeDrafts: true,
225226
IncludeTags: true,
226-
HasSha1: util.OptionalBoolTrue,
227+
HasSha1: optional.Some(true),
227228
RepoID: ctx.Repo.Repository.ID,
228229
}
229230

routers/web/user/search.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"code.gitea.io/gitea/models/db"
1010
user_model "code.gitea.io/gitea/models/user"
11+
"code.gitea.io/gitea/modules/util"
1112
"code.gitea.io/gitea/services/context"
1213
"code.gitea.io/gitea/services/convert"
1314
)
@@ -24,7 +25,7 @@ func Search(ctx *context.Context) {
2425
Keyword: ctx.FormTrim("q"),
2526
UID: ctx.FormInt64("uid"),
2627
Type: user_model.UserTypeIndividual,
27-
IsActive: ctx.FormOptionalBool("active"),
28+
IsActive: util.OptionalBoolFromGeneric(ctx.FormOptionalBool("active")),
2829
ListOptions: listOptions,
2930
})
3031
if err != nil {

services/context/base.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ import (
1717
"code.gitea.io/gitea/modules/httplib"
1818
"code.gitea.io/gitea/modules/json"
1919
"code.gitea.io/gitea/modules/log"
20+
"code.gitea.io/gitea/modules/optional"
2021
"code.gitea.io/gitea/modules/translation"
21-
"code.gitea.io/gitea/modules/util"
2222
"code.gitea.io/gitea/modules/web/middleware"
2323

2424
"github.com/go-chi/chi/v5"
@@ -207,17 +207,17 @@ func (b *Base) FormBool(key string) bool {
207207
return v
208208
}
209209

210-
// FormOptionalBool returns an OptionalBoolTrue or OptionalBoolFalse if the value
211-
// for the provided key exists in the form else it returns OptionalBoolNone
212-
func (b *Base) FormOptionalBool(key string) util.OptionalBool {
210+
// FormOptionalBool returns an optional.Some(true) or optional.Some(false) if the value
211+
// for the provided key exists in the form else it returns optional.None[bool]()
212+
func (b *Base) FormOptionalBool(key string) optional.Option[bool] {
213213
value := b.Req.FormValue(key)
214214
if len(value) == 0 {
215-
return util.OptionalBoolNone
215+
return optional.None[bool]()
216216
}
217217
s := b.Req.FormValue(key)
218218
v, _ := strconv.ParseBool(s)
219219
v = v || strings.EqualFold(s, "on")
220-
return util.OptionalBoolOf(v)
220+
return optional.Some(v)
221221
}
222222

223223
func (b *Base) SetFormString(key, value string) {

services/context/repo.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,7 @@ func RepoAssignment(ctx *Context) context.CancelFunc {
546546
ctx.Data["NumTags"], err = db.Count[repo_model.Release](ctx, repo_model.FindReleasesOptions{
547547
IncludeDrafts: true,
548548
IncludeTags: true,
549-
HasSha1: util.OptionalBoolTrue, // only draft releases which are created with existing tags
549+
HasSha1: optional.Some(true), // only draft releases which are created with existing tags
550550
RepoID: ctx.Repo.Repository.ID,
551551
})
552552
if err != nil {

0 commit comments

Comments
 (0)