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

feat(template): Make kebab-case filter working with digits in the middle of a word #106

Merged
merged 7 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
63 changes: 35 additions & 28 deletions Cargo.lock

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

10 changes: 10 additions & 0 deletions crates/weaver_forge/expected_output/converter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
This is a TEST
this is a test
THIS IS A TEST
This Is A Test
ThisIsATest
thisIsATest
this_is_a_test
THIS_IS_A_TEST
this-is-a-test
THIS-IS-A-TEST
23 changes: 21 additions & 2 deletions crates/weaver_forge/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

use std::collections::HashMap;
use std::path::Path;
use std::sync::OnceLock;

use convert_case::{Case, Casing};
use convert_case::Boundary::{DigitLower, DigitUpper, Hyphen, LowerDigit, UpperDigit};
use convert_case::{Case, Casing, Converter, Pattern};
use globset::{Glob, GlobSet, GlobSetBuilder};
use serde::Deserialize;

Expand Down Expand Up @@ -295,6 +297,8 @@ impl Default for CaseConvention {

impl CaseConvention {
pub fn convert(&self, text: &str) -> String {
static KEBAB_CASE: OnceLock<Converter> = OnceLock::new();

let text = text.replace('.', "_");
match self {
CaseConvention::LowerCase => text.to_case(Case::Lower),
Expand All @@ -304,7 +308,22 @@ impl CaseConvention {
CaseConvention::CamelCase => text.to_case(Case::Camel),
CaseConvention::SnakeCase => text.to_case(Case::Snake),
CaseConvention::ScreamingSnakeCase => text.to_case(Case::ScreamingSnake),
CaseConvention::KebabCase => text.to_case(Case::Kebab),
CaseConvention::KebabCase => {
// Convert to kebab case but do not consider digits
// as boundaries. So that `k8s` will stay `k8s` and
// not `k-8-s`.
let conv = KEBAB_CASE.get_or_init(|| {
Converter::new()
.add_boundary(Hyphen)
.remove_boundary(DigitLower)
lquerel marked this conversation as resolved.
Show resolved Hide resolved
.remove_boundary(DigitUpper)
.remove_boundary(UpperDigit)
.remove_boundary(LowerDigit)
.set_pattern(Pattern::Lowercase)
.set_delim("-")
});
conv.convert(&text)
}
CaseConvention::ScreamingKebabCase => text.to_case(Case::Cobol),
}
}
Expand Down
48 changes: 37 additions & 11 deletions crates/weaver_forge/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,7 @@ impl TemplateEngine {
.filter(|dir_entry| dir_entry.path().is_file())
.collect();

let config = TargetConfig::try_new(&self.path)?;
let tmpl_matcher = config.template_matcher()?;
let tmpl_matcher = self.target_config.template_matcher()?;

// Create a read-only context for the filter evaluations
let context = serde_json::to_value(context).map_err(|e| ContextSerializationFailed {
Expand Down Expand Up @@ -355,19 +354,19 @@ 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("lower_case", case_converter(CaseConvention::LowerCase));
lquerel marked this conversation as resolved.
Show resolved Hide resolved
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",
"screaming_snake_case",
case_converter(CaseConvention::ScreamingSnakeCase),
);
env.add_filter("kebab-case", case_converter(CaseConvention::KebabCase));
env.add_filter("kebab_case", case_converter(CaseConvention::KebabCase));
env.add_filter(
"SCREAMING-KEBAB-CASE",
"screaming_kebab_case",
case_converter(CaseConvention::ScreamingKebabCase),
);

Expand Down Expand Up @@ -457,18 +456,21 @@ fn split_id(value: Value) -> Result<Vec<Value>, minijinja::Error> {

#[cfg(test)]
mod tests {
use globset::Glob;
use std::collections::HashSet;
use std::fs;
use std::path::Path;

use walkdir::WalkDir;

use crate::config::{ApplicationMode, TemplateConfig};
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::filter::Filter;
use crate::registry::TemplateRegistry;

#[test]
Expand Down Expand Up @@ -550,6 +552,21 @@ mod tests {
expected: "this-is-a-test",
case: super::CaseConvention::KebabCase,
},
TestCase {
input: "This is a k8s test",
expected: "this-is-a-k8s-test",
case: super::CaseConvention::KebabCase,
},
TestCase {
input: "This is a K8S test",
expected: "this-is-a-k8s-test",
case: super::CaseConvention::KebabCase,
},
TestCase {
input: "This is 2 K8S test",
expected: "this-is-2-k8s-test",
case: super::CaseConvention::KebabCase,
},
TestCase {
input: "ThisIsATest",
expected: "THIS_IS_A_TEST",
Expand Down Expand Up @@ -581,9 +598,18 @@ mod tests {
#[test]
fn test() {
let logger = TestLogger::default();
let engine = super::TemplateEngine::try_new("test", super::GeneratorConfig::default())
let mut engine = super::TemplateEngine::try_new("test", super::GeneratorConfig::default())
.expect("Failed to create template engine");

// Add a template configuration for converter.md on top
// of the default template configuration. This is useful
// for test coverage purposes.
engine.target_config.templates.push(TemplateConfig {
pattern: Glob::new("converter.md").unwrap(),
filter: Filter::try_new(".").unwrap(),
application_mode: ApplicationMode::Single,
});

let registry_id = "default";
let mut registry = SemConvRegistry::try_from_path(registry_id, "data/*.yaml")
.expect("Failed to load registry");
Expand Down
11 changes: 11 additions & 0 deletions crates/weaver_forge/templates/test/converter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{%- set text = "This is a TEST" -%}
lquerel marked this conversation as resolved.
Show resolved Hide resolved
{{ text }}
{{ text | lower_case }}
{{ text | upper_case }}
{{ text | title_case }}
{{ text | pascal_case }}
{{ text | camel_case }}
{{ text | snake_case }}
{{ text | screaming_snake_case }}
{{ text | kebab_case }}
{{ text | screaming_kebab_case }}
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"
16 changes: 8 additions & 8 deletions docs/template-engine.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,15 +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.
- `lower_case`: Converts a string to lowercase.
- `upper_case`: Converts a string to UPPERCASE.
- `title_case`: Converts a string to TitleCase.
- `pascal_case`: Converts a string to PascalCase.
- `camel_case`: 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.
- `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
Loading