|
6 | 6 | import datetime
|
7 | 7 | import inspect
|
8 | 8 | from enum import Enum
|
9 |
| -from typing import Any, Callable, Mapping, get_origin |
| 9 | +from typing import Any, Callable, Mapping, Type, get_args, get_origin |
10 | 10 |
|
11 | 11 | import numpy as np
|
12 | 12 |
|
13 | 13 | from .typing import (
|
14 | 14 | KEY_FIELD_NAME,
|
15 | 15 | TABLE_TYPES,
|
16 |
| - analyze_type_info, |
17 |
| - encode_enriched_type, |
18 |
| - is_namedtuple_type, |
19 |
| - is_struct_type, |
20 |
| - AnalyzedTypeInfo, |
21 | 16 | AnalyzedAnyType,
|
| 17 | + AnalyzedBasicType, |
22 | 18 | AnalyzedDictType,
|
23 | 19 | AnalyzedListType,
|
24 |
| - AnalyzedBasicType, |
| 20 | + AnalyzedStructType, |
| 21 | + AnalyzedTypeInfo, |
25 | 22 | AnalyzedUnionType,
|
26 | 23 | AnalyzedUnknownType,
|
27 |
| - AnalyzedStructType, |
| 24 | + TypeKind, |
| 25 | + analyze_type_info, |
| 26 | + encode_enriched_type, |
| 27 | + is_namedtuple_type, |
28 | 28 | is_numpy_number_type,
|
| 29 | + is_struct_type, |
29 | 30 | )
|
30 | 31 |
|
31 | 32 |
|
32 |
| -def encode_engine_value(value: Any) -> Any: |
| 33 | +def encode_engine_value( |
| 34 | + value: Any, _in_struct: bool = False, type_hint: Type[Any] | str | None = None |
| 35 | +) -> Any: |
33 | 36 | """Encode a Python value to an engine value."""
|
34 | 37 | if dataclasses.is_dataclass(value):
|
35 | 38 | return [
|
36 |
| - encode_engine_value(getattr(value, f.name)) |
| 39 | + encode_engine_value( |
| 40 | + getattr(value, f.name), _in_struct=True, type_hint=f.type |
| 41 | + ) |
37 | 42 | for f in dataclasses.fields(value)
|
38 | 43 | ]
|
39 | 44 | if is_namedtuple_type(type(value)):
|
40 |
| - return [encode_engine_value(getattr(value, name)) for name in value._fields] |
| 45 | + annotations = type(value).__annotations__ |
| 46 | + return [ |
| 47 | + encode_engine_value( |
| 48 | + getattr(value, name), _in_struct=True, type_hint=annotations.get(name) |
| 49 | + ) |
| 50 | + for name in value._fields |
| 51 | + ] |
41 | 52 | if isinstance(value, np.number):
|
42 | 53 | return value.item()
|
43 | 54 | if isinstance(value, np.ndarray):
|
44 | 55 | return value
|
45 | 56 | if isinstance(value, (list, tuple)):
|
46 |
| - return [encode_engine_value(v) for v in value] |
| 57 | + return [encode_engine_value(v, _in_struct) for v in value] |
47 | 58 | if isinstance(value, dict):
|
| 59 | + is_json_type = type_hint and any( |
| 60 | + isinstance(arg, TypeKind) and arg.kind == "Json" |
| 61 | + for arg in get_args(type_hint)[1:] |
| 62 | + ) |
| 63 | + |
| 64 | + # For empty dicts, check type hints if in a struct context |
| 65 | + # when no contexts are provided, return an empty dict as default |
48 | 66 | if not value:
|
| 67 | + if _in_struct: |
| 68 | + return value if is_json_type else [] |
49 | 69 | return {}
|
50 | 70 |
|
51 | 71 | first_val = next(iter(value.values()))
|
52 | 72 | if is_struct_type(type(first_val)): # KTable
|
53 | 73 | return [
|
54 |
| - [encode_engine_value(k)] + encode_engine_value(v) |
| 74 | + [encode_engine_value(k, _in_struct)] |
| 75 | + + encode_engine_value(v, _in_struct) |
55 | 76 | for k, v in value.items()
|
56 | 77 | ]
|
57 | 78 | return value
|
|
0 commit comments