Coverage for kye/engine/load_json.py: 24%

17 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2024-01-12 15:29 -0700

1from kye.types import Type 

2from typing import Any 

3import re 

4 

5def json_to_edges(typ: Type, val: Any, loc='', row=None, table: str=None, edge='<root>'): 

6 if val is None: 

7 return 

8 elif type(val) is list: 

9 for i, item in enumerate(val): 

10 yield from json_to_edges( 

11 table=table, 

12 row=row, 

13 loc=f'{loc}[{i}]', 

14 typ=typ, 

15 edge=edge, 

16 val=item 

17 ) 

18 elif type(val) is dict: 

19 for key, item in val.items(): 

20 if typ.has_edge(key): 

21 yield from json_to_edges( 

22 table=typ.ref, 

23 row=loc, 

24 loc=f'{loc}.{key}', 

25 typ=typ.get_edge(key), 

26 edge=key, 

27 val=item 

28 ) 

29 else: 

30 assert table is not None 

31 if type(val) is float: 

32 val = re.sub(r'\.0$', '', str(val)) 

33 

34 yield { 

35 'loc': loc, 

36 'tbl': table, 

37 'row': row, 

38 'col': edge, 

39 'val': val, 

40 }