Special cases of refs to data (#41)

1. Numeric indices will be converted to strings for refs beginning with `data`
   if there is not valid numeric key
2. Error out if input document already contains value for a ref

Signed-off-by: Anand Krishnamoorthi <anakrish@microsoft.com>
This commit is contained in:
Anand Krishnamoorthi
2023-11-11 00:15:56 -08:00
committed by GitHub
parent 02c6c6b827
commit 1c492144b3
6 changed files with 164 additions and 87 deletions
+13 -5
View File
@@ -17,19 +17,17 @@ pub fn get_path_string(refr: &Expr, document: Option<&str>) -> Result<String> {
comps.push(&field.text());
expr = Some(refr);
}
Some(Expr::RefBrack { refr, index, .. })
if matches!(index.as_ref(), Expr::String(_)) =>
{
Some(Expr::RefBrack { refr, index, .. }) => {
if let Expr::String(s) = index.as_ref() {
comps.push(&s.text());
expr = Some(refr);
}
expr = Some(refr);
}
Some(Expr::Var(v)) => {
comps.push(&v.text());
expr = None;
}
_ => bail!("internal error: not a simple ref"),
_ => bail!("internal error: not a simple ref {expr:?}"),
}
}
if let Some(d) = document {
@@ -92,3 +90,13 @@ pub fn gather_functions<'a>(modules: &[&'a Module]) -> Result<FunctionTable<'a>>
}
Ok(table)
}
pub fn get_root_var(mut expr: &Expr) -> Result<&str> {
loop {
match expr {
Expr::Var(v) => return Ok(*v.text()),
Expr::RefDot { refr, .. } | Expr::RefBrack { refr, .. } => expr = refr,
_ => bail!("internal error: analyzer: could not get rule prefix"),
}
}
}