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 new flatten method so we can generate notes in semconv #80

Merged
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
5 changes: 4 additions & 1 deletion crates/weaver_forge/expected_output/attribute_groups.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Semantic Convention Attribute Groups


## flatten
- one
- two
- three
## Group `attributes.jvm.memory` (attribute_group)

### Brief
Expand Down
13 changes: 13 additions & 0 deletions crates/weaver_forge/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,7 @@ impl TemplateEngine {
"field_name",
case_converter(self.target_config.field_name.clone()),
);
env.add_filter("flatten", flatten);

// env.add_filter("unique_attributes", extensions::unique_attributes);
// env.add_filter("instrument", extensions::instrument);
Expand Down Expand Up @@ -408,6 +409,18 @@ impl TemplateEngine {
}
}

// Helper filter to work around lack of `list.append()` support in minijinja.
// Will take a list of lists and return a new list containing only elements of sublists.
fn flatten(value: Value) -> Result<Value, minijinja::Error> {
let mut result = Vec::new();
for sublist in value.try_iter()? {
for item in sublist.try_iter()? {
result.push(item);
}
}
Ok(Value::from(result))
}

#[cfg(test)]
mod tests {
use std::collections::HashSet;
Expand Down
6 changes: 6 additions & 0 deletions crates/weaver_forge/templates/test/attribute_groups.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Semantic Convention Attribute Groups

## flatten
{%- set test = [["one", "two"], ["three"]] | flatten -%}
{% for item in test %}
- {{item}}
{%- endfor -%}

{% for group in ctx %}
## Group `{{ group.id }}` ({{ group.type }})

Expand Down
2 changes: 1 addition & 1 deletion crates/weaver_forge/templates/test/weaver.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ type_mapping:
"int[]": "[]int64"
"double[]": "[]double"
"boolean[]": "[]bool"
"string[]": "[]string"
"string[]": "[]string"
Loading