Commit 3078fe33907ce889747270ee82757a38cdb0d65f
1 parent
1287ce90
refactor(codegen):精简 CodegenBuilder.sanitizeComment 引号替换实现并补充单测
Showing
2 changed files
with
27 additions
and
13 deletions
yudao-module-infra/src/main/java/cn/iocoder/yudao/module/infra/service/codegen/inner/CodegenBuilder.java
| ... | ... | @@ -12,6 +12,7 @@ import cn.iocoder.yudao.module.infra.enums.codegen.CodegenColumnListConditionEnu |
| 12 | 12 | import cn.iocoder.yudao.module.infra.enums.codegen.CodegenTemplateTypeEnum; |
| 13 | 13 | import com.baomidou.mybatisplus.generator.config.po.TableField; |
| 14 | 14 | import com.baomidou.mybatisplus.generator.config.po.TableInfo; |
| 15 | +import com.google.common.annotations.VisibleForTesting; | |
| 15 | 16 | import com.google.common.collect.Sets; |
| 16 | 17 | import org.springframework.stereotype.Component; |
| 17 | 18 | |
| ... | ... | @@ -117,10 +118,8 @@ public class CodegenBuilder { |
| 117 | 118 | table.setBusinessName(toCamelCase(subAfter(tableName, '_', false)).toLowerCase()); |
| 118 | 119 | // 驼峰 + 首字母大写;第一步,第一个 _ 前缀的后面,作为 class 名字;第二步,驼峰命名 |
| 119 | 120 | table.setClassName(upperFirst(toCamelCase(subAfter(tableName, '_', false)))); |
| 120 | - // 处理表注释中的特殊字符,避免生成的代码编译错误(先转义,再去掉"表"字) | |
| 121 | - String tableComment = sanitizeColumnComment(table.getTableComment()); | |
| 122 | - // 去除结尾的表,作为类描述 | |
| 123 | - table.setClassComment(StrUtil.removeSuffixIgnoreCase(tableComment, "表")); | |
| 121 | + // 去除结尾的表,作为类描述;注释中的英文引号替换为中文引号,避免破坏生成代码中的字符串字面量 | |
| 122 | + table.setClassComment(StrUtil.removeSuffixIgnoreCase(sanitizeComment(table.getTableComment()), "表")); | |
| 124 | 123 | table.setTemplateType(CodegenTemplateTypeEnum.ONE.getType()); |
| 125 | 124 | } |
| 126 | 125 | |
| ... | ... | @@ -130,8 +129,7 @@ public class CodegenBuilder { |
| 130 | 129 | for (CodegenColumnDO column : columns) { |
| 131 | 130 | column.setTableId(tableId); |
| 132 | 131 | column.setOrdinalPosition(index++); |
| 133 | - // 处理字段注释中的特殊字符,避免生成的代码编译错误 | |
| 134 | - column.setColumnComment(sanitizeColumnComment(column.getColumnComment())); | |
| 132 | + column.setColumnComment(sanitizeComment(column.getColumnComment())); | |
| 135 | 133 | // 特殊处理:Byte => Integer |
| 136 | 134 | if (Byte.class.getSimpleName().equals(column.getJavaType())) { |
| 137 | 135 | column.setJavaType(Integer.class.getSimpleName()); |
| ... | ... | @@ -220,18 +218,19 @@ public class CodegenBuilder { |
| 220 | 218 | column.setExample(randomEle(new String[]{"你猜", "随便", "你说的对"})); |
| 221 | 219 | } |
| 222 | 220 | } |
| 223 | - | |
| 221 | + | |
| 224 | 222 | /** |
| 225 | - * 清理字段注释中的特殊字符 | |
| 226 | - * 将英文引号替换为中文引号,避免生成的代码出现语法错误 | |
| 223 | + * 将注释中的英文引号替换为中文引号,避免破坏生成代码中的字符串字面量 | |
| 227 | 224 | * |
| 228 | - * @param comment 原始字段注释 | |
| 229 | - * @return 清理后的字段注释 | |
| 225 | + * @param comment 原始注释 | |
| 226 | + * @return 清理后的注释 | |
| 230 | 227 | */ |
| 231 | - private String sanitizeColumnComment(String comment) { | |
| 228 | + @VisibleForTesting | |
| 229 | + String sanitizeComment(String comment) { | |
| 232 | 230 | if (StrUtil.isEmpty(comment)) { |
| 233 | 231 | return comment; |
| 234 | 232 | } |
| 235 | - return comment.replace("\"", "\u201c").replace("\"", "\u201d").replace("'", "\u2018").replace("'", "\u2019"); | |
| 233 | + return comment.replace('"', '“').replace('\'', '‘'); | |
| 236 | 234 | } |
| 235 | + | |
| 237 | 236 | } | ... | ... |
yudao-module-infra/src/test/java/cn/iocoder/yudao/module/infra/service/codegen/inner/CodegenBuilderTest.java
| ... | ... | @@ -84,4 +84,19 @@ public class CodegenBuilderTest extends BaseMockitoUnitTest { |
| 84 | 84 | assertEquals("input", column.getHtmlType()); |
| 85 | 85 | } |
| 86 | 86 | |
| 87 | + @Test | |
| 88 | + public void testSanitizeComment() { | |
| 89 | + // 1. null / 空字符串:原样返回 | |
| 90 | + assertNull(codegenBuilder.sanitizeComment(null)); | |
| 91 | + assertEquals("", codegenBuilder.sanitizeComment("")); | |
| 92 | + // 2. 无英文引号:原样返回 | |
| 93 | + assertEquals("无引号注释", codegenBuilder.sanitizeComment("无引号注释")); | |
| 94 | + // 3. 含英文双引号:替换为中文左双引号 | |
| 95 | + assertEquals("含“双“引号", codegenBuilder.sanitizeComment("含\"双\"引号")); | |
| 96 | + // 4. 含英文单引号:替换为中文左单引号 | |
| 97 | + assertEquals("含‘单‘引号", codegenBuilder.sanitizeComment("含'单'引号")); | |
| 98 | + // 5. 双 / 单引号混合 | |
| 99 | + assertEquals("“混‘搭“‘", codegenBuilder.sanitizeComment("\"混'搭\"'")); | |
| 100 | + } | |
| 101 | + | |
| 87 | 102 | } | ... | ... |