eval_user_query for OPA style results (#29)

Signed-off-by: Anand Krishnamoorthi <anakrish@microsoft.com>
This commit is contained in:
Anand Krishnamoorthi
2023-10-30 10:44:02 -07:00
committed by GitHub
parent 800e594d52
commit 63ecc44a48
8 changed files with 386 additions and 102 deletions
+30
View File
@@ -192,6 +192,10 @@ impl Value {
matches!(self, Value::Null)
}
pub fn is_empty_object(&self) -> bool {
self == &Value::new_object()
}
pub fn as_bool(&self) -> Result<&bool> {
match self {
Value::Bool(b) => Ok(b),
@@ -307,6 +311,32 @@ impl Value {
_ => bail!("internal error: make: not an selfect {self:?}"),
}
}
pub fn merge(&mut self, mut new: Value) -> Result<()> {
match (self, &mut new) {
(v @ Value::Undefined, _) => *v = new,
(Value::Set(ref mut set), Value::Set(new)) => {
Rc::make_mut(set).append(Rc::make_mut(new))
}
(Value::Object(map), Value::Object(new)) => {
for (k, v) in new.iter() {
match map.get(k) {
Some(pv) if *pv != *v => {
bail!(
"value for key `{}` generated multiple times: `{}` and `{}`",
serde_json::to_string_pretty(&k)?,
serde_json::to_string_pretty(&pv)?,
serde_json::to_string_pretty(&v)?,
)
}
_ => Rc::make_mut(map).insert(k.clone(), v.clone()),
};
}
}
_ => bail!("internal error: could not merge value"),
};
Ok(())
}
}
impl ops::Index<usize> for Value {
type Output = Value;