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
Add deriveable support for structs as View keys #106
Comments
As of #107, there are now private utility functions in core that handle converting a This issue is now purely an exercise in coming up with a well-designed proc macro that utilizes these functions to implement Key for an arbitrary structure or Enum. After we have built-in support for Enums, |
Maybe other things could also be derived like Maybe also Collection, but that would only be sensible for once without views etc. but at least for my use that would be really comftable :D. #[derive(Collection)]
#[collection_name = ["hoppi", "notification_subscription"]]
// or
#[collection_name = "hoppi.notification_subscription"] If you want I would look into implementing the macro. I have this a lot: impl Collection for SubscriptionInfoTable {
fn collection_name() -> Result<CollectionName, InvalidNameError> {
CollectionName::new("hoppi", "notification_subscription")
}
fn define_views(_schema: &mut Schematic) -> Result<(), bonsaidb::core::Error> {
Ok(())
}
}
impl DefaultSerialization for SubscriptionInfoTable {} |
Great suggestion, I've split it into its own issue to be tackled independently. |
If you write down the api and result, I'll implement the proc-macro. |
I realized that for #106, these functions would need to be public.
After the last commit, encode_composite_field and decode_composite_field have been exported. Thinking about it this morning, the implementation should be pretty straightforward:
The Key trait implementation should look similar to how [Tuples are implemented](https://github.com/khonsulabs/bonsaidb I'd imagine the derive syntax to look like this: #[derive(Key)]
struct MyCompositeKey {
a: String,
b: u64,
}
#[derive(Key)]
enum MyEnum {
A(String),
B{ id: u64 }
} Let me know if you're still wanting to tackle this @ModProg -- no rush as always! |
makes sense. We need to make sure that it is understood that the ordering is relevant right? For enums with assigned numbers we could use those I think. |
Yes, I figured I would end up writing that documentation.
Agreed, great idea. |
I just reread this, I see one todo that should probably be done asap as it would be breaking is the assigned numbers enums. Shouldn't be a big change, either. |
@ecton Would you be fine with these restrictions:
|
Great catch. I had a note to document this use case. By defaulting to u64, we end up with enums that are always 8 bytes wide. I think we should do this:
By encoding the default option using the I'm not sure how complex that will make your code -- currently Signed doesn't support math operations, so you'll have to do it as part of calling encode_composite_field. If making an artificial limitation of no u128 support makes the code significantly easier, I would be fine with that limitation. |
This request is a refinement of the idea from #65. View Keys need to be able to be compared on a byte-by-byte basis and resolve order, and the Key trait currently must be manually implemented for any non-integer types.
This should be done as a custom derive. The composite format should be lightweight and focus on the problems that multi-field comparisons might unveil -- e.g., a String key is variable length, and a shorter String should sort lower than a longer string, regardless of the bytes that follow in the next field.
The other thing a custom derive can do is offer the ability to support floating points. As far as I can come up with, there's not a reliable way to encode a floating point number such that it can be memcmp'ed. An attribute could be used to allow multiple strategies for utilizing a floating point key -- e.g., optionally multiply by 10^x then convert to a i64/u64.
The macro needs to be able to have the location of
bonsaidb_core
specified as an optional attribute. By default, it should use::bonsaidb::core
as the path to bonsaidb_core, but a user should be able to override it based on their usage. E.g.,#[view(core = bonsaidb_server::core)]
.The text was updated successfully, but these errors were encountered: