Skip to content

Commit 3a868ca

Browse files
committed
refactor: convert ContextKey to generic lookup
1 parent 05874da commit 3a868ca

2 files changed

Lines changed: 32 additions & 49 deletions

File tree

internal/api/context.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ const (
3131
adminUserKey = contextKey("admin_user")
3232
oauthTokenKey = contextKey("oauth_token") // for OAuth1.0, also known as request token
3333
oauthVerifierKey = contextKey("oauth_verifier")
34-
ssoProviderKey = contextKey("sso_provider")
3534
externalHostKey = contextKey("external_host")
3635
oauthClientStateKey = contextKey("oauth_client_state_id")
3736
flowStateContextKey = contextKey("flow_state")
@@ -239,15 +238,11 @@ func getOAuthVerifier(ctx context.Context) string {
239238
}
240239

241240
func withSSOProvider(ctx context.Context, provider *models.SSOProvider) context.Context {
242-
return context.WithValue(ctx, ssoProviderKey, provider)
241+
return shared.WithSSOProvider(ctx, provider)
243242
}
244243

245244
func getSSOProvider(ctx context.Context) *models.SSOProvider {
246-
obj := ctx.Value(ssoProviderKey)
247-
if obj == nil {
248-
return nil
249-
}
250-
return obj.(*models.SSOProvider)
245+
return shared.GetSSOProvider(ctx)
251246
}
252247

253248
func withExternalHost(ctx context.Context, u *url.URL) context.Context {

internal/api/shared/context.go

Lines changed: 30 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -7,82 +7,70 @@ import (
77
)
88

99
// ContextKey is the type for context keys to avoid collisions
10-
type ContextKey string
10+
type ContextKey[T any] string
1111

12-
func (c ContextKey) String() string {
12+
func (c ContextKey[T]) String() string {
1313
return "gotrue api context key " + string(c)
1414
}
1515

16+
func (key ContextKey[T]) Get(ctx context.Context) T {
17+
var zero T
18+
if ctx == nil {
19+
return zero
20+
}
21+
obj := ctx.Value(key)
22+
if obj == nil {
23+
return zero
24+
}
25+
return obj.(T)
26+
}
27+
28+
func (key ContextKey[T]) With(ctx context.Context, t T) context.Context {
29+
return context.WithValue(ctx, key, t)
30+
}
31+
1632
// Context keys used across packages
1733
const (
18-
UserKey ContextKey = "user"
19-
SessionKey ContextKey = "session"
20-
OAuthServerClientKey ContextKey = "oauth_server_client"
21-
SSOProviderKey ContextKey = "sso_provider"
34+
UserKey ContextKey[*models.User] = "user"
35+
SessionKey ContextKey[*models.Session] = "session"
36+
OAuthServerClientKey ContextKey[*models.OAuthServerClient] = "oauth_server_client"
37+
SSOProviderKey ContextKey[*models.SSOProvider] = "sso_provider"
2238
)
2339

2440
// GetUser reads the user from the context - shared implementation
2541
func GetUser(ctx context.Context) *models.User {
26-
if ctx == nil {
27-
return nil
28-
}
29-
obj := ctx.Value(UserKey)
30-
if obj == nil {
31-
return nil
32-
}
33-
return obj.(*models.User)
42+
return UserKey.Get(ctx)
3443
}
3544

3645
// WithUser adds the user to the context - shared implementation
3746
func WithUser(ctx context.Context, u *models.User) context.Context {
38-
return context.WithValue(ctx, UserKey, u)
47+
return UserKey.With(ctx, u)
3948
}
4049

4150
// GetSession reads the session from the context - shared implementation
4251
func GetSession(ctx context.Context) *models.Session {
43-
if ctx == nil {
44-
return nil
45-
}
46-
obj := ctx.Value(SessionKey)
47-
if obj == nil {
48-
return nil
49-
}
50-
return obj.(*models.Session)
52+
return SessionKey.Get(ctx)
5153
}
5254

5355
// WithSession adds the session to the context - shared implementation
5456
func WithSession(ctx context.Context, s *models.Session) context.Context {
55-
return context.WithValue(ctx, SessionKey, s)
57+
return SessionKey.With(ctx, s)
5658
}
5759

5860
// WithOAuthServerClient adds an OAuth server client to the context
5961
func WithOAuthServerClient(ctx context.Context, client *models.OAuthServerClient) context.Context {
60-
return context.WithValue(ctx, OAuthServerClientKey, client)
62+
return OAuthServerClientKey.With(ctx, client)
6163
}
6264

6365
// GetOAuthServerClient retrieves an OAuth server client from the context
6466
func GetOAuthServerClient(ctx context.Context) *models.OAuthServerClient {
65-
if ctx == nil {
66-
return nil
67-
}
68-
obj := ctx.Value(OAuthServerClientKey)
69-
if obj == nil {
70-
return nil
71-
}
72-
return obj.(*models.OAuthServerClient)
67+
return OAuthServerClientKey.Get(ctx)
7368
}
7469

7570
func GetSSOProvider(ctx context.Context) *models.SSOProvider {
76-
if ctx == nil {
77-
return nil
78-
}
79-
obj := ctx.Value(SSOProviderKey)
80-
if obj == nil {
81-
return nil
82-
}
83-
return obj.(*models.SSOProvider)
71+
return SSOProviderKey.Get(ctx)
8472
}
8573

8674
func WithSSOProvider(ctx context.Context, s *models.SSOProvider) context.Context {
87-
return context.WithValue(ctx, SSOProviderKey, s)
75+
return SSOProviderKey.With(ctx, s)
8876
}

0 commit comments

Comments
 (0)