Skip to content
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

Global DefaultOptions #4

Merged
merged 14 commits into from
Jul 1, 2024
Merged

Global DefaultOptions #4

merged 14 commits into from
Jul 1, 2024

Conversation

syntaqx
Copy link
Owner

@syntaqx syntaqx commented Jul 1, 2024

The intent of this is to enable you to globally set DefaultOptions so that it's not strictly necessary to set them across usages of cookie.Set

Additionally, this should allow for use of signed to be true by default.

TODO

  • This requires PopulateFromCookies to be able to understand unsigned as well when the default value is signed.
  • Validate that the default options are the same as the net/http package

Open to feedback during early stages of this PR.

Copy link

codecov bot commented Jul 1, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Please upload report for BASE (main@03ac385). Learn more about missing BASE report.
Report is 3 commits behind head on main.

Current head f222e6a differs from pull request most recent head f120b07

Please upload reports for the commit f120b07 to get more accurate results.

Additional details and impacted files
@@           Coverage Diff            @@
##             main        #4   +/-   ##
========================================
  Coverage        ?   100.00%           
========================================
  Files           ?         1           
  Lines           ?       146           
  Branches        ?         0           
========================================
  Hits            ?       146           
  Misses          ?         0           
  Partials        ?         0           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@syntaqx syntaqx marked this pull request as ready for review July 1, 2024 05:18
@syntaqx
Copy link
Owner Author

syntaqx commented Jul 1, 2024

Leaving the pull request open for feedback for a while before merging!

@syntaqx syntaqx merged commit cd764ac into main Jul 1, 2024
4 checks passed
@syntaqx syntaqx deleted the default-options branch July 1, 2024 16:52
`cookie.DefaultOptions` variable:

```go
cookie.DefaultOptions = &cookie.Options{

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit afraid by such behavior. It could lead to race conditions if your lib is used by multiple libs.

I would recommend a pattern that will be immutable

Something like

mySignedCookieHelper := cookie.WithOptions( /* */)

mySignedCookieHelper.SetCookie("foo", "bar")

Also without such a thing, tests could become a nightmare as you are accessing sane global variable.

Otherwise, you will need to use mutex lock/unlock.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@syntaqx

What do you think about this?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How would this lead to race conditions?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cookie.WithOptions would return a soped variable and no global variable will be used.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, but that's an entire refactor of the package to introduce instanced cookie managers rather than a global one...

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not necessarily entirely against this, but it's a pretty massive change in direction.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. It's put of the scope of your change. But, unless if I'm wrong. Before this PR there was no global variable that could cause race condition.

I think it's a needed refactoring, but yes, it requires to change a lot of things.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I encourage you to check out #8 as I'm currently exploring what this might look like. I think the ideal of having a DefaultManager available so package-level functions continue to work is ideal, but all of this is why I'm still v0 of my package 😆

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do appreciate.

For records, I'm not even sure current implementation, so not what you are doing with #8, would have passed tests if you used go test -race ./...

@syntaqx syntaqx added documentation Improvements or additions to documentation enhancement New feature or request labels Jul 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants