Consider a SessionBucket
trait to better enable strong typing
#2
maxcountryman
started this conversation in
Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
After announcing this crate there was a lengthy discussion regarding the strength of typing possible with key-value interfaces like
tower-sessions
(specifically we currently use an intermediate ofHashMap<String, serde_json::Value>
).To illustrate one approach to stronger typing, we have https://github.com/maxcountryman/tower-sessions/blob/main/examples/strongly-typed.rs.
Parts of this implementation could be encapsulated as a trait. For example,
SessionBucket
:Implementors would define their own
BucketData
and extractor and implementSessionBucket
for it. In our example above, we haveGuest
paired withGuestData
.I've used the word "bucket" here because part of what this enables is strongly typed buckets that live in the same session. In other words, you might define a set of several buckets, living alongside each other in the session. This is useful for name-spacing related data and ensuring it adheres to the user-defined structure. You might imagine an analytics bucket, a site preferences bucket, anonymous and authenticated user buckets, and so on.
It's unclear to me if this is something that should be brought into the crate directly.
Beta Was this translation helpful? Give feedback.
All reactions