Implement SQL Split as a newtype tuple struct

This commit is contained in:
Tobie Morgan Hitchcock 2022-05-04 22:02:26 +01:00
parent b6cc3ca1ff
commit 643592d750
2 changed files with 15 additions and 32 deletions

View file

@ -165,7 +165,7 @@ impl Iterator {
// Loop over each value // Loop over each value
for obj in &res { for obj in &res {
// Get the value at the path // Get the value at the path
let val = obj.pick(&split.split); let val = obj.pick(split);
// Set the value at the path // Set the value at the path
match val { match val {
Value::Array(v) => { Value::Array(v) => {
@ -173,7 +173,7 @@ impl Iterator {
// Make a copy of object // Make a copy of object
let mut obj = obj.clone(); let mut obj = obj.clone();
// Set the value at the path // Set the value at the path
obj.set(ctx, opt, txn, &split.split, val).await?; obj.set(ctx, opt, txn, split, val).await?;
// Add the object to the results // Add the object to the results
self.results.push(obj); self.results.push(obj);
} }
@ -182,7 +182,7 @@ impl Iterator {
// Make a copy of object // Make a copy of object
let mut obj = obj.clone(); let mut obj = obj.clone();
// Set the value at the path // Set the value at the path
obj.set(ctx, opt, txn, &split.split, val).await?; obj.set(ctx, opt, txn, split, val).await?;
// Add the object to the results // Add the object to the results
self.results.push(obj); self.results.push(obj);
} }

View file

@ -48,13 +48,18 @@ impl fmt::Display for Splits {
} }
#[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd, Serialize, Deserialize)] #[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd, Serialize, Deserialize)]
pub struct Split { pub struct Split(pub Idiom);
pub split: Idiom,
impl Deref for Split {
type Target = Idiom;
fn deref(&self) -> &Self::Target {
&self.0
}
} }
impl fmt::Display for Split { impl fmt::Display for Split {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.split) write!(f, "{}", self.0)
} }
} }
@ -68,12 +73,7 @@ pub fn split(i: &str) -> IResult<&str, Splits> {
fn split_raw(i: &str) -> IResult<&str, Split> { fn split_raw(i: &str) -> IResult<&str, Split> {
let (i, v) = basic(i)?; let (i, v) = basic(i)?;
Ok(( Ok((i, Split(v)))
i,
Split {
split: v,
},
))
} }
#[cfg(test)] #[cfg(test)]
@ -88,12 +88,7 @@ mod tests {
let res = split(sql); let res = split(sql);
assert!(res.is_ok()); assert!(res.is_ok());
let out = res.unwrap().1; let out = res.unwrap().1;
assert_eq!( assert_eq!(out, Splits(vec![Split(Idiom::parse("field"))]),);
out,
Splits(vec![Split {
split: Idiom::parse("field")
}])
);
assert_eq!("SPLIT ON field", format!("{}", out)); assert_eq!("SPLIT ON field", format!("{}", out));
} }
@ -103,12 +98,7 @@ mod tests {
let res = split(sql); let res = split(sql);
assert!(res.is_ok()); assert!(res.is_ok());
let out = res.unwrap().1; let out = res.unwrap().1;
assert_eq!( assert_eq!(out, Splits(vec![Split(Idiom::parse("field"))]),);
out,
Splits(vec![Split {
split: Idiom::parse("field")
}])
);
assert_eq!("SPLIT ON field", format!("{}", out)); assert_eq!("SPLIT ON field", format!("{}", out));
} }
@ -120,14 +110,7 @@ mod tests {
let out = res.unwrap().1; let out = res.unwrap().1;
assert_eq!( assert_eq!(
out, out,
Splits(vec![ Splits(vec![Split(Idiom::parse("field")), Split(Idiom::parse("other.field")),])
Split {
split: Idiom::parse("field")
},
Split {
split: Idiom::parse("other.field")
},
])
); );
assert_eq!("SPLIT ON field, other.field", format!("{}", out)); assert_eq!("SPLIT ON field, other.field", format!("{}", out));
} }