fix: 修复转大驼峰命名失败
This commit is contained in:
parent
16a8170e86
commit
8f3ea1e87f
|
@ -3,8 +3,6 @@ package cn.bunny.core;
|
||||||
import com.google.common.base.CaseFormat;
|
import com.google.common.base.CaseFormat;
|
||||||
import org.assertj.core.util.introspection.CaseFormatUtils;
|
import org.assertj.core.util.introspection.CaseFormatUtils;
|
||||||
|
|
||||||
import java.util.regex.Pattern;
|
|
||||||
|
|
||||||
/* 类型转换,数据库转Java类型等 */
|
/* 类型转换,数据库转Java类型等 */
|
||||||
public class TypeConvertCore {
|
public class TypeConvertCore {
|
||||||
|
|
||||||
|
@ -16,17 +14,17 @@ public class TypeConvertCore {
|
||||||
|
|
||||||
columnType = columnType.toLowerCase();
|
columnType = columnType.toLowerCase();
|
||||||
return switch (columnType) {
|
return switch (columnType) {
|
||||||
case "varchar", "char", "text", "longtext", "mediumtext", "tinytext" -> "String";
|
case "varchar" , "char" , "text" , "longtext" , "mediumtext" , "tinytext" -> "String";
|
||||||
case "int", "integer", "tinyint", "smallint" -> "Integer";
|
case "int" , "integer" , "tinyint" , "smallint" -> "Integer";
|
||||||
case "bigint" -> "Long";
|
case "bigint" -> "Long";
|
||||||
case "decimal", "numeric" -> "BigDecimal";
|
case "decimal" , "numeric" -> "BigDecimal";
|
||||||
case "float" -> "Float";
|
case "float" -> "Float";
|
||||||
case "double" -> "Double";
|
case "double" -> "Double";
|
||||||
case "boolean", "bit", "tinyint unsigned" -> "Boolean";
|
case "boolean" , "bit" , "tinyint unsigned" -> "Boolean";
|
||||||
case "date", "year" -> "Date";
|
case "date" , "year" -> "Date";
|
||||||
case "time" -> "Time";
|
case "time" -> "Time";
|
||||||
case "datetime", "timestamp" -> "LocalDateTime";
|
case "datetime" , "timestamp" -> "LocalDateTime";
|
||||||
case "blob", "longblob", "mediumblob", "tinyblob" -> "byte[]";
|
case "blob" , "longblob" , "mediumblob" , "tinyblob" -> "byte[]";
|
||||||
default -> "Object";
|
default -> "Object";
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -49,27 +47,15 @@ public class TypeConvertCore {
|
||||||
public static String convertToCamelCase(String name, boolean firstLetterCapital) {
|
public static String convertToCamelCase(String name, boolean firstLetterCapital) {
|
||||||
if (name == null || name.isEmpty()) return name;
|
if (name == null || name.isEmpty()) return name;
|
||||||
|
|
||||||
|
// 转成小驼峰
|
||||||
|
String lowerCamelCase = CaseFormatUtils.toCamelCase(name);
|
||||||
|
|
||||||
// 首字母不大写
|
// 首字母不大写
|
||||||
if (!firstLetterCapital) return CaseFormatUtils.toCamelCase(name);
|
if (!firstLetterCapital) {
|
||||||
|
return lowerCamelCase;
|
||||||
// 检查是否全大写带下划线 (UPPER_UNDERSCORE)
|
|
||||||
if (Pattern.matches("^[A-Z]+(_[A-Z]+)*$", name)) {
|
|
||||||
return CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, name);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 检查是否小写带下划线 (LOWER_UNDERSCORE)
|
// 将小驼峰转成大驼峰
|
||||||
if (Pattern.matches("^[a-z]+(_[a-z]+)*$", name)) {
|
return CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_CAMEL, lowerCamelCase);
|
||||||
return CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, name);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 检查是否大驼峰 (UpperCamelCase)
|
|
||||||
if (Character.isUpperCase(name.charAt(0)) &&
|
|
||||||
!name.contains("_") &&
|
|
||||||
name.chars().anyMatch(Character::isLowerCase)) {
|
|
||||||
return CaseFormat.UPPER_CAMEL.to(CaseFormat.UPPER_CAMEL, name);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 默认认为是小驼峰 (lowerCamelCase)
|
|
||||||
return CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_CAMEL, name);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,11 +8,11 @@ import java.util.Map;
|
||||||
public class VmsUtil {
|
public class VmsUtil {
|
||||||
|
|
||||||
private static final Map<String, String> TYPE_MAPPINGS = Map.of(
|
private static final Map<String, String> TYPE_MAPPINGS = Map.of(
|
||||||
"controller", "Controller",
|
"controller" , "Controller" ,
|
||||||
"service", "Service",
|
"service" , "Service" ,
|
||||||
"serviceImpl", "ServiceImpl",
|
"serviceImpl" , "ServiceImpl" ,
|
||||||
"mapper", "Mapper",
|
"mapper" , "Mapper" ,
|
||||||
"resourceMapper", "Mapper"
|
"resourceMapper" , "Mapper"
|
||||||
);
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -22,19 +22,20 @@ public class VmsUtil {
|
||||||
* @param className 类名
|
* @param className 类名
|
||||||
*/
|
*/
|
||||||
public static String handleVmFilename(String path, String className) {
|
public static String handleVmFilename(String path, String className) {
|
||||||
String[] splitPaths = path.split("/");
|
String[] splitPaths = path.split("/" );
|
||||||
int splitPathsSize = splitPaths.length - 1;
|
int splitPathsSize = splitPaths.length - 1;
|
||||||
|
|
||||||
// 大驼峰名称
|
// 大驼峰名称
|
||||||
String CamelCase = TypeConvertCore.convertToCamelCase(className, true);
|
String CamelCase = TypeConvertCore.convertToCamelCase(className, true);
|
||||||
// 小驼峰名称
|
// 小驼峰名称
|
||||||
String camelCase = TypeConvertCore.convertToCamelCase(className);
|
String camelCase = TypeConvertCore.convertToCamelCase(className);
|
||||||
|
System.out.println("CamelCase:" + CamelCase);
|
||||||
|
System.out.println("camelCase:" + camelCase);
|
||||||
// 当前文件名
|
// 当前文件名
|
||||||
String filename = splitPaths[splitPathsSize];
|
String filename = splitPaths[splitPathsSize];
|
||||||
filename = filename.replace(".vm", "");
|
filename = filename.replace(".vm" , "" );
|
||||||
|
|
||||||
String[] split = filename.split("\\.");
|
String[] split = filename.split("\\." );
|
||||||
// 文件名称
|
// 文件名称
|
||||||
String name = split[0];
|
String name = split[0];
|
||||||
// 文件扩展名
|
// 文件扩展名
|
||||||
|
@ -46,15 +47,15 @@ public class VmsUtil {
|
||||||
// 判断是否是 Java 或者 xml 文件
|
// 判断是否是 Java 或者 xml 文件
|
||||||
String typeMappingsFilename = TYPE_MAPPINGS.get(name);
|
String typeMappingsFilename = TYPE_MAPPINGS.get(name);
|
||||||
typeMappingsFilename = typeMappingsFilename == null ? "" : typeMappingsFilename;
|
typeMappingsFilename = typeMappingsFilename == null ? "" : typeMappingsFilename;
|
||||||
if (filename.contains("java") || filename.contains("xml")) {
|
if (filename.contains("java" ) || filename.contains("xml" )) {
|
||||||
filename = CamelCase + typeMappingsFilename + "." + extension;
|
filename = CamelCase + typeMappingsFilename + "." + extension;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (filename.contains("vue") && !filename.contains("index")) {
|
if (filename.contains("vue" ) && !filename.contains("index" )) {
|
||||||
filename = CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_HYPHEN, camelCase) + "-" + name + "." + extension;
|
filename = CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_HYPHEN, camelCase) + "-" + name + "." + extension;
|
||||||
}
|
}
|
||||||
|
|
||||||
splitPaths[splitPathsSize] = filename;
|
splitPaths[splitPathsSize] = filename;
|
||||||
return String.join("/", splitPaths);
|
return String.join("/" , splitPaths);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,28 +9,35 @@ public class StringFormatTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void test1() {
|
void test1() {
|
||||||
System.out.println(CaseFormatUtils.toCamelCase("user_login"));
|
System.out.println(CaseFormatUtils.toCamelCase("user_login" ));
|
||||||
System.out.println(CaseFormatUtils.toCamelCase("userLogin"));
|
System.out.println(CaseFormatUtils.toCamelCase("userLogin" ));
|
||||||
System.out.println(CaseFormatUtils.toCamelCase("UserLogin"));
|
System.out.println(CaseFormatUtils.toCamelCase("UserLogin" ));
|
||||||
|
|
||||||
System.out.println("--------------------------------");
|
System.out.println("--------------------------------" );
|
||||||
|
|
||||||
System.out.println(StringUtils.lowerCase("user_login"));
|
System.out.println(StringUtils.lowerCase("user_login" ));
|
||||||
System.out.println(StringUtils.lowerCase("userLogin"));
|
System.out.println(StringUtils.lowerCase("userLogin" ));
|
||||||
System.out.println(StringUtils.lowerCase("UserLogin"));
|
System.out.println(StringUtils.lowerCase("UserLogin" ));
|
||||||
|
|
||||||
System.out.println("--------------------------------");
|
System.out.println("--------------------------------" );
|
||||||
|
|
||||||
System.out.println(StringUtils.upperCase("user_login"));
|
System.out.println(StringUtils.upperCase("user_login" ));
|
||||||
System.out.println(StringUtils.upperCase("userLogin"));
|
System.out.println(StringUtils.upperCase("userLogin" ));
|
||||||
System.out.println(StringUtils.upperCase("UserLogin"));
|
System.out.println(StringUtils.upperCase("UserLogin" ));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void test2() {
|
void test2() {
|
||||||
System.out.println(TypeConvertCore.convertToCamelCase("user_login_A"));
|
System.out.println(TypeConvertCore.convertToCamelCase("user_login_A" ));
|
||||||
System.out.println(TypeConvertCore.convertToCamelCase("User_Login_A"));
|
System.out.println(TypeConvertCore.convertToCamelCase("User_Login_A" ));
|
||||||
System.out.println(TypeConvertCore.convertToCamelCase("userLoginA"));
|
System.out.println(TypeConvertCore.convertToCamelCase("userLoginA" ));
|
||||||
System.out.println(TypeConvertCore.convertToCamelCase("UserLoginA"));
|
System.out.println(TypeConvertCore.convertToCamelCase("UserLoginA" ));
|
||||||
|
|
||||||
|
System.out.println("--------------------------------" );
|
||||||
|
|
||||||
|
System.out.println(TypeConvertCore.convertToCamelCase("i18n_type_A" , true));
|
||||||
|
System.out.println(TypeConvertCore.convertToCamelCase("User_Login_A" , true));
|
||||||
|
System.out.println(TypeConvertCore.convertToCamelCase("userLoginA" , true));
|
||||||
|
System.out.println(TypeConvertCore.convertToCamelCase("UserLoginA" , true));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,8 @@
|
||||||
|
{
|
||||||
|
"hash": "727e0168",
|
||||||
|
"configHash": "10198a12",
|
||||||
|
"lockfileHash": "530f8857",
|
||||||
|
"browserHash": "41b39cb9",
|
||||||
|
"optimized": {},
|
||||||
|
"chunks": {}
|
||||||
|
}
|
|
@ -0,0 +1,3 @@
|
||||||
|
{
|
||||||
|
"type": "module"
|
||||||
|
}
|
Loading…
Reference in New Issue