aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/models/search.rs98
1 files changed, 84 insertions, 14 deletions
diff --git a/src/models/search.rs b/src/models/search.rs
index 5b2b928..c9e7e91 100644
--- a/src/models/search.rs
+++ b/src/models/search.rs
@@ -257,17 +257,20 @@ pub enum PropertyCondition {
Files(FilesCondition),
Relation(RelationCondition),
Formula(FormulaCondition),
- /// Returns pages when **any** of the filters inside the provided vector match.
- Or(Vec<PropertyCondition>),
- /// Returns pages when **all** of the filters inside the provided vector match.
- And(Vec<PropertyCondition>),
}
#[derive(Serialize, Debug, Eq, PartialEq, Clone)]
-pub struct FilterCondition {
- pub property: String,
- #[serde(flatten)]
- pub condition: PropertyCondition,
+#[serde(untagged)]
+pub enum FilterCondition {
+ Property {
+ property: String,
+ #[serde(flatten)]
+ condition: PropertyCondition,
+ },
+ /// Returns pages when **all** of the filters inside the provided vector match.
+ And { and: Vec<FilterCondition> },
+ /// Returns pages when **any** of the filters inside the provided vector match.
+ Or { or: Vec<FilterCondition> },
}
#[derive(Serialize, Debug, Eq, PartialEq, Hash, Copy, Clone)]
@@ -376,13 +379,15 @@ impl From<NotionSearch> for SearchRequest {
#[cfg(test)]
mod tests {
mod text_filters {
- use crate::models::search::PropertyCondition::RichText;
- use crate::models::search::{FilterCondition, TextCondition};
+ use crate::models::search::PropertyCondition::{Checkbox, Number, RichText, Select};
+ use crate::models::search::{
+ CheckboxCondition, FilterCondition, NumberCondition, SelectCondition, TextCondition,
+ };
use serde_json::json;
#[test]
fn text_property_equals() -> Result<(), Box<dyn std::error::Error>> {
- let json = serde_json::to_value(&FilterCondition {
+ let json = serde_json::to_value(&FilterCondition::Property {
property: "Name".to_string(),
condition: RichText(TextCondition::Equals("Test".to_string())),
})?;
@@ -396,7 +401,7 @@ mod tests {
#[test]
fn text_property_contains() -> Result<(), Box<dyn std::error::Error>> {
- let json = serde_json::to_value(&FilterCondition {
+ let json = serde_json::to_value(&FilterCondition::Property {
property: "Name".to_string(),
condition: RichText(TextCondition::Contains("Test".to_string())),
})?;
@@ -410,7 +415,7 @@ mod tests {
#[test]
fn text_property_is_empty() -> Result<(), Box<dyn std::error::Error>> {
- let json = serde_json::to_value(&FilterCondition {
+ let json = serde_json::to_value(&FilterCondition::Property {
property: "Name".to_string(),
condition: RichText(TextCondition::IsEmpty),
})?;
@@ -424,7 +429,7 @@ mod tests {
#[test]
fn text_property_is_not_empty() -> Result<(), Box<dyn std::error::Error>> {
- let json = serde_json::to_value(&FilterCondition {
+ let json = serde_json::to_value(&FilterCondition::Property {
property: "Name".to_string(),
condition: RichText(TextCondition::IsNotEmpty),
})?;
@@ -435,5 +440,70 @@ mod tests {
Ok(())
}
+
+ #[test]
+ fn compound_query_and() -> Result<(), Box<dyn std::error::Error>> {
+ let json = serde_json::to_value(&FilterCondition::And {
+ and: vec![
+ FilterCondition::Property {
+ property: "Seen".to_string(),
+ condition: Checkbox(CheckboxCondition::Equals(false)),
+ },
+ FilterCondition::Property {
+ property: "Yearly visitor count".to_string(),
+ condition: Number(NumberCondition::GreaterThan(serde_json::Number::from(
+ 1000000,
+ ))),
+ },
+ ],
+ })?;
+ assert_eq!(
+ dbg!(json),
+ json!({"and":[
+ {"property":"Seen","checkbox":{"equals":false}},
+ {"property":"Yearly visitor count","number":{"greater_than":1000000}}
+ ]})
+ );
+
+ Ok(())
+ }
+
+ #[test]
+ fn compound_query_or() -> Result<(), Box<dyn std::error::Error>> {
+ let json = serde_json::to_value(&FilterCondition::Or {
+ or: vec![
+ FilterCondition::Property {
+ property: "Description".to_string(),
+ condition: RichText(TextCondition::Contains("fish".to_string())),
+ },
+ FilterCondition::And {
+ and: vec![
+ FilterCondition::Property {
+ property: "Food group".to_string(),
+ condition: Select(SelectCondition::Equals(
+ "🥦Vegetable".to_string(),
+ )),
+ },
+ FilterCondition::Property {
+ property: "Is protein rich?".to_string(),
+ condition: Checkbox(CheckboxCondition::Equals(true)),
+ },
+ ],
+ },
+ ],
+ })?;
+ assert_eq!(
+ dbg!(json),
+ json!({"or":[
+ {"property":"Description","rich_text":{"contains":"fish"}},
+ {"and":[
+ {"property":"Food group","select":{"equals":"🥦Vegetable"}},
+ {"property":"Is protein rich?","checkbox":{"equals":true}}
+ ]}
+ ]})
+ );
+
+ Ok(())
+ }
}
}
height='13' alt='Gravatar' /> Steven 1-2/+2 These links make it easy to click and jump to another section. 2023-09-13docs(guide): fix expect assertion example in guide for `spyOn` (#5294)Gravatar wing 1-1/+1 Fixes example with `spyOn` and assertions. The example failed because the spied function would be called once but the expectation asserted 0 calls. 2023-09-13add uninstall instructions (#5311)Gravatar Andrew Brown 1-0/+24 it's always good to have uninstall instructions as well as install, and I couldn't find them anywhere else on the site. IMO, this gives users a little more confidence to try new tools out, as they know it's easily reversible. I'm not familiar with how to uninstall with Docker, so would appreciate some help there. We could possibly add a note for "bash" on how to remove Bun from the `$PATH`. 2023-09-13docs: update lockfile diff instructions (#5275)Gravatar Guilherme J. Tramontina 1-3/+7 2023-09-13fix(node/fetch): Make data URL fetch consistent with node (#5126)Gravatar David Hewitt 2-7/+37 2023-09-13avoid inserting extraneous"accept-encoding" header (#5057)Gravatar iidebyo 2-1/+22 * add no extraneous accept-encoding header test * ensure fetch honors no decompress opt * fix format on test/js/node/http/node-http.test.ts 2023-09-13docs(runtime): fix plugins loader extensions typo (#5250)Gravatar Zong 1-1/+1 2023-09-13chore: make comment grammatically correct (#5140)Gravatar Gareth Jones 1-1/+1 2023-09-13Add informative message on 'bun create react' (#5248)Gravatar Colin McDonnell 1-0/+22 2023-09-13docs(runtime): fix jsx FragmentFactory output example (#5243)Gravatar Zong 1-1/+1 2023-09-13file.exists() needs to be awaited to get the value (#5061)Gravatar amt8u 1-1/+1 2023-09-13Update discordjs.md (#5227)Gravatar Antonin CLAUZIER 1-1/+1 2023-09-13decode regex if needed (#5167)Gravatar Dylan Conway 5-126/+167 * decode regex if non-ascii * make it comptime * add test * use `bun.BabyList(u16)` 2023-09-13Correct the configuration file names. (#5234)Gravatar Nathan Hammond 2-2/+2 2023-09-12Update tsconfig.json for bun initGravatar Colin McDonnell 1-1/+1 2023-09-12js/node/stream.js: call write() callback when encoding is not provided (#4841)Gravatar cfal 2-3/+7 * js/node/stream.js: call write() callback when encoding is not provided * js/out/InternalModuleRegistryConstants.h: update 2023-09-12docs: Made bun-types install as dev dependency in example (#5120)Gravatar Gordon Goldbach 1-1/+1 2023-09-12Various docs (#5201)Gravatar Colin McDonnell 4-4/+147 * Updates * Improve jest guide * Improve 2023-09-12Use git's --global flag for lockfile diffs instead of manually modifying ↵Gravatar Southpaw 1-17/+7 config files (#5143) 2023-09-12docs: fix typo in import.meta.resolve (#5146)Gravatar Jonathan Neal 1-1/+1 2023-09-12Update hot.md (#4990)Gravatar Nazeel 1-1/+1 2023-09-12Update simple.md (#4997)Gravatar Tom Redman 1-1/+1 Remove errant slash preventing the correct console log 2023-09-12fix typo and grammar errors (#5046)Gravatar xnacly 1-3/+3 2023-09-12clang and llvm on arch install v16, update to use v15 (#5069)Gravatar mi4uu 1-1/+10 2023-09-12Add missing full stop on nodejs-apis.md (#5072)Gravatar Diogo Goncalves 1-5/+5 2023-09-12udate README.md (#5127)Gravatar Toby 1-1/+1 update path 2023-09-12docs: fix typos (#5151)Gravatar Samuel Rigaud 17-18/+18 2023-09-12fix lifecycle docu (#5159)Gravatar Thomas Rupprecht 1-4/+4 2023-09-12Clean up Modules docGravatar Colin McDonnell 1-66/+73