Skip to content

Commit a89a5ad

Browse files
authored
feat(*): add event default dataOnly & kind field value (#101)
Signed-off-by: lsytj0413 <511121939@qq.com> Signed-off-by: lsytj0413 <511121939@qq.com>
1 parent 1ae306c commit a89a5ad

File tree

2 files changed

+130
-24
lines changed

2 files changed

+130
-24
lines changed

model/event.go

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,19 @@ import (
2323
validator "github.com/go-playground/validator/v10"
2424
)
2525

26+
// EventKind defines this event as either `consumed` or `produced`
27+
type EventKind string
28+
2629
const (
27-
// EventKindConsumed ...
30+
// EventKindConsumed means the event continuation of workflow instance execution
2831
EventKindConsumed EventKind = "consumed"
29-
// EventKindProduced ...
32+
33+
// EventKindProduced means the event was created during worflow instance execution
3034
EventKindProduced EventKind = "produced"
3135
)
3236

3337
func init() {
3438
val.GetValidator().RegisterStructValidation(EventStructLevelValidation, Event{})
35-
val.GetValidator().RegisterStructValidation(EventRefStructLevelValidation, EventRef{})
3639
}
3740

3841
// EventStructLevelValidation custom validator for event kind consumed
@@ -43,22 +46,7 @@ func EventStructLevelValidation(structLevel validator.StructLevel) {
4346
}
4447
}
4548

