|
| 1 | +// Copyright 2022 The Serverless Workflow Specification Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package model |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "encoding/json" |
| 20 | + "fmt" |
| 21 | + "reflect" |
| 22 | + "strconv" |
| 23 | + |
| 24 | + validator "github.com/go-playground/validator/v10" |
| 25 | + "k8s.io/apimachinery/pkg/util/intstr" |
| 26 | + |
| 27 | + val "github.com/serverlessworkflow/sdk-go/v2/validator" |
| 28 | +) |
| 29 | + |
| 30 | +func init() { |
| 31 | + val.GetValidator().RegisterStructValidationCtx(ForEachStateStructLevelValidation, ForEachState{}) |
| 32 | +} |
| 33 | + |
| 34 | +// ForEachModeType Specifies how iterations are to be performed (sequentially or in parallel) |
| 35 | +type ForEachModeType string |
| 36 | + |
| 37 | +const ( |
| 38 | + // ForEachModeTypeSequential specifies iterations should be done sequentially. |
| 39 | + ForEachModeTypeSequential ForEachModeType = "sequential" |
| 40 | + // ForEachModeTypeParallel specifies iterations should be done parallel. |
| 41 | + ForEachModeTypeParallel ForEachModeType = "parallel" |
| 42 | +) |
| 43 | + |
| 44 | +// ForEachState used to execute actions for each element of a data set. |
| 45 | +type ForEachState struct { |
| 46 | + BaseState |
| 47 | + // Workflow expression selecting an array element of the states data |
| 48 | + InputCollection string `json:"inputCollection" validate:"required"` |
| 49 | + // Workflow expression specifying an array element of the states data to add the results of each iteration |
| 50 | + OutputCollection string `json:"outputCollection,omitempty"` |
| 51 | + // Name of the iteration parameter that can be referenced in actions/workflow. For each parallel iteration, this param should contain an unique element of the inputCollection array |
| 52 | + IterationParam string `json:"iterationParam,omitempty"` |
| 53 | + // Specifies how upper bound on how many iterations may run in parallel |
| 54 | + BatchSize *intstr.IntOrString `json:"batchSize,omitempty"` |
| 55 | + // Actions to be executed for each of the elements of inputCollection |
| 56 | + Actions []Action `json:"actions,omitempty" validate:"required,min=1,dive"` |
| 57 | + // State specific timeout |
| 58 | + Timeouts *ForEachStateTimeout `json:"timeouts,omitempty"` |
| 59 | + // Mode Specifies how iterations are to be performed (sequentially or in parallel) |
| 60 | + // Defaults to parallel |
| 61 | + Mode ForEachModeType `json:"mode,omitempty"` |
| 62 | +} |
| 63 | + |
| 64 | +type forEachStateForUnmarshal ForEachState |
| 65 | + |
| 66 | +func (f *ForEachState) UnmarshalJSON(data []byte) error { |
| 67 | + v := forEachStateForUnmarshal{ |
| 68 | + Mode: ForEachModeTypeParallel, |
| 69 | + } |
| 70 | + err := json.Unmarshal(data, &v) |
| 71 | + if err != nil { |
| 72 | + return fmt.Errorf("forEachState value '%s' is not supported, it must be an object or string", string(data)) |
| 73 | + } |
| 74 | + |
| 75 | + *f = ForEachState(v) |
| 76 | + return nil |
| 77 | +} |
| 78 | + |
| 79 | +// ForEachStateStructLevelValidation custom validator for ForEachState |
| 80 | +func ForEachStateStructLevelValidation(_ context.Context, structLevel validator.StructLevel) { |
| 81 | + stateObj := structLevel.Current().Interface().(ForEachState) |
| 82 | + |
| 83 | + if stateObj.Mode != ForEachModeTypeParallel { |
| 84 | + return |
| 85 | + } |
| 86 | + |
| 87 | + if stateObj.BatchSize == nil { |
| 88 | + return |
| 89 | + } |
| 90 | + |
| 91 | + switch stateObj.BatchSize.Type { |
| 92 | + case intstr.Int: |
| 93 | + if stateObj.BatchSize.IntVal <= 0 { |
| 94 | + structLevel.ReportError(reflect.ValueOf(stateObj.BatchSize), "BatchSize", "batchSize", "gt0", "") |
| 95 | + } |
| 96 | + case intstr.String: |
| 97 | + v, err := strconv.Atoi(stateObj.BatchSize.StrVal) |
| 98 | + if err != nil { |
| 99 | + structLevel.ReportError(reflect.ValueOf(stateObj.BatchSize), "BatchSize", "batchSize", "gt0", err.Error()) |
| 100 | + return |
| 101 | + } |
| 102 | + |
| 103 | + if v <= 0 { |
| 104 | + structLevel.ReportError(reflect.ValueOf(stateObj.BatchSize), "BatchSize", "batchSize", "gt0", "") |
| 105 | + } |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +// ForEachStateTimeout defines timeout settings for foreach state |
| 110 | +type ForEachStateTimeout struct { |
| 111 | + StateExecTimeout *StateExecTimeout `json:"stateExecTimeout,omitempty"` |
| 112 | + ActionExecTimeout string `json:"actionExecTimeout,omitempty" validate:"omitempty,iso8601duration"` |
| 113 | +} |
0 commit comments