Skip to content

Commit 5110906

Browse files
authored
JSON or as a YAML file: workflow.states must be int32 (#178)
* JSON or as a YAML file: workflow.states must be int32 Fixes #177 Signed-off-by: Spolti <filippespolti@gmail.com> * add a full test scenario with the compensate workflow Signed-off-by: Spolti <filippespolti@gmail.com> * move workflow to testdata directory Signed-off-by: Spolti <filippespolti@gmail.com> --------- Signed-off-by: Spolti <filippespolti@gmail.com>
1 parent bb71213 commit 5110906

File tree

4 files changed

+130
-8
lines changed

4 files changed

+130
-8
lines changed

model/object.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,11 @@ import (
3333
//
3434
// +kubebuilder:validation:Type=object
3535
type Object struct {
36-
Type Type `json:"type,inline"`
37-
IntVal int32 `json:"intVal,inline"`
38-
StrVal string `json:"strVal,inline"`
39-
RawValue json.RawMessage `json:"rawValue,inline"`
36+
Type Type `json:"type,inline"`
37+
IntVal int32 `json:"intVal,inline"`
38+
StrVal string `json:"strVal,inline"`
39+
RawValue json.RawMessage `json:"rawValue,inline"`
40+
BoolValue bool `json:"boolValue,inline"`
4041
}
4142

4243
type Type int64
@@ -45,6 +46,7 @@ const (
4546
Integer Type = iota
4647
String
4748
Raw
49+
Boolean
4850
)
4951

5052
func FromInt(val int) Object {
@@ -58,6 +60,10 @@ func FromString(val string) Object {
5860
return Object{Type: String, StrVal: val}
5961
}
6062

63+
func FromBool(val bool) Object {
64+
return Object{Type: Boolean, BoolValue: val}
65+
}
66+
6167
func FromRaw(val interface{}) Object {
6268
custom, err := json.Marshal(val)
6369
if err != nil {
@@ -73,6 +79,9 @@ func (obj *Object) UnmarshalJSON(data []byte) error {
7379
if data[0] == '"' {
7480
obj.Type = String
7581
return json.Unmarshal(data, &obj.StrVal)
82+
} else if data[0] == 't' || data[0] == 'f' {
83+
obj.Type = Boolean
84+
return json.Unmarshal(data, &obj.BoolValue)
7685
} else if data[0] == '{' {
7786
obj.Type = Raw
7887
return json.Unmarshal(data, &obj.RawValue)
@@ -86,6 +95,8 @@ func (obj Object) MarshalJSON() ([]byte, error) {
8695
switch obj.Type {
8796
case String:
8897
return []byte(fmt.Sprintf(`%q`, obj.StrVal)), nil
98+
case Boolean:
99+
return []byte(fmt.Sprintf(`%t`, obj.BoolValue)), nil
89100
case Integer:
90101
return []byte(fmt.Sprintf(`%d`, obj.IntVal)), nil
91102
case Raw:

parser/parser_test.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ package parser
1616

1717
import (
1818
"encoding/json"
19-
"fmt"
2019
"os"
2120
"path/filepath"
2221
"strings"
@@ -526,7 +525,8 @@ func TestFromFile(t *testing.T) {
526525
// Inject state
527526
assert.Equal(t, "HelloInject", w.States[7].Name)
528527
assert.Equal(t, model.StateTypeInject, w.States[7].Type)
529-
assert.Equal(t, map[string]model.Object{"result": model.FromString("Hello World, last state!")}, w.States[7].InjectState.Data)
528+
assert.Equal(t, model.FromString("Hello World, last state!"), w.States[7].InjectState.Data["result"])
529+
assert.Equal(t, model.FromBool(false), w.States[7].InjectState.Data["boolValue"])
530530
assert.Equal(t, "PT11M", w.States[7].InjectState.Timeouts.StateExecTimeout.Total)
531531
assert.Equal(t, "PT22M", w.States[7].InjectState.Timeouts.StateExecTimeout.Single)
532532

@@ -930,10 +930,8 @@ states:
930930
terminate: true
931931
`))
932932
assert.Nil(t, err)
933-
fmt.Println(err)
934933
assert.NotNil(t, workflow)
935934
b, err := json.Marshal(workflow)
936-
937935
assert.Nil(t, err)
938936

939937
// workflow and auth metadata
@@ -1023,4 +1021,17 @@ states:
10231021
assert.Regexp(t, `validation for \'DataConditions\' failed on the \'required\' tag`, err)
10241022
assert.Nil(t, workflow)
10251023
})
1024+
1025+
t.Run("Test complex workflow with compensate transitions", func(t *testing.T) {
1026+
workflow, err := FromFile("./testdata/workflows/compensate.sw.json")
1027+
1028+
assert.Nil(t, err)
1029+
assert.NotNil(t, workflow)
1030+
b, err := json.Marshal(workflow)
1031+
assert.Nil(t, err)
1032+
1033+
workflow = nil
1034+
err = json.Unmarshal(b, &workflow)
1035+
assert.Nil(t, err)
1036+
})
10261037
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
{
2+
"id": "compensation",
3+
"version": "1.0",
4+
"name": "Workflow Error example",
5+
"description": "An example of how compensation works",
6+
"specVersion": "0.8",
7+
"start": "printStatus",
8+
"functions": [
9+
{
10+
"name": "PrintOutput",
11+
"type": "custom",
12+
"operation": "sysout"
13+
}
14+
],
15+
"states": [
16+
{
17+
"name": "printStatus",
18+
"type": "inject",
19+
"data": {
20+
"compensated": false
21+
},
22+
"compensatedBy": "compensating",
23+
"transition": "branch"
24+
},
25+
{
26+
"name": "branch",
27+
"type": "switch",
28+
"dataConditions": [
29+
{
30+
"condition": ".shouldCompensate==true",
31+
"transition": {
32+
"nextState": "finish_compensate",
33+
"compensate": true
34+
}
35+
},
36+
{
37+
"condition": ".shouldCompensate==false",
38+
"transition": {
39+
"nextState": "finish_not_compensate",
40+
"compensate": false
41+
}
42+
}
43+
],
44+
"defaultCondition": {
45+
"end": true
46+
}
47+
},
48+
{
49+
"name": "compensating",
50+
"usedForCompensation": true,
51+
"type": "inject",
52+
"data": {
53+
"compensated": true
54+
},
55+
"transition": "compensating_more"
56+
},
57+
{
58+
"name": "compensating_more",
59+
"usedForCompensation": true,
60+
"type": "inject",
61+
"data": {
62+
"compensating_more": "Real Betis Balompie"
63+
},
64+
"end": true
65+
},
66+
{
67+
"name": "finish_compensate",
68+
"type": "operation",
69+
"actions": [
70+
{
71+
"name": "finish_compensate_sysout",
72+
"functionRef": {
73+
"refName": "PrintOutput",
74+
"arguments": {
75+
"message": "completed"
76+
}
77+
}
78+
}
79+
],
80+
"end": true
81+
},
82+
{
83+
"name": "finish_not_compensate",
84+
"type": "operation",
85+
"actions": [
86+
{
87+
"name": "finish_not_compensate_sysout",
88+
"functionRef": {
89+
"refName": "PrintOutput",
90+
"arguments": {
91+
"message": "completed"
92+
}
93+
}
94+
}
95+
],
96+
"end": true
97+
}
98+
]
99+
}

parser/testdata/workflows/greetings-v08-spec.sw.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ states:
170170
type: inject
171171
data:
172172
result: Hello World, last state!
173+
boolValue: false
173174
timeouts:
174175
stateExecTimeout:
175176
total: PT11M

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy