-
Notifications
You must be signed in to change notification settings - Fork 50
/
serialization.go
82 lines (66 loc) · 2.11 KB
/
serialization.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package history
import (
"encoding/json"
"errors"
)
func (e *Event) UnmarshalJSON(data []byte) error {
type Aevent Event
a := &struct {
// Attributes allows us to defer unmarshaling the events. Has to match the struct tag in Event
Attributes json.RawMessage `json:"attr,omitempty"`
*Aevent
}{}
if err := json.Unmarshal(data, &a); err != nil {
return err
}
*e = *(*Event)(a.Aevent)
attributes, err := DeserializeAttributes(e.Type, a.Attributes)
if err != nil {
return err
}
e.Attributes = attributes
return nil
}
func SerializeAttributes(attributes interface{}) ([]byte, error) {
return json.Marshal(attributes)
}
func DeserializeAttributes(eventType EventType, attributes []byte) (attr interface{}, err error) {
switch eventType {
case EventType_WorkflowExecutionStarted:
attr = &ExecutionStartedAttributes{}
case EventType_WorkflowExecutionFinished:
attr = &ExecutionCompletedAttributes{}
case EventType_WorkflowExecutionCanceled:
attr = &ExecutionCanceledAttributes{}
case EventType_WorkflowTaskStarted:
attr = &WorkflowTaskStartedAttributes{}
case EventType_ActivityScheduled:
attr = &ActivityScheduledAttributes{}
case EventType_ActivityCompleted:
attr = &ActivityCompletedAttributes{}
case EventType_ActivityFailed:
attr = &ActivityFailedAttributes{}
case EventType_SignalReceived:
attr = &SignalReceivedAttributes{}
case EventType_SideEffectResult:
attr = &SideEffectResultAttributes{}
case EventType_TimerScheduled:
attr = &TimerScheduledAttributes{}
case EventType_TimerFired:
attr = &TimerFiredAttributes{}
case EventType_TimerCanceled:
attr = &TimerCanceledAttributes{}
case EventType_SubWorkflowScheduled:
attr = &SubWorkflowScheduledAttributes{}
case EventType_SubWorkflowCancellationRequested:
attr = &SubWorkflowCancellationRequestedAttributes{}
case EventType_SubWorkflowCompleted:
attr = &SubWorkflowCompletedAttributes{}
case EventType_SubWorkflowFailed:
attr = &SubWorkflowFailedAttributes{}
default:
return nil, errors.New("unknown event type when deserializing attributes")
}
err = json.Unmarshal([]byte(attributes), &attr)
return attr, err
}