Skip to content

Commit 1607755

Browse files
committed
Added ability to describe keys
After implementing khonsulabs#278, I had the idea that a generic schema browser could benefit from knowing information about the Keys used in BonsaiDb. Since almost every user will either use basic primary keys which BonsaiDb implements Key for or will use the `Key` derive macro, I realized this functionality could encode rich information about most `KeyEncoding` implementations. This process also includes the ability to annotate extra information. For example, the LimitedResolutionTimestamp encodes the epoch information as an attribute, which would allow an editor to show the encoded timestame as a date/time in the correct time zone to the user.
1 parent 33229ca commit 1607755

File tree

14 files changed

+827
-76
lines changed

14 files changed

+827
-76
lines changed

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
101101
[#240]: https://github.com/khonsulabs/bonsaidb/issues/240
102102
[#254]: https://github.com/khonsulabs/bonsaidb/issues/254
103103

104+
- `KeyEncoding::describe` is a new function that allows a key encoder to
105+
document the data contained within the encoded representation. The goal of
106+
this is to allow tools to generically operate on key types used within
107+
BonsaiDb. This function is automatically implemented when using the `Key`
108+
derive.
109+
110+
The new `KeyDescription` type uses this function to create a nested
111+
representation of the contained information.
112+
113+
Types that utilize a custom encoding format can use `KeyDescription::Other` to
114+
uniquely identify the key.
115+
104116
- `Schematic` has had several methods changed to `impl Iterator` of the original
105117
type being returned to avoid extra unnecessary allocations. These methods are:
106118

crates/bonsaidb-core/src/connection.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use crate::admin::{Role, User};
1717
use crate::document::{
1818
CollectionDocument, CollectionHeader, Document, HasHeader, Header, OwnedDocument,
1919
};
20-
use crate::key::{ByteSource, IntoPrefixRange, Key, KeyEncoding};
20+
use crate::key::{ByteSource, IntoPrefixRange, Key, KeyEncoding, KeyKind, KeyVisitor};
2121
use crate::permissions::Permissions;
2222
use crate::schema::view::map::MappedDocuments;
2323
use crate::schema::{
@@ -3406,6 +3406,13 @@ impl<'k> KeyEncoding<'k, Self> for SensitiveString {
34063406

34073407
const LENGTH: Option<usize> = None;
34083408

3409+
fn describe<Visitor>(visitor: &mut Visitor)
3410+
where
3411+
Visitor: KeyVisitor,
3412+
{
3413+
visitor.visit_type(KeyKind::String);
3414+
}
3415+
34093416
fn as_ord_bytes(&'k self) -> Result<std::borrow::Cow<'k, [u8]>, Self::Error> {
34103417
self.0.as_ord_bytes()
34113418
}
@@ -3463,6 +3470,13 @@ impl<'k> KeyEncoding<'k, Self> for SensitiveBytes {
34633470

34643471
const LENGTH: Option<usize> = None;
34653472

3473+
fn describe<Visitor>(visitor: &mut Visitor)
3474+
where
3475+
Visitor: KeyVisitor,
3476+
{
3477+
visitor.visit_type(KeyKind::Bytes);
3478+
}
3479+
34663480
fn as_ord_bytes(&'k self) -> Result<std::borrow::Cow<'k, [u8]>, Self::Error> {
34673481
self.0.as_ord_bytes()
34683482
}

crates/bonsaidb-core/src/document/id.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use serde::de::Visitor;
1010
use serde::{Deserialize, Serialize};
1111
use tinyvec::{Array, TinyVec};
1212

13-
use crate::key::{ByteSource, Key, KeyEncoding};
13+
use crate::key::{ByteSource, Key, KeyEncoding, KeyKind, KeyVisitor};
1414

1515
/// The serialized representation of a document's unique ID.
1616
#[derive(Default, Ord, Hash, Eq, PartialEq, PartialOrd, Clone)]
@@ -319,6 +319,13 @@ where
319319

320320
const LENGTH: Option<usize> = None;
321321

322+
fn describe<Visitor>(visitor: &mut Visitor)
323+
where
324+
Visitor: KeyVisitor,
325+
{
326+
visitor.visit_type(KeyKind::Bytes);
327+
}
328+
322329
fn as_ord_bytes(&'k self) -> Result<Cow<'k, [u8]>, Self::Error> {
323330
Ok(Cow::Borrowed(self))
324331
}

0 commit comments

Comments
 (0)