Coverage for app/ddd/infrastructure/util/convert.py: 100%

12 statements  

« prev     ^ index     » next       coverage.py v7.6.10, created at 2025-01-15 01:44 +0000

1from collections.abc import Callable 

2from typing import Any 

3 

4from pydantic.alias_generators import to_camel, to_snake 

5 

6 

7def recursive_to_convert(d: dict[str,Any], convert: Callable[[str],str]) -> dict[str,Any]: 

8 """ 

9 再帰的にケース変換を行う. 

10 

11 ケースが混ざると後勝ちになる 

12 """ 

13 new_d = {} 

14 for k, v in d.items(): 

15 new_d[convert(k)] = recursive_to_convert(v,convert) if isinstance(v,dict) else v 

16 return new_d 

17 

18def recursive_to_camel(d: dict[str,Any]) -> dict[str,Any]: 

19 return recursive_to_convert(d, to_camel) 

20 

21def recursive_to_snake(d: dict[str,Any]) -> dict[str,Any]: 

22 return recursive_to_convert(d, to_snake)