Skip to content

Commit 9a600c0

Browse files
committed
Move sync map to utils
1 parent 6e6084b commit 9a600c0

File tree

2 files changed

+28
-26
lines changed

2 files changed

+28
-26
lines changed

coderd/coderdtest/oidctest/idp.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import (
1818
"testing"
1919
"time"
2020

21+
"github.com/coder/coder/v2/coderd/util/syncmap"
22+
2123
"github.com/coreos/go-oidc/v3/oidc"
2224
"github.com/go-chi/chi/v5"
2325
"github.com/go-jose/go-jose/v3"
@@ -50,14 +52,14 @@ type FakeIDP struct {
5052

5153
// These maps are used to control the state of the IDP.
5254
// That is the various access tokens, refresh tokens, states, etc.
53-
codeToStateMap *SyncMap[string, string]
55+
codeToStateMap *syncmap.Map[string, string]
5456
// Token -> Email
55-
accessTokens *SyncMap[string, string]
57+
accessTokens *syncmap.Map[string, string]
5658
// Refresh Token -> Email
57-
refreshTokensUsed *SyncMap[string, bool]
58-
refreshTokens *SyncMap[string, string]
59-
stateToIDTokenClaims *SyncMap[string, jwt.MapClaims]
60-
refreshIDTokenClaims *SyncMap[string, jwt.MapClaims]
59+
refreshTokensUsed *syncmap.Map[string, bool]
60+
refreshTokens *syncmap.Map[string, string]
61+
stateToIDTokenClaims *syncmap.Map[string, jwt.MapClaims]
62+
refreshIDTokenClaims *syncmap.Map[string, jwt.MapClaims]
6163

6264
// hooks
6365
// hookValidRedirectURL can be used to reject a redirect url from the
@@ -151,12 +153,12 @@ func NewFakeIDP(t testing.TB, opts ...FakeIDPOpt) *FakeIDP {
151153
clientID: uuid.NewString(),
152154
clientSecret: uuid.NewString(),
153155
logger: slog.Make(),
154-
codeToStateMap: NewSyncMap[string, string](),
155-
accessTokens: NewSyncMap[string, string](),
156-
refreshTokens: NewSyncMap[string, string](),
157-
refreshTokensUsed: NewSyncMap[string, bool](),
158-
stateToIDTokenClaims: NewSyncMap[string, jwt.MapClaims](),
159-
refreshIDTokenClaims: NewSyncMap[string, jwt.MapClaims](),
156+
codeToStateMap: syncmap.New[string, string](),
157+
accessTokens: syncmap.New[string, string](),
158+
refreshTokens: syncmap.New[string, string](),
159+
refreshTokensUsed: syncmap.New[string, bool](),
160+
stateToIDTokenClaims: syncmap.New[string, jwt.MapClaims](),
161+
refreshIDTokenClaims: syncmap.New[string, jwt.MapClaims](),
160162
hookOnRefresh: func(_ string) error { return nil },
161163
hookUserInfo: func(email string) jwt.MapClaims { return jwt.MapClaims{} },
162164
}

coderd/coderdtest/oidctest/map.go renamed to coderd/util/syncmap/map.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
1-
package oidctest
1+
package syncmap
22

33
import "sync"
44

5-
// SyncMap is a type safe sync.Map
6-
type SyncMap[K, V any] struct {
5+
// Map is a type safe sync.Map
6+
type Map[K, V any] struct {
77
m sync.Map
88
}
99

10-
func NewSyncMap[K, V any]() *SyncMap[K, V] {
11-
return &SyncMap[K, V]{
10+
func New[K, V any]() *Map[K, V] {
11+
return &Map[K, V]{
1212
m: sync.Map{},
1313
}
1414
}
1515

16-
func (m *SyncMap[K, V]) Store(k K, v V) {
16+
func (m *Map[K, V]) Store(k K, v V) {
1717
m.m.Store(k, v)
1818
}
1919

2020
//nolint:forcetypeassert
21-
func (m *SyncMap[K, V]) Load(key K) (value V, ok bool) {
21+
func (m *Map[K, V]) Load(key K) (value V, ok bool) {
2222
v, ok := m.m.Load(key)
2323
if !ok {
2424
var empty V
@@ -27,12 +27,12 @@ func (m *SyncMap[K, V]) Load(key K) (value V, ok bool) {
2727
return v.(V), ok
2828
}
2929

30-
func (m *SyncMap[K, V]) Delete(key K) {
30+
func (m *Map[K, V]) Delete(key K) {
3131
m.m.Delete(key)
3232
}
3333

3434
//nolint:forcetypeassert
35-
func (m *SyncMap[K, V]) LoadAndDelete(key K) (actual V, loaded bool) {
35+
func (m *Map[K, V]) LoadAndDelete(key K) (actual V, loaded bool) {
3636
act, loaded := m.m.LoadAndDelete(key)
3737
if !loaded {
3838
var empty V
@@ -42,7 +42,7 @@ func (m *SyncMap[K, V]) LoadAndDelete(key K) (actual V, loaded bool) {
4242
}
4343

4444
//nolint:forcetypeassert
45-
func (m *SyncMap[K, V]) LoadOrStore(key K, value V) (actual V, loaded bool) {
45+
func (m *Map[K, V]) LoadOrStore(key K, value V) (actual V, loaded bool) {
4646
act, loaded := m.m.LoadOrStore(key, value)
4747
if !loaded {
4848
var empty V
@@ -51,16 +51,16 @@ func (m *SyncMap[K, V]) LoadOrStore(key K, value V) (actual V, loaded bool) {
5151
return act.(V), loaded
5252
}
5353

54-
func (m *SyncMap[K, V]) CompareAndSwap(key K, old V, new V) bool {
54+
func (m *Map[K, V]) CompareAndSwap(key K, old V, new V) bool {
5555
return m.m.CompareAndSwap(key, old, new)
5656
}
5757

58-
func (m *SyncMap[K, V]) CompareAndDelete(key K, old V) (deleted bool) {
58+
func (m *Map[K, V]) CompareAndDelete(key K, old V) (deleted bool) {
5959
return m.m.CompareAndDelete(key, old)
6060
}
6161

6262
//nolint:forcetypeassert
63-
func (m *SyncMap[K, V]) Swap(key K, value V) (previous any, loaded bool) {
63+
func (m *Map[K, V]) Swap(key K, value V) (previous any, loaded bool) {
6464
previous, loaded = m.m.Swap(key, value)
6565
if !loaded {
6666
var empty V
@@ -70,7 +70,7 @@ func (m *SyncMap[K, V]) Swap(key K, value V) (previous any, loaded bool) {
7070
}
7171

7272
//nolint:forcetypeassert
73-
func (m *SyncMap[K, V]) Range(f func(key K, value V) bool) {
73+
func (m *Map[K, V]) Range(f func(key K, value V) bool) {
7474
m.m.Range(func(key, value interface{}) bool {
7575
return f(key.(K), value.(V))
7676
})

0 commit comments

Comments
 (0)