Idiom Optional Part (#4514)

This commit is contained in:
Micha de Vries 2024-08-16 17:53:28 +02:00 committed by GitHub
parent 4749af1ba4
commit 78b5f8f1d0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 54 additions and 10 deletions

View file

@ -152,7 +152,8 @@ impl<'a> KnnConditionRewriter<'a> {
| Part::Last | Part::Last
| Part::First | Part::First
| Part::Field(_) | Part::Field(_)
| Part::Index(_) => Some(p.clone()), | Part::Index(_)
| Part::Optional => Some(p.clone()),
Part::Where(v) => self.eval_value(v).map(Part::Where), Part::Where(v) => self.eval_value(v).map(Part::Where),
Part::Graph(_) => None, Part::Graph(_) => None,
Part::Value(v) => self.eval_value(v).map(Part::Value), Part::Value(v) => self.eval_value(v).map(Part::Value),

View file

@ -25,6 +25,7 @@ pub enum Part {
Method(#[serde(with = "no_nul_bytes")] String, Vec<Value>), Method(#[serde(with = "no_nul_bytes")] String, Vec<Value>),
#[revision(start = 2)] #[revision(start = 2)]
Destructure(Vec<DestructurePart>), Destructure(Vec<DestructurePart>),
Optional,
} }
impl From<i32> for Part { impl From<i32> for Part {
@ -128,6 +129,7 @@ impl fmt::Display for Part {
f.write_str(" }") f.write_str(" }")
} }
} }
Part::Optional => write!(f, "?"),
} }
} }
} }

View file

