Skip to content

Commit b109d5a

Browse files
insistencegitee-org
authored andcommitted
!4 RuoYi-Vue3-FastAPI v1.0.1
Merge pull request !4 from insistence/develop
2 parents d60d6ae + d828433 commit b109d5a

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

ruoyi-fastapi-backend/utils/common_util.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import pandas as pd
22
import io
33
import os
4+
import re
45
from openpyxl import Workbook
56
from openpyxl.styles import Alignment, PatternFill
67
from openpyxl.utils import get_column_letter
@@ -39,10 +40,21 @@ def worship():
3940

4041
class CamelCaseUtil:
4142
"""
42-
下划线形式(snake_case)转换为小驼峰形式(camelCase)工具方法
43+
小驼峰形式(camelCase)与下划线形式(snake_case)互相转换工具方法
4344
"""
4445
@classmethod
45-
def __to_camel_case(cls, snake_str):
46+
def camel_to_snake(cls, camel_str):
47+
"""
48+
小驼峰形式字符串(camelCase)转换为下划线形式字符串(snake_case)
49+
:param camel_str: 小驼峰形式字符串
50+
:return: 下划线形式字符串
51+
"""
52+
# 在大写字母前添加一个下划线,然后将整个字符串转为小写
53+
words = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', camel_str)
54+
return re.sub('([a-z0-9])([A-Z])', r'\1_\2', words).lower()
55+
56+
@classmethod
57+
def snake_to_camel(cls, snake_str):
4658
"""
4759
下划线形式字符串(snake_case)转换为小驼峰形式字符串(camelCase)
4860
:param snake_str: 下划线形式字符串
@@ -64,7 +76,7 @@ def transform_result(cls, result):
6476
return result
6577
# 如果是字典,直接转换键
6678
elif isinstance(result, dict):
67-
return {cls.__to_camel_case(k): v for k, v in result.items()}
79+
return {cls.snake_to_camel(k): v for k, v in result.items()}
6880
# 如果是一组字典或其他类型的列表,遍历列表进行转换
6981
elif isinstance(result, list):
7082
return [cls.transform_result(row) if isinstance(row, (dict, Row)) else (cls.transform_result({c.name: getattr(row, c.name) for c in row.__table__.columns}) if row else row) for row in result]

0 commit comments

Comments
 (0)