Skip to content

Commit

Permalink
Merge pull request #337 from nyaruka/assets_v50
Browse files Browse the repository at this point in the history
Rework assets so whether or not they are managed as sets is configured at the type level
  • Loading branch information
rowanseymour authored Jul 31, 2018
2 parents 6b5780d + 1c33ab4 commit 768f7c5
Show file tree
Hide file tree
Showing 14 changed files with 94 additions and 81 deletions.
8 changes: 4 additions & 4 deletions cmd/flowrunner/testdata/default.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[
{
"type": "field_set",
"type": "field",
"url": "http://testserver/assets/field",
"content": [
{
Expand Down Expand Up @@ -31,7 +31,7 @@
]
},
{
"type": "group_set",
"type": "group",
"url": "http://testserver/assets/group",
"content": [
{
Expand All @@ -50,7 +50,7 @@
]
},
{
"type": "channel_set",
"type": "channel",
"url": "http://testserver/assets/channel",
"content": [
{
Expand All @@ -77,7 +77,7 @@
]
},
{
"type": "label_set",
"type": "label",
"url": "http://testserver/assets/label",
"content": [
{
Expand Down
2 changes: 1 addition & 1 deletion cmd/flowrunner/testdata/flows/brochure.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
}
},
{
"type": "group_set",
"type": "group",
"url": "http://testserver/assets/group",
"content": [
{
Expand Down
4 changes: 2 additions & 2 deletions cmd/flowrunner/testdata/flows/dynamic_groups.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
}
},
{
"type": "field_set",
"type": "field",
"url": "http://testserver/assets/field",
"content": [
{
Expand All @@ -90,7 +90,7 @@
]
},
{
"type": "group_set",
"type": "group",
"url": "http://testserver/assets/group",
"content": [
{
Expand Down
2 changes: 1 addition & 1 deletion cmd/flowrunner/testdata/flows/resthook.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
}
},
{
"type": "resthook_set",
"type": "resthook",
"url": "http://testserver/assets/resthook",
"content": [
{
Expand Down
8 changes: 4 additions & 4 deletions cmd/flowserver/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ var testFlowMissingGroupAssets = `[
}
},
{
"type": "group_set",
"type": "group",
"url": "http://testserver/assets/group",
"content": [
{
Expand Down Expand Up @@ -199,9 +199,9 @@ var startRequestTemplate = `{
"assets": %s,
"asset_server": {
"type_urls": {
"flow": "http://testserver/assets/flow/{uuid}/",
"field_set": "http://testserver/assets/field/",
"group_set": "http://testserver/assets/group/"
"flow": "http://testserver/assets/flow/",
"field": "http://testserver/assets/field/",
"group": "http://testserver/assets/group/"
}
},
"trigger": {
Expand Down
10 changes: 5 additions & 5 deletions cmd/flowserver/static/start.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
}
},
{
"type": "field_set",
"type": "field",
"url": "http://testserver/assets/field",
"content": [
{
Expand All @@ -33,7 +33,7 @@
]
},
{
"type": "channel_set",
"type": "channel",
"url": "http://testserver/assets/channel",
"content": [
{
Expand All @@ -53,9 +53,9 @@
],
"asset_server": {
"type_urls": {
"flow": "http://testserver/assets/flow/{uuid}/",
"channel_set": "http://testserver/assets/channel/",
"field_set": "http://testserver/assets/field/"
"flow": "http://testserver/assets/flow/",
"channel": "http://testserver/assets/channel/",
"field": "http://testserver/assets/field/"
}
},
"trigger": {
Expand Down
6 changes: 3 additions & 3 deletions flows/assets/assets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestAssetCache(t *testing.T) {
asset, err := cache.GetAsset(server, assetType("pizza"), "")
assert.EqualError(t, err, "asset type 'pizza' not supported by asset server")

asset, err = cache.GetAsset(server, assetTypeLabelSet, "")
asset, err = cache.GetAsset(server, assetTypeLabel, "")
assert.NoError(t, err)
assert.Equal(t, server.MockedRequests(), []string{"http://testserver/assets/label/"})

Expand All @@ -30,7 +30,7 @@ func TestAssetCache(t *testing.T) {
assert.NotNil(t, labelSet.FindByName("Spam"))

// check that we can refetch without making another server request
asset, err = cache.GetAsset(server, assetTypeLabelSet, "")
asset, err = cache.GetAsset(server, assetTypeLabel, "")
assert.NoError(t, err)
assert.Equal(t, server.MockedRequests(), []string{"http://testserver/assets/label/"})
}
Expand All @@ -50,7 +50,7 @@ func TestAssetServer(t *testing.T) {
url, err := server.getAssetURL(assetType("pizza"), "")
assert.EqualError(t, err, "asset type 'pizza' not supported by asset server")

url, err = server.getAssetURL(assetTypeGroupSet, "")
url, err = server.getAssetURL(assetTypeGroup, "")
assert.NoError(t, err)
assert.Equal(t, "http://testserver/assets/group/", url)

Expand Down
35 changes: 3 additions & 32 deletions flows/assets/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,11 @@ import (
"sync"
"time"

"github.com/nyaruka/goflow/flows"
"github.com/nyaruka/goflow/flows/definition"
"github.com/nyaruka/goflow/utils"

"github.com/karlseguin/ccache"
)

type assetType string

const (
assetTypeChannelSet assetType = "channel_set"
assetTypeFieldSet assetType = "field_set"
assetTypeFlow assetType = "flow"
assetTypeGroupSet assetType = "group_set"
assetTypeLabelSet assetType = "label_set"
assetTypeLocationHierarchySet assetType = "location_hierarchy_set"
assetTypeResthookSet assetType = "resthook_set"
)

// AssetCache fetches and caches assets for the engine
type AssetCache struct {
cache *ccache.Cache
Expand Down Expand Up @@ -132,25 +118,10 @@ func (c *AssetCache) Include(data json.RawMessage) error {

// reads an asset from the given raw JSON data
func readAsset(data json.RawMessage, itemType assetType) (interface{}, error) {
var assetReader func(data json.RawMessage) (interface{}, error)

if itemType == assetTypeLocationHierarchySet {
assetReader = func(data json.RawMessage) (interface{}, error) { return flows.ReadLocationHierarchySet(data) }
} else if itemType == assetTypeChannelSet {
assetReader = func(data json.RawMessage) (interface{}, error) { return flows.ReadChannelSet(data) }
} else if itemType == assetTypeFieldSet {
assetReader = func(data json.RawMessage) (interface{}, error) { return flows.ReadFieldSet(data) }
} else if itemType == assetTypeFlow {
assetReader = func(data json.RawMessage) (interface{}, error) { return definition.ReadFlow(data) }
} else if itemType == assetTypeGroupSet {
assetReader = func(data json.RawMessage) (interface{}, error) { return flows.ReadGroupSet(data) }
} else if itemType == assetTypeLabelSet {
assetReader = func(data json.RawMessage) (interface{}, error) { return flows.ReadLabelSet(data) }
} else if itemType == assetTypeResthookSet {
assetReader = func(data json.RawMessage) (interface{}, error) { return flows.ReadResthookSet(data) }
} else {
cfg := typeConfigs[itemType]
if cfg == nil {
return nil, fmt.Errorf("unsupported asset type: %s", itemType)
}

return assetReader(data)
return cfg.reader(data)
}
17 changes: 8 additions & 9 deletions flows/assets/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"io/ioutil"
"net/http"
"strings"

"github.com/nyaruka/goflow/utils"

Expand Down Expand Up @@ -58,7 +57,7 @@ func (s *assetServer) getAssetURL(itemType assetType, itemUUID string) (string,
}

if itemUUID != "" {
url = strings.Replace(url, "{uuid}", itemUUID, -1)
url = fmt.Sprintf("%s%s/", url, itemUUID)
}

return url, nil
Expand Down Expand Up @@ -112,13 +111,13 @@ func NewMockAssetServer() *MockAssetServer {
return &MockAssetServer{
assetServer: assetServer{
typeURLs: map[assetType]string{
assetTypeChannelSet: "http://testserver/assets/channel/",
assetTypeFieldSet: "http://testserver/assets/field/",
assetTypeFlow: "http://testserver/assets/flow/{uuid}/",
assetTypeGroupSet: "http://testserver/assets/group/",
assetTypeLabelSet: "http://testserver/assets/label/",
assetTypeLocationHierarchySet: "http://testserver/assets/location_hierarchy/",
assetTypeResthookSet: "http://testserver/assets/resthook/",
assetTypeChannel: "http://testserver/assets/channel/",
assetTypeField: "http://testserver/assets/field/",
assetTypeFlow: "http://testserver/assets/flow/",
assetTypeGroup: "http://testserver/assets/group/",
assetTypeLabel: "http://testserver/assets/label/",
assetTypeLocationHierarchy: "http://testserver/assets/location_hierarchy/",
assetTypeResthook: "http://testserver/assets/resthook/",
},
},
mockResponses: map[string]json.RawMessage{},
Expand Down
14 changes: 7 additions & 7 deletions flows/assets/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ func NewSessionAssets(cache *AssetCache, server AssetServer) flows.SessionAssets

// HasLocations returns whether locations are supported as an asset item type
func (s *sessionAssets) HasLocations() bool {
return s.server.isTypeSupported(assetTypeLocationHierarchySet)
return s.server.isTypeSupported(assetTypeLocationHierarchy)
}

// GetLocationHierarchy gets the location hierarchy asset for the session
func (s *sessionAssets) GetLocationHierarchySet() (*flows.LocationHierarchySet, error) {
asset, err := s.cache.GetAsset(s.server, assetTypeLocationHierarchySet, "")
asset, err := s.cache.GetAsset(s.server, assetTypeLocationHierarchy, "")
if err != nil {
return nil, err
}
Expand All @@ -52,7 +52,7 @@ func (s *sessionAssets) GetChannel(uuid flows.ChannelUUID) (flows.Channel, error

// GetChannelSet gets the set of all channels asset for the session
func (s *sessionAssets) GetChannelSet() (*flows.ChannelSet, error) {
asset, err := s.cache.GetAsset(s.server, assetTypeChannelSet, "")
asset, err := s.cache.GetAsset(s.server, assetTypeChannel, "")
if err != nil {
return nil, err
}
Expand All @@ -78,7 +78,7 @@ func (s *sessionAssets) GetField(key string) (*flows.Field, error) {

// GetFieldSet gets the set of all fields asset for the session
func (s *sessionAssets) GetFieldSet() (*flows.FieldSet, error) {
asset, err := s.cache.GetAsset(s.server, assetTypeFieldSet, "")
asset, err := s.cache.GetAsset(s.server, assetTypeField, "")
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -117,7 +117,7 @@ func (s *sessionAssets) GetGroup(uuid flows.GroupUUID) (*flows.Group, error) {

// GetGroupSet gets the set of all groups asset for the session
func (s *sessionAssets) GetGroupSet() (*flows.GroupSet, error) {
asset, err := s.cache.GetAsset(s.server, assetTypeGroupSet, "")
asset, err := s.cache.GetAsset(s.server, assetTypeGroup, "")
if err != nil {
return nil, err
}
Expand All @@ -142,7 +142,7 @@ func (s *sessionAssets) GetLabel(uuid flows.LabelUUID) (*flows.Label, error) {
}

func (s *sessionAssets) GetLabelSet() (*flows.LabelSet, error) {
asset, err := s.cache.GetAsset(s.server, assetTypeLabelSet, "")
asset, err := s.cache.GetAsset(s.server, assetTypeLabel, "")
if err != nil {
return nil, err
}
Expand All @@ -154,7 +154,7 @@ func (s *sessionAssets) GetLabelSet() (*flows.LabelSet, error) {
}

func (s *sessionAssets) GetResthookSet() (*flows.ResthookSet, error) {
asset, err := s.cache.GetAsset(s.server, assetTypeResthookSet, "")
asset, err := s.cache.GetAsset(s.server, assetTypeResthook, "")
if err != nil {
return nil, err
}
Expand Down
43 changes: 43 additions & 0 deletions flows/assets/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package assets

import (
"encoding/json"

"github.com/nyaruka/goflow/flows"
"github.com/nyaruka/goflow/flows/definition"
)

type assetType string

const (
assetTypeChannel assetType = "channel"
assetTypeField assetType = "field"
assetTypeFlow assetType = "flow"
assetTypeGroup assetType = "group"
assetTypeLabel assetType = "label"
assetTypeLocationHierarchy assetType = "location_hierarchy"
assetTypeResthook assetType = "resthook"
)

type assetReader func(data json.RawMessage) (interface{}, error)

type assetTypeConfig struct {
manageAsSet bool
reader assetReader
}

var typeConfigs = map[assetType]*assetTypeConfig{}

func registerAssetType(name assetType, manageAsSet bool, reader assetReader) {
typeConfigs[name] = &assetTypeConfig{manageAsSet: manageAsSet, reader: reader}
}

func init() {
registerAssetType(assetTypeChannel, true, func(data json.RawMessage) (interface{}, error) { return flows.ReadChannelSet(data) })
registerAssetType(assetTypeField, true, func(data json.RawMessage) (interface{}, error) { return flows.ReadFieldSet(data) })
registerAssetType(assetTypeFlow, false, func(data json.RawMessage) (interface{}, error) { return definition.ReadFlow(data) })
registerAssetType(assetTypeGroup, true, func(data json.RawMessage) (interface{}, error) { return flows.ReadGroupSet(data) })
registerAssetType(assetTypeLabel, true, func(data json.RawMessage) (interface{}, error) { return flows.ReadLabelSet(data) })
registerAssetType(assetTypeLocationHierarchy, true, func(data json.RawMessage) (interface{}, error) { return flows.ReadLocationHierarchySet(data) })
registerAssetType(assetTypeResthook, true, func(data json.RawMessage) (interface{}, error) { return flows.ReadResthookSet(data) })
}
8 changes: 4 additions & 4 deletions flows/definition/testdata/flow_validation.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@
}
},
{
"type": "field_set",
"type": "field",
"url": "http://testserver/assets/field",
"content": [
{
Expand Down Expand Up @@ -134,7 +134,7 @@
]
},
{
"type": "group_set",
"type": "group",
"url": "http://testserver/assets/group",
"content": [
{
Expand All @@ -153,7 +153,7 @@
]
},
{
"type": "channel_set",
"type": "channel",
"url": "http://testserver/assets/channel",
"content": [
{
Expand All @@ -166,7 +166,7 @@
]
},
{
"type": "label_set",
"type": "label",
"url": "http://testserver/assets/label",
"content": [
{
Expand Down
Loading

0 comments on commit 768f7c5

Please sign in to comment.