46-
// EventRefStructLevelValidation custom validator for event kind consumed
47-
func EventRefStructLevelValidation(structLevel validator.StructLevel) {
48-
eventRef := structLevel.Current().Interface().(EventRef)
49-
50-
if len(eventRef.ResultEventTimeout) > 0 {
51-
err := val.ValidateISO8601TimeDuration(eventRef.ResultEventTimeout)
52-
if err != nil {
53-
structLevel.ReportError(reflect.ValueOf(eventRef.ResultEventTimeout), "ResultEventTimeout", "resultEventTimeout", "iso8601duration", "")
54-
}
55-
}
56-
}
57-
58-
// EventKind ...
59-
type EventKind string
60-
61-
// Event ...
49+
// Event used to define events and their correlations
6250
type Event struct {
6351
Common
6452
// Unique event name
@@ -67,31 +55,50 @@ type Event struct {
6755
Source string `json:"source,omitempty"`
6856
// CloudEvent type
6957
Type string `json:"type" validate:"required"`
70-
// Defines the CloudEvent as either 'consumed' or 'produced' by the workflow. Default is 'consumed'
58+
// Defines the CloudEvent as either 'consumed' or 'produced' by the workflow.
59+
// Defaults to `consumed`
7160
Kind EventKind `json:"kind,omitempty"`
7261
// If `true`, only the Event payload is accessible to consuming Workflow states. If `false`, both event payload and context attributes should be accessible"
62+
// Defaults to true
7363
DataOnly bool `json:"dataOnly,omitempty"`
7464
// CloudEvent correlation definitions
7565
Correlation []Correlation `json:"correlation,omitempty" validate:"omitempty,dive"`
7666
}
7767

78-
// Correlation ...
68+
type eventForUnmarshal Event
69+
70+
// UnmarshalJSON unmarshal Event object from json bytes
71+
func (e *Event) UnmarshalJSON(data []byte) error {
72+
v := eventForUnmarshal{
73+
DataOnly: true,
74+
Kind: EventKindConsumed,
75+
}
76+
err := json.Unmarshal(data, &v)
77+
if err != nil {
78+
return err
79+
}
80+
81+
*e = Event(v)
82+
return nil
83+
}
84+
85+
// Correlation define event correlation rules for an event. Only used for `consumed` events
7986
type Correlation struct {
8087
// CloudEvent Extension Context Attribute name
8188
ContextAttributeName string `json:"contextAttributeName" validate:"required"`
8289
// CloudEvent Extension Context Attribute value
8390
ContextAttributeValue string `json:"contextAttributeValue,omitempty"`
8491
}
8592

86-
// EventRef ...
93+
// EventRef defining invocation of a function via event
8794
type EventRef struct {
8895
// Reference to the unique name of a 'produced' event definition
8996
TriggerEventRef string `json:"triggerEventRef" validate:"required"`
9097
// Reference to the unique name of a 'consumed' event definition
9198
ResultEventRef string `json:"resultEventRef" validate:"required"`
9299

93100
// ResultEventTimeout defines maximum amount of time (ISO 8601 format) to wait for the result event. If not defined it be set to the actionExecutionTimeout
94-
ResultEventTimeout string `json:"resultEventTimeout,omitempty"`
101+
ResultEventTimeout string `json:"resultEventTimeout,omitempty" validate:"omitempty,iso8601duration"`
95102

96103
// TODO: create StringOrMap structure
97104
// If string type, an expression which selects parts of the states data output to become the data (payload) of the event referenced by 'triggerEventRef'.
@@ -114,7 +121,7 @@ func (e *EventRef) UnmarshalJSON(data []byte) error {
114121
}
115122
err := json.Unmarshal(data, &v)
116123
if err != nil {
117-
return nil
124+
return err
118125
}
119126

120127
*e = EventRef(v)

model/event_test.go

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

1717
import (
18+
"encoding/json"
1819
"testing"
1920

2021
"github.com/stretchr/testify/assert"
@@ -65,3 +66,101 @@ func TestEventRefStructLevelValidation(t *testing.T) {
6566
})
6667
}
6768
}
69+
70+
func TestEventRefUnmarshalJSON(t *testing.T) {
71+
type testCase struct {
72+
desp string
73+
data string
74+
expect EventRef
75+
err string
76+
}
77+
testCases := []testCase{
78+
{
79+
desp: "all field",
80+
data: `{"invoke": "async"}`,
81+
expect: EventRef{
82+
Invoke: InvokeKindAsync,
83+
},
84+
err: ``,
85+
},
86+
{
87+
desp: "invoke unset",
88+
data: `{}`,
89+
expect: EventRef{
90+
Invoke: InvokeKindSync,
91+
},
92+
err: ``,
93+
},
94+
{
95+
desp: "invalid json format",
96+
data: `{"invoke": 1}`,
97+
expect: EventRef{},
98+
err: `json: cannot unmarshal number into Go struct field eventRefForUnmarshal.invoke of type model.InvokeKind`,
99+
},
100+
}
101+
for _, tc := range testCases {
102+
t.Run(tc.desp, func(t *testing.T) {
103+
var v EventRef
104+
err := json.Unmarshal([]byte(tc.data), &v)
105+
106+
if tc.err != "" {
107+
assert.Error(t, err)
108+
assert.Regexp(t, tc.err, err)
109+
return
110+
}
111+
112+
assert.NoError(t, err)
113+
assert.Equal(t, tc.expect, v)
114+
})
115+
}
116+
}
117+
118+
func TestEventUnmarshalJSON(t *testing.T) {
119+
type testCase struct {
120+
desp string
121+
data string
122+
expect Event
123+
err string
124+
}
125+
testCases := []testCase{
126+
{
127+
desp: "all field",
128+
data: `{"dataOnly": false, "kind": "produced"}`,
129+
expect: Event{
130+
DataOnly: false,
131+
Kind: EventKindProduced,
132+
},
133+
err: ``,
134+
},
135+
{
136+
desp: "optional field dataOnly & kind unset",
137+
data: `{}`,
138+
expect: Event{
139+
DataOnly: true,
140+
Kind: EventKindConsumed,
141+
},
142+
err: ``,
143+
},
144+
{
145+
desp: "invalid json format",
146+
data: `{"dataOnly": "false", "kind": "produced"}`,
147+
expect: Event{},
148+
err: `json: cannot unmarshal string into Go struct field eventForUnmarshal.dataOnly of type bool`,
149+
},
150+
}
151+
for _, tc := range testCases {
152+
t.Run(tc.desp, func(t *testing.T) {
153+
var v Event
154+
err := json.Unmarshal([]byte(tc.data), &v)
155+
156+
if tc.err != "" {
157+
assert.Error(t, err)
158+
assert.Regexp(t, tc.err, err)
159+
return
160+
}
161+
162+
assert.NoError(t, err)
163+
assert.Equal(t, tc.expect, v)
164+
})
165+
}
166+
}

0 commit comments

Comments
 (0)