Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a raw flag to data block #28

Merged
merged 1 commit into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 22 additions & 15 deletions pdl-live/src/pdl_ast.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1240,6 +1240,10 @@ export type Repeat =
| ErrorBlock
| EmptyBlock
)[];
/**
* Define how to combine the result of each iteration.
*
*/
export type IterationType = "sequence" | "array" | "document";
export type Trace1 =
| (
Expand Down Expand Up @@ -1425,6 +1429,11 @@ export type Repeat1 =
| ErrorBlock
| EmptyBlock
)[];
/**
* Define how to combine the result of each iteration.
*
*/
export type IterationType1 = "sequence" | "array" | "document";
export type Trace2 =
| (
| number
Expand Down Expand Up @@ -1614,6 +1623,11 @@ export type Repeat2 =
*
*/
export type NumIterations = number;
/**
* Define how to combine the result of each iteration.
*
*/
export type IterationType2 = "sequence" | "array" | "document";
export type Trace3 =
| (
| number
Expand Down Expand Up @@ -1930,6 +1944,10 @@ export type Fallback12 =
export type Role12 = string | null;
export type HasError12 = boolean;
export type Kind12 = "data";
/**
* Do not evaluate expressions inside strings.
*/
export type Raw = boolean;
/**
* Name of the variable used to store the result of the execution of the block.
*
Expand Down Expand Up @@ -3878,6 +3896,7 @@ export interface DataBlock {
has_error?: HasError12;
kind?: Kind12;
data: Data;
raw?: Raw;
}
/**
* Type specification of the result of the block.
Expand Down Expand Up @@ -4044,11 +4063,7 @@ export interface RepeatBlock {
kind?: Kind10;
repeat: Repeat2;
num_iterations: NumIterations;
/**
* Define how to combine the result of each iteration.
*
*/
as?: IterationType & string;
as?: IterationType2;
trace?: Trace3;
}
/**
Expand Down Expand Up @@ -4132,11 +4147,7 @@ export interface RepeatUntilBlock {
kind?: Kind9;
repeat: Repeat1;
until: Until;
/**
* Define how to combine the result of each iteration.
*
*/
as?: IterationType & string;
as?: IterationType1;
trace?: Trace2;
}
/**
Expand Down Expand Up @@ -4220,11 +4231,7 @@ export interface ForBlock {
kind?: Kind8;
for: For;
repeat: Repeat;
/**
* Define how to combine the result of each iteration.
*
*/
as?: IterationType & string;
as?: IterationType;
trace?: Trace1;
}
/**
Expand Down
6 changes: 6 additions & 0 deletions pdl-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -3835,6 +3835,12 @@
"data": {
"description": "Value defined.",
"title": "Data"
},
"raw": {
"default": false,
"description": "Do not evaluate expressions inside strings.",
"title": "Raw",
"type": "boolean"
}
},
"required": [
Expand Down
2 changes: 1 addition & 1 deletion pdl/pdl.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def exec_program(
state = InterpreterState(**config)
scope = scope or {}
loc = loc or empty_block_location
result = process_prog(state, scope, prog, loc)
result, _, _, _ = process_prog(state, scope, prog, loc)
return result


Expand Down
2 changes: 2 additions & 0 deletions pdl/pdl_ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,8 @@ class DataBlock(Block):
kind: Literal[BlockKind.DATA] = BlockKind.DATA
data: ExpressionType
"""Value defined."""
raw: bool = False
"""Do not evaluate expressions inside strings."""


class DocumentBlock(Block):
Expand Down
2 changes: 2 additions & 0 deletions pdl/pdl_dumper.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ def block_to_dict(block: pdl_ast.BlockType) -> int | float | str | dict[str, Any
d["get"] = block.get
case DataBlock():
d["data"] = block.data
if block.raw:
d["raw"] = block.raw
case ApiBlock():
d["api"] = block.api
d["url"] = block.url
Expand Down
21 changes: 13 additions & 8 deletions pdl/pdl_interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,16 +315,21 @@ def step_block_body(
yield YieldBackgroundMessage(background)
case DataBlock(data=v):
block.location = append(loc, "data")
result, errors = process_expr(scope, v, append(loc, "data"))
if len(errors) != 0:
result = None
background = []
trace = handle_error(
block, append(loc, "data"), None, errors, block.model_copy()
)
else:
if block.raw:
result = v
background = [{"role": state.role, "content": stringify(result)}]
trace = block.model_copy()
else:
result, errors = process_expr(scope, v, append(loc, "data"))
if len(errors) != 0:
result = None
background = []
trace = handle_error(
block, append(loc, "data"), None, errors, block.model_copy()
)
else:
background = [{"role": state.role, "content": stringify(result)}]
trace = block.model_copy()
if state.yield_result:
yield YieldResultMessage(result)
if state.yield_background:
Expand Down
64 changes: 64 additions & 0 deletions tests/test_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
from pdl.pdl import exec_str


def test_int():
prog_str = """
data: 1
"""
result = exec_str(prog_str)
assert result == 1


def test_array():
prog_str = """
data:
- 1
- 2
- 3
- bye
"""
result = exec_str(prog_str)
assert result == [1, 2, 3, "bye"]


def test_object():
prog_str = """
data:
document: Hello
model:
- a
- b
"""
result = exec_str(prog_str)
assert result == {"document": "Hello", "model": ["a", "b"]}


def test_expr():
prog_str = """
defs:
x: a
y: b
data:
document: Hello
model:
- "{{ x }}"
- "{{ y }}"
"""
result = exec_str(prog_str)
assert result == {"document": "Hello", "model": ["a", "b"]}


def test_raw():
prog_str = """
defs:
x: a
y: b
data:
document: Hello
model:
- "{{ x }}"
- "{{ y }}"
raw: true
"""
result = exec_str(prog_str)
assert result == {"document": "Hello", "model": ["{{ x }}", "{{ y }}"]}