Skip to content

Commit f665618

Browse files
6543KN4CK3R
andauthored
migrate some more "OptionalBool" to "Option[bool]" (#29479)
just some refactoring bits towards replacing **util.OptionalBool** with **optional.Option[bool]** --------- Co-authored-by: KN4CK3R <admin@oldschoolhack.me>
1 parent c7dcb58 commit f665618

23 files changed

+183
-174
lines changed

models/repo/repo_list.go

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"code.gitea.io/gitea/models/unit"
1414
user_model "code.gitea.io/gitea/models/user"
1515
"code.gitea.io/gitea/modules/container"
16+
"code.gitea.io/gitea/modules/optional"
1617
"code.gitea.io/gitea/modules/setting"
1718
"code.gitea.io/gitea/modules/structs"
1819
"code.gitea.io/gitea/modules/util"
@@ -125,31 +126,31 @@ type SearchRepoOptions struct {
125126
// None -> include public and private
126127
// True -> include just private
127128
// False -> include just public
128-
IsPrivate util.OptionalBool
129+
IsPrivate optional.Option[bool]
129130
// None -> include collaborative AND non-collaborative
130131
// True -> include just collaborative
131132
// False -> include just non-collaborative
132-
Collaborate util.OptionalBool
133+
Collaborate optional.Option[bool]
133134
// What type of unit the user can be collaborative in,
134135
// it is ignored if Collaborate is False.
135136
// TypeInvalid means any unit type.
136137
UnitType unit.Type
137138
// None -> include forks AND non-forks
138139
// True -> include just forks
139140
// False -> include just non-forks
140-
Fork util.OptionalBool
141+
Fork optional.Option[bool]
141142
// None -> include templates AND non-templates
142143
// True -> include just templates
143144
// False -> include just non-templates
144-
Template util.OptionalBool
145+
Template optional.Option[bool]
145146
// None -> include mirrors AND non-mirrors
146147
// True -> include just mirrors
147148
// False -> include just non-mirrors
148-
Mirror util.OptionalBool
149+
Mirror optional.Option[bool]
149150
// None -> include archived AND non-archived
150151
// True -> include just archived
151152
// False -> include just non-archived
152-
Archived util.OptionalBool
153+
Archived optional.Option[bool]
153154
// only search topic name
154155
TopicOnly bool
155156
// only search repositories with specified primary language
@@ -159,7 +160,7 @@ type SearchRepoOptions struct {
159160
// None -> include has milestones AND has no milestone
160161
// True -> include just has milestones
161162
// False -> include just has no milestone
162-
HasMilestones util.OptionalBool
163+
HasMilestones optional.Option[bool]
163164
// LowerNames represents valid lower names to restrict to
164165
LowerNames []string
165166
// When specified true, apply some filters over the conditions:
@@ -359,12 +360,12 @@ func SearchRepositoryCondition(opts *SearchRepoOptions) builder.Cond {
359360
)))
360361
}
361362

