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 @@ -28,6 +28,13 @@ func FromPtr[T any](v *T) Option[T] {
28
28
return Some (* v )
29
29
}
30
30
31
+ func FromMapLookup [K comparable , V any ](m map [K ]V , k K ) Option [V ] {
32
+ if v , ok := m [k ]; ok {
33
+ return Some (v )
34
+ }
35
+ return None [V ]()
36
+ }
37
+
31
38
func FromNonDefault [T comparable ](v T ) Option [T ] {
32
39
var zero T
33
40
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 @@ -391,7 +391,7 @@ func Edit(ctx *context.APIContext) {
391
391
Description : optional .Some (form .Description ),
392
392
Website : optional .Some (form .Website ),
393
393
Location : optional .Some (form .Location ),
394
- Visibility : optional .FromNonDefault (api .VisibilityModes [ form .Visibility ] ),
394
+ Visibility : optional .FromMapLookup (api .VisibilityModes , form .Visibility ),
395
395
RepoAdminChangeTeamAccess : optional .FromPtr (form .RepoAdminChangeTeamAccess ),
396
396
}
397
397
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