Enable support for dynamic expressions in record IDs

This commit is contained in:
Tobie Morgan Hitchcock 2022-09-04 10:52:01 +01:00
parent 0e2b134de2
commit 82e347027d
2 changed files with 32 additions and 0 deletions

View file

@ -1,8 +1,13 @@
use crate::ctx::Context;
use crate::dbs::Options;
use crate::dbs::Transaction;
use crate::err::Error;
use crate::sql::error::IResult;
use crate::sql::escape::escape_id;
use crate::sql::id::{id, Id};
use crate::sql::ident::ident_raw;
use crate::sql::serde::is_internal_serialization;
use crate::sql::value::Value;
use derive::Store;
use nom::branch::alt;
use nom::character::complete::char;
@ -58,6 +63,32 @@ impl fmt::Display for Thing {
}
}
impl Thing {
pub(crate) async fn compute(
&self,
ctx: &Context<'_>,
opt: &Options,
txn: &Transaction,
doc: Option<&Value>,
) -> Result<Value, Error> {
Ok(Value::Thing(Thing {
tb: self.tb.clone(),
id: match &self.id {
Id::Number(v) => Id::Number(*v),
Id::String(v) => Id::String(v.clone()),
Id::Object(v) => match v.compute(ctx, opt, txn, doc).await? {
Value::Object(v) => Id::Object(v),
_ => unreachable!(),
},
Id::Array(v) => match v.compute(ctx, opt, txn, doc).await? {
Value::Array(v) => Id::Array(v),
_ => unreachable!(),
},
},
}))
}
}
impl Serialize for Thing {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where

View file

@ -1115,6 +1115,7 @@ impl Value {
Value::Null => Ok(Value::Null),
Value::True => Ok(Value::True),
Value::False => Ok(Value::False),
Value::Thing(v) => v.compute(ctx, opt, txn, doc).await,
Value::Param(v) => v.compute(ctx, opt, txn, doc).await,
Value::Idiom(v) => v.compute(ctx, opt, txn, doc).await,
Value::Array(v) => v.compute(ctx, opt, txn, doc).await,