Skip to content

Commit 2d45040

Browse files
authored
feat(action): add invoke/onParentComplete/Id field (#93)
Signed-off-by: lsytj0413 <511121939@qq.com> Signed-off-by: lsytj0413 <511121939@qq.com>
1 parent 7db29d7 commit 2d45040

File tree

5 files changed

+348
-35
lines changed

5 files changed

+348
-35
lines changed

model/action.go

Lines changed: 79 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,20 @@
1515
package model
1616

1717
import (
18+
"bytes"
1819
"encoding/json"
20+
"fmt"
1921
)
2022

21-
// Action ...
23+
// Action specify invocations of services or other workflows during workflow execution.
2224
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
2530
FunctionRef *FunctionRef `json:"functionRef,omitempty"`
26-
// References a 'trigger' and 'result' reusable event definitions
31+
// EventRef references a 'trigger' and 'result' reusable event definitions
2732
EventRef *EventRef `json:"eventRef,omitempty"`
2833
// References a sub-workflow to be executed
2934
SubFlowRef *WorkflowRef `json:"subFlowRef,omitempty"`
@@ -43,7 +48,7 @@ type Action struct {
4348

4449
type actionForUnmarshal Action
4550

46-
// UnmarshalJSON ...
51+
// UnmarshalJSON implements json.Unmarshaler
4752
func (a *Action) UnmarshalJSON(data []byte) error {
4853
v := actionForUnmarshal{
4954
ActionDataFilter: ActionDataFilter{UseResults: true},
@@ -56,34 +61,54 @@ func (a *Action) UnmarshalJSON(data []byte) error {
5661
return nil
5762
}
5863

59-
// FunctionRef ...
64+
// FunctionRef defines the reference to a reusable function definition
6065
type FunctionRef struct {
6166
// Name of the referenced function
6267
RefName string `json:"refName" validate:"required"`
6368
// Function arguments
69+
// TODO: validate it as required if function type is graphql
6470
Arguments map[string]interface{} `json:"arguments,omitempty"`
6571
// String containing a valid GraphQL selection set
72+
// TODO: validate it as required if function type is graphql
6673
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"`
6778
}
6879

69-
// UnmarshalJSON ...
80+
type functionRefForUnmarshal FunctionRef
81+
82+
// UnmarshalJSON implements json.Unmarshaler
7083
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 '"':
7392
f.RefName, err = unmarshalString(data)
7493
if err != nil {
7594
return err
7695
}
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)
77108
return nil
78109
}
79110

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))
87112
}
88113

89114
// WorkflowRef holds a reference for a workflow definition
@@ -92,32 +117,55 @@ type WorkflowRef struct {
92117
WorkflowID string `json:"workflowId" validate:"required"`
93118
// Sub-workflow version
94119
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"`
95128
}
96129

97-
// UnmarshalJSON ...
130+
type workflowRefForUnmarshal WorkflowRef
131+
132+
// UnmarshalJSON implements json.Unmarshaler
98133
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 '"':
101142
s.WorkflowID, err = unmarshalString(data)
102143
if err != nil {
103144
return err
104145
}
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)
105159
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
112160
}
113161

114-
return nil
162+
return fmt.Errorf("subFlowRef value '%s' not support, it must be an object or string", string(data))
115163
}
116164

117-
// Sleep ...
165+
// Sleep defines time periods workflow execution should sleep before & after function execution
118166
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"`
123171
}

model/action_data_filter.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ type ActionDataFilter struct {
3737

3838
type actionDataFilterForUnmarshal ActionDataFilter
3939

40+
// UnmarshalJSON implements json.Unmarshaler
4041
func (f *ActionDataFilter) UnmarshalJSON(data []byte) error {
4142
data = bytes.TrimSpace(data)
4243
if len(data) == 0 {

0 commit comments

Comments
 (0)