Skip to content

Commit b3349d9

Browse files
committed
Fixes in pdl_dumper
1 parent 2b34a9a commit b3349d9

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

src/pdl/pdl_dumper.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
CallBlock,
1313
CodeBlock,
1414
ContributeTarget,
15+
ContributeValue,
1516
DataBlock,
1617
EmptyBlock,
1718
ErrorBlock,
@@ -82,6 +83,8 @@ def block_to_dict(block: pdl_ast.BlockType, json_compatible: bool) -> DumpedBloc
8283
d["kind"] = str(block.kind)
8384
if block.description is not None:
8485
d["description"] = block.description
86+
if block.role is not None:
87+
d["role"] = block.role
8588
if block.spec is not None:
8689
d["spec"] = block.spec
8790
if block.defs is not None:
@@ -205,7 +208,7 @@ def block_to_dict(block: pdl_ast.BlockType, json_compatible: bool) -> DumpedBloc
205208
if block.assign is not None:
206209
d["def"] = block.assign
207210
if set(block.contribute) != {ContributeTarget.RESULT, ContributeTarget.CONTEXT}:
208-
d["contribute"] = block.contribute
211+
d["contribute"] = contribute_to_list(block.contribute)
209212
if block.result is not None:
210213
if isinstance(block.result, FunctionBlock):
211214
d["result"] = ""
@@ -266,6 +269,18 @@ def location_to_dict(location: LocationType) -> dict[str, Any]:
266269
return {"path": location.path, "file": location.file, "table": location.table}
267270

268271

272+
def contribute_to_list(
273+
contribute: Sequence[ContributeTarget | dict[str, ContributeValue]]
274+
) -> list[str | dict[str, Any]]:
275+
acc: list[str | dict[str, Any]] = []
276+
for contrib in contribute:
277+
if isinstance(contrib, str):
278+
acc.append(str(contrib))
279+
elif isinstance(contrib, dict):
280+
acc.append({str(k): v.model_dump() for k, v in contrib.items()})
281+
return acc
282+
283+
269284
# def scope_to_dict(scope: ScopeType) -> dict[str, Any]:
270285
# d = {}
271286
# for x, v in scope.items():

tests/test_dump.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import pathlib
2+
3+
from pdl.pdl_ast_utils import iter_block_children
4+
from pdl.pdl_dumper import dump_yaml, program_to_dict
5+
from pdl.pdl_parser import PDLParseError, parse_file, parse_str
6+
from pdl.pdl_ast import BlockType, IncludeBlock
7+
8+
def has_include(block: BlockType) -> bool:
9+
if isinstance(block, IncludeBlock):
10+
return True
11+
else:
12+
b = False
13+
def f(x):
14+
nonlocal b
15+
if has_include(x):
16+
b = True
17+
iter_block_children(f,block)
18+
return b
19+
20+
def test_ast_iterators() -> None:
21+
for yaml_file_name in pathlib.Path(".").glob("**/*.pdl"):
22+
try:
23+
ast1, _ = parse_file(yaml_file_name)
24+
if has_include(ast1.root):
25+
continue
26+
d = program_to_dict(ast1)
27+
s = dump_yaml(d)
28+
ast2, _ = parse_str(s)
29+
json1 = ast1.model_dump_json()
30+
json2 = ast2.model_dump_json()
31+
if str(yaml_file_name) == "examples/talk/7-chatbot-roles.pdl":
32+
pass
33+
assert json1 == json2, yaml_file_name
34+
except PDLParseError:
35+
pass

0 commit comments

Comments
 (0)