@@ -3,6 +3,7 @@ package coderd
3
3
import (
4
4
"fmt"
5
5
"net/http"
6
+ "slices"
6
7
7
8
"github.com/google/uuid"
8
9
@@ -373,6 +374,8 @@ func (api *API) patchOrganizationIDPSyncMapping(rw http.ResponseWriter, r *http.
373
374
}
374
375
375
376
// Remove entries
377
+
378
+ // Option 1: the way I was already doing it
376
379
for _ , mapping := range req .Remove {
377
380
for i , it := range newMapping [mapping .Given ] {
378
381
if it == mapping .Gets {
@@ -381,6 +384,26 @@ func (api *API) patchOrganizationIDPSyncMapping(rw http.ResponseWriter, r *http.
381
384
}
382
385
}
383
386
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
+
384
407
settings = idpsync.OrganizationSyncSettings {
385
408
Field : existing .Field ,
386
409
Mapping : newMapping ,
@@ -393,7 +416,7 @@ func (api *API) patchOrganizationIDPSyncMapping(rw http.ResponseWriter, r *http.
393
416
}
394
417
395
418
return nil
396
- }, & database. TxOptions {} )
419
+ })
397
420
if err != nil {
398
421
httpapi .InternalServerError (rw , err )
399
422
return
0 commit comments