Skip to content

Commit 12cbc25

Browse files
authored
tweak Display impl for Core's SchemaType (#1175)
Signed-off-by: Craig Disselkoen <[email protected]>
1 parent 6aa2071 commit 12cbc25

File tree

3 files changed

+27
-34
lines changed

3 files changed

+27
-34
lines changed

cedar-policy-core/src/entities.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2401,7 +2401,7 @@ mod schema_based_parsing_tests {
24012401
&entitiesjson,
24022402
&miette::Report::new(e),
24032403
&ExpectedErrorMessageBuilder::error("error during entity deserialization")
2404-
.source(r#"in attribute `hr_contacts` on `Employee::"12UA45"`, type mismatch: value was expected to have type (set of `HR`), but actually has type record with attributes: {"id" => (optional) string, "type" => (optional) string}: `{"id": "aaaaa", "type": "HR"}`"#)
2404+
.source(r#"in attribute `hr_contacts` on `Employee::"12UA45"`, type mismatch: value was expected to have type [`HR`], but actually has type { "id" => (optional) string, "type" => (optional) string }: `{"id": "aaaaa", "type": "HR"}`"#)
24052405
.build()
24062406
);
24072407
});
@@ -2589,7 +2589,7 @@ mod schema_based_parsing_tests {
25892589
&entitiesjson,
25902590
&miette::Report::new(e),
25912591
&ExpectedErrorMessageBuilder::error_starts_with("entity does not conform to the schema")
2592-
.source(r#"in attribute `json_blob` on `Employee::"12UA45"`, type mismatch: value was expected to have type record with attributes: "#)
2592+
.source(r#"in attribute `json_blob` on `Employee::"12UA45"`, type mismatch: value was expected to have type {"#)
25932593
.build()
25942594
);
25952595
});

cedar-policy-core/src/entities/json/schema_types.rs

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -220,38 +220,31 @@ impl std::fmt::Display for SchemaType {
220220
Self::Bool => write!(f, "bool"),
221221
Self::Long => write!(f, "long"),
222222
Self::String => write!(f, "string"),
223-
Self::Set { element_ty } => write!(f, "(set of {})", &element_ty),
224-
Self::EmptySet => write!(f, "empty-set"),
223+
Self::Set { element_ty } => write!(f, "[{element_ty}]"),
224+
Self::EmptySet => write!(f, "[]"),
225225
Self::Record { attrs, open_attrs } => {
226-
if attrs.is_empty() && *open_attrs {
227-
write!(f, "any record")
228-
} else if attrs.is_empty() {
229-
write!(f, "empty record")
230-
} else {
231-
if *open_attrs {
232-
write!(f, "record with at least attributes: {{")?;
233-
} else {
234-
write!(f, "record with attributes: {{")?;
226+
write!(f, "{{ ")?;
227+
// sorting attributes ensures that there is a single, deterministic
228+
// Display output for each `SchemaType`, which is important for
229+
// tests that check equality of error messages
230+
for (i, (k, v)) in attrs
231+
.iter()
232+
.sorted_unstable_by_key(|(k, _)| SmolStr::clone(k))
233+
.enumerate()
234+
{
235+
write!(f, "{k:?} => {v}")?;
236+
if i < (attrs.len() - 1) {
237+
write!(f, ", ")?;
235238
}
236-
// sorting attributes ensures that there is a single, deterministic
237-
// Display output for each `SchemaType`, which is important for
238-
// tests that check equality of error messages
239-
for (i, (k, v)) in attrs
240-
.iter()
241-
.sorted_unstable_by_key(|(k, _)| SmolStr::clone(k))
242-
.enumerate()
243-
{
244-
write!(f, "{k:?} => {v}")?;
245-
if i < (attrs.len() - 1) {
246-
write!(f, ", ")?;
247-
}
248-
}
249-
write!(f, "}}")?;
250-
Ok(())
251239
}
240+
if *open_attrs {
241+
write!(f, ", ...")?;
242+
}
243+
write!(f, " }}")?;
244+
Ok(())
252245
}
253246
Self::Entity { ty } => write!(f, "`{ty}`"),
254-
Self::Extension { name } => write!(f, "{}", name),
247+
Self::Extension { name } => write!(f, "{name}"),
255248
}
256249
}
257250
}

cedar-policy/src/tests.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1998,7 +1998,7 @@ mod schema_based_parsing_tests {
19981998
"",
19991999
&Report::new(err),
20002000
&ExpectedErrorMessageBuilder::error("error during entity deserialization")
2001-
.source(r#"in attribute `hr_contacts` on `Employee::"12UA45"`, type mismatch: value was expected to have type (set of `HR`), but actually has type record with attributes: {"id" => (optional) string, "type" => (optional) string}: `{"id": "aaaaa", "type": "HR"}`"#)
2001+
.source(r#"in attribute `hr_contacts` on `Employee::"12UA45"`, type mismatch: value was expected to have type [`HR`], but actually has type { "id" => (optional) string, "type" => (optional) string }: `{"id": "aaaaa", "type": "HR"}`"#)
20022002
.build()
20032003
);
20042004

@@ -2142,7 +2142,7 @@ mod schema_based_parsing_tests {
21422142
"",
21432143
&Report::new(err),
21442144
&ExpectedErrorMessageBuilder::error_starts_with("entity does not conform to the schema")
2145-
.source(r#"in attribute `json_blob` on `Employee::"12UA45"`, type mismatch: value was expected to have type record with attributes: "#)
2145+
.source(r#"in attribute `json_blob` on `Employee::"12UA45"`, type mismatch: value was expected to have type {"#)
21462146
.build()
21472147
);
21482148

@@ -2487,7 +2487,7 @@ mod schema_based_parsing_tests {
24872487
"",
24882488
&Report::new(err),
24892489
&ExpectedErrorMessageBuilder::error("error during entity deserialization")
2490-
.source(r#"in attribute `hr_contacts` on `Employee::"12UA45"`, type mismatch: value was expected to have type (set of `HR`), but actually has type record with attributes: {"id" => (optional) string, "type" => (optional) string}: `{"id": "aaaaa", "type": "HR"}`"#)
2490+
.source(r#"in attribute `hr_contacts` on `Employee::"12UA45"`, type mismatch: value was expected to have type [`HR`], but actually has type { "id" => (optional) string, "type" => (optional) string }: `{"id": "aaaaa", "type": "HR"}`"#)
24912491
.build()
24922492
);
24932493

@@ -2638,8 +2638,8 @@ mod schema_based_parsing_tests {
26382638
expect_err(
26392639
"",
26402640
&Report::new(err),
2641-
&ExpectedErrorMessageBuilder::error_starts_with("entity does not conform to the schema")
2642-
.source(r#"in attribute `json_blob` on `Employee::"12UA45"`, type mismatch: value was expected to have type record with attributes: "#)
2641+
&ExpectedErrorMessageBuilder::error("entity does not conform to the schema")
2642+
.source(r#"in attribute `json_blob` on `Employee::"12UA45"`, type mismatch: value was expected to have type { "inner1" => (required) bool, "inner2" => (required) string, "inner3" => (required) { "innerinner" => (required) `Employee` } }, but actually has type { "inner1" => (optional) long, "inner2" => (optional) string, "inner3" => (optional) { "innerinner" => (optional) `Employee` } }: `{"inner1": 33, "inner2": "-*/", "inner3": {"innerinner": Employee::"09AE76"}}`"#)
26432643
.build()
26442644
);
26452645

0 commit comments

Comments
 (0)