File tree Expand file tree Collapse file tree 4 files changed +15
-2
lines changed Expand file tree Collapse file tree 4 files changed +15
-2
lines changed Original file line number Diff line number Diff line change @@ -22,6 +22,13 @@ func FromPtr[T any](v *T) Option[T] {
22
22
return Some (* v )
23
23
}
24
24
25
+ func FromMapLookup [K comparable , V any ](m map [K ]V , k K ) Option [V ] {
26
+ if v , ok := m [k ]; ok {
27
+ return Some (v )
28
+ }
29
+ return None [V ]()
30
+ }
31
+
25
32
func FromNonDefault [T comparable ](v T ) Option [T ] {
26
33
var zero T
27
34
if v == zero {
Original file line number Diff line number Diff line change @@ -56,6 +56,12 @@ func TestOption(t *testing.T) {
56
56
opt3 := optional .FromNonDefault (1 )
57
57
assert .True (t , opt3 .Has ())
58
58
assert .Equal (t , int (1 ), opt3 .Value ())
59
+
60
+ opt4 := optional .FromMapLookup (map [string ]int {"a" : 1 }, "a" )
61
+ assert .True (t , opt4 .Has ())
62
+ assert .Equal (t , 1 , opt4 .Value ())
63
+ opt4 = optional .FromMapLookup (map [string ]int {"a" : 1 }, "b" )
64
+ assert .False (t , opt4 .Has ())
59
65
}
60
66
61
67
func Test_ParseBool (t * testing.T ) {
Original file line number Diff line number Diff line change @@ -240,7 +240,7 @@ func EditUser(ctx *context.APIContext) {
240
240
Description : optional .FromPtr (form .Description ),
241
241
IsActive : optional .FromPtr (form .Active ),
242
242
IsAdmin : user_service .UpdateOptionFieldFromPtr (form .Admin ),
243
- Visibility : optional .FromNonDefault (api .VisibilityModes [ form .Visibility ] ),
243
+ Visibility : optional .FromMapLookup (api .VisibilityModes , form .Visibility ),
244
244
AllowGitHook : optional .FromPtr (form .AllowGitHook ),
245
245
AllowImportLocal : optional .FromPtr (form .AllowImportLocal ),
246
246
MaxRepoCreation : optional .FromPtr (form .MaxRepoCreation ),
Original file line number Diff line number Diff line change @@ -393,7 +393,7 @@ func Edit(ctx *context.APIContext) {
393
393
Description : optional .Some (form .Description ),
394
394
Website : optional .Some (form .Website ),
395
395
Location : optional .Some (form .Location ),
396
- Visibility : optional .FromNonDefault (api .VisibilityModes [ form .Visibility ] ),
396
+ Visibility : optional .FromMapLookup (api .VisibilityModes , form .Visibility ),
397
397
RepoAdminChangeTeamAccess : optional .FromPtr (form .RepoAdminChangeTeamAccess ),
398
398
}
399
399
if err := user_service .UpdateUser (ctx , ctx .Org .Organization .AsUser (), opts ); err != nil {
You can’t perform that action at this time.
0 commit comments