362-
if opts.IsPrivate != util.OptionalBoolNone {
363-
cond = cond.And(builder.Eq{"is_private": opts.IsPrivate.IsTrue()})
363+
if opts.IsPrivate.Has() {
364+
cond = cond.And(builder.Eq{"is_private": opts.IsPrivate.Value()})
364365
}
365366

366-
if opts.Template != util.OptionalBoolNone {
367-
cond = cond.And(builder.Eq{"is_template": opts.Template == util.OptionalBoolTrue})
367+
if opts.Template.Has() {
368+
cond = cond.And(builder.Eq{"is_template": opts.Template.Value()})
368369
}
369370

370371
// Restrict to starred repositories
@@ -380,11 +381,11 @@ func SearchRepositoryCondition(opts *SearchRepoOptions) builder.Cond {
380381
// Restrict repositories to those the OwnerID owns or contributes to as per opts.Collaborate
381382
if opts.OwnerID > 0 {
382383
accessCond := builder.NewCond()
383-
if opts.Collaborate != util.OptionalBoolTrue {
384+
if !opts.Collaborate.Value() {
384385
accessCond = builder.Eq{"owner_id": opts.OwnerID}
385386
}
386387

387-
if opts.Collaborate != util.OptionalBoolFalse {
388+
if opts.Collaborate.ValueOrDefault(true) {
388389
// A Collaboration is:
389390

390391
collaborateCond := builder.NewCond()
@@ -472,31 +473,32 @@ func SearchRepositoryCondition(opts *SearchRepoOptions) builder.Cond {
472473
Where(builder.Eq{"language": opts.Language}).And(builder.Eq{"is_primary": true})))
473474
}
474475

475-
if opts.Fork != util.OptionalBoolNone || opts.OnlyShowRelevant {
476-
if opts.OnlyShowRelevant && opts.Fork == util.OptionalBoolNone {
476+
if opts.Fork.Has() || opts.OnlyShowRelevant {
477+
if opts.OnlyShowRelevant && !opts.Fork.Has() {
477478
cond = cond.And(builder.Eq{"is_fork": false})
478479
} else {
479-
cond = cond.And(builder.Eq{"is_fork": opts.Fork == util.OptionalBoolTrue})
480+
cond = cond.And(builder.Eq{"is_fork": opts.Fork.Value()})
480481
}
481482
}
482483

483-
if opts.Mirror != util.OptionalBoolNone {
484-
cond = cond.And(builder.Eq{"is_mirror": opts.Mirror == util.OptionalBoolTrue})
484+
if opts.Mirror.Has() {
485+
cond = cond.And(builder.Eq{"is_mirror": opts.Mirror.Value()})
485486
}
486487

487488
if opts.Actor != nil && opts.Actor.IsRestricted {
488489
cond = cond.And(AccessibleRepositoryCondition(opts.Actor, unit.TypeInvalid))
489490
}
490491

491-
if opts.Archived != util.OptionalBoolNone {
492-
cond = cond.And(builder.Eq{"is_archived": opts.Archived == util.OptionalBoolTrue})
492+
if opts.Archived.Has() {
493+
cond = cond.And(builder.Eq{"is_archived": opts.Archived.Value()})
493494
}
494495

495-
switch opts.HasMilestones {
496-
case util.OptionalBoolTrue:
497-
cond = cond.And(builder.Gt{"num_milestones": 0})
498-
case util.OptionalBoolFalse:
499-
cond = cond.And(builder.Eq{"num_milestones": 0}.Or(builder.IsNull{"num_milestones"}))
496+
if opts.HasMilestones.Has() {
497+
if opts.HasMilestones.Value() {
498+
cond = cond.And(builder.Gt{"num_milestones": 0})
499+
} else {
500+
cond = cond.And(builder.Eq{"num_milestones": 0}.Or(builder.IsNull{"num_milestones"}))
501+
}
500502
}
501503

502504
if opts.OnlyShowRelevant {

models/repo/repo_list_test.go

Lines changed: 39 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"code.gitea.io/gitea/models/db"
1111
repo_model "code.gitea.io/gitea/models/repo"
1212
"code.gitea.io/gitea/models/unittest"
13-
"code.gitea.io/gitea/modules/util"
13+
"code.gitea.io/gitea/modules/optional"
1414

1515
"github.com/stretchr/testify/assert"
1616
)
@@ -27,62 +27,62 @@ func getTestCases() []struct {
2727
}{
2828
{
2929
name: "PublicRepositoriesByName",
30-
opts: &repo_model.SearchRepoOptions{Keyword: "big_test_", ListOptions: db.ListOptions{PageSize: 10}, Collaborate: util.OptionalBoolFalse},
30+
opts: &repo_model.SearchRepoOptions{Keyword: "big_test_", ListOptions: db.ListOptions{PageSize: 10}, Collaborate: optional.Some(false)},
3131
count: 7,
3232
},
3333
{
3434
name: "PublicAndPrivateRepositoriesByName",
35-
opts: &repo_model.SearchRepoOptions{Keyword: "big_test_", ListOptions: db.ListOptions{Page: 1, PageSize: 10}, Private: true, Collaborate: util.OptionalBoolFalse},
35+
opts: &repo_model.SearchRepoOptions{Keyword: "big_test_", ListOptions: db.ListOptions{Page: 1, PageSize: 10}, Private: true, Collaborate: optional.Some(false)},
3636
count: 14,
3737
},
3838
{
3939
name: "PublicAndPrivateRepositoriesByNameWithPagesizeLimitFirstPage",
40-
opts: &repo_model.SearchRepoOptions{Keyword: "big_test_", ListOptions: db.ListOptions{Page: 1, PageSize: 5}, Private: true, Collaborate: util.OptionalBoolFalse},
40+
opts: &repo_model.SearchRepoOptions{Keyword: "big_test_", ListOptions: db.ListOptions{Page: 1, PageSize: 5}, Private: true, Collaborate: optional.Some(false)},
4141
count: 14,
4242
},
4343
{
4444
name: "PublicAndPrivateRepositoriesByNameWithPagesizeLimitSecondPage",
45-
opts: &repo_model.SearchRepoOptions{Keyword: "big_test_", ListOptions: db.ListOptions{Page: 2, PageSize: 5}, Private: true, Collaborate: util.OptionalBoolFalse},
45+
opts: &repo_model.SearchRepoOptions{Keyword: "big_test_", ListOptions: db.ListOptions{Page: 2, PageSize: 5}, Private: true, Collaborate: optional.Some(false)},
4646
count: 14,
4747
},
4848
{
4949
name: "PublicAndPrivateRepositoriesByNameWithPagesizeLimitThirdPage",
50-
opts: &repo_model.SearchRepoOptions{Keyword: "big_test_", ListOptions: db.ListOptions{Page: 3, PageSize: 5}, Private: true, Collaborate: util.OptionalBoolFalse},
50+
opts: &repo_model.SearchRepoOptions{Keyword: "big_test_", ListOptions: db.ListOptions{Page: 3, PageSize: 5}, Private: true, Collaborate: optional.Some(false)},
5151
count: 14,
5252
},
5353
{
5454
name: "PublicAndPrivateRepositoriesByNameWithPagesizeLimitFourthPage",
55-
opts: &repo_model.SearchRepoOptions{Keyword: "big_test_", ListOptions: db.ListOptions{Page: 3, PageSize: 5}, Private: true, Collaborate: util.OptionalBoolFalse},
55+
opts: &repo_model.SearchRepoOptions{Keyword: "big_test_", ListOptions: db.ListOptions{Page: 3, PageSize: 5}, Private: true, Collaborate: optional.Some(false)},
5656
count: 14,
5757
},
5858
{
5959
name: "PublicRepositoriesOfUser",
60-
opts: &repo_model.SearchRepoOptions{ListOptions: db.ListOptions{Page: 1, PageSize: 10}, OwnerID: 15, Collaborate: util.OptionalBoolFalse},
60+
opts: &repo_model.SearchRepoOptions{ListOptions: db.ListOptions{Page: 1, PageSize: 10}, OwnerID: 15, Collaborate: optional.Some(false)},
6161
count: 2,
6262
},
6363
{
6464
name: "PublicRepositoriesOfUser2",
65-
opts: &repo_model.SearchRepoOptions{ListOptions: db.ListOptions{Page: 1, PageSize: 10}, OwnerID: 18, Collaborate: util.OptionalBoolFalse},
65+
opts: &repo_model.SearchRepoOptions{ListOptions: db.ListOptions{Page: 1, PageSize: 10}, OwnerID: 18, Collaborate: optional.Some(false)},
6666
count: 0,
6767
},
6868
{
6969
name: "PublicRepositoriesOfOrg3",
70-
opts: &repo_model.SearchRepoOptions{ListOptions: db.ListOptions{Page: 1, PageSize: 10}, OwnerID: 20, Collaborate: util.OptionalBoolFalse},
70+
opts: &repo_model.SearchRepoOptions{ListOptions: db.ListOptions{Page: 1, PageSize: 10}, OwnerID: 20, Collaborate: optional.Some(false)},
7171
count: 2,
7272
},
7373
{
7474
name: "PublicAndPrivateRepositoriesOfUser",
75-
opts: &repo_model.SearchRepoOptions{ListOptions: db.ListOptions{Page: 1, PageSize: 10}, OwnerID: 15, Private: true, Collaborate: util.OptionalBoolFalse},
75+
opts: &repo_model.SearchRepoOptions{ListOptions: db.ListOptions{Page: 1, PageSize: 10}, OwnerID: 15, Private: true, Collaborate: optional.Some(false)},
7676
count: 4,
7777
},
7878
{
7979
name: "PublicAndPrivateRepositoriesOfUser2",
80-
opts: &repo_model.SearchRepoOptions{ListOptions: db.ListOptions{Page: 1, PageSize: 10}, OwnerID: 18, Private: true, Collaborate: util.OptionalBoolFalse},
80+
opts: &repo_model.SearchRepoOptions{ListOptions: db.ListOptions{Page: 1, PageSize: 10}, OwnerID: 18, Private: true, Collaborate: optional.Some(false)},
8181
count: 0,
8282
},
8383
{
8484
name: "PublicAndPrivateRepositoriesOfOrg3",
85-
opts: &repo_model.SearchRepoOptions{ListOptions: db.ListOptions{Page: 1, PageSize: 10}, OwnerID: 20, Private: true, Collaborate: util.OptionalBoolFalse},
85+
opts: &repo_model.SearchRepoOptions{ListOptions: db.ListOptions{Page: 1, PageSize: 10}, OwnerID: 20, Private: true, Collaborate: optional.Some(false)},
8686
count: 4,
8787
},
8888
{
@@ -117,32 +117,32 @@ func getTestCases() []struct {
117117
},
118118
{
119119
name: "PublicRepositoriesOfOrganization",
120-
opts: &repo_model.SearchRepoOptions{ListOptions: db.ListOptions{Page: 1, PageSize: 10}, OwnerID: 17, Collaborate: util.OptionalBoolFalse},
120+
opts: &repo_model.SearchRepoOptions{ListOptions: db.ListOptions{Page: 1, PageSize: 10}, OwnerID: 17, Collaborate: optional.Some(false)},
121121
count: 1,
122122
},
123123
{
124124
name: "PublicAndPrivateRepositoriesOfOrganization",
125-
opts: &repo_model.SearchRepoOptions{ListOptions: db.ListOptions{Page: 1, PageSize: 10}, OwnerID: 17, Private: true, Collaborate: util.OptionalBoolFalse},
125+
opts: &repo_model.SearchRepoOptions{ListOptions: db.ListOptions{Page: 1, PageSize: 10}, OwnerID: 17, Private: true, Collaborate: optional.Some(false)},
126126
count: 2,
127127
},
128128
{
129129
name: "AllPublic/PublicRepositoriesByName",
130-
opts: &repo_model.SearchRepoOptions{Keyword: "big_test_", ListOptions: db.ListOptions{PageSize: 10}, AllPublic: true, Collaborate: util.OptionalBoolFalse},
130+
opts: &repo_model.SearchRepoOptions{Keyword: "big_test_", ListOptions: db.ListOptions{PageSize: 10}, AllPublic: true, Collaborate: optional.Some(false)},
131131
count: 7,
132132
},
133133
{
134134
name: "AllPublic/PublicAndPrivateRepositoriesByName",
135-
opts: &repo_model.SearchRepoOptions{Keyword: "big_test_", ListOptions: db.ListOptions{Page: 1, PageSize: 10}, Private: true, AllPublic: true, Collaborate: util.OptionalBoolFalse},
135+
opts: &repo_model.SearchRepoOptions{Keyword: "big_test_", ListOptions: db.ListOptions{Page: 1, PageSize: 10}, Private: true, AllPublic: true, Collaborate: optional.Some(false)},
136136
count: 14,
137137
},
138138
{
139139
name: "AllPublic/PublicRepositoriesOfUserIncludingCollaborative",
140-
opts: &repo_model.SearchRepoOptions{ListOptions: db.ListOptions{Page: 1, PageSize: 10}, OwnerID: 15, AllPublic: true, Template: util.OptionalBoolFalse},
140+
opts: &repo_model.SearchRepoOptions{ListOptions: db.ListOptions{Page: 1, PageSize: 10}, OwnerID: 15, AllPublic: true, Template: optional.Some(false)},
141141
count: 33,
142142
},
143143
{
144144
name: "AllPublic/PublicAndPrivateRepositoriesOfUserIncludingCollaborative",
145-
opts: &repo_model.SearchRepoOptions{ListOptions: db.ListOptions{Page: 1, PageSize: 10}, OwnerID: 15, Private: true, AllPublic: true, AllLimited: true, Template: util.OptionalBoolFalse},
145+
opts: &repo_model.SearchRepoOptions{ListOptions: db.ListOptions{Page: 1, PageSize: 10}, OwnerID: 15, Private: true, AllPublic: true, AllLimited: true, Template: optional.Some(false)},
146146
count: 38,
147147
},
148148
{
@@ -157,12 +157,12 @@ func getTestCases() []struct {
157157
},
158158
{
159159
name: "AllPublic/PublicRepositoriesOfOrganization",
160-
opts: &repo_model.SearchRepoOptions{ListOptions: db.ListOptions{Page: 1, PageSize: 10}, OwnerID: 17, AllPublic: true, Collaborate: util.OptionalBoolFalse, Template: util.OptionalBoolFalse},
160+
opts: &repo_model.SearchRepoOptions{ListOptions: db.ListOptions{Page: 1, PageSize: 10}, OwnerID: 17, AllPublic: true, Collaborate: optional.Some(false), Template: optional.Some(false)},
161161
count: 33,
162162
},
163163
{
164164
name: "AllTemplates",
165-
opts: &repo_model.SearchRepoOptions{ListOptions: db.ListOptions{Page: 1, PageSize: 10}, Template: util.OptionalBoolTrue},
165+
opts: &repo_model.SearchRepoOptions{ListOptions: db.ListOptions{Page: 1, PageSize: 10}, Template: optional.Some(true)},
166166
count: 2,
167167
},
168168
{
@@ -190,7 +190,7 @@ func TestSearchRepository(t *testing.T) {
190190
PageSize: 10,
191191
},
192192
Keyword: "repo_12",
193-
Collaborate: util.OptionalBoolFalse,
193+
Collaborate: optional.Some(false),
194194
})
195195

196196
assert.NoError(t, err)
@@ -205,7 +205,7 @@ func TestSearchRepository(t *testing.T) {
205205
PageSize: 10,
206206
},
207207
Keyword: "test_repo",
208-
Collaborate: util.OptionalBoolFalse,
208+
Collaborate: optional.Some(false),
209209
})
210210

211211
assert.NoError(t, err)
@@ -220,7 +220,7 @@ func TestSearchRepository(t *testing.T) {
220220
},
221221
Keyword: "repo_13",
222222
Private: true,
223-
Collaborate: util.OptionalBoolFalse,
223+
Collaborate: optional.Some(false),
224224
})
225225

226226
assert.NoError(t, err)
@@ -236,7 +236,7 @@ func TestSearchRepository(t *testing.T) {
236236
},
237237
Keyword: "test_repo",
238238
Private: true,
239-
Collaborate: util.OptionalBoolFalse,
239+
Collaborate: optional.Some(false),
240240
})
241241

242242
assert.NoError(t, err)
@@ -257,7 +257,7 @@ func TestSearchRepository(t *testing.T) {
257257
PageSize: 10,
258258
},
259259
Keyword: "description_14",
260-
Collaborate: util.OptionalBoolFalse,
260+
Collaborate: optional.Some(false),
261261
IncludeDescription: true,
262262
})
263263

@@ -274,7 +274,7 @@ func TestSearchRepository(t *testing.T) {
274274
PageSize: 10,
275275
},
276276
Keyword: "description_14",
277-
Collaborate: util.OptionalBoolFalse,
277+
Collaborate: optional.Some(false),
278278
IncludeDescription: false,
279279
})
280280

@@ -327,30 +327,25 @@ func TestSearchRepository(t *testing.T) {
327327
assert.False(t, repo.IsPrivate)
328328
}
329329

330-
if testCase.opts.Fork == util.OptionalBoolTrue && testCase.opts.Mirror == util.OptionalBoolTrue {
331-
assert.True(t, repo.IsFork || repo.IsMirror)
330+
if testCase.opts.Fork.Value() && testCase.opts.Mirror.Value() {
331+
assert.True(t, repo.IsFork && repo.IsMirror)
332332
} else {
333-
switch testCase.opts.Fork {
334-
case util.OptionalBoolFalse:
335-
assert.False(t, repo.IsFork)
336-
case util.OptionalBoolTrue:
337-
assert.True(t, repo.IsFork)
333+
if testCase.opts.Fork.Has() {
334+
assert.Equal(t, testCase.opts.Fork.Value(), repo.IsFork)
338335
}
339336

340-
switch testCase.opts.Mirror {
341-
case util.OptionalBoolFalse:
342-
assert.False(t, repo.IsMirror)
343-
case util.OptionalBoolTrue:
344-
assert.True(t, repo.IsMirror)
337+
if testCase.opts.Mirror.Has() {
338+
assert.Equal(t, testCase.opts.Mirror.Value(), repo.IsMirror)
345339
}
346340
}
347341

348342
if testCase.opts.OwnerID > 0 && !testCase.opts.AllPublic {
349-
switch testCase.opts.Collaborate {
350-
case util.OptionalBoolFalse:
351-
assert.Equal(t, testCase.opts.OwnerID, repo.Owner.ID)
352-
case util.OptionalBoolTrue:
353-
assert.NotEqual(t, testCase.opts.OwnerID, repo.Owner.ID)
343+
if testCase.opts.Collaborate.Has() {
344+
if testCase.opts.Collaborate.Value() {
345+
assert.NotEqual(t, testCase.opts.OwnerID, repo.Owner.ID)
346+
} else {
347+
assert.Equal(t, testCase.opts.OwnerID, repo.Owner.ID)
348+
}
354349
}
355350
}
356351
}

0 commit comments

Comments
 (0)