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

Additional filters and tests #163

Merged
merged 2 commits into from
May 17, 2024
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
44 changes: 25 additions & 19 deletions Cargo.lock

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

11 changes: 10 additions & 1 deletion crates/weaver_forge/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ The following filters are available:
- `screaming_snake_case`: Converts a string to SCREAMING_SNAKE_CASE.
- `kebab_case`: Converts a string to kebab-case.
- `screaming_kebab_case`: Converts a string to SCREAMING-KEBAB-CASE.
- `capitalize_first`: Capitalizes the first letter of a string.
- `acronym`: Replaces acronyms in the input string with the full name defined in the `acronyms` section of the `weaver.yaml` configuration file.
- `split_ids`: Splits a string by '.' creating a list of nested ids.
- `type_mapping`: Converts a semantic convention type to a target type (see weaver.yaml section `type_mapping`).
Expand All @@ -215,8 +216,12 @@ e.g. \[\[a,b\],\[c\]\] => \[a,b,c\]
- `attribute_namespace`: Converts {namespace}.{attribute_id} to {namespace}.
- `required`: Filters a list of `Attribute`s to include only the required attributes. The "conditionally_required" attributes are not returned by this filter.
- `not_required`: Filters a list of `Attribute`s to only include non-required attributes. The "conditionally_required" attributes are returned by this filter.
- `instantiated_type`: Filters a type to return the instantiated type.
- `enum_type`: Filters a type to return the enum type or an error if the type is not an enum.
- `markdown_to_html`: Converts a markdown string to an HTML string.
- `text_map`: Converts an input into a string based on the `text_maps` section of the `weaver.yaml` configuration file and a named text_map.
- `map_text`: Converts an input into a string based on the `text_maps` section of the `weaver.yaml` configuration file
and a named text_map. The first parameter is the name of the text_map (required). The second parameter is the default
value if the name of the text map or the input are not found in the `text_maps` section (optional).
- `ansi_black`: Format a text using the black ansi code.
- `ansi_red`: Format a text using the red ansi code.
- `ansi_green`: Format a text using the green ansi code.
Expand Down Expand Up @@ -275,5 +280,9 @@ generation of assets.
- `stable`: Tests if an `Attribute` is stable.
- `experimental`: Tests if an `Attribute` is experimental.
- `deprecated`: Tests if an `Attribute` is deprecated.
- `enum`: Tests if an attribute has an enum type.
- `simple_type`: Tests if a type is a simple type (i.e.: string | string[] | int | int[] | double | double[] | boolean | boolean[]).
- `template_type`: Tests if a type is a template type (i.e.: template[]).
- `enum_type`: Tests if a type is an enum type.

> Please open an issue if you have any suggestions for new tests. They are easy to implement.
173 changes: 173 additions & 0 deletions crates/weaver_forge/src/extensions/case.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
// SPDX-License-Identifier: Apache-2.0

//! Case converter filters used by the template engine.

use crate::config::CaseConvention;
use minijinja::Environment;

/// Add case converter filters to the environment.
pub(crate) fn add_filters(env: &mut Environment<'_>) {
env.add_filter("lower_case", case_converter(CaseConvention::LowerCase));
env.add_filter("upper_case", case_converter(CaseConvention::UpperCase));
env.add_filter("title_case", case_converter(CaseConvention::TitleCase));
env.add_filter("pascal_case", case_converter(CaseConvention::PascalCase));
env.add_filter("camel_case", case_converter(CaseConvention::CamelCase));
env.add_filter("snake_case", case_converter(CaseConvention::SnakeCase));
env.add_filter(
"screaming_snake_case",
case_converter(CaseConvention::ScreamingSnakeCase),
);
env.add_filter("kebab_case", case_converter(CaseConvention::KebabCase));
env.add_filter(
"screaming_kebab_case",
case_converter(CaseConvention::ScreamingKebabCase),
);
env.add_filter("capitalize_first", capitalize_first);
}

