-
Notifications
You must be signed in to change notification settings - Fork 20
/
envelope.go
88 lines (81 loc) · 2.43 KB
/
envelope.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
83
84
85
86
87
88
package events
import (
"fmt"
"github.com/nyaruka/goflow/flows"
"github.com/nyaruka/goflow/utils"
)
// ReadEvents reads the events from the given envelopes
func ReadEvents(envelopes []*utils.TypedEnvelope) ([]flows.Event, error) {
events := make([]flows.Event, len(envelopes))
for e, envelope := range envelopes {
event, err := EventFromEnvelope(envelope)
if err != nil {
return nil, err
}
event.SetFromCaller(true)
events[e] = event
}
return events, nil
}
// EventFromEnvelope reads a single event from the given envelope
func EventFromEnvelope(envelope *utils.TypedEnvelope) (flows.Event, error) {
var event flows.Event
switch envelope.Type {
case TypeBroadcastCreated:
event = &BroadcastCreatedEvent{}
case TypeContactChanged:
event = &ContactChangedEvent{}
case TypeContactChannelChanged:
event = &ContactChannelChangedEvent{}
case TypeContactFieldChanged:
event = &ContactFieldChangedEvent{}
case TypeContactGroupsAdded:
event = &ContactGroupsAddedEvent{}
case TypeContactGroupsRemoved:
event = &ContactGroupsRemovedEvent{}
case TypeContactPropertyChanged:
event = &ContactPropertyChangedEvent{}
case TypeContactURNAdded:
event = &ContactURNAddedEvent{}
case TypeEmailCreated:
event = &EmailCreatedEvent{}
case TypeEnvironmentChanged:
event = &EnvironmentChangedEvent{}
case TypeError:
event = &ErrorEvent{}
case TypeFlowTriggered:
event = &FlowTriggeredEvent{}
case TypeInputLabelsAdded:
event = &InputLabelsAddedEvent{}
case TypeMsgCreated:
event = &MsgCreatedEvent{}
case TypeMsgReceived:
event = &MsgReceivedEvent{}
case TypeMsgWait:
event = &MsgWaitEvent{}
case TypeNothingWait:
event = &NothingWaitEvent{}
case TypeRunExpired:
event = &RunExpiredEvent{}
case TypeRunResultChanged:
event = &RunResultChangedEvent{}
case TypeSessionTriggered:
event = &SessionTriggeredEvent{}
case TypeWebhookCalled:
event = &WebhookCalledEvent{}
default:
return nil, fmt.Errorf("Unknown event type: %s", envelope.Type)
}
return event, utils.UnmarshalAndValidate(envelope.Data, event, fmt.Sprintf("event[type=%s]", envelope.Type))
}
// EventsToEnvelopes converts the given events to typed envelopes
func EventsToEnvelopes(events []flows.Event) ([]*utils.TypedEnvelope, error) {
var err error
envelopes := make([]*utils.TypedEnvelope, len(events))
for e, event := range events {
if envelopes[e], err = utils.EnvelopeFromTyped(event); err != nil {
return nil, err
}
}
return envelopes, nil
}