@@ -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
1733const (
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
2541func 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
3746func 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
4251func 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
5456func 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
5961func 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
6466func 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
7570func 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
8674func 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