-
Notifications
You must be signed in to change notification settings - Fork 19
/
source.go
125 lines (109 loc) · 3.59 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
// Package static is an implementation of AssetSource which loads assets from a static JSON file.
package static
import (
"encoding/json"
"io/ioutil"
"github.com/nyaruka/goflow/assets"
"github.com/nyaruka/goflow/assets/static/types"
"github.com/nyaruka/goflow/utils"
"github.com/pkg/errors"
)
// StaticSource is an asset source which loads assets from a static JSON file
type StaticSource struct {
s struct {
Channels []*types.Channel `json:"channels" validate:"omitempty,dive"`
Fields []*types.Field `json:"fields" validate:"omitempty,dive"`
Flows []*types.Flow `json:"flows" validate:"omitempty,dive"`
Groups []*types.Group `json:"groups" validate:"omitempty,dive"`
Labels []*types.Label `json:"labels" validate:"omitempty,dive"`
Locations []*utils.LocationHierarchy `json:"locations"`
Resthooks []*types.Resthook `json:"resthooks" validate:"omitempty,dive"`
Templates []*types.Template `json:"templates" 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, errors.Wrap(err, "unable to read assets")
}
return s, nil
}
// LoadSource loads a new static source from the given JSON file
func LoadSource(path string) (*StaticSource, error) {
data, err := ioutil.ReadFile(path)
if err != nil {
return nil, errors.Wrapf(err, "error reading file '%s'", path)
}
return NewSource(data)
}
var _ assets.AssetSource = (*StaticSource)(nil)
// 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
}
// 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) Flow(uuid assets.FlowUUID) (assets.Flow, error) {
for _, flow := range s.s.Flows {
if flow.UUID() == uuid {
return flow, nil
}
}
return nil, errors.Errorf("no such flow with UUID '%s'", uuid)
}
// 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
}
// 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
}