diff --git a/jcommon/codegen/src/main/java/run/mone/ai/codegen/util/StrUtil.java b/jcommon/codegen/src/main/java/run/mone/ai/codegen/util/StrUtil.java new file mode 100644 index 000000000..df4fbfde3 --- /dev/null +++ b/jcommon/codegen/src/main/java/run/mone/ai/codegen/util/StrUtil.java @@ -0,0 +1,43 @@ +package run.mone.ai.codegen.util; + +import lombok.extern.slf4j.Slf4j; + +/** + * @author zhangxiaowei6 + * @Date 2024/9/4 16:31 + */ + +@Slf4j +public class StrUtil { + + public static String toCamelCase(String input) { + if (input == null || input.isEmpty()) { + return input; + } + + StringBuilder result = new StringBuilder(); + boolean capitalizeNext = false; + + for (int i = 0; i < input.length(); i++) { + char currentChar = input.charAt(i); + + if (currentChar == ' ') { + capitalizeNext = true; + } else { + if (capitalizeNext) { + result.append(Character.toUpperCase(currentChar)); + capitalizeNext = false; + } else { + result.append(Character.toLowerCase(currentChar)); + } + } + } + + // 确保第一个字符是小写的 + if (result.length() > 0) { + result.setCharAt(0, Character.toLowerCase(result.charAt(0))); + } + + return result.toString(); + } +} diff --git a/jcommon/codegen/src/main/java/run/mone/ai/codegen/util/TemplateUtils.java b/jcommon/codegen/src/main/java/run/mone/ai/codegen/util/TemplateUtils.java index 1c3aaac36..80d26fcd9 100644 --- a/jcommon/codegen/src/main/java/run/mone/ai/codegen/util/TemplateUtils.java +++ b/jcommon/codegen/src/main/java/run/mone/ai/codegen/util/TemplateUtils.java @@ -38,7 +38,7 @@ public static String renderTemplate(String template, Map gt.registerFunction(it.getKey(), it.getValue())); Template t = gt.getTemplate(template); m.forEach((k, v) -> t.binding(k, v)); diff --git a/jcommon/codegen/src/main/resources/tlp/testSpring.java b/jcommon/codegen/src/main/resources/tlp/testSpring.java index 3e639ed7d..e7e36c682 100644 --- a/jcommon/codegen/src/main/resources/tlp/testSpring.java +++ b/jcommon/codegen/src/main/resources/tlp/testSpring.java @@ -22,8 +22,8 @@ public class ${testName} { -@Resource -private ${serviceName} ${strutil.toLowerCase(serviceName)}Service; + @Resource + private ${serviceName} ${className}Service; } \ No newline at end of file diff --git a/jcommon/codegen/src/test/java/com/xiaomi/youpin/codegen/test/StrTest.java b/jcommon/codegen/src/test/java/com/xiaomi/youpin/codegen/test/StrTest.java new file mode 100644 index 000000000..8c7710708 --- /dev/null +++ b/jcommon/codegen/src/test/java/com/xiaomi/youpin/codegen/test/StrTest.java @@ -0,0 +1,43 @@ +package com.xiaomi.youpin.codegen.test; + +import org.junit.Test; +import run.mone.ai.codegen.util.StrUtil; + +import static org.junit.Assert.assertEquals; + + +public class StrTest { + + + + + @Test + public void testToCamelCase() { + String input = "hello world"; + String expected = "helloWorld"; + String actual = StrUtil.toCamelCase(input); + assertEquals(expected, actual); + + input = "java programming language"; + expected = "javaProgrammingLanguage"; + actual = StrUtil.toCamelCase(input); + assertEquals(expected, actual); + + input = " leading and trailing spaces "; + expected = "leadingAndTrailingSpaces"; + actual = StrUtil.toCamelCase(input); + assertEquals(expected, actual); + + input = ""; + expected = ""; + actual = StrUtil.toCamelCase(input); + assertEquals(expected, actual); + + input = null; + expected = null; + actual = StrUtil.toCamelCase(input); + assertEquals(expected, actual); + } + + +}