15
15
package model
16
16
17
17
import (
18
+ "bytes"
18
19
"encoding/json"
20
+ "fmt"
19
21
)
20
22
21
- // Action .. .
23
+ // Action specify invocations of services or other workflows during workflow execution .
22
24
type Action struct {
23
- // Unique action definition name
24
- Name string `json:"name,omitempty"`
25
+ // ID defines Unique action identifier
26
+ ID string `json:"id,omitempty"`
27
+ // Name defines Unique action definition name
28
+ Name string `json:"name,omitempty"`
29
+ // FunctionRef references a reusable function definition
25
30
FunctionRef * FunctionRef `json:"functionRef,omitempty"`
26
- // References a 'trigger' and 'result' reusable event definitions
31
+ // EventRef references a 'trigger' and 'result' reusable event definitions
27
32
EventRef * EventRef `json:"eventRef,omitempty"`
28
33
// References a sub-workflow to be executed
29
34
SubFlowRef * WorkflowRef `json:"subFlowRef,omitempty"`
@@ -43,7 +48,7 @@ type Action struct {
43
48
44
49
type actionForUnmarshal Action
45
50
46
- // UnmarshalJSON ...
51
+ // UnmarshalJSON implements json.Unmarshaler
47
52
func (a * Action ) UnmarshalJSON (data []byte ) error {
48
53
v := actionForUnmarshal {
49
54
ActionDataFilter : ActionDataFilter {UseResults : true },
@@ -56,34 +61,54 @@ func (a *Action) UnmarshalJSON(data []byte) error {
56
61
return nil
57
62
}
58
63
59
- // FunctionRef ...
64
+ // FunctionRef defines the reference to a reusable function definition
60
65
type FunctionRef struct {
61
66
// Name of the referenced function
62
67
RefName string `json:"refName" validate:"required"`
63
68
// Function arguments
69
+ // TODO: validate it as required if function type is graphql
64
70
Arguments map [string ]interface {} `json:"arguments,omitempty"`
65
71
// String containing a valid GraphQL selection set
72
+ // TODO: validate it as required if function type is graphql
66
73
SelectionSet string `json:"selectionSet,omitempty"`
74
+
75
+ // Invoke specifies if the subflow should be invoked sync or async.
76
+ // Defaults to sync.
77
+ Invoke string `json:"invoke,omitempty" validate:"required,oneof=async sync"`
67
78
}
68
79
69
- // UnmarshalJSON ...
80
+ type functionRefForUnmarshal FunctionRef
81
+
82
+ // UnmarshalJSON implements json.Unmarshaler
70
83
func (f * FunctionRef ) UnmarshalJSON (data []byte ) error {
71
- funcRef := make (map [string ]interface {})
72
- if err := json .Unmarshal (data , & funcRef ); err != nil {
84
+ data = bytes .TrimSpace (data )
85
+ if len (data ) == 0 {
86
+ return fmt .Errorf ("no bytes to unmarshal" )
87
+ }
88
+
89
+ var err error
90
+ switch data [0 ] {
91
+ case '"' :
73
92
f .RefName , err = unmarshalString (data )
74
93
if err != nil {
75
94
return err
76
95
}
96
+ f .Invoke = "sync"
97
+ return nil
98
+ case '{' :
99
+ v := functionRefForUnmarshal {
100
+ Invoke : "sync" ,
101
+ }
102
+ err = json .Unmarshal (data , & v )
103
+ if err != nil {
104
+ // TODO: replace the error message with correct type's name
105
+ return err
106
+ }
107
+ * f = FunctionRef (v )
77
108
return nil
78
109
}
79
110
80
- f .RefName = requiresNotNilOrEmpty (funcRef ["refName" ])
81
- if _ , found := funcRef ["arguments" ]; found {
82
- f .Arguments = funcRef ["arguments" ].(map [string ]interface {})
83
- }
84
- f .SelectionSet = requiresNotNilOrEmpty (funcRef ["selectionSet" ])
85
-
86
- return nil
111
+ return fmt .Errorf ("functionRef value '%s' not support, it must be an object or string" , string (data ))
87
112
}
88
113
89
114
// WorkflowRef holds a reference for a workflow definition
@@ -92,32 +117,55 @@ type WorkflowRef struct {
92
117
WorkflowID string `json:"workflowId" validate:"required"`
93
118
// Sub-workflow version
94
119
Version string `json:"version,omitempty"`
120
+
121
+ // Invoke specifies if the subflow should be invoked sync or async.
122
+ // Defaults to sync.
123
+ Invoke string `json:"invoke,omitempty" validate:"required,oneof=async sync"`
124
+
125
+ // OnParantComplete specifies how subflow execution should behave when parent workflow completes if invoke is 'async'。
126
+ // Defaults to terminate.
127
+ OnParentComplete string `json:"onParentComplete,omitempty" validate:"required,oneof=terminate continue"`
95
128
}
96
129
97
- // UnmarshalJSON ...
130
+ type workflowRefForUnmarshal WorkflowRef
131
+
132
+ // UnmarshalJSON implements json.Unmarshaler
98
133
func (s * WorkflowRef ) UnmarshalJSON (data []byte ) error {
99
- subflowRef := make (map [string ]json.RawMessage )
100
- if err := json .Unmarshal (data , & subflowRef ); err != nil {
134
+ data = bytes .TrimSpace (data )
135
+ if len (data ) == 0 {
136
+ return fmt .Errorf ("no bytes to unmarshal" )
137
+ }
138
+
139
+ var err error
140
+ switch data [0 ] {
141
+ case '"' :
101
142
s .WorkflowID , err = unmarshalString (data )
102
143
if err != nil {
103
144
return err
104
145
}
146
+ s .Invoke , s .OnParentComplete = "sync" , "terminate"
147
+ return nil
148
+ case '{' :
149
+ v := workflowRefForUnmarshal {
150
+ Invoke : "sync" ,
151
+ OnParentComplete : "terminate" ,
152
+ }
153
+ err = json .Unmarshal (data , & v )
154
+ if err != nil {
155
+ // TODO: replace the error message with correct type's name
156
+ return err
157
+ }
158
+ * s = WorkflowRef (v )
105
159
return nil
106
- }
107
- if err := unmarshalKey ("version" , subflowRef , & s .Version ); err != nil {
108
- return err
109
- }
110
- if err := unmarshalKey ("workflowId" , subflowRef , & s .WorkflowID ); err != nil {
111
- return err
112
160
}
113
161
114
- return nil
162
+ return fmt . Errorf ( "subFlowRef value '%s' not support, it must be an object or string" , string ( data ))
115
163
}
116
164
117
- // Sleep ...
165
+ // Sleep defines time periods workflow execution should sleep before & after function execution
118
166
type Sleep struct {
119
- // Before Amount of time (ISO 8601 duration format) to sleep before function/subflow invocation. Does not apply if 'eventRef' is defined.
120
- Before string `json:"before,omitempty"`
121
- // After Amount of time (ISO 8601 duration format) to sleep after function/subflow invocation. Does not apply if 'eventRef' is defined.
122
- After string `json:"after,omitempty"`
167
+ // Before defines amount of time (ISO 8601 duration format) to sleep before function/subflow invocation. Does not apply if 'eventRef' is defined.
168
+ Before string `json:"before,omitempty" validate:"omitempty,iso8601duration" `
169
+ // After defines amount of time (ISO 8601 duration format) to sleep after function/subflow invocation. Does not apply if 'eventRef' is defined.
170
+ After string `json:"after,omitempty" validate:"omitempty,iso8601duration" `
123
171
}
0 commit comments