From ac62a5d595cd920c395c85a8751ee6fcbec93c54 Mon Sep 17 00:00:00 2001 From: Trevor McMaster Date: Fri, 22 Sep 2023 11:35:10 -0600 Subject: [PATCH] config-updater updates - Added fix for empty strings that are parsed as templates with no pieces - Added initial code to allow parsing of Segments with rest into multiple segments --- lib/config/src/configv1/select_parser.rs | 45 ++++++++++++++---------- 1 file changed, 26 insertions(+), 19 deletions(-) diff --git a/lib/config/src/configv1/select_parser.rs b/lib/config/src/configv1/select_parser.rs index 514f4afa..91185266 100644 --- a/lib/config/src/configv1/select_parser.rs +++ b/lib/config/src/configv1/select_parser.rs @@ -1324,13 +1324,18 @@ pub struct Template { impl std::fmt::Display for Template { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - let pieces: Vec = self - .pieces - .clone() - .into_iter() - .map(|piece| format!("{piece}")) - .collect(); - write!(f, "{}", pieces.join("")) + // Empty strings get turned into empty Templates with no pieces + if self.pieces.len() == 0 { + write!(f, "\"\"") + } else { + let pieces: Vec = self + .pieces + .clone() + .into_iter() + .map(|piece| format!("{piece}")) + .collect(); + write!(f, "{}", pieces.join("")) + } } } @@ -2716,22 +2721,22 @@ pub mod template_convert { pub fn dump(self) -> Vec { self.pieces .into_iter() - .map(|p| match p { - TemplatePiece::NotExpression(s) => Segment::Outer(s), + .flat_map(|p| match p.clone() { + TemplatePiece::NotExpression(s) => vec![Segment::Outer(s.to_owned())].into_iter(), TemplatePiece::Expression(e) => match e { ValueOrExpression::Value(Value::Json(j)) => { log::warn!("not sure what this is supposed to be for, so please update manually: {j:?}"); - Segment::Placeholder + vec![Segment::Placeholder].into_iter() }, ValueOrExpression::Value(Value::Path(p)) => match *p { Path { start: PathStart::Ident(s), rest, .. - } if rest.is_empty() => Segment::SingleSource(s), + } if rest.is_empty() => vec![Segment::SingleSource(s.to_owned())].into_iter(), other => { log::warn!("template value path {other:?} must be updated manually"); - Segment::Placeholder + vec![Segment::Placeholder].into_iter() } }, ValueOrExpression::Expression(Expression { @@ -2745,33 +2750,35 @@ pub mod template_convert { start: PathStart::Ident(s), rest, .. - } if rest.is_empty() => Segment::SingleSource(s), + } if rest.is_empty() => vec![Segment::SingleSource(s)].into_iter(), + // TODO: Handle !rest.is_empty() Path { start: PathStart::FunctionCall(f), rest, .. } if rest.is_empty() => { + // TODO: Handle !rest.is_empty() log::debug!("template expression function {f:?}"); if let FunctionCall::Collect(_) = f { log::warn!("template expression collect {f:?} must be updated manually"); - Segment::Placeholder + vec![Segment::Placeholder].into_iter() } else { - Segment::SingleExpression(f.to_convert()) + vec![Segment::SingleExpression(f.to_convert())].into_iter() } }, other => { - log::warn!("template expression path {other} must be updated manually"); - Segment::Placeholder + log::warn!("template expression path {other:?} must be updated manually"); + vec![Segment::Placeholder].into_iter() } } }, other => { log::warn!("template segment {other:?} must be updated manually"); - Segment::Placeholder + vec![Segment::Placeholder].into_iter() } }, }) - .collect() + .collect::>() } }