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
for obj in &res {
// Get the value at the path
let val = obj.pick(&split.split);
let val = obj.pick(split);
// Set the value at the path
match val {
Value::Array(v) => {
@ -173,7 +173,7 @@ impl Iterator {
// Make a copy of object
let mut obj = obj.clone();
// 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
self.results.push(obj);
}
@ -182,7 +182,7 @@ impl Iterator {
// Make a copy of object
let mut obj = obj.clone();
// 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
self.results.push(obj);
}

View file

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