@ -106,29 +106,37 @@ impl Value {
stk.run(|stk| v.get(stk, ctx, opt, doc, path.next())).await stk.run(|stk| v.get(stk, ctx, opt, doc, path.next())).await
} }
Some(v) => stk.run(|stk| v.get(stk, ctx, opt, doc, path.next())).await, Some(v) => stk.run(|stk| v.get(stk, ctx, opt, doc, path.next())).await,
None => Ok(Value::None), None => {
stk.run(|stk| Value::None.get(stk, ctx, opt, doc, path.next())).await
}
}, },
Part::Graph(_) => match v.rid() { Part::Graph(_) => match v.rid() {
Some(v) => { Some(v) => {
let v = Value::Thing(v); let v = Value::Thing(v);
stk.run(|stk| v.get(stk, ctx, opt, doc, path)).await stk.run(|stk| v.get(stk, ctx, opt, doc, path)).await
} }
None => Ok(Value::None), None => {
stk.run(|stk| Value::None.get(stk, ctx, opt, doc, path.next())).await
}
}, },
Part::Field(f) => match v.get(f.as_str()) { Part::Field(f) => match v.get(f.as_str()) {
Some(v) => stk.run(|stk| v.get(stk, ctx, opt, doc, path.next())).await, Some(v) => stk.run(|stk| v.get(stk, ctx, opt, doc, path.next())).await,
None => Ok(Value::None), None => {
stk.run(|stk| Value::None.get(stk, ctx, opt, doc, path.next())).await
}
}, },
Part::Index(i) => match v.get(&i.to_string()) { Part::Index(i) => match v.get(&i.to_string()) {
Some(v) => stk.run(|stk| v.get(stk, ctx, opt, doc, path.next())).await, Some(v) => stk.run(|stk| v.get(stk, ctx, opt, doc, path.next())).await,
None => Ok(Value::None), None => {
stk.run(|stk| Value::None.get(stk, ctx, opt, doc, path.next())).await
}
}, },
Part::Value(x) => match stk.run(|stk| x.compute(stk, ctx, opt, doc)).await? { Part::Value(x) => match stk.run(|stk| x.compute(stk, ctx, opt, doc)).await? {
Value::Strand(f) => match v.get(f.as_str()) { Value::Strand(f) => match v.get(f.as_str()) {
Some(v) => stk.run(|stk| v.get(stk, ctx, opt, doc, path.next())).await, Some(v) => stk.run(|stk| v.get(stk, ctx, opt, doc, path.next())).await,
None => Ok(Value::None), None => Ok(Value::None),
}, },
_ => Ok(Value::None), _ => stk.run(|stk| Value::None.get(stk, ctx, opt, doc, path.next())).await,
}, },
Part::All => stk.run(|stk| self.get(stk, ctx, opt, doc, path.next())).await, Part::All => stk.run(|stk| self.get(stk, ctx, opt, doc, path.next())).await,
Part::Destructure(p) => { Part::Destructure(p) => {
@ -189,15 +197,21 @@ impl Value {
} }
Part::First => match v.first() { Part::First => match v.first() {
Some(v) => stk.run(|stk| v.get(stk, ctx, opt, doc, path.next())).await, Some(v) => stk.run(|stk| v.get(stk, ctx, opt, doc, path.next())).await,
None => Ok(Value::None), None => {
stk.run(|stk| Value::None.get(stk, ctx, opt, doc, path.next())).await
}
}, },
Part::Last => match v.last() { Part::Last => match v.last() {
Some(v) => stk.run(|stk| v.get(stk, ctx, opt, doc, path.next())).await, Some(v) => stk.run(|stk| v.get(stk, ctx, opt, doc, path.next())).await,
None => Ok(Value::None), None => {
stk.run(|stk| Value::None.get(stk, ctx, opt, doc, path.next())).await
}
}, },
Part::Index(i) => match v.get(i.to_usize()) { Part::Index(i) => match v.get(i.to_usize()) {
Some(v) => stk.run(|stk| v.get(stk, ctx, opt, doc, path.next())).await, Some(v) => stk.run(|stk| v.get(stk, ctx, opt, doc, path.next())).await,
None => Ok(Value::None), None => {
stk.run(|stk| Value::None.get(stk, ctx, opt, doc, path.next())).await
}
}, },
Part::Where(w) => { Part::Where(w) => {
let mut a = Vec::new(); let mut a = Vec::new();
@ -219,7 +233,7 @@ impl Value {
Some(v) => stk.run(|stk| v.get(stk, ctx, opt, doc, path.next())).await, Some(v) => stk.run(|stk| v.get(stk, ctx, opt, doc, path.next())).await,
None => Ok(Value::None), None => Ok(Value::None),
}, },
_ => Ok(Value::None), _ => stk.run(|stk| Value::None.get(stk, ctx, opt, doc, path.next())).await,
}, },
Part::Method(name, args) => { Part::Method(name, args) => {
let v = stk let v = stk
@ -345,6 +359,15 @@ impl Value {
} }
v => { v => {
match p { match p {
Part::Optional => match &self {
Value::None => Ok(Value::None),
_ => {
stk.run(|stk| {
Value::None.get(stk, ctx, opt, doc, path.next_method())
})
.await
}
},
Part::Flatten => { Part::Flatten => {
stk.run(|stk| v.get(stk, ctx, opt, None, path.next())).await stk.run(|stk| v.get(stk, ctx, opt, None, path.next())).await
} }

View file

@ -135,6 +135,10 @@ impl Parser<'_> {
let mut res = start; let mut res = start;
loop { loop {
match self.peek_kind() { match self.peek_kind() {
t!("?") => {
self.pop_peek();
res.push(Part::Optional);
}
t!("...") => { t!("...") => {
self.pop_peek(); self.pop_peek();
res.push(Part::Flatten); res.push(Part::Flatten);

14
lib/tests/idiom.rs Normal file
View file

@ -0,0 +1,14 @@
mod helpers;
mod parse;
use helpers::Test;
use surrealdb::err::Error;
#[tokio::test]
async fn idiom_chain_part_optional() -> Result<(), Error> {
let sql = r#"
{}.prop.is_bool();
{}.prop?.is_bool();
"#;
Test::new(sql).await?.expect_val("false")?.expect_val("None")?;
Ok(())
}