Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

assemble: add numeric information to inference #1563

Merged
merged 1 commit into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions crates/assemble/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,16 @@ pub fn inference(shape: &Shape, exists: Exists) -> flow::Inference {
} else {
None
},
numeric: None,
numeric: if shape.type_.overlaps(types::INT_OR_FRAC) {
Some(flow::inference::Numeric {
has_minimum: shape.numeric.minimum.is_some(),
minimum: shape.numeric.minimum.map(Into::into).unwrap_or_default(),
has_maximum: shape.numeric.maximum.is_some(),
maximum: shape.numeric.maximum.map(Into::into).unwrap_or_default(),
})
} else {
None
},
}
}

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

Expand All @@ -570,14 +579,20 @@ mod test {
min_length: 10,
max_length: Some(123),
},
numeric: NumericShape {
minimum: None,
maximum: Some(json::Number::Unsigned(1000)),
},
..Shape::anything()
};

let out1 = inference(&shape, Exists::Must);
shape.type_ = types::BOOLEAN;
let out2 = inference(&shape, Exists::May);
shape.type_ = types::INTEGER | types::STRING;
let out3 = inference(&shape, Exists::May);

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

#[test]
Expand Down
29 changes: 28 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]"
expression: "&[out1, out2, out3]"
---
[
Inference {
Expand Down Expand Up @@ -35,4 +35,31 @@ expression: "&[out1, out2]"
exists: May,
numeric: None,
},
Inference {
types: [
"integer",
"string",
],
string: Some(
String {
content_type: "a/type",
format: "date-time",
content_encoding: "BaSE64",
max_length: 123,
},
),
title: "the title",
description: "the description",
default_json: "{\"hello\":\"world\"}",
secret: true,
exists: May,
numeric: Some(
Numeric {
has_minimum: false,
minimum: 0.0,
has_maximum: true,
maximum: 1000.0,
},
),
},
]
10 changes: 10 additions & 0 deletions crates/json/src/number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,16 @@ impl TryFrom<Number> for serde_json::Value {
}
}

impl Into<f64> for Number {
fn into(self) -> f64 {
match self {
Unsigned(n) => n as f64,
Signed(n) => n as f64,
Float(n) => n,
}
}
}

impl Ord for Number {
fn cmp(&self, other: &Self) -> Ordering {
match (self, other) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,14 @@ Outcome {
default_json: "",
secret: false,
exists: Must,
numeric: None,
numeric: Some(
Numeric {
has_minimum: false,
minimum: 0.0,
has_maximum: false,
maximum: 0.0,
},
),
},
),
},
Expand Down Expand Up @@ -450,7 +457,14 @@ Outcome {
default_json: "",
secret: false,
exists: Must,
numeric: None,
numeric: Some(
Numeric {
has_minimum: false,
minimum: 0.0,
has_maximum: false,
maximum: 0.0,
},
),
},
),
},
Expand Down Expand Up @@ -705,7 +719,14 @@ Outcome {
default_json: "",
secret: false,
exists: Must,
numeric: None,
numeric: Some(
Numeric {
has_minimum: false,
minimum: 0.0,
has_maximum: false,
maximum: 0.0,
},
),
},
),
},
Expand Down
Loading
Loading