Skip to content

Commit

Permalink
Merge pull request #336 from nyaruka/locations_as_set
Browse files Browse the repository at this point in the history
LocationHierarchy assets should be managed like a set like other asset types
  • Loading branch information
rowanseymour authored Jul 30, 2018
2 parents 9c5ed68 + c936115 commit 6b5780d
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 58 deletions.
18 changes: 9 additions & 9 deletions flows/assets/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ import (
type assetType string

const (
assetTypeChannelSet assetType = "channel_set"
assetTypeFieldSet assetType = "field_set"
assetTypeFlow assetType = "flow"
assetTypeGroupSet assetType = "group_set"
assetTypeLabelSet assetType = "label_set"
assetTypeLocationHierarchy assetType = "location_hierarchy"
assetTypeResthookSet assetType = "resthook_set"
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
Expand Down Expand Up @@ -134,8 +134,8 @@ func (c *AssetCache) Include(data json.RawMessage) error {
func readAsset(data json.RawMessage, itemType assetType) (interface{}, error) {
var assetReader func(data json.RawMessage) (interface{}, error)

if itemType == assetTypeLocationHierarchy {
assetReader = func(data json.RawMessage) (interface{}, error) { return utils.ReadLocationHierarchy(data) }
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 {
Expand Down
14 changes: 7 additions & 7 deletions flows/assets/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,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/",
assetTypeLocationHierarchy: "http://testserver/assets/location_hierarchy/",
assetTypeResthookSet: "http://testserver/assets/resthook/",
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/",
},
},
mockResponses: map[string]json.RawMessage{},
Expand Down
11 changes: 5 additions & 6 deletions flows/assets/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"

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

// our implementation of SessionAssets - the high-level API for asset access from the engine
Expand All @@ -22,20 +21,20 @@ 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(assetTypeLocationHierarchy)
return s.server.isTypeSupported(assetTypeLocationHierarchySet)
}

// GetLocationHierarchy gets the location hierarchy asset for the session
func (s *sessionAssets) GetLocationHierarchy() (*utils.LocationHierarchy, error) {
asset, err := s.cache.GetAsset(s.server, assetTypeLocationHierarchy, "")
func (s *sessionAssets) GetLocationHierarchySet() (*flows.LocationHierarchySet, error) {
asset, err := s.cache.GetAsset(s.server, assetTypeLocationHierarchySet, "")
if err != nil {
return nil, err
}
hierarchy, isType := asset.(*utils.LocationHierarchy)
set, isType := asset.(*flows.LocationHierarchySet)
if !isType {
return nil, fmt.Errorf("asset cache contains asset with wrong type")
}
return hierarchy, nil
return set, nil
}

// GetChannel gets a channel asset for the session
Expand Down
2 changes: 1 addition & 1 deletion flows/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ type SessionAssets interface {
GetLabelSet() (*LabelSet, error)

HasLocations() bool
GetLocationHierarchy() (*utils.LocationHierarchy, error)
GetLocationHierarchySet() (*LocationHierarchySet, error)

GetResthookSet() (*ResthookSet, error)
}
Expand Down
37 changes: 37 additions & 0 deletions flows/location.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package flows

import (
"encoding/json"
"strings"

"github.com/nyaruka/goflow/excellent/types"
Expand Down Expand Up @@ -46,3 +47,39 @@ func (p LocationPath) ToXJSON(env utils.Environment) types.XText {
}

var _ types.XValue = LocationPath("")

// LocationHierarchySet defines the unordered set of all location hierarchies for a session
type LocationHierarchySet struct {
hierarchies []*utils.LocationHierarchy
}

// NewLocationHierarchySet creates a new location hierarchy set from the given list of hierarchies
func NewLocationHierarchySet(hierarchies []*utils.LocationHierarchy) *LocationHierarchySet {
return &LocationHierarchySet{hierarchies: hierarchies}
}

// All returns all hierarchies in this location hierarchy set
func (s *LocationHierarchySet) All() []*utils.LocationHierarchy {
return s.hierarchies
}

//------------------------------------------------------------------------------------------
// JSON Encoding / Decoding
//------------------------------------------------------------------------------------------

// ReadLocationHierarchySet reads a location hierarchy set from the given JSON
func ReadLocationHierarchySet(data json.RawMessage) (*LocationHierarchySet, error) {
items, err := utils.UnmarshalArray(data)
if err != nil {
return nil, err
}

hierarchies := make([]*utils.LocationHierarchy, len(items))
for d := range items {
if hierarchies[d], err = utils.ReadLocationHierarchy(items[d]); err != nil {
return nil, err
}
}

return NewLocationHierarchySet(hierarchies), nil
}
9 changes: 8 additions & 1 deletion flows/runs/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,14 @@ func (e *runEnvironment) Languages() utils.LanguageList {
func (e *runEnvironment) Locations() (*utils.LocationHierarchy, error) {
sessionAssets := e.run.Session().Assets()
if sessionAssets.HasLocations() {
return sessionAssets.GetLocationHierarchy()
hierarchies, err := sessionAssets.GetLocationHierarchySet()
if err != nil {
return nil, err
}

// in the future we might support more than one hiearchy per session,
// but for now we only use the first one
return hierarchies.All()[0], nil
}

return nil, nil
Expand Down
70 changes: 36 additions & 34 deletions test/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,41 +173,43 @@ var sessionAssets = `[
]
},
{
"type": "location_hierarchy",
"type": "location_hierarchy_set",
"url": "http://testserver/assets/location_hierarchy",
"content": {
"id": "2342",
"name": "Rwanda",
"aliases": ["Ruanda"],
"children": [
{
"id": "234521",
"name": "Kigali City",
"aliases": ["Kigali", "Kigari"],
"children": [
{
"id": "57735322",
"name": "Gasabo",
"children": [
{
"id": "575743222",
"name": "Gisozi"
},
{
"id": "457378732",
"name": "Ndera"
}
]
},
{
"id": "46547322",
"name": "Nyarugenge",
"children": []
}
]
}
]
}
"content": [
{
"id": "2342",
"name": "Rwanda",
"aliases": ["Ruanda"],
"children": [
{
"id": "234521",
"name": "Kigali City",
"aliases": ["Kigali", "Kigari"],
"children": [
{
"id": "57735322",
"name": "Gasabo",
"children": [
{
"id": "575743222",
"name": "Gisozi"
},
{
"id": "457378732",
"name": "Ndera"
}
]
},
{
"id": "46547322",
"name": "Nyarugenge",
"children": []
}
]
}
]
}
]
},
{
"type": "resthook_set",
Expand Down

0 comments on commit 6b5780d

Please sign in to comment.