Skip to content

Commit

Permalink
feat(sdk-python): Port in-line expressions to Python SDK (#1166)
Browse files Browse the repository at this point in the history
Ports #1076 and #1164 SDK changes to Python.
  • Loading branch information
Snarr authored Dec 2, 2024
1 parent 979c869 commit 9d1de83
Show file tree
Hide file tree
Showing 9 changed files with 362 additions and 110 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -214,5 +214,5 @@ public interface WfRunVariable extends LHExpression {
* provided by the Json Path is mutated.
* @param rhs is the value to set this WfRunVariable to.
*/
void assignTo(Serializable rhs);
void assign(Serializable rhs);
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,14 @@ class WfRunVariableImpl implements WfRunVariable {

public WfRunVariableImpl(String name, Object typeOrDefaultVal, WorkflowThreadImpl parent) {
this.name = name;
this.typeOrDefaultVal = typeOrDefaultVal;
this.parent = parent;

if (typeOrDefaultVal == null) {
throw new IllegalArgumentException(
"The 'typeOrDefaultVal' argument must be either a VariableType or a default value, but a null value was provided.");
}
this.typeOrDefaultVal = typeOrDefaultVal;

// As per GH Issue #582, the default is now PRIVATE_VAR.
this.accessLevel = WfRunVariableAccessLevel.PRIVATE_VAR;
initializeType();
Expand All @@ -49,13 +54,8 @@ private void initializeType() {
if (typeOrDefaultVal instanceof VariableType) {
this.type = (VariableType) typeOrDefaultVal;
} else {
try {
this.defaultValue = LHLibUtil.objToVarVal(typeOrDefaultVal);
this.type = LHLibUtil.fromValueCase(defaultValue.getValueCase());
} catch (LHSerdeError e) {
throw new IllegalArgumentException(
"Was unable to convert provided default value to LH Variable Type", e);
}
setDefaultValue(typeOrDefaultVal);
this.type = LHLibUtil.fromValueCase(defaultValue.getValueCase());
}
}

Expand Down Expand Up @@ -98,15 +98,21 @@ public WfRunVariable required() {

@Override
public WfRunVariable withDefault(Object defaultVal) {
setDefaultValue(defaultVal);

if (!LHLibUtil.fromValueCase(defaultValue.getValueCase()).equals(type)) {
throw new IllegalArgumentException("Default value type does not match LH variable type " + type);
}

return this;
}

private void setDefaultValue(Object defaultVal) {
try {
VariableValue attempt = LHLibUtil.objToVarVal(defaultVal);
if (!LHLibUtil.fromValueCase(attempt.getValueCase()).equals(type)) {
throw new IllegalArgumentException("Default value type does not match variable type");
}
this.defaultValue = LHLibUtil.objToVarVal(defaultVal);
} catch (LHSerdeError e) {
throw new IllegalArgumentException("Was unable to convert provided default value to LH Variable Type", e);
}
return this;
}

@Override
Expand Down Expand Up @@ -175,7 +181,7 @@ public WorkflowConditionImpl isNotIn(Serializable rhs) {
}

@Override
public void assignTo(Serializable rhs) {
public void assign(Serializable rhs) {
parent.mutate(this, VariableMutationType.ASSIGN, rhs);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ void mutationsShouldUseVariableAssignment() {
// Deprecated the literal_value and node_output approach
Workflow workflow = new WorkflowImpl("obiwan", wf -> {
WfRunVariable myVar = wf.addVariable("my-var", VariableType.STR);
myVar.assignTo("some-value");
myVar.assign("some-value");
});

PutWfSpecRequest wfSpec = workflow.compileWorkflow();
Expand All @@ -570,7 +570,7 @@ void nodeOutputMutationsShouldAlsoUseVariableAssignments() {
// Deprecated the literal_value and node_output approach
Workflow workflow = new WorkflowImpl("obiwan", wf -> {
WfRunVariable myVar = wf.addVariable("my-var", VariableType.STR);
myVar.assignTo(wf.execute("use-the-force"));
myVar.assign(wf.execute("use-the-force"));
});

PutWfSpecRequest wfSpec = workflow.compileWorkflow();
Expand All @@ -590,7 +590,7 @@ void nodeOutputMutationsShouldCarryJsonPath() {
// Deprecated the literal_value and node_output approach
Workflow workflow = new WorkflowImpl("obiwan", wf -> {
WfRunVariable myVar = wf.addVariable("my-var", VariableType.STR);
myVar.assignTo(wf.execute("use-the-force").jsonPath("$.hello.there"));
myVar.assign(wf.execute("use-the-force").jsonPath("$.hello.there"));
});

PutWfSpecRequest wfSpec = workflow.compileWorkflow();
Expand All @@ -614,7 +614,7 @@ void assigningVariablesToOtherVariablesShouldUseVariableAssignment() {
Workflow workflow = new WorkflowImpl("obiwan", wf -> {
WfRunVariable myVar = wf.addVariable("my-var", VariableType.STR);
WfRunVariable otherVar = wf.addVariable("other-var", VariableType.STR);
myVar.assignTo(otherVar);
myVar.assign(otherVar);
});

PutWfSpecRequest wfSpec = workflow.compileWorkflow();
Expand All @@ -632,7 +632,7 @@ void assigningVariablesToOtherVariablesShouldCarryJsonPath() {
Workflow workflow = new WorkflowImpl("obiwan", wf -> {
WfRunVariable myVar = wf.addVariable("my-var", VariableType.STR);
WfRunVariable otherVar = wf.addVariable("other-var", VariableType.JSON_OBJ);
myVar.assignTo(otherVar.jsonPath("$.hello.there"));
myVar.assign(otherVar.jsonPath("$.hello.there"));
});

PutWfSpecRequest wfSpec = workflow.compileWorkflow();
Expand Down
2 changes: 1 addition & 1 deletion sdk-python/examples/basic/example_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,4 @@ async def main() -> None:


if __name__ == "__main__":
asyncio.run(main())
asyncio.run(main())
Loading

0 comments on commit 9d1de83

Please sign in to comment.