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] {
2222 return Some (* v )
2323}
2424
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+
2532func FromNonDefault [T comparable ](v T ) Option [T ] {
2633 var zero T
2734 if v == zero {
Original file line number Diff line number Diff line change @@ -56,6 +56,12 @@ func TestOption(t *testing.T) {
5656 opt3 := optional .FromNonDefault (1 )
5757 assert .True (t , opt3 .Has ())
5858 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 ())
5965}
6066
6167func Test_ParseBool (t * testing.T ) {
Original file line number Diff line number Diff line change @@ -240,7 +240,7 @@ func EditUser(ctx *context.APIContext) {
240240 Description : optional .FromPtr (form .Description ),
241241 IsActive : optional .FromPtr (form .Active ),
242242 IsAdmin : user_service .UpdateOptionFieldFromPtr (form .Admin ),
243- Visibility : optional .FromNonDefault (api .VisibilityModes [ form .Visibility ] ),
243+ Visibility : optional .FromMapLookup (api .VisibilityModes , form .Visibility ),
244244 AllowGitHook : optional .FromPtr (form .AllowGitHook ),
245245 AllowImportLocal : optional .FromPtr (form .AllowImportLocal ),
246246 MaxRepoCreation : optional .FromPtr (form .MaxRepoCreation ),
Original file line number Diff line number Diff line change @@ -393,7 +393,7 @@ func Edit(ctx *context.APIContext) {
393393 Description : optional .Some (form .Description ),
394394 Website : optional .Some (form .Website ),
395395 Location : optional .Some (form .Location ),
396- Visibility : optional .FromNonDefault (api .VisibilityModes [ form .Visibility ] ),
396+ Visibility : optional .FromMapLookup (api .VisibilityModes , form .Visibility ),
397397 RepoAdminChangeTeamAccess : optional .FromPtr (form .RepoAdminChangeTeamAccess ),
398398 }
399399 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