-
Notifications
You must be signed in to change notification settings - Fork 20
/
source.go
185 lines (164 loc) · 5.35 KB
/
source.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// Package static is an implementation of Source which loads assets from a static JSON file.
package static
import (
"encoding/json"
"fmt"
"os"
"strings"
"github.com/nyaruka/goflow/assets"
"github.com/nyaruka/goflow/envs"
"github.com/nyaruka/goflow/utils"
)
// StaticSource is an asset source which loads assets from a static JSON file
type StaticSource struct {
s struct {
Channels []*Channel `json:"channels" validate:"omitempty,dive"`
Classifiers []*Classifier `json:"classifiers" validate:"omitempty,dive"`
Fields []*Field `json:"fields" validate:"omitempty,dive"`
Flows []*Flow `json:"flows" validate:"omitempty,dive"`
Globals []*Global `json:"globals" validate:"omitempty,dive"`
Groups []*Group `json:"groups" validate:"omitempty,dive"`
Labels []*Label `json:"labels" validate:"omitempty,dive"`
Locations []*envs.LocationHierarchy `json:"locations"`
OptIns []*OptIn `json:"optins" validate:"omitempty,dive"`
Resthooks []*Resthook `json:"resthooks" validate:"omitempty,dive"`
Templates []*Template `json:"templates" validate:"omitempty,dive"`
Topics []*Topic `json:"topics" validate:"omitempty,dive"`
Users []*User `json:"users" validate:"omitempty,dive"`
}
}
// NewEmptySource creates a new empty source with no assets
func NewEmptySource() *StaticSource {
return &StaticSource{}
}
// NewSource creates a new static source from the given JSON
func NewSource(data json.RawMessage) (*StaticSource, error) {
s := &StaticSource{}
if err := utils.UnmarshalAndValidate(data, &s.s); err != nil {
return nil, fmt.Errorf("unable to read assets: %w", err)
}
return s, nil
}
// LoadSource loads a new static source from the given JSON file
func LoadSource(path string) (*StaticSource, error) {
data, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("error reading file '%s': %w", path, err)
}
return NewSource(data)
}
// Channels returns all channel assets
func (s *StaticSource) Channels() ([]assets.Channel, error) {
set := make([]assets.Channel, len(s.s.Channels))
for i := range s.s.Channels {
set[i] = s.s.Channels[i]
}
return set, nil
}
// Classifiers returns all classifier assets
func (s *StaticSource) Classifiers() ([]assets.Classifier, error) {
set := make([]assets.Classifier, len(s.s.Classifiers))
for i := range s.s.Classifiers {
set[i] = s.s.Classifiers[i]
}
return set, nil
}
// Fields returns all field assets
func (s *StaticSource) Fields() ([]assets.Field, error) {
set := make([]assets.Field, len(s.s.Fields))
for i := range s.s.Fields {
set[i] = s.s.Fields[i]
}
return set, nil
}
// Flow returns the flow asset with the given UUID
func (s *StaticSource) FlowByUUID(uuid assets.FlowUUID) (assets.Flow, error) {
for _, flow := range s.s.Flows {
if flow.UUID() == uuid {
return flow, nil
}
}
return nil, fmt.Errorf("no such flow with UUID '%s'", uuid)
}
// Flow returns the flow asset with the given UUID
func (s *StaticSource) FlowByName(name string) (assets.Flow, error) {
for _, flow := range s.s.Flows {
if strings.EqualFold(flow.Name(), name) {
return flow, nil
}
}
return nil, fmt.Errorf("no such flow with name '%s'", name)
}
// Globals returns all global assets
func (s *StaticSource) Globals() ([]assets.Global, error) {
set := make([]assets.Global, len(s.s.Globals))
for i := range s.s.Globals {
set[i] = s.s.Globals[i]
}
return set, nil
}
// Groups returns all group assets
func (s *StaticSource) Groups() ([]assets.Group, error) {
set := make([]assets.Group, len(s.s.Groups))
for i := range s.s.Groups {
set[i] = s.s.Groups[i]
}
return set, nil
}
// Labels returns all label assets
func (s *StaticSource) Labels() ([]assets.Label, error) {
set := make([]assets.Label, len(s.s.Labels))
for i := range s.s.Labels {
set[i] = s.s.Labels[i]
}
return set, nil
}
// Locations returns all location assets
func (s *StaticSource) Locations() ([]assets.LocationHierarchy, error) {
set := make([]assets.LocationHierarchy, len(s.s.Locations))
for i := range s.s.Locations {
set[i] = s.s.Locations[i]
}
return set, nil
}
// OptIns returns all optin assets
func (s *StaticSource) OptIns() ([]assets.OptIn, error) {
set := make([]assets.OptIn, len(s.s.OptIns))
for i := range s.s.OptIns {
set[i] = s.s.OptIns[i]
}
return set, nil
}
// Resthooks returns all resthook assets
func (s *StaticSource) Resthooks() ([]assets.Resthook, error) {
set := make([]assets.Resthook, len(s.s.Resthooks))
for i := range s.s.Resthooks {
set[i] = s.s.Resthooks[i]
}
return set, nil
}
// Templates returns all template assets
func (s *StaticSource) Templates() ([]assets.Template, error) {
set := make([]assets.Template, len(s.s.Templates))
for i := range s.s.Templates {
set[i] = s.s.Templates[i]
}
return set, nil
}
// Topics returns all topic assets
func (s *StaticSource) Topics() ([]assets.Topic, error) {
set := make([]assets.Topic, len(s.s.Topics))
for i := range s.s.Topics {
set[i] = s.s.Topics[i]
}
return set, nil
}
// Users returns all user assets
func (s *StaticSource) Users() ([]assets.User, error) {
set := make([]assets.User, len(s.s.Users))
for i := range s.s.Users {
set[i] = s.s.Users[i]
}
return set, nil
}
var _ assets.Source = (*StaticSource)(nil)