Skip to content

Commit eac04f7

Browse files
committed
✨ add WorkflowNodeCondition.
1 parent e4cb36b commit eac04f7

4 files changed

+261
-0
lines changed

workflow_node.go

+33
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,43 @@ type WorkflowNodeVariables struct {
66
GeneratedVariableList []NodeVariable `json:"generated_variable_list"`
77
}
88

9+
func (vars *WorkflowNodeVariables) GetVariable(name string) *NodeVariable {
10+
for _, variable := range vars.VariableList {
11+
if variable.Name == name {
12+
return &variable
13+
}
14+
}
15+
16+
for _, variable := range vars.GeneratedVariableList {
17+
if variable.Name == name {
18+
return &variable
19+
}
20+
}
21+
22+
return nil
23+
}
24+
925
type WorkflowNode struct {
1026
Name string `json:"name"`
1127
Status NodeStatus `json:"status"`
1228
Type NodeType `json:"node_type"`
1329
VariableList WorkflowNodeVariables `json:"variable_list"`
1430
InheritedVariableList []WorkflowNodeVariables `json:"inherited_variable_list"`
1531
}
32+
33+
func (node *WorkflowNode) GetVariable(name string) *NodeVariable {
34+
var variable *NodeVariable
35+
variable = node.VariableList.GetVariable(name)
36+
if variable != nil {
37+
return variable
38+
}
39+
40+
for _, variableList := range node.InheritedVariableList {
41+
variable = variableList.GetVariable(name)
42+
if variable != nil {
43+
return variable
44+
}
45+
}
46+
47+
return nil
48+
}

workflow_node_condition.go

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package workflowmodel
2+
3+
type WorkflowNodeCondition interface {
4+
IsFit(node *WorkflowNode) bool
5+
}
6+
7+
type WorkflowNodeStatusCondition struct {
8+
Checker NodeStatusValueChecker
9+
}
10+
11+
func (c *WorkflowNodeStatusCondition) IsFit(node *WorkflowNode) bool {
12+
return c.Checker.CheckValue(node.Status)
13+
}
14+
15+
type WorkflowNodeVariableCondition struct {
16+
VariableName string
17+
Checker StringValueChecker
18+
}
19+
20+
func (c *WorkflowNodeVariableCondition) IsFit(node *WorkflowNode) bool {
21+
variable := node.GetVariable(c.VariableName)
22+
if variable == nil {
23+
return false
24+
}
25+
26+
return c.Checker.CheckValue(variable.Value)
27+
}

workflow_node_condition_test.go

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package workflowmodel_test
2+
3+
import (
4+
"github.com/perillaroc/workflow-model-go"
5+
"testing"
6+
)
7+
8+
func TestWorkflowNodeStatusCondition_IsFit(t *testing.T) {
9+
condition := workflowmodel.WorkflowNodeStatusCondition{
10+
Checker: &workflowmodel.NodeStatusEqualValueChecker{
11+
ExpectedValue: workflowmodel.Active},
12+
}
13+
14+
node := &workflowmodel.WorkflowNode{
15+
Status: workflowmodel.Active,
16+
}
17+
18+
if !condition.IsFit(node) {
19+
t.Errorf("node status is not active")
20+
}
21+
}
22+
23+
func TestWorkflowNodeVariableCondition_IsFit(t *testing.T) {
24+
condition := workflowmodel.WorkflowNodeVariableCondition{
25+
VariableName: "ECF_DATE",
26+
Checker: &workflowmodel.StringEqualValueChecker{
27+
ExpectedValue: "20190316"},
28+
}
29+
30+
node1 := &workflowmodel.WorkflowNode{
31+
VariableList: workflowmodel.WorkflowNodeVariables{
32+
VariableList: []workflowmodel.NodeVariable{
33+
{
34+
Name: "ECF_DATE",
35+
Value: "20190316",
36+
Type: workflowmodel.Variable,
37+
},
38+
},
39+
},
40+
}
41+
42+
if !condition.IsFit(node1) {
43+
t.Errorf("ECF_DATE is not 20190316")
44+
}
45+
46+
node2 := &workflowmodel.WorkflowNode{
47+
VariableList: workflowmodel.WorkflowNodeVariables{
48+
VariableList: []workflowmodel.NodeVariable{
49+
{
50+
Name: "ECF_DATE",
51+
Value: "20190315",
52+
Type: workflowmodel.Variable,
53+
},
54+
},
55+
},
56+
}
57+
58+
if condition.IsFit(node2) {
59+
t.Errorf("ECF_DATE is 20190316")
60+
}
61+
62+
}

workflow_node_test.go

+139
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,57 @@ import (
77
"testing"
88
)
99

