-
Notifications
You must be signed in to change notification settings - Fork 1
refactor app context to use composition #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| package app | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "github.com/google/uuid" | ||
| ) | ||
|
|
||
| type ContextKey string | ||
|
|
||
| const ( | ||
| TraceIDKey ContextKey = "trace_id" | ||
Talento90 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| UserIDKey ContextKey = "user_id" | ||
| TenantIDKey ContextKey = "tenant_id" | ||
| ) | ||
|
|
||
| // Context carries the context of the current execution. | ||
| type Context struct { | ||
| // original context | ||
| context.Context | ||
| } | ||
|
|
||
| // UserID returns the user id | ||
| func (sc *Context) UserID() (string, bool) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unsure why you return the bool about the presence, you are not doing it for TraceID
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The idea is that |
||
| userID := sc.Context.Value(UserIDKey) | ||
|
|
||
| if id, ok := userID.(string); ok { | ||
| return id, ok | ||
Talento90 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| return "", false | ||
| } | ||
|
|
||
| // SetUserID sets the user id | ||
| func (sc *Context) SetUserID(userID string) *Context { | ||
| sc.Context = context.WithValue(sc.Context, UserIDKey, userID) | ||
|
|
||
| return sc | ||
| } | ||
|
|
||
| // TenantID returns the tenant id | ||
| func (sc *Context) TenantID() (string, bool) { | ||
| tenantIDKey := sc.Context.Value(TenantIDKey) | ||
|
|
||
| if id, ok := tenantIDKey.(string); ok { | ||
| return id, true | ||
| } | ||
|
|
||
| return "", false | ||
| } | ||
|
|
||
| // SetTenantID sets the user id | ||
| func (sc *Context) SetTenantID(tenantID string) *Context { | ||
| sc.Context = context.WithValue(sc.Context, TenantIDKey, tenantID) | ||
|
|
||
| return sc | ||
| } | ||
|
|
||
| // SetTraceID sets the trace id | ||
| func (sc *Context) SetTraceID(traceID string) *Context { | ||
| sc.Context = context.WithValue(sc.Context, TraceIDKey, traceID) | ||
|
|
||
| return sc | ||
| } | ||
|
|
||
| // TraceID returns the trace identifier for the current flow | ||
| func (sc *Context) TraceID() string { | ||
| traceID := sc.Context.Value(TraceIDKey) | ||
|
|
||
| if id, ok := traceID.(string); ok { | ||
| return id | ||
| } | ||
|
|
||
| return "" | ||
| } | ||
|
|
||
| // FromContext returns a new Context from a context.Context | ||
| func FromContext(ctx context.Context) Context { | ||
| appCtx := NewContext(ctx) | ||
|
|
||
| if traceID, ok := ctx.Value(TraceIDKey).(string); ok { | ||
Talento90 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| appCtx.SetTraceID(traceID) | ||
| } | ||
|
|
||
| if userID, ok := ctx.Value(UserIDKey).(string); ok { | ||
| appCtx.SetUserID(userID) | ||
| } | ||
|
|
||
| if tenantID, ok := ctx.Value(TenantIDKey).(string); ok { | ||
| appCtx.SetTenantID(tenantID) | ||
| } | ||
|
|
||
| return appCtx | ||
| } | ||
|
|
||
| // NewContext returns a new Context | ||
| func NewContext(ctx context.Context) Context { | ||
| ctx = context.WithValue(ctx, TraceIDKey, uuid.NewString()) | ||
| return Context{Context: ctx} | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.