Skip to content

Conversation

@Talento90
Copy link
Owner

Description

Refactor appcontext.Context to use the composition pattern to include the native context.Context.

@codecov-commenter
Copy link

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 ☂️

type AppContext struct {
// Context for cancellation
ctx context.Context
type Context interface {
Copy link
Contributor

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

Copy link
Contributor

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

Copy link
Owner Author

@Talento90 Talento90 May 7, 2024

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.

Copy link
Owner Author

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)
``

Copy link
Contributor

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

@ccoVeille
Copy link
Contributor

It sounds better.

Now, next remark

You have

package apperror with Error
package appcontext with Context

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

@Talento90
Copy link
Owner Author

It sounds better.

Now, next remark

You have

package apperror with Error package appcontext with Context

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 app package will contain a lot of utilities. I will also need to refactor all names to be explicit.

app.NewError instead of apperror.New or app.NewNotFoundError instead of app.NewNotFound.

@ccoVeille
Copy link
Contributor

Having 2 files in the same packages is not a big issue :P

@ccoVeille
Copy link
Contributor

Here is a dump of the error reported by golangci-lint (with a lot of linters enabled)

golangci-lint-results.txt

@Talento90
Copy link
Owner Author

I have merged the appcontext and apperror into the app package.

Which linters are you using? I can see most errors are about "british" vs "american" english.

@ccoVeille
Copy link
Contributor

I have merged the appcontext and apperror into the app package.

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

@Talento90 Talento90 force-pushed the refactor-app-context branch from c2fbf52 to 90867bc Compare May 8, 2024 21:36
}

// UserID returns the user id
func (sc *Context) UserID() (string, bool) {
Copy link
Contributor

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

Copy link
Owner Author

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.

@Talento90 Talento90 requested a review from ccoVeille May 11, 2024 14:23
func NewContext(ctx context.Context) Context {
appCtx := Context{Context: ctx}

if _, ok := ctx.Value(TraceIDKey).(string); !ok {
Copy link
Contributor

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 empty

Copy link
Owner Author

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.

Copy link
Contributor

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?

Copy link
Owner Author

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
Copy link
Contributor

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

@ccoVeille
Copy link
Contributor

LGTM thanks

@Talento90 Talento90 merged commit f930705 into main May 14, 2024
@Talento90 Talento90 deleted the refactor-app-context branch May 14, 2024 10:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants