Skip to content

Commit

Permalink
feat(template): Add more filters around case conversion (#105)
Browse files Browse the repository at this point in the history
  • Loading branch information
lquerel authored Apr 12, 2024
1 parent 1a594d8 commit 516dbbf
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 6 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: 3 additions & 0 deletions crates/weaver_forge/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ pub enum CaseConvention {
LowerCase,
#[serde(rename = "UPPERCASE")]
UpperCase,
#[serde(rename = "TitleCase")]
TitleCase,
#[serde(rename = "PascalCase")]
PascalCase,
#[serde(rename = "camelCase")]
Expand Down Expand Up @@ -297,6 +299,7 @@ impl CaseConvention {
match self {
CaseConvention::LowerCase => text.to_case(Case::Lower),
CaseConvention::UpperCase => text.to_case(Case::Upper),
CaseConvention::TitleCase => text.to_case(Case::Title),
CaseConvention::PascalCase => text.to_case(Case::Pascal),
CaseConvention::CamelCase => text.to_case(Case::Camel),
CaseConvention::SnakeCase => text.to_case(Case::Snake),
Expand Down
6 changes: 6 additions & 0 deletions crates/weaver_forge/src/extensions/case_converter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ 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,
Expand All @@ -27,6 +28,11 @@ 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)
Expand Down
127 changes: 125 additions & 2 deletions crates/weaver_forge/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use error::Error::{
};
use weaver_logger::Logger;

use crate::config::{ApplicationMode, TargetConfig};
use crate::config::{ApplicationMode, CaseConvention, TargetConfig};
use crate::debug::error_summary;
use crate::error::Error::InvalidConfigFile;
use crate::extensions::case_converter::case_converter;
Expand Down Expand Up @@ -355,6 +355,22 @@ impl TemplateEngine {
"field_name",
case_converter(self.target_config.field_name.clone()),
);
env.add_filter("lowercase", case_converter(CaseConvention::LowerCase));
env.add_filter("UPPERCASE", case_converter(CaseConvention::UpperCase));
env.add_filter("TitleCase", case_converter(CaseConvention::TitleCase));
env.add_filter("PascalCase", case_converter(CaseConvention::PascalCase));
env.add_filter("camelCase", 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("flatten", flatten);
env.add_filter("split_id", split_id);

Expand Down Expand Up @@ -447,14 +463,121 @@ mod tests {

use walkdir::WalkDir;

use crate::debug::print_dedup_errors;
use weaver_diff::diff_output;
use weaver_logger::TestLogger;
use weaver_resolver::SchemaResolver;
use weaver_semconv::SemConvRegistry;

use crate::debug::print_dedup_errors;
use crate::registry::TemplateRegistry;

#[test]
fn test_case_converter() {
struct TestCase {
input: &'static str,
expected: &'static str,
case: super::CaseConvention,
}

let test_cases = vec![
TestCase {
input: "ThisIsATest",
expected: "this is a test",
case: super::CaseConvention::LowerCase,
},
TestCase {
input: "This is a TEST",
expected: "this is a test",
case: super::CaseConvention::LowerCase,
},
TestCase {
input: "ThisIsATest",
expected: "THIS IS A TEST",
case: super::CaseConvention::UpperCase,
},
TestCase {
input: "This is a TEST",
expected: "THIS IS A TEST",
case: super::CaseConvention::UpperCase,
},
TestCase {
input: "ThisIsATest",
expected: "This Is A Test",
case: super::CaseConvention::TitleCase,
},
TestCase {
input: "This is a TEST",
expected: "This Is A Test",
case: super::CaseConvention::TitleCase,
},
TestCase {
input: "ThisIsATest",
expected: "this_is_a_test",
case: super::CaseConvention::SnakeCase,
},
TestCase {
input: "This is a test",
expected: "this_is_a_test",
case: super::CaseConvention::SnakeCase,
},
TestCase {
input: "ThisIsATest",
expected: "ThisIsATest",
case: super::CaseConvention::PascalCase,
},
TestCase {
input: "This is a test",
expected: "ThisIsATest",
case: super::CaseConvention::PascalCase,
},
TestCase {
input: "ThisIsATest",
expected: "thisIsATest",
case: super::CaseConvention::CamelCase,
},
TestCase {
input: "This is a test",
expected: "thisIsATest",
case: super::CaseConvention::CamelCase,
},
TestCase {
input: "ThisIsATest",
expected: "this-is-a-test",
case: super::CaseConvention::KebabCase,
},
TestCase {
input: "This is a test",
expected: "this-is-a-test",
case: super::CaseConvention::KebabCase,
},
TestCase {
input: "ThisIsATest",
expected: "THIS_IS_A_TEST",
case: super::CaseConvention::ScreamingSnakeCase,
},
TestCase {
input: "This is a test",
expected: "THIS_IS_A_TEST",
case: super::CaseConvention::ScreamingSnakeCase,
},
TestCase {
input: "ThisIsATest",
expected: "THIS-IS-A-TEST",
case: super::CaseConvention::ScreamingKebabCase,
},
TestCase {
input: "This is a test",
expected: "THIS-IS-A-TEST",
case: super::CaseConvention::ScreamingKebabCase,
},
];

for test_case in test_cases {
let result = super::case_converter(test_case.case)(test_case.input);
assert_eq!(result, test_case.expected);
}
}

#[test]
fn test() {
let logger = TestLogger::default();
Expand Down
9 changes: 9 additions & 0 deletions docs/template-engine.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,15 @@ The following filters are available:
- `struct_name`: Converts a string to a struct name.
- `field_name`: Converts a string to a field name.
- `type_mapping`: Converts a semantic convention type to a language type.
- `lowercase`: Converts a string to lowercase.
- `UPPERCASE`: Converts a string to UPPERCASE.
- `TitleCase`: Converts a string to TitleCase.
- `PascalCase`: Converts a string to PascalCase.
- `camelCase`: Converts a string to camelCase.
- `snake_case`: Converts a string to snake_case.
- `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.

> Note: Other filters might be introduced in the future.
Expand Down

0 comments on commit 516dbbf

Please sign in to comment.