Skip to content

Commit

Permalink
Merge branch 'master' into doc/change-reminder-task-method-name
Browse files Browse the repository at this point in the history
  • Loading branch information
KarlaCarvajal authored Dec 4, 2024
2 parents 4f809ce + 806d5a1 commit 8c7b576
Show file tree
Hide file tree
Showing 12 changed files with 398 additions and 127 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 @@ -60,24 +60,17 @@ public LHTaskSignature(String taskDefName, Object executable, String lhTaskMetho
+ " on "
+ executable.getClass());
}
VariableType returnType = LHLibUtil.javaClassToLHVarType(taskMethod.getReturnType());
boolean maskedValue = false;
String outputSchemaVarName = "output";
if (taskMethod.isAnnotationPresent(LHType.class)) {
LHType type = taskMethod.getAnnotation(LHType.class);
maskedValue = type.masked();
if (!type.name().isEmpty() || !type.name().isBlank()) {
outputSchemaVarName = type.name();
}

Class<?> returnType = taskMethod.getReturnType();

buildInputVarsSignature();

if (!void.class.isAssignableFrom(returnType)) {
buildOutputSchemaSignature(returnType);
}
outputSchema = TaskDefOutputSchema.newBuilder()
.setValueDef(VariableDef.newBuilder()
.setType(returnType)
.setName(outputSchemaVarName)
.setMaskedValue(maskedValue)
.build())
.build();
}

private void buildInputVarsSignature() {
for (int i = 0; i < taskMethod.getParameterCount(); i++) {
Parameter param = taskMethod.getParameters()[i];
if (param.getType().equals(WorkerContext.class)) {
Expand Down Expand Up @@ -105,6 +98,26 @@ public LHTaskSignature(String taskDefName, Object executable, String lhTaskMetho
}
}

private void buildOutputSchemaSignature(Class<?> classReturnType) {
VariableType returnType = LHLibUtil.javaClassToLHVarType(classReturnType);
boolean maskedValue = false;
String outputSchemaVarName = "output";
if (taskMethod.isAnnotationPresent(LHType.class)) {
LHType type = taskMethod.getAnnotation(LHType.class);
maskedValue = type.masked();
if (!type.name().isEmpty() || !type.name().isBlank()) {
outputSchemaVarName = type.name();
}
}
outputSchema = TaskDefOutputSchema.newBuilder()
.setValueDef(VariableDef.newBuilder()
.setType(returnType)
.setName(outputSchemaVarName)
.setMaskedValue(maskedValue)
.build())
.build();
}

private String varNameFromParameterName(Parameter param) {
if (!param.isNamePresent()) {
log.warn(
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 8c7b576

Please sign in to comment.