Skip to content

Commit b9db1dd

Browse files
authored
fix(*): continueAs doesn't have invoke & onParentComplete field (#102)
Signed-off-by: lsytj0413 <511121939@qq.com> Signed-off-by: lsytj0413 <511121939@qq.com>
1 parent a89a5ad commit b9db1dd

File tree

5 files changed

+290
-202
lines changed

5 files changed

+290
-202
lines changed

model/action_test.go

Lines changed: 0 additions & 157 deletions
Original file line numberDiff line numberDiff line change
@@ -15,170 +15,13 @@
1515
package model
1616

1717
import (
18-
"encoding/json"
1918
"testing"
2019

2120
"github.com/stretchr/testify/assert"
2221

2322
val "github.com/serverlessworkflow/sdk-go/v2/validator"
2423
)
2524

26-
func TestWorkflowRefUnmarshalJSON(t *testing.T) {
27-
type testCase struct {
28-
desp string
29-
data string
30-
expect WorkflowRef
31-
err string
32-
}
33-
testCases := []testCase{
34-
{
35-
desp: "normal object test",
36-
data: `{"workflowId": "1", "version": "2", "invoke": "async", "onParentComplete": "continue"}`,
37-
expect: WorkflowRef{
38-
WorkflowID: "1",
39-
Version: "2",
40-
Invoke: InvokeKindAsync,
41-
OnParentComplete: "continue",
42-
},
43-
err: ``,
44-
},
45-
{
46-
desp: "normal object test & defaults",
47-
data: `{"workflowId": "1"}`,
48-
expect: WorkflowRef{
49-
WorkflowID: "1",
50-
Version: "",
51-
Invoke: InvokeKindSync,
52-
OnParentComplete: "terminate",
53-
},
54-
err: ``,
55-
},
56-
{
57-
desp: "normal string test",
58-
data: `"1"`,
59-
expect: WorkflowRef{
60-
WorkflowID: "1",
61-
Version: "",
62-
Invoke: InvokeKindSync,
63-
OnParentComplete: "terminate",
64-
},
65-
err: ``,
66-
},
67-
{
68-
desp: "empty data",
69-
data: ` `,
70-
expect: WorkflowRef{},
71-
err: `unexpected end of JSON input`,
72-
},
73-
{
74-
desp: "invalid string format",
75-
data: `"1`,
76-
expect: WorkflowRef{},
77-
err: `unexpected end of JSON input`,
78-
},
79-
{
80-
desp: "invalid json format",
81-
data: `{"workflowId": 1, "version": "2", "invoke": "async", "onParentComplete": "continue"}`,
82-
expect: WorkflowRef{},
83-
err: `json: cannot unmarshal number into Go struct field workflowRefForUnmarshal.workflowId of type string`,
84-
},
85-
{
86-
desp: "invalid string or object",
87-
data: `1`,
88-
expect: WorkflowRef{},
89-
err: `subFlowRef value '1' is not supported, it must be an object or string`,
90-
},
91-
}
92-
for _, tc := range testCases {
93-
t.Run(tc.desp, func(t *testing.T) {
94-
var v WorkflowRef
95-
err := json.Unmarshal([]byte(tc.data), &v)
96-
97-
if tc.err != "" {
98-
assert.Error(t, err)
99-
assert.Regexp(t, tc.err, err)
100-
return
101-
}
102-
103-
assert.NoError(t, err)
104-
assert.Equal(t, tc.expect, v)
105-
})
106-
}
107-
}
108-
109-
func TestWorkflowRefValidate(t *testing.T) {
110-
type testCase struct {
111-
desp string
112-
workflowRef WorkflowRef
113-
err string
114-
}
115-
testCases := []testCase{
116-
{
117-
desp: "all field & defaults",
118-
workflowRef: WorkflowRef{
119-
WorkflowID: "1",
120-
Version: "2",
121-
Invoke: InvokeKindSync,
122-
OnParentComplete: "terminate",
123-
},
124-
err: ``,
125-
},
126-
{
127-
desp: "all field",
128-
workflowRef: WorkflowRef{
129-
WorkflowID: "1",
130-
Version: "2",
131-
Invoke: InvokeKindAsync,
132-
OnParentComplete: "continue",
133-
},
134-
err: ``,
135-
},
136-
{
137-
desp: "missing workflowId",
138-
workflowRef: WorkflowRef{
139-
WorkflowID: "",
140-
Version: "2",
141-
Invoke: InvokeKindSync,
142-
OnParentComplete: "terminate",
143-
},
144-
err: `Key: 'WorkflowRef.WorkflowID' Error:Field validation for 'WorkflowID' failed on the 'required' tag`,
145-
},
146-
{
147-
desp: "invalid invoke",
148-
workflowRef: WorkflowRef{
149-
WorkflowID: "1",
150-
Version: "2",
151-
Invoke: "sync1",
152-
OnParentComplete: "terminate",
153-
},
154-
err: `Key: 'WorkflowRef.Invoke' Error:Field validation for 'Invoke' failed on the 'oneof' tag`,
155-
},
156-
{
157-
desp: "invalid onParentComplete",
158-
workflowRef: WorkflowRef{
159-
WorkflowID: "1",
160-
Version: "2",
161-
Invoke: InvokeKindSync,
162-
OnParentComplete: "terminate1",
163-
},
164-
err: `Key: 'WorkflowRef.OnParentComplete' Error:Field validation for 'OnParentComplete' failed on the 'oneof' tag`,
165-
},
166-
}
167-
for _, tc := range testCases {
168-
t.Run(tc.desp, func(t *testing.T) {
169-
err := val.GetValidator().Struct(tc.workflowRef)
170-
171-
if tc.err != "" {
172-
assert.Error(t, err)
173-
assert.Regexp(t, tc.err, err)
174-
return
175-
}
176-
177-
assert.NoError(t, err)
178-
})
179-
}
180-
}
181-
18225
func TestSleepValidate(t *testing.T) {
18326
type testCase struct {
18427
desp string

model/workflow.go

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package model
1616

1717
import (
18+
"bytes"
1819
"encoding/json"
1920
"fmt"
2021
"reflect"
@@ -506,7 +507,11 @@ func (e *End) UnmarshalJSON(data []byte) error {
506507

507508
// ContinueAs can be used to stop the current workflow execution and start another one (of the same or a different type)
508509
type ContinueAs struct {
509-
WorkflowRef
510+
// Unique id of the workflow to continue execution as.
511+
WorkflowID string `json:"workflowId" validate:"required"`
512+
// Version of the workflow to continue execution as.
513+
Version string `json:"version,omitempty"`
514+
510515
// TODO: add object or string data type
511516
// If string type, an expression which selects parts of the states data output to become the workflow data input of
512517
// continued execution. If object type, a custom object to become the workflow data input of the continued execution
@@ -518,39 +523,28 @@ type ContinueAs struct {
518523
type continueAsForUnmarshal ContinueAs
519524

520525
func (c *ContinueAs) UnmarshalJSON(data []byte) error {
521-
continueAs := make(map[string]json.RawMessage)
522-
if err := json.Unmarshal(data, &continueAs); err != nil {
526+
data = bytes.TrimSpace(data)
527+
if len(data) == 0 {
528+
return fmt.Errorf("no bytes to unmarshal")
529+
}
530+
531+
var err error
532+
switch data[0] {
533+
case '"':
523534
c.WorkflowID, err = unmarshalString(data)
535+
return err
536+
case '{':
537+
v := continueAsForUnmarshal{}
538+
err = json.Unmarshal(data, &v)
524539
if err != nil {
525540
return err
526541
}
527-
return nil
528-
}
529-
530-
if err := unmarshalKey("data", continueAs, &c.Data); err != nil {
531-
return err
532-
}
533-
if err := unmarshalKey("workflowExecTimeout", continueAs, &c.WorkflowExecTimeout); err != nil {
534-
return err
535-
}
536-
537-
v := continueAsForUnmarshal{
538-
WorkflowRef: WorkflowRef{
539-
Invoke: "sync",
540-
OnParentComplete: "terminate",
541-
},
542-
Data: c.Data,
543-
WorkflowExecTimeout: c.WorkflowExecTimeout,
544-
}
545542

546-
err := json.Unmarshal(data, &v)
547-
if err != nil {
548-
return fmt.Errorf("continueAs value '%s' is not supported, it must be an object or string", string(data))
543+
*c = ContinueAs(v)
544+
return nil
549545
}
550546

551-
*c = ContinueAs(v)
552-
return nil
553-
547+
return fmt.Errorf("continueAs value '%s' is not supported, it must be an object or string", string(data))
554548
}
555549

556550
// ProduceEvent ...

0 commit comments

Comments
 (0)