From 8e23059df6385291a39a4d0968844364bdf26968 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=A0=A1=E7=82=9C?= Date: Thu, 15 Aug 2024 18:51:33 +0800 Subject: [PATCH 1/6] update --- jcommon/codegen/pom.xml | 6 ++-- .../run/mone/ai/codegen/CodeGenerator.java | 36 ++++++++++++++++--- .../src/main/resources/tlp/testSpring.java | 29 +++++++++++++++ .../codegen/test/FeatureGeneratorTest.java | 4 +-- jcommon/pom.xml | 6 ++-- 5 files changed, 67 insertions(+), 14 deletions(-) create mode 100644 jcommon/codegen/src/main/resources/tlp/testSpring.java diff --git a/jcommon/codegen/pom.xml b/jcommon/codegen/pom.xml index cfc3d4421..e1b9ffdac 100644 --- a/jcommon/codegen/pom.xml +++ b/jcommon/codegen/pom.xml @@ -6,10 +6,10 @@ run.mone jcommon - 1.6.0-jdk21-SNAPSHOT + 1.6.2-jdk17-SNAPSHOT codegen - 1.6.0-jdk21-SNAPSHOT + 1.6.2-jdk17-SNAPSHOT commons-io @@ -54,7 +54,7 @@ run.mone infra-result - 1.6.0-jdk21-SNAPSHOT + 1.6.1-jdk17-SNAPSHOT thrift diff --git a/jcommon/codegen/src/main/java/run/mone/ai/codegen/CodeGenerator.java b/jcommon/codegen/src/main/java/run/mone/ai/codegen/CodeGenerator.java index 57483329f..f42de3b03 100644 --- a/jcommon/codegen/src/main/java/run/mone/ai/codegen/CodeGenerator.java +++ b/jcommon/codegen/src/main/java/run/mone/ai/codegen/CodeGenerator.java @@ -24,7 +24,17 @@ public class CodeGenerator { private static String serviceName = "s"; - private static String basePath = "/Users/wodiwudi/java/nr-car-account"; + private static String testPackageName = "TP"; + + private static String testPath = ""; + + private static String mainClass = ""; + + private static String author = ""; + + private static String testType = ""; + + private static String basePath = "you base path"; private static boolean createPojo = false; @@ -38,6 +48,8 @@ public class CodeGenerator { private static boolean createController = false; + private static final String SPRING_BOOT_TEST_TYPE = "springboot"; + /** * 主方法 * 判断输入参数长度是否大于 0,调用方法处理参数并提取数据 @@ -53,6 +65,11 @@ public static void main(String[] args) { className = map.get("pojoName"); testName = map.get("testName"); serviceName = map.get("serviceName"); + testPackageName = map.get("testPackageName"); + testPath = map.get("testPath"); + mainClass = map.get("mainClass"); + author = map.get("author"); + testType = map.get("testType"); } if (StringUtils.isEmpty(className)) { return; @@ -61,6 +78,11 @@ public static void main(String[] args) { data.put("className", className); data.put("author", "goodjava@qq.com"); data.put("serviceName", serviceName); + data.put("testPackageName", testPackageName); + data.put("testPath", testPath); + data.put("mainClass", mainClass); + data.put("testAuthor", author); + data.put("testType", testType); Optional first = Arrays.stream(testName.split("\\.")).findFirst(); data.put("testName", first.get()); // 调用方法并获取结果 @@ -86,10 +108,14 @@ public static void main(String[] args) { if (createTest) { String cn = testName; - System.out.println("create test:" + cn); - String test = TemplateUtils.renderTemplateFromFile("tlp/test.java", data); - TemplateUtils.writeStringToFile(test, basePath + "/your project/src/test/java/com/xiaomi/nr/car/account/constant/" + cn); - + System.out.println("create test cn :" + cn + "path:" + testPath); + String test; + if (SPRING_BOOT_TEST_TYPE.equals(testType)) { + test = TemplateUtils.renderTemplateFromFile("tlp/testSpring.java", data); + } else { + test = TemplateUtils.renderTemplateFromFile("tlp/test.java", data); + } + TemplateUtils.writeStringToFile(test, testPath + cn); } if (createController) { diff --git a/jcommon/codegen/src/main/resources/tlp/testSpring.java b/jcommon/codegen/src/main/resources/tlp/testSpring.java new file mode 100644 index 000000000..3e639ed7d --- /dev/null +++ b/jcommon/codegen/src/main/resources/tlp/testSpring.java @@ -0,0 +1,29 @@ +package ${testPackageName}; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit.jupiter.SpringExtension; +import javax.annotation.Resource; +import org.mockito.Mockito; + +import java.util.ArrayList; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.*; +import static org.mockito.ArgumentMatchers.*; + +/** + * @author ${testAuthor} + */ +@ExtendWith(SpringExtension.class) +@SpringBootTest(classes = ${mainClass}.class) +public class ${testName} { + + +@Resource +private ${serviceName} ${strutil.toLowerCase(serviceName)}Service; + + +} \ No newline at end of file diff --git a/jcommon/codegen/src/test/java/com/xiaomi/youpin/codegen/test/FeatureGeneratorTest.java b/jcommon/codegen/src/test/java/com/xiaomi/youpin/codegen/test/FeatureGeneratorTest.java index ff39ead0e..565a8901c 100644 --- a/jcommon/codegen/src/test/java/com/xiaomi/youpin/codegen/test/FeatureGeneratorTest.java +++ b/jcommon/codegen/src/test/java/com/xiaomi/youpin/codegen/test/FeatureGeneratorTest.java @@ -16,7 +16,6 @@ public class FeatureGeneratorTest { @Test public void testCreateTable() { FeatureGenerateBo featureGenerateBo = new FeatureGenerateBo(); - featureGenerateBo.setType(FeatureGeneratType.TABLE); featureGenerateBo.setJdbcUrl(dbUrl); featureGenerateBo.setUserName(dbUser); featureGenerateBo.setPassword(dbPwd); @@ -27,13 +26,12 @@ public void testCreateTable() { " PRIMARY KEY (`id`)\n" + ");"; featureGenerateBo.setSql(sql); - FeatureGenerator.generateWithTemplate(featureGenerateBo); + FeatureGenerator.generateWithGenerator(featureGenerateBo); } @Test public void testGenerateWithMybatis() { FeatureGenerateBo featureGenerateBo = new FeatureGenerateBo(); - featureGenerateBo.setType(FeatureGeneratType.CODE_WITH_MYBATIS_GENERATOR); featureGenerateBo.setJdbcUrl(dbUrl); featureGenerateBo.setUserName(dbUser); featureGenerateBo.setPassword(dbPwd); diff --git a/jcommon/pom.xml b/jcommon/pom.xml index 66ebff815..ba7d08e15 100644 --- a/jcommon/pom.xml +++ b/jcommon/pom.xml @@ -5,7 +5,7 @@ 4.0.0 run.mone jcommon - 1.6.0-jdk21-SNAPSHOT + 1.6.2-jdk17-SNAPSHOT pom ${project.artifactId} The parent project of jcommon @@ -222,8 +222,8 @@ maven-compiler-plugin 3.11.0 - 20 - 20 + 17 + 17 true UTF-8 From 401048d2ca731025868191904230831effa48cda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=A0=A1=E7=82=9C?= Date: Mon, 19 Aug 2024 11:20:37 +0800 Subject: [PATCH 2/6] refactor codegen --- jcommon/codegen/pom.xml | 168 ++++++++++-------- .../run/mone/ai/codegen/CodeGenerator.java | 42 +++++ .../mone/ai/codegen/util/TemplateUtils.java | 35 ++++ .../xiaomi/youpin/codegen/test/GenTest.java | 17 +- jcommon/pom.xml | 2 +- 5 files changed, 189 insertions(+), 75 deletions(-) diff --git a/jcommon/codegen/pom.xml b/jcommon/codegen/pom.xml index e1b9ffdac..ac01aba6f 100644 --- a/jcommon/codegen/pom.xml +++ b/jcommon/codegen/pom.xml @@ -2,77 +2,105 @@ - 4.0.0 - - run.mone - jcommon + 4.0.0 + + run.mone + jcommon + 1.6.0-jdk21-SNAPSHOT + + codegen 1.6.2-jdk17-SNAPSHOT - - codegen - 1.6.2-jdk17-SNAPSHOT - - - commons-io - commons-io - 2.6 - compile - - - run.mone - codegen-temlates - 1.0-SNAPSHOT - compile - - - com.ibeetl - beetl - 2.7.14 - - - com.mybatis-flex - mybatis-flex-codegen - 1.7.8 - - - org.springframework - spring-jdbc - 5.3.29 - true - - - com.zaxxer - HikariCP - 4.0.3 - true - - - org.apache.commons - commons-lang3 - 3.8.1 - compile - - - run.mone - infra-result - 1.6.1-jdk17-SNAPSHOT - - - thrift - org.apache.thrift - - - - - org.mybatis.generator - mybatis-generator-core - 1.4.0 - - - mysql - mysql-connector-java - 8.0.26 - + + + commons-io + commons-io + 2.6 + compile + + + run.mone + codegen-temlates + 1.0-SNAPSHOT + compile + + + com.ibeetl + beetl + 3.15.4.RELEASE + true + + + com.mybatis-flex + mybatis-flex-codegen + 1.7.8 + + + org.springframework + spring-jdbc + 5.3.29 + true + + + com.zaxxer + HikariCP + 4.0.3 + true + + + org.apache.commons + commons-lang3 + 3.8.1 + compile + + + run.mone + infra-result + 1.6.1-jdk17-SNAPSHOT + + + thrift + org.apache.thrift + + + + + org.mybatis.generator + mybatis-generator-core + 1.4.0 + + + mysql + mysql-connector-java + 8.0.26 + + + org.mockito + mockito-core + 3.9.0 + + + org.mockito + mockito-inline + 3.9.0 + + - + + + + maven-compiler-plugin + 3.11.0 + + 17 + 17 + true + UTF-8 + + ${project.basedir}/src/main/java + + + + + diff --git a/jcommon/codegen/src/main/java/run/mone/ai/codegen/CodeGenerator.java b/jcommon/codegen/src/main/java/run/mone/ai/codegen/CodeGenerator.java index f42de3b03..dc2ddbb8b 100644 --- a/jcommon/codegen/src/main/java/run/mone/ai/codegen/CodeGenerator.java +++ b/jcommon/codegen/src/main/java/run/mone/ai/codegen/CodeGenerator.java @@ -124,6 +124,48 @@ public static void main(String[] args) { } } + public static void createAllUnitTest(Map map) { + if (map.isEmpty()) { + return; + } + //方便ai调用的时候,设置表名 + className = map.get("pojoName"); + testName = map.get("testName"); + serviceName = map.get("serviceName"); + testPackageName = map.get("testPackageName"); + testPath = map.get("testPath"); + mainClass = map.get("mainClass"); + author = map.get("author"); + testType = map.get("testType"); + + if (StringUtils.isEmpty(className)) { + return; + } + Map data = new HashMap<>(); + data.put("className", className); + data.put("author", "goodjava@qq.com"); + data.put("serviceName", serviceName); + data.put("testPackageName", testPackageName); + data.put("testPath", testPath); + data.put("mainClass", mainClass); + data.put("testAuthor", author); + data.put("testType", testType); + Optional first = Arrays.stream(testName.split("\\.")).findFirst(); + data.put("testName", first.get()); + + if (createTest) { + String cn = testName; + System.out.println("create test cn :" + cn + "path:" + testPath); + String test; + if (SPRING_BOOT_TEST_TYPE.equals(testType)) { + test = TemplateUtils.renderTemplateFromFileV2("tlp/testSpring.java", data); + } else { + test = TemplateUtils.renderTemplateFromFileV2("tlp/test.java", data); + } + TemplateUtils.writeStringToFile(test, testPath + cn); + } + } + private static Map parseArgsAndExtractData(String[] args) { String jsonStr = args[0]; 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 4d2805cbe..1c3aaac36 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 @@ -14,6 +14,8 @@ import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; +import java.nio.ByteBuffer; +import java.nio.channels.Channels; import java.nio.charset.StandardCharsets; import java.util.List; import java.util.Map; @@ -62,6 +64,39 @@ public static String renderTemplateFromFile(String templateFileName, Map m) { + try { + InputStream is = TemplateUtils.class.getClassLoader().getResourceAsStream(templateFileName); + if (is == null) { + throw new IOException("Resource not found: " + templateFileName); + } + + // 使用Channels和ByteBuffer读取所有字节 + ByteBuffer byteBuffer = ByteBuffer.allocate(1024); + int bytesRead; + while ((bytesRead = Channels.newChannel(is).read(byteBuffer)) != -1) { + if (byteBuffer.remaining() == 0) { + ByteBuffer newBuffer = ByteBuffer.allocate(byteBuffer.capacity() * 2); + byteBuffer.flip(); + newBuffer.put(byteBuffer); + byteBuffer = newBuffer; + } + } + byteBuffer.flip(); + byte[] bytes = new byte[byteBuffer.limit()]; + byteBuffer.get(bytes); + + // 将字节数组转换为字符串 + String template = new String(bytes, StandardCharsets.UTF_8); + + return renderTemplate(template, m); + } catch (IOException ex) { + log.error("Error reading template file", ex); + } + return ""; + } + //把String写到指定文件中(class) public static void writeStringToFile(String content, String filePath) { try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) { diff --git a/jcommon/codegen/src/test/java/com/xiaomi/youpin/codegen/test/GenTest.java b/jcommon/codegen/src/test/java/com/xiaomi/youpin/codegen/test/GenTest.java index 281b2c8ee..8f34f10f4 100644 --- a/jcommon/codegen/src/test/java/com/xiaomi/youpin/codegen/test/GenTest.java +++ b/jcommon/codegen/src/test/java/com/xiaomi/youpin/codegen/test/GenTest.java @@ -16,19 +16,27 @@ package com.xiaomi.youpin.codegen.test; +import com.google.common.collect.Lists; import com.xiaomi.youpin.codegen.*; import com.xiaomi.youpin.codegen.bo.ApiHeaderBo; import com.xiaomi.youpin.codegen.bo.Dependency; import com.xiaomi.youpin.infra.rpc.Result; +import org.apache.commons.lang3.tuple.Pair; import org.junit.Test; +import run.mone.ai.codegen.util.TemplateUtils; +import javax.annotation.Resource; import java.util.ArrayList; import java.util.HashMap; import java.util.List; +import java.util.Map; public class GenTest { + @Resource + private TemplateUtils templateUtils; + @Test public void testFilter() throws Exception { FilterGen filterGen = new FilterGen(); @@ -38,8 +46,8 @@ public void testFilter() throws Exception { @Test public void testHttpRequestGen() throws Exception { HttpRequestGen httpRequestGen = new HttpRequestGen(); - httpRequestGen.generateJavaReq("getUserInfo",2,13,"[{\"paramNotNull\":\"0\",\"paramType\":\"13\",\"paramName\":\"obj\",\"paramKey\":\"subObj\",\"paramValue\":\"\",\"paramLimit\":\"\",\"paramNote\":\"\",\"paramValueList\":[],\"default\":0,\"childList\":[{\"paramNotNull\":\"0\",\"paramType\":\"0\",\"paramName\":\"sub_dzx\",\"paramKey\":\"subName\",\"paramValue\":\"\",\"paramLimit\":\"\",\"paramNote\":\"\",\"paramValueList\":[],\"default\":0,\"childList\":[]},{\"paramNotNull\":\"0\",\"paramType\":\"3\",\"paramName\":\"666\",\"paramKey\":\"subID\",\"paramValue\":\"\",\"paramLimit\":\"\",\"paramNote\":\"\",\"paramValueList\":[],\"default\":0,\"childList\":[]}]}]"); - httpRequestGen.generateJavaReq("getUserInfo",2,13,"[{\"paramNotNull\":\"0\",\"paramType\":\"13\",\"paramName\":\"obj\",\"paramKey\":\"subObj\",\"paramValue\":\"\",\"paramLimit\":\"\",\"paramNote\":\"\",\"paramValueList\":[],\"default\":0,\"childList\":[{\"paramNotNull\":\"0\",\"paramType\":\"0\",\"paramName\":\"sub_dzx\",\"paramKey\":\"subName\",\"paramValue\":\"\",\"paramLimit\":\"\",\"paramNote\":\"\",\"paramValueList\":[],\"default\":0,\"childList\":[]},{\"paramNotNull\":\"0\",\"paramType\":\"3\",\"paramName\":\"666\",\"paramKey\":\"subID\",\"paramValue\":\"\",\"paramLimit\":\"\",\"paramNote\":\"\",\"paramValueList\":[],\"default\":0,\"childList\":[]}]},{\"paramNotNull\":\"0\",\"paramType\":\"0\",\"paramName\":\"dzx\",\"paramKey\":\"userName\",\"paramValue\":\"\",\"paramLimit\":\"\",\"paramNote\":\"\",\"paramValueList\":[],\"default\":0,\"childList\":[]}]"); + httpRequestGen.generateJavaReq("getUserInfo", 2, 13, "[{\"paramNotNull\":\"0\",\"paramType\":\"13\",\"paramName\":\"obj\",\"paramKey\":\"subObj\",\"paramValue\":\"\",\"paramLimit\":\"\",\"paramNote\":\"\",\"paramValueList\":[],\"default\":0,\"childList\":[{\"paramNotNull\":\"0\",\"paramType\":\"0\",\"paramName\":\"sub_dzx\",\"paramKey\":\"subName\",\"paramValue\":\"\",\"paramLimit\":\"\",\"paramNote\":\"\",\"paramValueList\":[],\"default\":0,\"childList\":[]},{\"paramNotNull\":\"0\",\"paramType\":\"3\",\"paramName\":\"666\",\"paramKey\":\"subID\",\"paramValue\":\"\",\"paramLimit\":\"\",\"paramNote\":\"\",\"paramValueList\":[],\"default\":0,\"childList\":[]}]}]"); + httpRequestGen.generateJavaReq("getUserInfo", 2, 13, "[{\"paramNotNull\":\"0\",\"paramType\":\"13\",\"paramName\":\"obj\",\"paramKey\":\"subObj\",\"paramValue\":\"\",\"paramLimit\":\"\",\"paramNote\":\"\",\"paramValueList\":[],\"default\":0,\"childList\":[{\"paramNotNull\":\"0\",\"paramType\":\"0\",\"paramName\":\"sub_dzx\",\"paramKey\":\"subName\",\"paramValue\":\"\",\"paramLimit\":\"\",\"paramNote\":\"\",\"paramValueList\":[],\"default\":0,\"childList\":[]},{\"paramNotNull\":\"0\",\"paramType\":\"3\",\"paramName\":\"666\",\"paramKey\":\"subID\",\"paramValue\":\"\",\"paramLimit\":\"\",\"paramNote\":\"\",\"paramValueList\":[],\"default\":0,\"childList\":[]}]},{\"paramNotNull\":\"0\",\"paramType\":\"0\",\"paramName\":\"dzx\",\"paramKey\":\"userName\",\"paramValue\":\"\",\"paramLimit\":\"\",\"paramNote\":\"\",\"paramValueList\":[],\"default\":0,\"childList\":[]}]"); List headers = new ArrayList<>(); ApiHeaderBo header1 = new ApiHeaderBo(); @@ -50,7 +58,7 @@ public void testHttpRequestGen() throws Exception { header2.setHeaderValue("true"); headers.add(header1); headers.add(header2); - Result result = httpRequestGen.generateCurlReq(0,"/Api/getUserInfo",1,"[{\"paramNotNull\":\"0\",\"paramType\":\"13\",\"paramName\":\"obj\",\"paramKey\":\"subObj\",\"paramValue\":\"\",\"paramLimit\":\"\",\"paramNote\":\"\",\"paramValueList\":[],\"default\":0,\"childList\":[{\"paramNotNull\":\"0\",\"paramType\":\"0\",\"paramName\":\"sub_dzx\",\"paramKey\":\"subName\",\"paramValue\":\"\",\"paramLimit\":\"\",\"paramNote\":\"\",\"paramValueList\":[],\"default\":0,\"childList\":[]},{\"paramNotNull\":\"0\",\"paramType\":\"3\",\"paramName\":\"666\",\"paramKey\":\"subID\",\"paramValue\":\"\",\"paramLimit\":\"\",\"paramNote\":\"\",\"paramValueList\":[],\"default\":0,\"childList\":[]}]}]",headers); + Result result = httpRequestGen.generateCurlReq(0, "/Api/getUserInfo", 1, "[{\"paramNotNull\":\"0\",\"paramType\":\"13\",\"paramName\":\"obj\",\"paramKey\":\"subObj\",\"paramValue\":\"\",\"paramLimit\":\"\",\"paramNote\":\"\",\"paramValueList\":[],\"default\":0,\"childList\":[{\"paramNotNull\":\"0\",\"paramType\":\"0\",\"paramName\":\"sub_dzx\",\"paramKey\":\"subName\",\"paramValue\":\"\",\"paramLimit\":\"\",\"paramNote\":\"\",\"paramValueList\":[],\"default\":0,\"childList\":[]},{\"paramNotNull\":\"0\",\"paramType\":\"3\",\"paramName\":\"666\",\"paramKey\":\"subID\",\"paramValue\":\"\",\"paramLimit\":\"\",\"paramNote\":\"\",\"paramValueList\":[],\"default\":0,\"childList\":[]}]}]", headers); System.out.print(result.getData()); } @@ -137,9 +145,10 @@ public void testDDD() throws Exception { new DDDProGen().generateAndZip("/tmp/work", "abcd", "com.xiaomi.youpin", "com.xiaomi.youpin.abcdefg", "dfz", "1.0.0", dep); //new CNSalesCrmGen().generateAndZip("/home/work", "testt", "com.xiaomi.test", "com.xiaomi.test.testt", "dfz", "1.0.0", dep); } + @Test public void testFaas() throws Exception { FaasGen gen = new FaasGen(); - gen.generateAndZip("/tmp/work", "project-faas", "com.xiaomi.youpin", "com.xiaomi.youpin.projectmimimi", "dfz", "1.0.0","modulex","functiony"); + gen.generateAndZip("/tmp/work", "project-faas", "com.xiaomi.youpin", "com.xiaomi.youpin.projectmimimi", "dfz", "1.0.0", "modulex", "functiony"); } } diff --git a/jcommon/pom.xml b/jcommon/pom.xml index ba7d08e15..03d0efe59 100644 --- a/jcommon/pom.xml +++ b/jcommon/pom.xml @@ -5,7 +5,7 @@ 4.0.0 run.mone jcommon - 1.6.2-jdk17-SNAPSHOT + 1.6.0-jdk21-SNAPSHOT pom ${project.artifactId} The parent project of jcommon From f4c3274efaf3e74315e41d7713a9ad5488ecf84f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=A0=A1=E7=82=9C?= Date: Mon, 19 Aug 2024 14:54:45 +0800 Subject: [PATCH 3/6] change pom --- jcommon/codegen/pom.xml | 20 +------------------- jcommon/pom.xml | 4 ++-- 2 files changed, 3 insertions(+), 21 deletions(-) diff --git a/jcommon/codegen/pom.xml b/jcommon/codegen/pom.xml index ac01aba6f..d79f646eb 100644 --- a/jcommon/codegen/pom.xml +++ b/jcommon/codegen/pom.xml @@ -9,7 +9,7 @@ 1.6.0-jdk21-SNAPSHOT codegen - 1.6.2-jdk17-SNAPSHOT + 1.6.0-jdk21-SNAPSHOT commons-io @@ -85,22 +85,4 @@ 3.9.0 - - - - - maven-compiler-plugin - 3.11.0 - - 17 - 17 - true - UTF-8 - - ${project.basedir}/src/main/java - - - - - diff --git a/jcommon/pom.xml b/jcommon/pom.xml index 03d0efe59..66ebff815 100644 --- a/jcommon/pom.xml +++ b/jcommon/pom.xml @@ -222,8 +222,8 @@ maven-compiler-plugin 3.11.0 - 17 - 17 + 20 + 20 true UTF-8 From fbc89e05d02a0f42d80c30ac6a22c19f8fca210f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=A0=A1=E7=82=9C?= Date: Mon, 19 Aug 2024 14:58:49 +0800 Subject: [PATCH 4/6] change infra pom --- jcommon/codegen/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jcommon/codegen/pom.xml b/jcommon/codegen/pom.xml index d79f646eb..01f03b86c 100644 --- a/jcommon/codegen/pom.xml +++ b/jcommon/codegen/pom.xml @@ -55,7 +55,7 @@ run.mone infra-result - 1.6.1-jdk17-SNAPSHOT + 1.6.0-jdk21-SNAPSHOT thrift From ddba72d09d45386e766638e148d67e8628c89fe9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=A0=A1=E7=82=9C?= Date: Wed, 21 Aug 2024 18:28:47 +0800 Subject: [PATCH 5/6] change template --- .../src/main/java/run/mone/ai/codegen/util/TemplateUtils.java | 2 +- jcommon/codegen/src/main/resources/tlp/testSpring.java | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) 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..3b10f02c6 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} ${strutil.toLowerCase(serviceName)}Service; } \ No newline at end of file From e9b36155f6494ca4292367a8ce4630c53af18805 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=A0=A1=E7=82=9C?= Date: Wed, 4 Sep 2024 17:14:41 +0800 Subject: [PATCH 6/6] refactor: fix template --- .../run/mone/ai/codegen/util/StrUtil.java | 43 +++++++++++++++++++ .../src/main/resources/tlp/testSpring.java | 2 +- .../xiaomi/youpin/codegen/test/StrTest.java | 43 +++++++++++++++++++ 3 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 jcommon/codegen/src/main/java/run/mone/ai/codegen/util/StrUtil.java create mode 100644 jcommon/codegen/src/test/java/com/xiaomi/youpin/codegen/test/StrTest.java 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/resources/tlp/testSpring.java b/jcommon/codegen/src/main/resources/tlp/testSpring.java index 3b10f02c6..e7e36c682 100644 --- a/jcommon/codegen/src/main/resources/tlp/testSpring.java +++ b/jcommon/codegen/src/main/resources/tlp/testSpring.java @@ -23,7 +23,7 @@ public class ${testName} { @Resource - private ${serviceName} ${strutil.toLowerCase(serviceName)}Service; + 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); + } + + +}