From 8fe575768807152fe40e36df7b00140c56b66ad3 Mon Sep 17 00:00:00 2001 From: Tobie Morgan Hitchcock Date: Sat, 31 Dec 2022 19:00:32 +0000 Subject: [PATCH] Ensure non-defined array fields are removed from records Related to #1341 --- lib/src/sql/value/every.rs | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/lib/src/sql/value/every.rs b/lib/src/sql/value/every.rs index 2d037f07..75a1341a 100644 --- a/lib/src/sql/value/every.rs +++ b/lib/src/sql/value/every.rs @@ -30,13 +30,11 @@ impl Value { // Current path part is an array Value::Array(v) => match arrays { // Let's log all individual array items - true => v - .iter() - .enumerate() - .flat_map(|(i, v)| { + true => std::iter::once(prev.clone()) + .chain(v.iter().enumerate().rev().flat_map(|(i, v)| { let p = Part::from(i.to_owned()); v._every(steps, arrays, prev.clone().push(p)) - }) + })) .collect::>(), // Let's not log individual array items false => vec![prev], @@ -65,12 +63,15 @@ mod tests { fn every_including_array_indexes() { let val = Value::parse("{ test: { something: [{ age: 34, tags: ['code', 'databases'] }, { age: 36, tags: ['design', 'operations'] }] } }"); let res = vec![ - Idiom::parse("test.something[0].age"), - Idiom::parse("test.something[0].tags[0]"), - Idiom::parse("test.something[0].tags[1]"), + Idiom::parse("test.something"), Idiom::parse("test.something[1].age"), - Idiom::parse("test.something[1].tags[0]"), + Idiom::parse("test.something[1].tags"), Idiom::parse("test.something[1].tags[1]"), + Idiom::parse("test.something[1].tags[0]"), + Idiom::parse("test.something[0].age"), + Idiom::parse("test.something[0].tags"), + Idiom::parse("test.something[0].tags[1]"), + Idiom::parse("test.something[0].tags[0]"), ]; assert_eq!(res, val.every(false, true)); } @@ -87,14 +88,17 @@ mod tests { let val = Value::parse("{ test: { something: [{ age: 34, tags: ['code', 'databases'] }, { age: 36, tags: ['design', 'operations'] }] } }"); let res = vec![ Idiom::parse("test"), - Idiom::parse("test.something[0]"), - Idiom::parse("test.something[0].age"), - Idiom::parse("test.something[0].tags[0]"), - Idiom::parse("test.something[0].tags[1]"), + Idiom::parse("test.something"), Idiom::parse("test.something[1]"), Idiom::parse("test.something[1].age"), - Idiom::parse("test.something[1].tags[0]"), + Idiom::parse("test.something[1].tags"), Idiom::parse("test.something[1].tags[1]"), + Idiom::parse("test.something[1].tags[0]"), + Idiom::parse("test.something[0]"), + Idiom::parse("test.something[0].age"), + Idiom::parse("test.something[0].tags"), + Idiom::parse("test.something[0].tags[1]"), + Idiom::parse("test.something[0].tags[0]"), ]; assert_eq!(res, val.every(true, true)); }