-
Notifications
You must be signed in to change notification settings - Fork 20
/
channel.go
123 lines (96 loc) · 2.65 KB
/
channel.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package flows
import (
"encoding/json"
"fmt"
"github.com/nyaruka/goflow/utils"
)
type channel struct {
uuid ChannelUUID
name string
address string
channelType ChannelType
}
// UUID returns the UUID of this channel
func (c *channel) UUID() ChannelUUID { return c.uuid }
// Name returns the name of this channel
func (c *channel) Name() string { return c.name }
// Name returns the address of this channel
func (c *channel) Address() string { return c.address }
// Type returns the type of this channel
func (c *channel) Type() ChannelType { return c.channelType }
// Resolve satisfies our resolver interface
func (c *channel) Resolve(key string) interface{} {
switch key {
case "uuid":
return c.uuid
case "name":
return c.name
case "address":
return c.address
case "type":
return c.channelType
}
return fmt.Errorf("No field '%s' on channel", key)
}
// Default returns the default value for a channel, which is itself
func (c *channel) Default() interface{} {
return c
}
// String returns the default string value for a channel, which is its name
func (c *channel) String() string {
return c.name
}
var _ utils.VariableResolver = (*channel)(nil)
//------------------------------------------------------------------------------------------
// JSON Encoding / Decoding
//------------------------------------------------------------------------------------------
type channelEnvelope struct {
UUID ChannelUUID `json:"uuid"`
Name string `json:"name"`
Address string `json:"address"`
ChannelType ChannelType `json:"type"`
}
// ReadChannels decodes channels from the passed in JSON
func ReadChannels(data []json.RawMessage) ([]Channel, error) {
channels := make([]Channel, len(data))
var err error
for c := range data {
channels[c], err = ReadChannel(data[c])
if err != nil {
return nil, err
}
}
return channels, nil
}
// ReadChannel decodes a channel from the passed in JSON
func ReadChannel(data json.RawMessage) (Channel, error) {
c := &channel{}
err := json.Unmarshal(data, c)
if err != nil {
return nil, err
}
return c, nil
}
// UnmarshalJSON is our custom unmarshalling of a channel
func (c *channel) UnmarshalJSON(data []byte) error {
var ce channelEnvelope
var err error
err = json.Unmarshal(data, &ce)
if err != nil {
return err
}
c.uuid = ce.UUID
c.name = ce.Name
c.address = ce.Address
c.channelType = ce.ChannelType
return nil
}
// MarshalJSON is our custom marshalling of a channel
func (c *channel) MarshalJSON() ([]byte, error) {
var ce channelEnvelope
ce.UUID = c.uuid
ce.Name = c.name
ce.Address = c.address
ce.ChannelType = c.channelType
return json.Marshal(ce)
}