# -*- coding: utf-8 -*- """Java 侧驼峰迁移: 1. application.yml 移除 jackson.property-naming-strategy: SNAKE_CASE 2. @JsonProperty("snake_case") -> @JsonProperty("camelCase") 3. Map 出入参 key(.put/.get/.remove/.has/.containsKey)snake -> camel 排除 Redis 登录态内部存储(LoginInfo / RedisTokenStore),那是自造 key,与 API 无关 4. @Select SQL 里 `as snake_alias` 输出别名 -> camel 注意:绝不动 MyBatis 条件列名(eq/orderBy 等),那才是数据库字段。 """ import re from pathlib import Path from _root import ROOT SRC = ROOT / "yoshop-java" / "src" / "main" / "java" YML = ROOT / "yoshop-java" / "src" / "main" / "resources" / "application.yml" # Redis 登录态内部存储 key 不转(自造 JSON,与 API 出入参无关) EXCLUDE_FILES = { SRC / "cn" / "yoshop" / "common" / "auth" / "LoginInfo.java", SRC / "cn" / "yoshop" / "common" / "auth" / "RedisTokenStore.java", } SNAKE = re.compile(r'^[a-z]+(_[a-z0-9]+)+$') def to_camel(s: str) -> str: return re.sub(r'_([a-z0-9])', lambda m: m.group(1).upper(), s) # 方法名 -> 是否处理。只处理 Map/JsonNode 的 key 读写,不碰数据库列名。 KEY_METHODS = ("put", "putIfAbsent", "get", "remove", "has", "containsKey", "getOrDefault") CALL = re.compile(r'\.(' + "|".join(KEY_METHODS) + r')\("([a-z0-9_]+)"(?=[,)])') JP = re.compile(r'@JsonProperty\("([a-z0-9_]+)"\)') SQL_AS = re.compile(r'\bas ([a-z]+(_[a-z0-9]+)+)\b') changed_files = [] total = {"jp": 0, "call": 0, "sql": 0} for f in sorted(SRC.rglob("*.java")): text = f.read_text(encoding="utf-8") orig = text def jp_repl(m): total["jp"] += 1 return '@JsonProperty("%s")' % to_camel(m.group(1)) def call_repl(m): key = m.group(2) if not SNAKE.match(key): return m.group(0) total["call"] += 1 return '.%s("%s")' % (m.group(1), to_camel(key)) def sql_repl(m): total["sql"] += 1 return 'as %s' % to_camel(m.group(1)) text = JP.sub(jp_repl, text) if f not in EXCLUDE_FILES: text = CALL.sub(call_repl, text) text = SQL_AS.sub(sql_repl, text) if text != orig: f.write_text(text, encoding="utf-8") changed_files.append(str(f.relative_to(SRC))) # application.yml:移除全局 SNAKE_CASE yml_text = YML.read_text(encoding="utf-8") new_yml = re.sub(r'\n[ \t]*property-naming-strategy:.*', '', yml_text) if new_yml != yml_text: YML.write_text(new_yml, encoding="utf-8") print("application.yml: 已移除 property-naming-strategy: SNAKE_CASE") print("改动文件 %d 个;@JsonProperty %d 处、Map key %d 处、SQL 别名 %d 处" % (len(changed_files), total["jp"], total["call"], total["sql"])) for n in changed_files: print(" ", n)