-
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
Conversation
Welcome to Codecov 🎉Once you merge this PR into your default branch, you're all set! Codecov will compare coverage reports and display results in all future pull requests. Thanks for integrating Codecov - We've got you covered ☂️ |
appcontext/app_context.go
Outdated
| type AppContext struct { | ||
| // Context for cancellation | ||
| ctx context.Context | ||
| type Context interface { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't get why you need an interface here ?
I mean you should explain it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
because context.Context is an interface
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To be honest, it does need to be an interface. I have updated it to be a simple struct. app context.Context.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The other option is to add a static method where you must pass the context every time.
appcontext.SetUser(parentCtx, "userId")
userId, ok := appcontext.GetUser(parentCtx)
``
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's what we did at work
|
It sounds better. Now, next remark You have package apperror with Error I would like to suggest you to move both in package app, this way you have no "appwhatever.Whatever" the repetition seems strange to me |
That's a good point but the other end is that
|
|
Having 2 files in the same packages is not a big issue :P |
|
Here is a dump of the error reported by golangci-lint (with a lot of linters enabled) |
|
I have merged the Which linters are you using? I can see most errors are about "british" vs "american" english. |
I pushed #7 I didn't notice you were already using golangci and govulneck, I have never thought about running them directly via go run. Very interesting |
c2fbf52 to
90867bc
Compare
| } | ||
|
|
||
| // UserID returns the user id | ||
| func (sc *Context) UserID() (string, bool) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The 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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The idea is that TraceID() always exist, so I set it in the New() if it is absent. To allow debugging easily.
| func NewContext(ctx context.Context) Context { | ||
| appCtx := Context{Context: ctx} | ||
|
|
||
| if _, ok := ctx.Value(TraceIDKey).(string); !ok { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would have called TraceID, and define one if the if was == "", my problem is that you somehow define two places where you check if you have a context.
This comment is made on your current code.
I think you could also consider to do not play/check/set with traceid in app.NewContext, but to define one when none is present in GetTraceID.
It would also fix an issue that someone with your current GetTraceID implementation.
Someone could define a context like this
ctx : = app.Context{Context: context.Background()} // possible as app.Context cannot be nothing but Exported
traceID := ctx.TraceID() // this would be emptyThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you never call the TraceID() and pass the context as a value instead of a reference, the TraceID value will be mismatched.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed, but the points I raised are still valid, no?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I added in the constructor and getter but it does not feel right 🤔
app/error.go
Outdated
| ErrorConflict ErrorType = "conflict" | ||
| // Timeout error | ||
| ErrorTimeout ErrorType = "timeout" | ||
| // Cancelled error |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You forgot to update the comment
It should be ErrorCancelled
While you did it for severity
|
LGTM thanks |
Description
Refactor
appcontext.Contextto use the composition pattern to include the nativecontext.Context.