Skip to content

Commit

Permalink
add step data model
Browse files Browse the repository at this point in the history
  • Loading branch information
mfleader committed Dec 4, 2023
1 parent 3ed1440 commit 244b2ee
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 16 deletions.
76 changes: 63 additions & 13 deletions workflow/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ func (e *executor) Prepare(workflow *Workflow, workflowContext map[string][]byte
return nil, err
}

dataModel, err := getDatModel(runnableSteps, stepLifecycles)
if err != nil {
return nil, err
}

// Now we can construct an internal data model for the output data model provided by the steps. This is the schema
// the expressions evaluate against. You can use this to do static code analysis on the expressions.
internalDataModel := e.buildInternalDataModel(typedInput, stepOutputProperties)
Expand All @@ -124,7 +129,7 @@ func (e *executor) Prepare(workflow *Workflow, workflowContext map[string][]byte
// through them again to verify that all inputs are valid. So verify that all required inputs are present, schemas
// are valid, etc.
// Do this by looping through the steps' inputs, then verifying that the dag can provide them.
if err := e.verifyWorkflowStageInputs(workflow, workflowContext, stepLifecycles, dag, internalDataModel); err != nil {
if err := e.verifyWorkflowStageInputs(workflow, workflowContext, stepLifecycles, dag, internalDataModel, dataModel); err != nil {
return nil, err
}
// Stage 6: The output data model
Expand Down Expand Up @@ -336,13 +341,14 @@ func (e *executor) verifyWorkflowStageInputs(
stepLifecycles map[string]step.Lifecycle[step.LifecycleStageWithSchema],
dag dgraph.DirectedGraph[*DAGItem],
internalDataModel *schema.ScopeSchema,
dataModel any,
) error {
// Loop through all the steps in the engine
for stepID /*stepData is the unused key*/ := range workflow.Steps {
// Then loop through the stages of that step
lifecycle := stepLifecycles[stepID]
for _, stage := range lifecycle.Stages {
err := e.verifyStageInputs(dag, stepID, stage, workflowContext, internalDataModel)
err := e.verifyStageInputs(dag, stepID, stage, workflowContext, internalDataModel, dataModel)
if err != nil {
return err
}
Expand All @@ -357,6 +363,7 @@ func (e *executor) verifyStageInputs(
stage step.LifecycleStageWithSchema,
workflowContext map[string][]byte,
internalDataModel *schema.ScopeSchema,
dataModel any,
) error {
// First, get the parsed inputs of the stage
parsedInputs, err := e.getStageInputs(dag, stepID, stage)
Expand All @@ -381,7 +388,7 @@ func (e *executor) verifyStageInputs(
}
} else {
// It is present, so make sure it is compatible.
err := e.preValidateCompatibility(internalDataModel, providedInputForField, stageInputSchema, workflowContext)
err := e.preValidateCompatibility(internalDataModel, providedInputForField, stageInputSchema, workflowContext, dataModel)
if err != nil {
return fmt.Errorf("input validation failed for workflow step '%s' stage '%s' (%w)", stepID, stage.ID, err)
}
Expand All @@ -390,6 +397,36 @@ func (e *executor) verifyStageInputs(
return nil
}

func getDatModel(
runnableSteps map[string]step.RunnableStep,
stepLifecycles map[string]step.Lifecycle[step.LifecycleStageWithSchema]) (map[string]any, error) {

var i interface{}
dataModel := map[string]any{
WorkflowInputKey: i,
WorkflowStepsKey: map[string]any{}}
for stepID, _ := range runnableSteps {
//stepID := stepID
stepDataModel := map[string]any{}
for _, stage := range stepLifecycles[stepID].Stages {
steps := dataModel[WorkflowStepsKey].(map[string]any)
if _, ok := steps[stepID]; !ok {
steps[stepID] = map[string]any{}
}
for outputID, output := range stage.Outputs {
if _, ok := stepDataModel[stage.ID]; !ok {
stepDataModel[stage.ID] = map[string]any{}
}
stageModel := stepDataModel[stage.ID].(map[string]any)
stageModel[outputID] = output.ReflectedType()
}

}
dataModel[WorkflowStepsKey].(map[string]any)[stepID] = stepDataModel
}
return dataModel, nil
}

func (e *executor) getStageInputs(
dag dgraph.DirectedGraph[*DAGItem],
stepID string,
Expand Down Expand Up @@ -427,9 +464,9 @@ func (e *executor) getStageInputs(
}

func (e *executor) preValidateCompatibility(rootSchema schema.Scope, inputField any, propertySchema *schema.PropertySchema,
workflowContext map[string][]byte) error {
workflowContext map[string][]byte, dataModel any) error {
// Get the type/value structure
inputTypeStructure, err := e.createTypeStructure(rootSchema, inputField, workflowContext)
inputTypeStructure, err := e.createTypeStructure(rootSchema, inputField, workflowContext, dataModel)
if err != nil {
return err
}
Expand All @@ -441,13 +478,26 @@ func (e *executor) preValidateCompatibility(rootSchema schema.Scope, inputField
// When the literal is known, it includes the original value.
// When the literal is not known, but the schema is, it includes the value.
// When it encounters a map or list, it preserves it and recursively continues.
func (e *executor) createTypeStructure(rootSchema schema.Scope, inputField any, workflowContext map[string][]byte) (any, error) {
func (e *executor) createTypeStructure(rootSchema schema.Scope, inputField any, workflowContext map[string][]byte, dataModel any) (any, error) {

// Expression, so the exact value may not be known yet. So just get the type from it.
if expr, ok := inputField.(expressions.Expression); ok {
// Is expression, so evaluate it.
e.logger.Debugf("Evaluating expression %s...", expr.String())
return expr.Type(rootSchema, workflowContext)
//return expr.Type(rootSchema, workflowContext)
t, err := expr.Type(rootSchema, workflowContext)
if err != nil {
return nil, err
}
fmt.Printf("expr type: %v\n", t)

exprval, err := expr.Evaluate(dataModel, workflowContext)
if err != nil {
return nil, err
}
fmt.Printf("expr val: %v\n", exprval)

return t, nil
}

v := reflect.ValueOf(inputField)
Expand All @@ -458,7 +508,7 @@ func (e *executor) createTypeStructure(rootSchema schema.Scope, inputField any,
result := make([]any, v.Len())
for i := 0; i < v.Len(); i++ {
value := v.Index(i).Interface()
newValue, err := e.createTypeStructure(rootSchema, value, workflowContext)
newValue, err := e.createTypeStructure(rootSchema, value, workflowContext, dataModel)
if err != nil {
return nil, fmt.Errorf("failed to resolve slice expressions (%w)", err)
}
Expand All @@ -474,7 +524,7 @@ func (e *executor) createTypeStructure(rootSchema schema.Scope, inputField any,
return nil, fmt.Errorf("failed to generate type structure. Key is not of type string")
}
value := v.MapIndex(reflectedKey).Interface()
newValue, err := e.createTypeStructure(rootSchema, value, workflowContext)
newValue, err := e.createTypeStructure(rootSchema, value, workflowContext, dataModel)
if err != nil {
return nil, fmt.Errorf("failed to resolve map expressions (%w)", err)
}
Expand Down Expand Up @@ -550,11 +600,11 @@ func (e *executor) buildOutputProperties(
err,
)
}

stageOutputsLen := len(stage.Outputs)
if stageOutputsLen == 0 {
continue
}
//if stageOutputsLen == 0 {
// continue
//}
stageOutputProperties := make(map[string]*schema.PropertySchema, stageOutputsLen)

for outputID, outputSchema := range stage.Outputs {
Expand Down
9 changes: 6 additions & 3 deletions workflow/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ steps:
wait_time_ms: 0
# invalid wait for specification
# specifically, deploy does not have any outputs
wait_for: !expr steps.wait_1.deploy
wait_for: !expr $.steps.wait_1.deploy
outputs:
a:
b: !expr $.steps.wait_2.outputs
Expand All @@ -273,6 +273,9 @@ func TestDependOnNoOutputs(t *testing.T) {
// - wait_1 = {}; not having a property named 'deploy',
// - wait_1 = { deploy: nil }; the 'deploy' property has no outputs (i.e. nil output)
_, err := getTestImplPreparedWorkflow(t, invalidWaitfor)
assert.Error(t, err)
assert.Contains(t, err.Error(), "object wait_1 does not have a property named deploy")
assert.NoError(t, err)
//outputID, outputData, err := wf.Execute(context.Background(), map[any]any{})
//assert.Error(t, err)
//fmt.Printf("output id: %s\noutput: %v\n", outputID, outputData)
//assert.Contains(t, err.Error(), "object wait_1 does not have a property named deploy")
}

0 comments on commit 244b2ee

Please sign in to comment.