/// Converts input string to the specified case convention.
#[must_use]
pub fn case_converter(case_convention: CaseConvention) -> fn(&str) -> String {
match case_convention {
CaseConvention::LowerCase => lower_case,
CaseConvention::UpperCase => upper_case,
CaseConvention::TitleCase => title_case,
CaseConvention::CamelCase => camel_case,
CaseConvention::PascalCase => pascal_case,
CaseConvention::SnakeCase => snake_case,
CaseConvention::ScreamingSnakeCase => screaming_snake_case,
CaseConvention::KebabCase => kebab_case,
CaseConvention::ScreamingKebabCase => screaming_kebab_case,
}
}

/// Converts input string to lower case
fn lower_case(input: &str) -> String {
CaseConvention::LowerCase.convert(input)
}

/// Converts input string to upper case
fn upper_case(input: &str) -> String {
CaseConvention::UpperCase.convert(input)
}

/// Converts input string to title case
fn title_case(input: &str) -> String {
CaseConvention::TitleCase.convert(input)
}

/// Converts input string to camel case
fn camel_case(input: &str) -> String {
CaseConvention::CamelCase.convert(input)
}

/// Converts input string to pascal case
fn pascal_case(input: &str) -> String {
CaseConvention::PascalCase.convert(input)
}

/// Converts input string to snake case
fn snake_case(input: &str) -> String {
CaseConvention::SnakeCase.convert(input)
}

/// Converts input string to screaming snake case
fn screaming_snake_case(input: &str) -> String {
CaseConvention::ScreamingSnakeCase.convert(input)
}

/// Converts input string to kebab case
fn kebab_case(input: &str) -> String {
CaseConvention::KebabCase.convert(input)
}

/// Converts input string to screaming kebab case
fn screaming_kebab_case(input: &str) -> String {
CaseConvention::ScreamingKebabCase.convert(input)
}

/// Capitalize the first character of a string.
fn capitalize_first(input: &str) -> String {
let mut chars = input.chars();
let mut result = String::with_capacity(input.len());

if let Some(first_char) = chars.next() {
for c in first_char.to_uppercase() {
result.push(c);
}
}

result.extend(chars);

result
}

#[cfg(test)]
mod tests {
use crate::extensions::case::add_filters;
use minijinja::Environment;

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

add_filters(&mut env);

assert_eq!(
env.render_str("{{ 'Hello World' | lower_case }}", &ctx)
.unwrap(),
"hello world"
);
assert_eq!(
env.render_str("{{ 'Hello World' | upper_case }}", &ctx)
.unwrap(),
"HELLO WORLD"
);
assert_eq!(
env.render_str("{{ 'Hello World' | title_case }}", &ctx)
.unwrap(),
"Hello World"
);
assert_eq!(
env.render_str("{{ 'hello_world' | camel_case }}", &ctx)
.unwrap(),
"helloWorld"
);
assert_eq!(
env.render_str("{{ 'hello_world' | pascal_case }}", &ctx)
.unwrap(),
"HelloWorld"
);
assert_eq!(
env.render_str("{{ 'Hello World' | snake_case }}", &ctx)
.unwrap(),
"hello_world"
);
assert_eq!(
env.render_str("{{ 'Hello World' | screaming_snake_case }}", &ctx)
.unwrap(),
"HELLO_WORLD"
);
assert_eq!(
env.render_str("{{ 'Hello World' | kebab_case }}", &ctx)
.unwrap(),
"hello-world"
);
assert_eq!(
env.render_str("{{ 'Hello World' | screaming_kebab_case }}", &ctx)
.unwrap(),
"HELLO-WORLD"
);
assert_eq!(
env.render_str("{{ 'hello world' | capitalize_first }}", &ctx)
.unwrap(),
"Hello world"
);
assert_eq!(
env.render_str("{{ 'Hello WORLD' | capitalize_first }}", &ctx)
.unwrap(),
"Hello WORLD"
);
}
}
Loading
Loading