Skip to content

Commit

Permalink
Add generics for the method is_unique_on
Browse files Browse the repository at this point in the history
  • Loading branch information
photino committed Dec 3, 2024
1 parent 69cab4c commit 048f6be
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 19 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Generated by Cargo
# will have compiled files and executables
/target/
/examples/**/target
**/target/*

# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ version = "0.1.0"
edition = "2021"

[dependencies]
zino = { version = "0.26", features = ["axum"] }
zino = { version = "0.27", features = ["axum"] }
```

```rust
Expand Down
28 changes: 26 additions & 2 deletions zino-core/benches/uuid_simd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@ pub fn bench(c: &mut criterion::Criterion) {
value.to_string()
})
});
c.bench_function("encode_uuid_v4", |b| {
b.iter(|| {
let value = Uuid::new_v4();
value.as_hyphenated().encode_lower(&mut [0; 36]).to_owned()
})
});
c.bench_function("encode_uuid_v7", |b| {
b.iter(|| {
let value = Uuid::now_v7();
value.as_hyphenated().encode_lower(&mut [0; 36]).to_owned()
})
});
c.bench_function("format_uuid_v4_simd", |b| {
b.iter(|| {
let value = Uuid::new_v4();
Expand All @@ -26,16 +38,28 @@ pub fn bench(c: &mut criterion::Criterion) {
value.format_hyphenated().to_string()
})
});
c.bench_function("parse_uuid", |b| {
c.bench_function("parse_uuid_v4", |b| {
b.iter(|| {
let text = "67e55044-10b1-426f-9247-bb680e5fe0c8";
text.parse::<Uuid>()
})
});
c.bench_function("parse_uuid_simd", |b| {
c.bench_function("parse_uuid_v4_simd", |b| {
b.iter(|| {
let text = "67e55044-10b1-426f-9247-bb680e5fe0c8";
Uuid::parse(text.as_bytes())
})
});
c.bench_function("parse_uuid_v7", |b| {
b.iter(|| {
let text = "01936dc6-e48c-7d22-8e69-b29f85682fac";
text.parse::<Uuid>()
})
});
c.bench_function("parse_uuid_v7_simd", |b| {
b.iter(|| {
let text = "01936dc6-e48c-7d22-8e69-b29f85682fac";
Uuid::parse(text.as_bytes())
})
});
}
18 changes: 12 additions & 6 deletions zino-core/src/orm/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1878,15 +1878,21 @@ pub trait Schema: 'static + Send + Sync + ModelHooks {
}

/// Returns `true` if the model is unique on the column values.
async fn is_unique_on(&self, columns: Vec<(&str, JsonValue)>) -> Result<bool, Error> {
async fn is_unique_on<C, T>(&self, columns: Vec<(C, T)>) -> Result<bool, Error>
where
C: AsRef<str>,
T: IntoSqlValue,
{
let primary_key_name = Self::PRIMARY_KEY_NAME;
let mut query = Query::default();
let mut fields = vec![primary_key_name];
for (field, value) in columns.into_iter() {
fields.push(field);
query.add_filter(field, value);
let mut fields = Vec::with_capacity(columns.len() + 1);
fields.push(primary_key_name.to_owned());
for (col, value) in columns.into_iter() {
let field = col.as_ref();
fields.push(field.to_owned());
query.add_filter(field, value.into_sql_value());
}
query.allow_fields(&fields);
query.set_fields(fields);
query.set_limit(2);

let data = Self::find::<Map>(&query).await?;
Expand Down
14 changes: 7 additions & 7 deletions zino-derive/src/model_accessor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub(super) fn parse_token_stream(input: DeriveInput) -> TokenStream {
let field_ident = format_ident!("{}", field);
fields.push(field);
quote! {
(#field, self.#field_ident.to_string().into())
(#field, self.#field_ident.to_string())
}
})
.collect::<Vec<_>>();
Expand Down Expand Up @@ -210,7 +210,7 @@ pub(super) fn parse_token_stream(input: DeriveInput) -> TokenStream {
field_constraints.push(quote! {
let value = self.#ident;
if !value.is_nil() {
let columns = vec![(#name, value.to_string().into())];
let columns = vec![(#name, value)];
if !self.is_unique_on(columns).await? {
let message = format!("the value `{value}` is not unique");
validation.record(#name, message);
Expand All @@ -221,7 +221,7 @@ pub(super) fn parse_token_stream(input: DeriveInput) -> TokenStream {
field_constraints.push(quote! {
let value = self.#ident.as_str();
if !value.is_empty() {
let columns = vec![(#name, value.into())];
let columns = vec![(#name, value)];
if !self.is_unique_on(columns).await? {
let message = format!("the value `{value}` is not unique");
validation.record(#name, message);
Expand All @@ -231,7 +231,7 @@ pub(super) fn parse_token_stream(input: DeriveInput) -> TokenStream {
} else if type_name == "Option<String>" {
field_constraints.push(quote! {
if let Some(value) = self.#ident.as_deref() && !value.is_empty() {
let columns = vec![(#name, value.into())];
let columns = vec![(#name, value)];
if !self.is_unique_on(columns).await? {
let message = format!("the value `{value}` is not unique");
validation.record(#name, message);
Expand All @@ -241,7 +241,7 @@ pub(super) fn parse_token_stream(input: DeriveInput) -> TokenStream {
} else if type_name == "Option<Uuid>" {
field_constraints.push(quote! {
if let Some(value) = self.#ident && !value.is_nil() {
let columns = vec![(#name, value.to_string().into())];
let columns = vec![(#name, value)];
if !self.is_unique_on(columns).await? {
let message = format!("the value `{value}` is not unique");
validation.record(#name, message);
Expand All @@ -251,7 +251,7 @@ pub(super) fn parse_token_stream(input: DeriveInput) -> TokenStream {
} else if parser::check_option_type(type_name) {
field_constraints.push(quote! {
if let Some(value) = self.#ident {
let columns = vec![(#name, value.into())];
let columns = vec![(#name, value)];
if !self.is_unique_on(columns).await? {
let message = format!("the value `{value}` is not unique");
validation.record(#name, message);
Expand All @@ -261,7 +261,7 @@ pub(super) fn parse_token_stream(input: DeriveInput) -> TokenStream {
} else {
field_constraints.push(quote! {
let value = self.#ident;
let columns = vec![(#name, value.into())];
let columns = vec![(#name, value)];
if !self.is_unique_on(columns).await? {
let message = format!("the value `{value}` is not unique");
validation.record(#name, message);
Expand Down
2 changes: 1 addition & 1 deletion zino/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ version = "0.1.0"
edition = "2021"

[dependencies]
zino = { version = "0.26", features = ["axum"] }
zino = { version = "0.27", features = ["axum"] }
```

```rust,ignore
Expand Down

0 comments on commit 048f6be

Please sign in to comment.