* 支持 typing.Union 递归使用;
* 增加 typing.Union 对python基础类型的smart_choice, 之前仅支持ex-dataclass类型;
* 增加 ExFiled 属性 label,用于解决当非规范的字典key作为ex-dataclass属性变量名。;
python
ex_dataclass
class Header:
x_token : str = field(default=None, label="X-Token")
* 增加 ExFiled 属性 loads_factory 和 asdict_factory, 用于解决自定义类型的转换(即非python基础类型和ex-dataclass类型);
python
import datetime
from ex_dataclass import ex_dataclass, asdict, field, EXpack
dict -> 自定义类型;
def loads_f(v: str) -> datetime.datetime:
if v == "":
return datetime.datetime.now()
return datetime.datetime.strptime(v, "%Y-%m-%d")
自定义类型 -> dict;
def asdict_f(v: datetime.datetime) -> str:
return v.strftime("%Y-%m-%d")
ex_dataclass
class Date(EXpack):
created: datetime.datetime = field(default=None, loads_factory=loads_f, asdict_factory=asdict_f)
d = Date(**{"created": "2022-01-01"})
Date(created=datetime.datetime(2022, 1, 1, 0, 0))
print(d)
{'created': '2022-01-01'}
print(asdict(d))