Skip to content

Commit

Permalink
Merge branch 'main' into dependabot/cargo/toml-0.8.20
Browse files Browse the repository at this point in the history
  • Loading branch information
lquerel authored Feb 6, 2025
2 parents 034a0fe + dda4c07 commit 8cde510
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
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.
56 changes: 56 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 @@ -455,6 +456,14 @@ 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[]")
}

/// Returns a list of pairs {field, depth} from a body field in depth-first order
/// by default.
///
Expand Down Expand Up @@ -1367,6 +1376,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[]]", "false"),
("template[int]", "false"),
("template[int[]]", "false"),
("template[double]", "false"),
("template[double[]]", "false"),
("template[boolean]", "false"),
("template[boolean[]]", "false"),
("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

0 comments on commit 8cde510

Please sign in to comment.