|
37 | 37 | ]
|
38 | 38 |
|
39 | 39 |
|
| 40 | +def identity(x): |
| 41 | + return x |
| 42 | + |
| 43 | + |
40 | 44 | class IndefiniteList(UserList):
|
41 | 45 | def __init__(self, li: Primitive): # type: ignore
|
42 | 46 | super().__init__(li) # type: ignore
|
@@ -415,6 +419,25 @@ def _restore_dataclass_field(
|
415 | 419 | return f.type.from_primitive(v)
|
416 | 420 | elif isclass(f.type) and issubclass(f.type, IndefiniteList):
|
417 | 421 | return IndefiniteList(v)
|
| 422 | + elif hasattr(f.type, "__origin__") and (f.type.__origin__ is dict): |
| 423 | + t_args = f.type.__args__ |
| 424 | + if len(t_args) != 2: |
| 425 | + raise DeserializeException( |
| 426 | + f"Dict types need exactly two type arguments, but got {t_args}" |
| 427 | + ) |
| 428 | + key_t = t_args[0] |
| 429 | + val_t = t_args[1] |
| 430 | + if isclass(key_t) and issubclass(key_t, CBORSerializable): |
| 431 | + key_converter = key_t.from_primitive |
| 432 | + else: |
| 433 | + key_converter = identity |
| 434 | + if isclass(val_t) and issubclass(val_t, CBORSerializable): |
| 435 | + val_converter = val_t.from_primitive |
| 436 | + else: |
| 437 | + val_converter = identity |
| 438 | + if not isinstance(v, dict): |
| 439 | + raise DeserializeException(f"Expected dict type but got {type(v)}") |
| 440 | + return {key_converter(key): val_converter(val) for key, val in v.items()} |
418 | 441 | elif hasattr(f.type, "__origin__") and (
|
419 | 442 | f.type.__origin__ is Union or f.type.__origin__ is Optional
|
420 | 443 | ):
|
|
0 commit comments