Skip to content

Commit 096e2f7

Browse files
committed
here go
1 parent 7960a0c commit 096e2f7

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

enterprise/coderd/idpsync.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package coderd
33
import (
44
"fmt"
55
"net/http"
6+
"slices"
67

78
"github.com/google/uuid"
89

@@ -373,6 +374,8 @@ func (api *API) patchOrganizationIDPSyncMapping(rw http.ResponseWriter, r *http.
373374
}
374375

375376
// Remove entries
377+
378+
// Option 1: the way I was already doing it
376379
for _, mapping := range req.Remove {
377380
for i, it := range newMapping[mapping.Given] {
378381
if it == mapping.Gets {
@@ -381,6 +384,26 @@ func (api *API) patchOrganizationIDPSyncMapping(rw http.ResponseWriter, r *http.
381384
}
382385
}
383386

387+
// Option 2: the way you suggested on the PR
388+
for _, mapping := range req.Remove {
389+
newMapping[mapping.Given] = slices.DeleteFunc(newMapping[mapping.Given], func(u uuid.UUID) bool {
390+
return u == mapping.Gets
391+
})
392+
}
393+
394+
// Option 3: this "optimal" but comparatively very noisey and dense version
395+
// Create a map[key][]thingsToRemove
396+
removeMap := make(map[string][]uuid.UUID)
397+
for _, mapping := range req.Remove {
398+
removeMap[mapping.Given] = append(removeMap[mapping.Given], mapping.Gets)
399+
}
400+
// Use `DeleteFunc` to remove anything from the `newMapping` present in `removeMap`
401+
for given, toRemove := range removeMap {
402+
newMapping[given] = slices.DeleteFunc(newMapping[given], func(id uuid.UUID) bool {
403+
return slices.Contains(toRemove, id)
404+
})
405+
}
406+
384407
settings = idpsync.OrganizationSyncSettings{
385408
Field: existing.Field,
386409
Mapping: newMapping,
@@ -393,7 +416,7 @@ func (api *API) patchOrganizationIDPSyncMapping(rw http.ResponseWriter, r *http.
393416
}
394417

395418
return nil
396-
}, &database.TxOptions{})
419+
})
397420
if err != nil {
398421
httpapi.InternalServerError(rw, err)
399422
return

0 commit comments

Comments
 (0)