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

Add is_array filter and test for AttributeType #540

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
1 change: 1 addition & 0 deletions crates/weaver_forge/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -900,5 +900,6 @@ generation of assets.
boolean[]).
- `template_type`: Tests if a type is a template type (i.e.: template[]).
- `enum_type`: Tests if a type is an enum type.
- `array`: Tests if a type is an array type.

> Please open an issue if you have any suggestions for new tests. They are easy to implement.
66 changes: 66 additions & 0 deletions crates/weaver_forge/src/extensions/otel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ pub(crate) fn add_tests(env: &mut minijinja::Environment<'_>) {
env.add_test("simple_type", is_simple_type);
env.add_test("template_type", is_template_type);
env.add_test("enum_type", is_enum_type);
env.add_test("array", is_array);
}

/// Filters the input value to only include the required "object".
Expand Down Expand Up @@ -454,6 +455,24 @@ pub(crate) fn is_enum(attr: &Value) -> bool {
false
}

/// Returns true if the input type is array
pub(crate) fn is_array(attr_type: &Value) -> bool {
let Some(attr_type) = attr_type.as_str() else {
return false;
};
matches!(
attr_type,
"string[]"
| "int[]"
| "double[]"
| "boolean[]"
| "template[string[]]"
| "template[int[]]"
| "template[double[]]"
| "template[boolean[]]"
)
Copy link
Contributor

Choose a reason for hiding this comment

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

@lmolkova This test looks fine to me, but could you confirm: for semconvs, should a template[xxx[]] be considered an array, or should we treat any template as not being an array, even when templating an array of something?

Copy link
Author

Choose a reason for hiding this comment

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

Just added in documentation update and new test case for full coverage! just let me know if that needs updating!

Copy link
Contributor

Choose a reason for hiding this comment

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

thanks for checking! Templates are not really arrays, they are more like a maps - we also have a filter that checks if attribute has a template - is_template_type, so I believe is_array should be limited to

"string[]"
            | "int[]"
            | "double[]"
            | "boolean[]"

Copy link
Contributor

Choose a reason for hiding this comment

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

other that that, it looks great to me, thank you!

Copy link
Author

@arthurDiff arthurDiff Jan 11, 2025

Choose a reason for hiding this comment

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

Gotcha! thanks for the clarification! I just updated to those criteria! @lquerel @lmolkova

}

/// Returns a list of pairs {field, depth} from a body field in depth-first order
/// by default.
///
Expand Down Expand Up @@ -1366,6 +1385,53 @@ mod tests {
);
}

#[test]
fn test_is_array() {
let mut env = Environment::new();
let ctx = serde_json::Value::Null;

otel::add_filters(&mut env);
otel::add_tests(&mut env);

// (assert, result)
let test_cases = [
("string", "false"),
("string[]", "true"),
("int", "false"),
("int[]", "true"),
("double", "false"),
("double[]", "true"),
("boolean", "false"),
("boolean[]", "true"),
("template[string]", "false"),
("template[string[]]", "true"),
("template[int]", "false"),
("template[int[]]", "true"),
("template[double]", "false"),
("template[double[]]", "true"),
("template[boolean]", "false"),
("template[boolean[]]", "true"),
("enum {id}", "false"),
];

for case in test_cases {
assert_eq!(
env.render_str(
&format!(
"{{% if '{}' is array %}}true{{% else %}}false{{% endif %}}",
case.0
),
&ctx
)
.unwrap(),
case.1
);
}

// invalid value should return false
assert!(!otel::is_array(&Value::from(())));
}

/// Utility function to create an enum type from a list of member values.
fn enum_type(member_values: Vec<ValueSpec>) -> AttributeType {
let members = member_values
Expand Down