10+
func TestWorkflowNodeVariables_GetVariable(t *testing.T) {
11+
variableList := workflowmodel.WorkflowNodeVariables{
12+
Path: "/meso_post/00/initial",
13+
VariableList: []workflowmodel.NodeVariable{
14+
{
15+
Name: "BASE_DIR",
16+
Value: "/g1/u/nwp_pd/ecfworks/meso_post",
17+
Type: workflowmodel.Variable,
18+
},
19+
{
20+
Name: "BIN_DIR",
21+
Value: "/g1/u/nwp_pd/MESO_POST/bin",
22+
Type: workflowmodel.Variable,
23+
},
24+
{
25+
Name: "ECF_DATE",
26+
Value: "20190316",
27+
Type: workflowmodel.Variable,
28+
},
29+
},
30+
GeneratedVariableList: []workflowmodel.NodeVariable{
31+
{
32+
Name: "ECF_NAME",
33+
Value: "/meso_post/00/initial",
34+
Type: workflowmodel.GeneratedVariable,
35+
},
36+
{
37+
Name: "ECF_DATE",
38+
Value: "20190315",
39+
Type: workflowmodel.Variable,
40+
},
41+
},
42+
}
43+
44+
tests := []struct {
45+
name string
46+
expectedValue string
47+
}{
48+
{"BASE_DIR", "/g1/u/nwp_pd/ecfworks/meso_post"},
49+
{"BIN_DIR", "/g1/u/nwp_pd/MESO_POST/bin"},
50+
{"ECF_DATE", "20190316"},
51+
{"ECF_NAME", "/meso_post/00/initial"},
52+
}
53+
for _, test := range tests {
54+
variable := variableList.GetVariable(test.name)
55+
if variable.Value != test.expectedValue {
56+
t.Errorf("%s = %s, expected %s", test.name, variable.Value, test.expectedValue)
57+
}
58+
}
59+
}
60+
1061
func TestWorkflowNodeVariables_JSON(t *testing.T) {
1162
vars := workflowmodel.WorkflowNodeVariables{
1263
Path: "/meso_post/00/initial",
@@ -122,3 +173,91 @@ func TestWorkflowNode_JSON(t *testing.T) {
122173
fmt.Printf("json.Marshal(node) = %s\n", nodeJsonString)
123174

124175
}
176+
177+
func TestWorkflowNode_GetVariable(t *testing.T) {
178+
node := workflowmodel.WorkflowNode{
179+
Name: "initial",
180+
Status: workflowmodel.Queued,
181+
Type: workflowmodel.Task,
182+
VariableList: workflowmodel.WorkflowNodeVariables{
183+
Path: "/meso_post/00/initial",
184+
VariableList: []workflowmodel.NodeVariable{
185+
{
186+
Name: "BASE_DIR",
187+
Value: "/g1/u/nwp_pd/ecfworks/meso_post",
188+
Type: workflowmodel.Variable,
189+
},
190+
{
191+
Name: "BIN_DIR",
192+
Value: "/g1/u/nwp_pd/MESO_POST/bin",
193+
Type: workflowmodel.Variable,
194+
},
195+
},
196+
GeneratedVariableList: []workflowmodel.NodeVariable{
197+
{
198+
Name: "ECF_NAME",
199+
Value: "/meso_post/00/initial",
200+
Type: workflowmodel.GeneratedVariable,
201+
},
202+
},
203+
},
204+
InheritedVariableList: []workflowmodel.WorkflowNodeVariables{
205+
{
206+
Path: "/meso_post/00",
207+
VariableList: []workflowmodel.NodeVariable{
208+
{
209+
Name: "HH",
210+
Value: "00",
211+
Type: workflowmodel.Variable,
212+
},
213+
},
214+
GeneratedVariableList: []workflowmodel.NodeVariable{
215+
{
216+
Name: "ECF_NAME",
217+
Value: "/meso_post/00",
218+
Type: workflowmodel.GeneratedVariable,
219+
},
220+
},
221+
},
222+
{
223+
Path: "/meso_post",
224+
VariableList: []workflowmodel.NodeVariable{
225+
{
226+
Name: "SUITE",
227+
Value: "meso_post",
228+
Type: workflowmodel.Variable,
229+
},
230+
},
231+
GeneratedVariableList: []workflowmodel.NodeVariable{
232+
{
233+
Name: "ECF_NAME",
234+
Value: "/meso_post",
235+
Type: workflowmodel.GeneratedVariable,
236+
},
237+
{
238+
Name: "ECF_DATE",
239+
Value: "20190312",
240+
Type: workflowmodel.GeneratedVariable,
241+
},
242+
},
243+
},
244+
},
245+
}
246+
247+
tests := []struct {
248+
name string
249+
expectedValue string
250+
}{
251+
{"ECF_NAME", "/meso_post/00/initial"},
252+
{"SUITE", "meso_post"},
253+
{"HH", "00"},
254+
{"BASE_DIR", "/g1/u/nwp_pd/ecfworks/meso_post"},
255+
}
256+
257+
for _, test := range tests {
258+
variable := node.GetVariable(test.name)
259+
if variable.Value != test.expectedValue {
260+
t.Errorf("%s = %s, expected %s", test.name, variable.Value, test.expectedValue)
261+
}
262+
}
263+
}

0 commit comments

Comments
 (0)