Skip to content

Commit

Permalink
assemble: add array information to inference
Browse files Browse the repository at this point in the history
Includes the "array" information in projection inference if it is available.
  • Loading branch information
williamhbaker committed Dec 13, 2024
1 parent 24a66c1 commit 85b34fe
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 12 deletions.
45 changes: 42 additions & 3 deletions crates/assemble/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,22 @@ pub fn inference(shape: &Shape, exists: Exists) -> flow::Inference {
} else {
None
},
array: None,
array: if shape.type_.overlaps(types::ARRAY) {
Some(flow::inference::Array {
min_items: shape.array.min_items,
has_max_items: shape.array.max_items.is_some(),
max_items: shape.array.max_items.unwrap_or_default(),
item_types: shape
.array
.tuple
.iter()
.chain(shape.array.additional_items.as_deref())
.fold(types::INVALID, |acc, item| acc | item.type_)
.to_vec(),
})
} else {
None
},
}
}

Expand Down Expand Up @@ -484,7 +499,7 @@ pub fn pb_datetime(t: &time::OffsetDateTime) -> pbjson_types::Timestamp {
#[cfg(test)]
mod test {
use super::*;
use doc::shape::{NumericShape, StringShape};
use doc::shape::{ArrayShape, NumericShape, StringShape};
use serde_json::{json, Value};
use std::collections::BTreeMap;

Expand All @@ -507,6 +522,28 @@ mod test {
minimum: None,
maximum: Some(json::Number::Unsigned(1000)),
},
array: ArrayShape {
additional_items: Some(Box::new(Shape {
type_: types::STRING,
..Shape::anything()
})),
min_items: 10,
max_items: Some(20),
tuple: vec![
Shape {
type_: types::STRING,
..Shape::anything()
},
Shape {
type_: types::BOOLEAN,
..Shape::anything()
},
Shape {
type_: types::OBJECT,
..Shape::anything()
},
],
},
..Shape::anything()
};

Expand All @@ -515,8 +552,10 @@ mod test {
let out2 = inference(&shape, Exists::May);
shape.type_ = types::INTEGER | types::STRING;
let out3 = inference(&shape, Exists::May);
shape.type_ = types::ARRAY;
let out4 = inference(&shape, Exists::May);

insta::assert_debug_snapshot!(&[out1, out2, out3]);
insta::assert_debug_snapshot!(&[out1, out2, out3, out4]);
}

#[test]
Expand Down
26 changes: 25 additions & 1 deletion crates/assemble/src/snapshots/assemble__test__inference.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
source: crates/assemble/src/lib.rs
expression: "&[out1, out2, out3]"
expression: "&[out1, out2, out3, out4]"
---
[
Inference {
Expand Down Expand Up @@ -65,4 +65,28 @@ expression: "&[out1, out2, out3]"
),
array: None,
},
Inference {
types: [
"array",
],
string: None,
title: "the title",
description: "the description",
default_json: "{\"hello\":\"world\"}",
secret: true,
exists: May,
numeric: None,
array: Some(
Array {
min_items: 10,
has_max_items: true,
max_items: 20,
item_types: [
"boolean",
"object",
"string",
],
},
),
},
]
Original file line number Diff line number Diff line change
Expand Up @@ -1271,7 +1271,16 @@ Outcome {
secret: false,
exists: Must,
numeric: None,
array: None,
array: Some(
Array {
min_items: 5,
has_max_items: false,
max_items: 0,
item_types: [
"object",
],
},
),
},
),
},
Expand Down Expand Up @@ -1740,7 +1749,16 @@ Outcome {
secret: false,
exists: Must,
numeric: None,
array: None,
array: Some(
Array {
min_items: 5,
has_max_items: false,
max_items: 0,
item_types: [
"object",
],
},
),
},
),
},
Expand Down Expand Up @@ -2001,7 +2019,16 @@ Outcome {
secret: false,
exists: Must,
numeric: None,
array: None,
array: Some(
Array {
min_items: 5,
has_max_items: false,
max_items: 0,
item_types: [
"object",
],
},
),
},
),
},
Expand Down Expand Up @@ -2550,7 +2577,16 @@ Outcome {
secret: false,
exists: May,
numeric: None,
array: None,
array: Some(
Array {
min_items: 0,
has_max_items: false,
max_items: 0,
item_types: [
"object",
],
},
),
},
),
},
Expand Down Expand Up @@ -2927,7 +2963,16 @@ Outcome {
secret: false,
exists: May,
numeric: None,
array: None,
array: Some(
Array {
min_items: 0,
has_max_items: false,
max_items: 0,
item_types: [
"object",
],
},
),
},
),
},
Expand Down Expand Up @@ -3390,7 +3435,16 @@ Outcome {
secret: false,
exists: May,
numeric: None,
array: None,
array: Some(
Array {
min_items: 0,
has_max_items: false,
max_items: 0,
item_types: [
"object",
],
},
),
},
),
},
Expand Down Expand Up @@ -5180,7 +5234,16 @@ Outcome {
secret: false,
exists: May,
numeric: None,
array: None,
array: Some(
Array {
min_items: 0,
has_max_items: false,
max_items: 0,
item_types: [
"object",
],
},
),
},
),
},
Expand Down Expand Up @@ -6750,7 +6813,16 @@ Outcome {
secret: false,
exists: May,
numeric: None,
array: None,
array: Some(
Array {
min_items: 0,
has_max_items: false,
max_items: 0,
item_types: [
"object",
],
},
),
},
),
},
Expand Down

0 comments on commit 85b34fe

Please sign in to comment.