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

Added support for List of Array examples. #177

Merged
merged 6 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 0 additions & 2 deletions crates/weaver_checker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,8 @@ deny[schema_evolution_violation("attr_removed", old_group.id, old_attr.id)] {
}
```

> [!NOTE]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

> Note 1: The after_resolution stage is not yet fully supported by Weaver.

> [!NOTE]
> Note 2: An upcoming version of Weaver will also allow applying rules on two
> distinct versions of the registries (before or after resolution). This will
> enable the definition of schema evolution rules.
Expand Down
156 changes: 156 additions & 0 deletions crates/weaver_semconv/src/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,14 @@ pub enum Examples {
Bools(Vec<bool>),
/// A array of strings example.
Strings(Vec<String>),
/// List of arrays of integers example.
ListOfInts(Vec<Vec<i64>>),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a test to ensure this is working?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added tests... feels like I'm just testing serde_yaml though ;)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, and agreed. I had intended for those tests to be on full resolution ensuring these are preserved through the resolution process, but what you have is fine for now.

/// List of arrays of doubles example.
ListOfDoubles(Vec<Vec<OrderedFloat<f64>>>),
/// List of arrays of bools example.
ListOfBools(Vec<Vec<bool>>),
/// List of arrays of strings example.
ListOfStrings(Vec<Vec<String>>),
}

/// The different requirement level specifications.
Expand Down Expand Up @@ -772,6 +780,154 @@ mod tests {
assert_eq!(attr.tag(), Some("tag".to_owned()));
assert!(attr.is_required());
}

#[test]
fn test_examples_bool() {
let yaml = "---\ntrue";
let ex: Examples = serde_yaml::from_str(yaml).unwrap();
assert_eq!(ex, Examples::Bool(true));
}

#[test]
fn test_examples_int() {
let yaml = "---\n42";
let ex: Examples = serde_yaml::from_str(yaml).unwrap();
assert_eq!(ex, Examples::Int(42));
}

#[test]
fn test_examples_double() {
let yaml = "---\n3.15";
let ex: Examples = serde_yaml::from_str(yaml).unwrap();
assert_eq!(ex, Examples::Double(OrderedFloat(3.15)));
}

#[test]
fn test_examples_string() {
let yaml = "---\n\"foo\"";
let ex: Examples = serde_yaml::from_str(yaml).unwrap();
assert_eq!(ex, Examples::String("foo".to_owned()));
}

#[test]
fn test_examples_strings() {
let yaml = "---\n- \"foo\"\n- \"bar\"";
let ex: Examples = serde_yaml::from_str(yaml).unwrap();
assert_eq!(
ex,
Examples::Strings(vec!["foo".to_owned(), "bar".to_owned()])
);
}

#[test]
fn test_examples_ints() {
let yaml = "---\n- 42\n- 43";
let ex: Examples = serde_yaml::from_str(yaml).unwrap();
assert_eq!(ex, Examples::Ints(vec![42, 43]));
}

#[test]
fn test_examples_doubles() {
let yaml = "---\n- 3.15\n- 2.71";
let ex: Examples = serde_yaml::from_str(yaml).unwrap();
assert_eq!(
ex,
Examples::Doubles(vec![OrderedFloat(3.15), OrderedFloat(2.71)])
);
}

#[test]
fn test_examples_bools() {
let yaml = "---\n- true\n- false";
let ex: Examples = serde_yaml::from_str(yaml).unwrap();
assert_eq!(ex, Examples::Bools(vec![true, false]));
}

#[test]
fn test_examples_list_of_ints() {
let yaml = "---\n- [42, 43]\n- [44, 45]";
let ex: Examples = serde_yaml::from_str(yaml).unwrap();
assert_eq!(ex, Examples::ListOfInts(vec![vec![42, 43], vec![44, 45]]));
}

#[test]
fn test_examples_list_of_doubles() {
let yaml = "---\n- [3.15, 2.71]\n- [1.41, 1.61]";
let ex: Examples = serde_yaml::from_str(yaml).unwrap();
assert_eq!(
ex,
Examples::ListOfDoubles(vec![
vec![OrderedFloat(3.15), OrderedFloat(2.71)],
vec![OrderedFloat(1.41), OrderedFloat(1.61)]
])
);
}

#[test]
fn test_examples_list_of_bools() {
let yaml = "---\n- [true, false]\n- [false, true]";
let ex: Examples = serde_yaml::from_str(yaml).unwrap();
assert_eq!(
ex,
Examples::ListOfBools(vec![vec![true, false], vec![false, true]])
);
}

#[test]
fn test_examples_list_of_strings() {
let yaml = "---\n- [\"foo\", \"bar\"]\n- [\"baz\", \"qux\"]";
let ex: Examples = serde_yaml::from_str(yaml).unwrap();
assert_eq!(
ex,
Examples::ListOfStrings(vec![
vec!["foo".to_owned(), "bar".to_owned()],
vec!["baz".to_owned(), "qux".to_owned()]
])
);
}

#[test]
fn test_examples_list_of_ints_array_style() {
let yaml = "[ [42, 43], [44, 45] ]";
let ex: Examples = serde_yaml::from_str(yaml).unwrap();
assert_eq!(ex, Examples::ListOfInts(vec![vec![42, 43], vec![44, 45]]));
}

#[test]
fn test_examples_list_of_doubles_array_style() {
let yaml = "[ [3.15, 2.71], [1.41, 1.61] ]";
let ex: Examples = serde_yaml::from_str(yaml).unwrap();
assert_eq!(
ex,
Examples::ListOfDoubles(vec![
vec![OrderedFloat(3.15), OrderedFloat(2.71)],
vec![OrderedFloat(1.41), OrderedFloat(1.61)]
])
);
}

#[test]
fn test_examples_list_of_bools_array_style() {
let yaml = "[ [true, false], [false, true] ]";
let ex: Examples = serde_yaml::from_str(yaml).unwrap();
assert_eq!(
ex,
Examples::ListOfBools(vec![vec![true, false], vec![false, true]])
);
}

#[test]
fn test_examples_list_of_strings_array_style() {
let yaml = "[ [\"foo\", \"bar\"], [\"baz\", \"qux\"] ]";
let ex: Examples = serde_yaml::from_str(yaml).unwrap();
assert_eq!(
ex,
Examples::ListOfStrings(vec![
vec!["foo".to_owned(), "bar".to_owned()],
vec!["baz".to_owned(), "qux".to_owned()]
])
);
}
}

/// An attribute definition with its provenance (path or URL).
Expand Down
53 changes: 46 additions & 7 deletions crates/weaver_semconv_gen/src/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,15 @@
}
}

fn write_example_list<Out: Write, Element: std::fmt::Display>(
fn write_example_list<Out: Write, Element: std::fmt::Display + std::fmt::Debug>(
jsuereth marked this conversation as resolved.
Show resolved Hide resolved
out: &mut Out,
list: &[Element],
is_array: bool,
) -> Result<(), Error> {
if is_array {
write!(out, "`{:?}`", list)?;
return Ok(());

Check warning on line 68 in crates/weaver_semconv_gen/src/gen.rs

View check run for this annotation

Codecov / codecov/patch

crates/weaver_semconv_gen/src/gen.rs#L67-L68

Added lines #L67 - L68 were not covered by tests
}
let mut first = true;
for e in list {
if !first {
Expand All @@ -73,16 +78,40 @@
Ok(())
}

fn write_examples_string<Out: Write>(out: &mut Out, examples: &Examples) -> Result<(), Error> {
fn write_example_list_of_lists<Out: Write, Element: std::fmt::Display + std::fmt::Debug>(

Check warning on line 81 in crates/weaver_semconv_gen/src/gen.rs

View check run for this annotation

Codecov / codecov/patch

crates/weaver_semconv_gen/src/gen.rs#L81

Added line #L81 was not covered by tests
out: &mut Out,
list: &[Vec<Element>],
is_array: bool,
) -> Result<(), Error> {
let mut first = true;
for e in list {
if !first {
write!(out, "; ")?;

Check warning on line 89 in crates/weaver_semconv_gen/src/gen.rs

View check run for this annotation

Codecov / codecov/patch

crates/weaver_semconv_gen/src/gen.rs#L86-L89

Added lines #L86 - L89 were not covered by tests
}
write_example_list(out, e, is_array)?;
first = false;

Check warning on line 92 in crates/weaver_semconv_gen/src/gen.rs

View check run for this annotation

Codecov / codecov/patch

crates/weaver_semconv_gen/src/gen.rs#L91-L92

Added lines #L91 - L92 were not covered by tests
}
Ok(())

Check warning on line 94 in crates/weaver_semconv_gen/src/gen.rs

View check run for this annotation

Codecov / codecov/patch

crates/weaver_semconv_gen/src/gen.rs#L94

Added line #L94 was not covered by tests
}

fn write_examples_string<Out: Write>(
out: &mut Out,
examples: &Examples,
is_array: bool,
) -> Result<(), Error> {
match examples {
Examples::Bool(value) => Ok(write!(out, "`{value}`")?),
Examples::Int(value) => Ok(write!(out, "`{value}`")?),
Examples::Double(value) => Ok(write!(out, "`{value}`")?),
Examples::String(value) => Ok(write!(out, "`{value}`")?),
Examples::Ints(values) => write_example_list(out, values),
Examples::Doubles(values) => write_example_list(out, values),
Examples::Bools(values) => write_example_list(out, values),
Examples::Strings(values) => write_example_list(out, values),
Examples::Ints(values) => write_example_list(out, values, is_array),
Examples::Doubles(values) => write_example_list(out, values, is_array),
Examples::Bools(values) => write_example_list(out, values, is_array),

Check warning on line 109 in crates/weaver_semconv_gen/src/gen.rs

View check run for this annotation

Codecov / codecov/patch

crates/weaver_semconv_gen/src/gen.rs#L108-L109

Added lines #L108 - L109 were not covered by tests
Examples::Strings(values) => write_example_list(out, values, is_array),
Examples::ListOfInts(values) => write_example_list_of_lists(out, values, is_array),
Examples::ListOfDoubles(values) => write_example_list_of_lists(out, values, is_array),
Examples::ListOfBools(values) => write_example_list_of_lists(out, values, is_array),
Examples::ListOfStrings(values) => write_example_list_of_lists(out, values, is_array),

Check warning on line 114 in crates/weaver_semconv_gen/src/gen.rs

View check run for this annotation

Codecov / codecov/patch

crates/weaver_semconv_gen/src/gen.rs#L111-L114

Added lines #L111 - L114 were not covered by tests
}
}

Expand Down Expand Up @@ -183,6 +212,16 @@
matches!(&self.attribute.r#type, AttributeType::Enum { .. })
}

fn is_array(&self) -> bool {
matches!(
&self.attribute.r#type,
AttributeType::PrimitiveOrArray(PrimitiveOrArrayTypeSpec::Booleans)
| AttributeType::PrimitiveOrArray(PrimitiveOrArrayTypeSpec::Ints)
| AttributeType::PrimitiveOrArray(PrimitiveOrArrayTypeSpec::Doubles)
| AttributeType::PrimitiveOrArray(PrimitiveOrArrayTypeSpec::Strings)

Check warning on line 221 in crates/weaver_semconv_gen/src/gen.rs

View check run for this annotation

Codecov / codecov/patch

crates/weaver_semconv_gen/src/gen.rs#L218-L221

Added lines #L218 - L221 were not covered by tests
)
}

fn is_sampling_relevant(&self) -> bool {
self.attribute.sampling_relevant.unwrap_or(false)
}
Expand Down Expand Up @@ -307,7 +346,7 @@

fn write_examples<Out: Write>(&self, out: &mut Out) -> Result<(), Error> {
match &self.attribute.examples {
Some(examples) => write_examples_string(out, examples),
Some(examples) => write_examples_string(out, examples, self.is_array()),
None =>
// Enums can pull examples from the enum if not otherwise specified.
{
Expand Down
Loading