Skip to content

Commit

Permalink
feat(forge): Support params in JQ filters
Browse files Browse the repository at this point in the history
  • Loading branch information
lquerel committed Jun 4, 2024
1 parent d0eee43 commit 27229c4
Show file tree
Hide file tree
Showing 8 changed files with 244 additions and 176 deletions.
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion crates/weaver_codegen_test/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ fn main() {
let params: Params = serde_yaml::from_str(
r#"params:
attributes: true
metrics: true"#,
metrics: true
registry_prefix: \"registry.\""#,
)
.unwrap_or_else(|e| process_error(&logger, e));
let loader = FileSystemFileLoader::try_new(TEMPLATES_PATH.into(), TARGET)
Expand Down
115 changes: 66 additions & 49 deletions crates/weaver_codegen_test/templates/registry/rust/weaver.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ type_mapping:
template[string]: String # Not yet properly handled in codegen
template[string[]]: Vec<String> # Not yet properly handled in codegen

# Default parameter values
params:
attributes: true
metrics: true

templates:
- pattern: README.md
filter: .
Expand All @@ -23,17 +28,20 @@ templates:
# - groups are deduplicated by namespace.
# - groups are sorted by namespace.
filter: >
.groups
| map(select(.id | startswith("registry.")))
| map(select(.type == "attribute_group")
| {
id,
type,
brief,
prefix})
| unique_by(.id | split(".") | .[1])
| sort_by(.id | split(".") | .[1])
if: params.attributes is undefined or params.attributes
if $attributes then
.groups
| map(select(.id | startswith("registry.")))
| map(select(.type == "attribute_group")
| {
id,
type,
brief,
prefix})
| unique_by(.id | split(".") | .[1])
| sort_by(.id | split(".") | .[1])
else
empty
end
application_mode: single
- pattern: attributes/attributes.rs.j2
# The following JQ filter extracts the id, type, brief, prefix, and attributes of groups matching the following
Expand All @@ -42,25 +50,28 @@ templates:
# - groups of the type `attribute_group`.
# - groups are sorted by namespace.
filter: >
.groups
| map(select(.id | startswith("registry.")))
| map(select(.type == "attribute_group")
| {
id,
type,
brief,
prefix,
attributes})
| group_by(.id | split(".") | .[1])
| map({
id: (map(select(.id | endswith(".deprecated") | not)) | first).id,
type: (map(select(.id | endswith(".deprecated") | not)) | first).type,
brief: (map(select(.id | endswith(".deprecated") | not)) | first).brief,
prefix: (map(select(.id | endswith(".deprecated") | not)) | first).prefix,
attributes: map(.attributes) | add
})
| sort_by(.id | split(".") | .[1])
if: params.attributes is undefined or params.attributes
if $attributes then
.groups
| map(select(.id | startswith("registry.")))
| map(select(.type == "attribute_group")
| {
id,
type,
brief,
prefix,
attributes})
| group_by(.id | split(".") | .[1])
| map({
id: (map(select(.id | endswith(".deprecated") | not)) | first).id,
type: (map(select(.id | endswith(".deprecated") | not)) | first).type,
brief: (map(select(.id | endswith(".deprecated") | not)) | first).brief,
prefix: (map(select(.id | endswith(".deprecated") | not)) | first).prefix,
attributes: map(.attributes) | add
})
| sort_by(.id | split(".") | .[1])
else
empty
end
application_mode: each

# Templates for the `metric` group type
Expand All @@ -71,17 +82,20 @@ templates:
# - groups are deduplicated by namespace.
# - groups are sorted by prefix.
filter: >
.groups
| map(select(.id | startswith("metric.")))
| map(select(.type == "metric")
| {
id,
type,
brief,
prefix})
| unique_by(.id | split(".") | .[1])
| sort_by(.id | split(".") | .[1])
if: params.metrics is undefined or params.metrics
if $metrics then
.groups
| map(select(.id | startswith("metric.")))
| map(select(.type == "metric")
| {
id,
type,
brief,
prefix})
| unique_by(.id | split(".") | .[1])
| sort_by(.id | split(".") | .[1])
else
empty
end
application_mode: single
- pattern: metrics/metrics.rs.j2
# The following JQ filter extracts the id, type, brief, prefix, and attributes of groups matching the following
Expand All @@ -90,14 +104,17 @@ templates:
# - groups of the type `metric`.
# - groups are sorted by namespace.
filter: >
.groups
| map(select(.id | startswith("metric.")))
| group_by(.id | split(".") | .[1])
| map({
prefix: .[0].id | split(".") | .[1],
groups: .
})
if: params.metrics is undefined or params.metrics
if $metrics then
.groups
| map(select(.id | startswith("metric.")))
| group_by(.id | split(".") | .[1])
| map({
prefix: .[0].id | split(".") | .[1],
groups: .
})
else
empty
end
application_mode: each


Expand Down
4 changes: 2 additions & 2 deletions crates/weaver_codegen_test/tests/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ use opentelemetry::{global, KeyValue};
#[test]
fn test_codegen() {
// Use a format! macro to avoid compiler optimization
assert_eq!(format!("{}",attributes::ATTRIBUTES_PARAM), "true");
assert_eq!(format!("{}",metrics::METRICS_PARAM), "true");
assert_eq!(format!("{}", attributes::ATTRIBUTES_PARAM), "true");
assert_eq!(format!("{}", metrics::METRICS_PARAM), "true");

// Test the constants generated for the attributes
// In the generated API the attributes are typed, so the compiler will catch type errors
Expand Down
71 changes: 23 additions & 48 deletions crates/weaver_forge/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ use serde_yaml::Value;
use crate::error::Error;
use crate::error::Error::InvalidConfigFile;
use crate::file_loader::FileLoader;
use crate::filter::Filter;
use crate::WEAVER_YAML;

/// Case convention for naming of functions and structs.
Expand Down Expand Up @@ -98,104 +97,77 @@ fn default_templates() -> Vec<TemplateConfig> {
vec![
TemplateConfig {
pattern: Glob::new("**/registry.md").expect("Invalid pattern"),
filter: Filter::try_new(".").expect("Invalid filter"),
r#if: None,
filter: ".".to_owned(),
application_mode: ApplicationMode::Single,
},
TemplateConfig {
pattern: Glob::new("**/attribute_group.md").expect("Invalid pattern"),
filter: Filter::try_new(".groups[] | select(.type == \"attribute_group\")")
.expect("Invalid filter"),
r#if: None,
filter: ".groups[] | select(.type == \"attribute_group\")".to_owned(),
application_mode: ApplicationMode::Each,
},
TemplateConfig {
pattern: Glob::new("**/attribute_groups.md").expect("Invalid pattern"),
filter: Filter::try_new(".groups[] | select(.type == \"attribute_group\")")
.expect("Invalid filter"),
r#if: None,
filter: ".groups[] | select(.type == \"attribute_group\")".to_owned(),
application_mode: ApplicationMode::Single,
},
TemplateConfig {
pattern: Glob::new("**/event.md").expect("Invalid pattern"),
filter: Filter::try_new(".groups[] | select(.type == \"event\")")
.expect("Invalid filter"),
r#if: None,
filter: ".groups[] | select(.type == \"event\")".to_owned(),
application_mode: ApplicationMode::Each,
},
TemplateConfig {
pattern: Glob::new("**/events.md").expect("Invalid pattern"),
filter: Filter::try_new(".groups[] | select(.type == \"event\")")
.expect("Invalid filter"),
r#if: None,
filter: ".groups[] | select(.type == \"event\")".to_owned(),
application_mode: ApplicationMode::Single,
},
TemplateConfig {
pattern: Glob::new("**/group.md").expect("Invalid pattern"),
filter: Filter::try_new(".groups").expect("Invalid filter"),
r#if: None,
filter: ".groups".to_owned(),
application_mode: ApplicationMode::Each,
},
TemplateConfig {
pattern: Glob::new("**/groups.md").expect("Invalid pattern"),
filter: Filter::try_new(".groups").expect("Invalid filter"),
r#if: None,
filter: ".groups".to_owned(),
application_mode: ApplicationMode::Single,
},
TemplateConfig {
pattern: Glob::new("**/metric.md").expect("Invalid pattern"),
filter: Filter::try_new(".groups[] | select(.type == \"metric\")")
.expect("Invalid filter"),
r#if: None,
filter: ".groups[] | select(.type == \"metric\")".to_owned(),
application_mode: ApplicationMode::Each,
},
TemplateConfig {
pattern: Glob::new("**/metrics.md").expect("Invalid pattern"),
filter: Filter::try_new(".groups[] | select(.type == \"metric\")")
.expect("Invalid filter"),
r#if: None,
filter: ".groups[] | select(.type == \"metric\")".to_owned(),
application_mode: ApplicationMode::Single,
},
TemplateConfig {
pattern: Glob::new("**/resource.md").expect("Invalid pattern"),
filter: Filter::try_new(".groups[] | select(.type == \"resource\")")
.expect("Invalid filter"),
r#if: None,
filter: ".groups[] | select(.type == \"resource\")".to_owned(),
application_mode: ApplicationMode::Each,
},
TemplateConfig {
pattern: Glob::new("**/resources.md").expect("Invalid pattern"),
filter: Filter::try_new(".groups[] | select(.type == \"resource\")")
.expect("Invalid filter"),
r#if: None,
filter: ".groups[] | select(.type == \"resource\")".to_owned(),
application_mode: ApplicationMode::Single,
},
TemplateConfig {
pattern: Glob::new("**/scope.md").expect("Invalid pattern"),
filter: Filter::try_new(".groups[] | select(.type == \"scope\")")
.expect("Invalid filter"),
r#if: None,
filter: ".groups[] | select(.type == \"scope\")".to_owned(),
application_mode: ApplicationMode::Each,
},
TemplateConfig {
pattern: Glob::new("**/scopes.md").expect("Invalid pattern"),
filter: Filter::try_new(".groups[] | select(.type == \"scope\")")
.expect("Invalid filter"),
r#if: None,
filter: ".groups[] | select(.type == \"scope\")".to_owned(),
application_mode: ApplicationMode::Single,
},
TemplateConfig {
pattern: Glob::new("**/span.md").expect("Invalid pattern"),
filter: Filter::try_new(".groups[] | select(.type == \"span\")")
.expect("Invalid filter"),
r#if: None,
filter: ".groups[] | select(.type == \"span\")".to_owned(),
application_mode: ApplicationMode::Each,
},
TemplateConfig {
pattern: Glob::new("**/spans.md").expect("Invalid pattern"),
filter: Filter::try_new(".groups[] | select(.type == \"span\")")
.expect("Invalid filter"),
r#if: None,
filter: ".groups[] | select(.type == \"span\")".to_owned(),
application_mode: ApplicationMode::Single,
},
]
Expand Down Expand Up @@ -230,17 +202,20 @@ pub(crate) struct TemplateConfig {
/// The filter to apply to the registry before applying the template.
/// Applying a filter to a registry will return a list of elements from the
/// registry that satisfy the filter.
pub(crate) filter: Filter,
/// An optional `if` condition that must be satisfied to apply the template.
/// The condition is a Jinja2 expression that can use the all the variables
/// defined in the template context (e.g. ctx, params).
pub(crate) r#if: Option<String>,
/// By default, the filter is set to "." which means that the whole registry
/// is returned.
#[serde(default = "default_filter")]
pub(crate) filter: String,
/// The mode to apply the template.
/// `single`: Apply the template to the output of the filter as a whole.
/// `each`: Apply the template to each item of the list returned by the filter.
pub(crate) application_mode: ApplicationMode,
}

fn default_filter() -> String {
".".to_owned()

Check warning on line 216 in crates/weaver_forge/src/config.rs

View check run for this annotation

Codecov / codecov/patch

crates/weaver_forge/src/config.rs#L215-L216

Added lines #L215 - L216 were not covered by tests
}

/// A template matcher.
pub struct TemplateMatcher<'a> {
templates: &'a [TemplateConfig],
Expand Down
11 changes: 0 additions & 11 deletions crates/weaver_forge/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,17 +92,6 @@ pub enum Error {
error: String,
},

/// If expression evaluation failed.
#[error("If expression '{if_expr}' evaluation failed -> {error}")]
IfExprEvaluationFailed {
/// If expression
if_expr: String,
/// Error id used to deduplicate the error.
error_id: String,
/// Error message.
error: String,
},

/// Invalid template directory.
#[error("Invalid template directory: {0}")]
InvalidTemplateDirectory(PathBuf),
Expand Down
Loading

0 comments on commit 27229c4

Please sign in to comment.