Files
wwjcloud-nest-v1/wwjcloud-nest-v1/tools/java-to-nestjs-migration/converters/utils/object.converter.js
wanwu abf384b145 feat: 基于Java→V1映射表重写核心转换器
 新增文件:
- converters/JAVA_TO_V1_MAPPING.md (完整映射表文档)

 重写转换器 (基于V1框架能力):
1. mybatis/query-wrapper.converter.js
   - QueryWrapper → 标记TODO,需用TypeORM Repository重写

2. mybatis/mapper.converter.js
   - xxxMapper.selectPage() → this.xxxRepository.findAndCount()
   - xxxMapper.selectOne() → this.xxxRepository.findOne()
   - xxxMapper.insert/update() → this.xxxRepository.save()

3. mybatis/pagination.converter.js
   - IPage<T> → [T[], number]
   - Page<T> → { skip, take }

4. utils/json.converter.js
   - JSONUtil.parseObj() → JsonUtils.parseObject()
   - JSONUtil.toBean() → Object.assign()
   - JsonLoadUtils → fs + JsonUtils

5. utils/object.converter.js
   - ObjectUtil → CommonUtils (@wwjBoot)
   - BeanUtils.copyProperties() → Object.assign()
   - Assert → if + throw BadRequestException
   - ImageUtils → fs.readFileSync()

🎯 核心策略:
- 不改业务逻辑,只换Java写法为V1写法
- MyBatis → TypeORM Repository
- Java Utils → V1 Boot层Utils
- 映射V1已有能力,避免重复造轮子
2025-10-29 15:21:06 +08:00

114 lines
4.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* Object工具转换器
*
* Java Object utils → V1 CommonUtils (from @wwjBoot)
*
* 映射关系参考JAVA_TO_V1_MAPPING.md
* - ObjectUtil.isNotEmpty(obj) → CommonUtils.isNotEmpty(obj)
* - ObjectUtil.isEmpty(obj) → CommonUtils.isEmpty(obj)
* - BeanUtils.copyProperties(src, dest) → Object.assign(dest, src)
* - Assert.notNull(obj, msg) → if (!obj) throw new BadRequestException(msg)
* - Assert.isTrue(bool, msg) → if (!bool) throw new BadRequestException(msg)
*/
class ObjectConverter {
/**
* 转换Object工具
*/
convert(javaCode) {
let tsCode = javaCode;
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 【ObjectUtil】→ CommonUtils (@wwjBoot)
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// ObjectUtil.isNotEmpty(obj) → CommonUtils.isNotEmpty(obj)
tsCode = tsCode.replace(
/ObjectUtil\.isNotEmpty\(([^)]+)\)/g,
'CommonUtils.isNotEmpty($1)'
);
// ObjectUtil.isEmpty(obj) → CommonUtils.isEmpty(obj)
tsCode = tsCode.replace(
/ObjectUtil\.isEmpty\(([^)]+)\)/g,
'CommonUtils.isEmpty($1)'
);
// ObjectUtils.isNotEmpty(obj) → CommonUtils.isNotEmpty(obj)
tsCode = tsCode.replace(
/ObjectUtils\.isNotEmpty\(([^)]+)\)/g,
'CommonUtils.isNotEmpty($1)'
);
// ObjectUtils.isEmpty(obj) → CommonUtils.isEmpty(obj)
tsCode = tsCode.replace(
/ObjectUtils\.isEmpty\(([^)]+)\)/g,
'CommonUtils.isEmpty($1)'
);
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 【BeanUtils】→ Object.assign (TypeScript 原生)
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// BeanUtils.copyProperties(src, dest) → Object.assign(dest, src)
tsCode = tsCode.replace(
/BeanUtils\.copyProperties\(([^,]+),\s*([^)]+)\)/g,
'Object.assign($2, $1)'
);
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 【Assert】→ if + throw BadRequestException
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// Assert.notNull(obj, "msg") → if (!obj) throw new BadRequestException("msg")
tsCode = tsCode.replace(
/Assert\.notNull\(([^,]+),\s*([^)]+)\);?/g,
'if (!$1) throw new BadRequestException($2);'
);
// Assert.isTrue(condition, "msg") → if (!condition) throw new BadRequestException("msg")
tsCode = tsCode.replace(
/Assert\.isTrue\(([^,]+),\s*([^)]+)\);?/g,
'if (!($1)) throw new BadRequestException($2);'
);
// Assert.isFalse(condition, "msg") → if (condition) throw new BadRequestException("msg")
tsCode = tsCode.replace(
/Assert\.isFalse\(([^,]+),\s*([^)]+)\);?/g,
'if ($1) throw new BadRequestException($2);'
);
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 【ImageUtils】→ fs.readFileSync (Node.js 原生)
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// ImageUtils.imageToBase64(path) → fs.readFileSync(path, 'base64')
tsCode = tsCode.replace(
/ImageUtils\.imageToBase64\(([^)]+)\)/g,
'fs.readFileSync($1, \'base64\')'
);
return tsCode;
}
/**
* 分析需要的imports
*/
analyzeImports(tsCode) {
const imports = new Set();
// 检查是否使用了CommonUtils
if (tsCode.includes('CommonUtils')) {
imports.add('CommonUtils'); // 从@wwjBoot导入
}
// 检查是否使用了BadRequestException
if (tsCode.includes('BadRequestException')) {
imports.add('nestjs:BadRequestException');
}
return Array.from(imports);
}
}
module.exports = ObjectConverter;