fix: implement nil-safe Context methods on typhon.Request #176
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
While downstreaming a new
slog
feature to Monzo's internal monorepo, I discovered that the emptytyphon.Request
value panics when the variouscontext.Context
methods are called on it:This happens because
typhon.Request
embedscontext.Context
, and so the zero value oftyphon.Request
has a typed nil context. This PR adds wrappers for thecontext.Context
methods that behave likecontext.Background()
on the emptytyphon.Request
value instead.This is arguably not a bug: users of the library are expected to use either
typhon.NewRequest
ortyphon.NewRawRequest
to construct request values, these functions explicitly handle thenil
context case, and the library makes no promises about the validity of the zero value.Either way, we already have unit tests in our internal monorepo that build
typhon.Request
values manually and with the currenttyphon.Request
implementation those tests are unstable: introducing a new call to one of thecontext.Context
methods in a library (as I did in monzo/slog#22), can indirectly cause consumers of that library to panic. It's not difficult to add wrapper methods that work correctly for the emptytyphon.Request
value, so that's exactly what this PR does.