Skip to content

Commit

Permalink
Allowing all characters in schema names.
Browse files Browse the repository at this point in the history
Closes #143.
  • Loading branch information
ecton committed Jan 22, 2022
1 parent 798b539 commit aad8650
Show file tree
Hide file tree
Showing 38 changed files with 398 additions and 290 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Expand Up @@ -163,6 +163,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
`reduce()`, `version()`, and `unique()` have moved. If you're using a
`CollectionView`, the implementation should now be a combination of `View` and
`CollectionViewSchema`.
- `CollectionName`, `SchemaName`, and `Name` all no longer generate errors if
using invalid characters. When BonsaiDb needs to use these names in a context
that must be able to be parsed, the names are encoded automatically into a
safe format. This change also means that `View::view_name()`,
`Collection::collection_name()`, and `Schema::schema_name()` have been updated
to not return error types.

### Fixed

Expand Down
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -27,7 +27,7 @@ struct Shape {
}
impl Collection for Shape {
fn collection_name() -> Result<CollectionName, InvalidNameError> {
fn collection_name() -> CollectionName {
CollectionName::new("khonsulabs", "shapes")
}
Expand All @@ -46,7 +46,7 @@ impl View for ShapesByNumberOfSides {
type Key = u32;
type Value = usize;
fn name(&self) -> Result<Name, InvalidNameError> {
fn name(&self) -> Name {
Name::new("by-number-of-sides")
}
}
Expand Down
4 changes: 2 additions & 2 deletions benchmarks/benches/collections/bonsai.rs
@@ -1,7 +1,7 @@
use bonsaidb::{
core::{
connection::Connection,
schema::{Collection, CollectionName, DefaultSerialization, InvalidNameError, Schematic},
schema::{Collection, CollectionName, DefaultSerialization, Schematic},
test_util::TestDirectory,
Error,
},
Expand All @@ -16,7 +16,7 @@ use ubyte::ToByteUnit;
use crate::collections::ResizableDocument;

impl Collection for ResizableDocument {
fn collection_name() -> Result<CollectionName, InvalidNameError> {
fn collection_name() -> CollectionName {
CollectionName::new("khonsulabs", "resizable-docs")
}

Expand Down
20 changes: 10 additions & 10 deletions benchmarks/benches/commerce/bonsai.rs
Expand Up @@ -9,7 +9,7 @@ use bonsaidb::{
schema::{
view::map::Mappings, Collection, CollectionDocument, CollectionName,
CollectionViewSchema, DefaultSerialization, DefaultViewSerialization, InsertError,
InvalidNameError, Name, NamedCollection, ReduceResult, Schema, SchemaName, Schematic,
Name, NamedCollection, ReduceResult, Schema, SchemaName, Schematic,
SerializedCollection, View, ViewMapResult, ViewMappedValue,
},
transaction::{self, Transaction},
Expand Down Expand Up @@ -389,7 +389,7 @@ impl Operator<ReviewProduct> for BonsaiOperator {
}

impl Schema for Commerce {
fn schema_name() -> Result<SchemaName, InvalidNameError> {
fn schema_name() -> SchemaName {
SchemaName::new("benchmarks", "commerce")
}

Expand All @@ -405,7 +405,7 @@ impl Schema for Commerce {
}

impl Collection for Product {
fn collection_name() -> Result<CollectionName, InvalidNameError> {
fn collection_name() -> CollectionName {
CollectionName::new("benchmarks", "products")
}

Expand Down Expand Up @@ -438,7 +438,7 @@ impl View for ProductsByCategoryId {
type Key = u32;
type Value = u32;

fn name(&self) -> Result<Name, InvalidNameError> {
fn name(&self) -> Name {
Name::new("by-category")
}
}
Expand All @@ -465,7 +465,7 @@ impl NamedCollection for Product {
}

impl Collection for ProductReview {
fn collection_name() -> Result<CollectionName, InvalidNameError> {
fn collection_name() -> CollectionName {
CollectionName::new("benchmarks", "reviews")
}

Expand All @@ -485,7 +485,7 @@ impl View for ProductReviewsByProduct {
type Key = u32;
type Value = ProductRatings;

fn name(&self) -> Result<Name, InvalidNameError> {
fn name(&self) -> Name {
Name::new("by-product")
}
}
Expand Down Expand Up @@ -541,7 +541,7 @@ impl ProductRatings {
}

impl Collection for Category {
fn collection_name() -> Result<CollectionName, InvalidNameError> {
fn collection_name() -> CollectionName {
CollectionName::new("benchmarks", "categories")
}

Expand All @@ -553,7 +553,7 @@ impl Collection for Category {
impl DefaultSerialization for Category {}

impl Collection for Customer {
fn collection_name() -> Result<CollectionName, InvalidNameError> {
fn collection_name() -> CollectionName {
CollectionName::new("benchmarks", "customers")
}

Expand All @@ -565,7 +565,7 @@ impl Collection for Customer {
impl DefaultSerialization for Customer {}

impl Collection for Order {
fn collection_name() -> Result<CollectionName, InvalidNameError> {
fn collection_name() -> CollectionName {
CollectionName::new("benchmarks", "orders")
}

Expand All @@ -577,7 +577,7 @@ impl Collection for Order {
impl DefaultSerialization for Order {}

impl Collection for Cart {
fn collection_name() -> Result<CollectionName, InvalidNameError> {
fn collection_name() -> CollectionName {
CollectionName::new("benchmarks", "carts")
}

Expand Down
8 changes: 4 additions & 4 deletions book/book-examples/tests/view-example-enum.rs
Expand Up @@ -4,8 +4,8 @@ use bonsaidb::{
document::Document,
schema::{
view::{map::ViewMappedValue, EnumKey},
Collection, CollectionName, DefaultSerialization, DefaultViewSerialization,
InvalidNameError, Name, ReduceResult, View, ViewMapResult, ViewSchema,
Collection, CollectionName, DefaultSerialization, DefaultViewSerialization, Name,
ReduceResult, View, ViewMapResult, ViewSchema,
},
Error,
},
Expand Down Expand Up @@ -38,7 +38,7 @@ pub struct BlogPost {
// ANCHOR_END: struct

impl Collection for BlogPost {
fn collection_name() -> Result<CollectionName, InvalidNameError> {
fn collection_name() -> CollectionName {
CollectionName::new("view-example", "blog-post")
}

Expand All @@ -58,7 +58,7 @@ impl View for BlogPostsByCategory {
type Key = Option<Category>;
type Value = u32;

fn name(&self) -> Result<Name, InvalidNameError> {
fn name(&self) -> Name {
Name::new("by-category")
}
}
Expand Down
7 changes: 3 additions & 4 deletions book/book-examples/tests/view-example-string.rs
Expand Up @@ -4,8 +4,7 @@ use bonsaidb::{
document::Document,
schema::{
view::map::ViewMappedValue, Collection, CollectionName, DefaultSerialization,
DefaultViewSerialization, InvalidNameError, Name, ReduceResult, View, ViewMapResult,
ViewSchema,
DefaultViewSerialization, Name, ReduceResult, View, ViewMapResult, ViewSchema,
},
Error,
},
Expand All @@ -26,7 +25,7 @@ pub struct BlogPost {
// ANCHOR_END: struct

impl Collection for BlogPost {
fn collection_name() -> Result<CollectionName, InvalidNameError> {
fn collection_name() -> CollectionName {
CollectionName::new("view-example", "blog-post")
}

Expand All @@ -46,7 +45,7 @@ impl View for BlogPostsByCategory {
type Key = Option<String>;
type Value = u32;

fn name(&self) -> Result<Name, InvalidNameError> {
fn name(&self) -> Name {
Name::new("by-category")
}
}
Expand Down
18 changes: 9 additions & 9 deletions crates/bonsaidb-client/src/client/remote_database.rs
Expand Up @@ -63,7 +63,7 @@ impl<A: CustomApi> Connection for RemoteDatabase<A> {
.send_request(Request::Database {
database: self.name.to_string(),
request: DatabaseRequest::Get {
collection: C::collection_name()?,
collection: C::collection_name(),
id,
},
})
Expand All @@ -89,7 +89,7 @@ impl<A: CustomApi> Connection for RemoteDatabase<A> {
.send_request(Request::Database {
database: self.name.to_string(),
request: DatabaseRequest::GetMultiple {
collection: C::collection_name()?,
collection: C::collection_name(),
ids: ids.to_vec(),
},
})
Expand All @@ -114,7 +114,7 @@ impl<A: CustomApi> Connection for RemoteDatabase<A> {
.send_request(Request::Database {
database: self.name.to_string(),
request: DatabaseRequest::List {
collection: C::collection_name()?,
collection: C::collection_name(),
ids: ids.into(),
order,
limit,
Expand Down Expand Up @@ -149,7 +149,7 @@ impl<A: CustomApi> Connection for RemoteDatabase<A> {
.schema
.view::<V>()
.ok_or(bonsaidb_core::Error::CollectionNotFound)?
.view_name()?,
.view_name(),
key: key.map(|key| key.serialized()).transpose()?,
order,
limit,
Expand Down Expand Up @@ -190,7 +190,7 @@ impl<A: CustomApi> Connection for RemoteDatabase<A> {
.schema
.view::<V>()
.ok_or(bonsaidb_core::Error::CollectionNotFound)?
.view_name()?,
.view_name(),
key: key.map(|key| key.serialized()).transpose()?,
order,
limit,
Expand Down Expand Up @@ -229,7 +229,7 @@ impl<A: CustomApi> Connection for RemoteDatabase<A> {
.schema
.view::<V>()
.ok_or(bonsaidb_core::Error::CollectionNotFound)?
.view_name()?,
.view_name(),
key: key.map(|key| key.serialized()).transpose()?,
access_policy,
grouped: false,
Expand Down Expand Up @@ -265,7 +265,7 @@ impl<A: CustomApi> Connection for RemoteDatabase<A> {
.schema
.view::<V>()
.ok_or(bonsaidb_core::Error::CollectionNotFound)?
.view_name()?,
.view_name(),
key: key.map(|key| key.serialized()).transpose()?,
access_policy,
grouped: true,
Expand Down Expand Up @@ -310,7 +310,7 @@ impl<A: CustomApi> Connection for RemoteDatabase<A> {
.schema
.view::<V>()
.ok_or(bonsaidb_core::Error::CollectionNotFound)?
.view_name()?,
.view_name(),
key: key.map(|key| key.serialized()).transpose()?,
access_policy,
},
Expand Down Expand Up @@ -391,7 +391,7 @@ impl<A: CustomApi> Connection for RemoteDatabase<A> {
.send_request(Request::Database {
database: self.name.to_string(),
request: DatabaseRequest::CompactCollection {
name: C::collection_name()?,
name: C::collection_name(),
},
})
.await?
Expand Down
6 changes: 3 additions & 3 deletions crates/bonsaidb-core/src/admin/database.rs
Expand Up @@ -3,8 +3,8 @@ use serde::{Deserialize, Serialize};
use crate::{
define_basic_unique_mapped_view,
schema::{
Collection, CollectionDocument, CollectionName, DefaultSerialization, InvalidNameError,
NamedCollection, SchemaName, Schematic,
Collection, CollectionDocument, CollectionName, DefaultSerialization, NamedCollection,
SchemaName, Schematic,
},
Error,
};
Expand All @@ -19,7 +19,7 @@ pub struct Database {
}

impl Collection for Database {
fn collection_name() -> Result<CollectionName, InvalidNameError> {
fn collection_name() -> CollectionName {
CollectionName::new("bonsaidb", "databases")
}

Expand Down
6 changes: 3 additions & 3 deletions crates/bonsaidb-core/src/admin/group.rs
Expand Up @@ -5,8 +5,8 @@ use crate::{
define_basic_unique_mapped_view,
permissions::Statement,
schema::{
Collection, CollectionDocument, CollectionName, DefaultSerialization, InvalidNameError,
NamedCollection, Schematic,
Collection, CollectionDocument, CollectionName, DefaultSerialization, NamedCollection,
Schematic,
},
Error,
};
Expand Down Expand Up @@ -38,7 +38,7 @@ impl PermissionGroup {

#[async_trait]
impl Collection for PermissionGroup {
fn collection_name() -> Result<CollectionName, InvalidNameError> {
fn collection_name() -> CollectionName {
CollectionName::new("khonsulabs", "permission-group")
}

Expand Down
4 changes: 2 additions & 2 deletions crates/bonsaidb-core/src/admin/mod.rs
@@ -1,5 +1,5 @@
use crate::{
schema::{InvalidNameError, Schema, SchemaName, Schematic},
schema::{Schema, SchemaName, Schematic},
Error,
};

Expand Down Expand Up @@ -27,7 +27,7 @@ pub use self::{group::PermissionGroup, role::Role, user::User};
pub struct Admin;

impl Schema for Admin {
fn schema_name() -> Result<SchemaName, InvalidNameError> {
fn schema_name() -> SchemaName {
SchemaName::new("khonsulabs", "bonsaidb-admin")
}

Expand Down
7 changes: 3 additions & 4 deletions crates/bonsaidb-core/src/admin/password_config.rs
Expand Up @@ -9,8 +9,7 @@ use crate::{
password_config,
schema::{
view::{DefaultViewSerialization, ViewSchema},
Collection, CollectionName, DefaultSerialization, InvalidNameError, Name, View,
ViewMapResult,
Collection, CollectionName, DefaultSerialization, Name, View, ViewMapResult,
},
};

Expand Down Expand Up @@ -66,7 +65,7 @@ impl Deref for PasswordConfig {
}

impl Collection for PasswordConfig {
fn collection_name() -> Result<crate::schema::CollectionName, InvalidNameError> {
fn collection_name() -> CollectionName {
CollectionName::new("khonsulabs", "password-config")
}

Expand All @@ -86,7 +85,7 @@ impl View for Singleton {
type Key = ();
type Value = ();

fn name(&self) -> Result<Name, InvalidNameError> {
fn name(&self) -> Name {
Name::new("singleton")
}
}
Expand Down
6 changes: 3 additions & 3 deletions crates/bonsaidb-core/src/admin/role.rs
Expand Up @@ -3,8 +3,8 @@ use serde::{Deserialize, Serialize};
use crate::{
define_basic_unique_mapped_view,
schema::{
Collection, CollectionDocument, CollectionName, DefaultSerialization, InvalidNameError,
NamedCollection, Schematic,
Collection, CollectionDocument, CollectionName, DefaultSerialization, NamedCollection,
Schematic,
},
Error,
};
Expand Down Expand Up @@ -35,7 +35,7 @@ impl Role {
}

impl Collection for Role {
fn collection_name() -> Result<CollectionName, InvalidNameError> {
fn collection_name() -> CollectionName {
CollectionName::new("khonsulabs", "role")
}

Expand Down

0 comments on commit aad8650

Please sign in to comment.