diff --git a/pkg/envoy/resource.go b/pkg/envoy/resource.go deleted file mode 100644 index cafd6c831..000000000 --- a/pkg/envoy/resource.go +++ /dev/null @@ -1,85 +0,0 @@ -package envoy - -type ( - Resource interface { - Identifiers() ResourceIdentifiers - ResourceType() string - Refs() NodeRefSet - } - - NodeRefSet []*NodeRef - NodeRef struct { - // @todo check with Denis regarding strings here (the cdocs comment) - ResourceType string - Identifiers ResourceIdentifiers - } - - ResourceIdentifiers map[string]bool - - nodeIndex map[string]map[string]*node -) - -func (ri ResourceIdentifiers) Add(ii ...string) ResourceIdentifiers { - for _, i := range ii { - if len(i) > 0 { - ri[i] = true - } - } - - return ri -} - -func (ri ResourceIdentifiers) Remove(ii ...string) ResourceIdentifiers { - for _, i := range ii { - delete(ri, i) - } - - return ri -} - -func (ri ResourceIdentifiers) HasAny(ii ...string) bool { - for _, i := range ii { - if ri[i] { - return true - } - } - - return false -} - -func (ri ResourceIdentifiers) StringSlice() []string { - ss := make([]string, 0, len(ri)) - for k := range ri { - ss = append(ss, k) - } - return ss -} - -func (ri nodeIndex) Add(nn ...*node) { - for _, n := range nn { - rt := n.res.ResourceType() - if _, has := ri[rt]; !has { - ri[rt] = make(map[string]*node) - } - - for i := range n.res.Identifiers() { - ri[rt][i] = n - } - } -} - -func (ri nodeIndex) GetRef(ref *NodeRef) *node { - res, has := ri[ref.ResourceType] - if !has { - return nil - } - - for i := range ref.Identifiers { - r, has := res[i] - if has { - return r - } - } - - return nil -} diff --git a/pkg/envoy/resource/base.go b/pkg/envoy/resource/base.go new file mode 100644 index 000000000..20a96cdec --- /dev/null +++ b/pkg/envoy/resource/base.go @@ -0,0 +1,47 @@ +package resource + +type ( + base struct { + rt string + ii Identifiers + rr RefSet + } +) + +// State management methods + +// AddIdentifier adds a set of identifiers to the current resource +func (t *base) AddIdentifier(ss ...string) { + if t.ii == nil { + t.ii = make(Identifiers) + } + + t.ii.Add(ss...) +} + +// AddRef adds a new reference to the current resource +func (t *base) AddRef(rt string, ii ...string) { + if t.rr == nil { + t.rr = make(RefSet, 0, 10) + } + + ref := &Ref{ResourceType: rt, Identifiers: Identifiers{}.Add(ii...)} + t.rr = append(t.rr, ref) +} + +// SetResourceType sets the resource type of the current resource struct +func (t *base) SetResourceType(rt string) { + t.rt = rt +} + +// Resource interface methods + +func (t *base) Identifiers() Identifiers { + return t.ii +} +func (t *base) ResourceType() string { + return t.rt +} +func (t *base) Refs() RefSet { + return t.rr +} diff --git a/pkg/envoy/resource/compose_module.go b/pkg/envoy/resource/compose_module.go new file mode 100644 index 000000000..11d29ce65 --- /dev/null +++ b/pkg/envoy/resource/compose_module.go @@ -0,0 +1,56 @@ +package resource + +import ( + "fmt" + "strconv" + + "github.com/cortezaproject/corteza-server/compose/types" +) + +type ( + ComposeModule struct { + *base + Res *types.Module + } +) + +func NewComposeModule(mod *types.Module) *ComposeModule { + r := &ComposeModule{base: &base{}} + r.SetResourceType("compose:module") + r.Res = mod + + if mod.Handle != "" { + r.AddIdentifier(mod.Handle) + } + if mod.Name != "" { + r.AddIdentifier(mod.Name) + } + if mod.ID > 0 { + r.AddIdentifier(strconv.FormatUint(mod.ID, 10)) + } + + // Field deps. + for _, f := range mod.Fields { + switch f.Kind { + case "Record": + refM := f.Options.String("module") + if refM != "" && refM != "0" { + r.AddRef("compose:module", refM) + } + } + } + + return r +} + +func (m *ComposeModule) SearchQuery() types.ModuleFilter { + f := types.ModuleFilter{Query: ""} + + f.Handle = m.Res.Handle + f.Name = m.Res.Name + if m.Res.ID > 0 { + f.Query = fmt.Sprintf("moduleID=%d", m.Res.ID) + } + + return f +} diff --git a/pkg/envoy/resource/compose_namespace.go b/pkg/envoy/resource/compose_namespace.go new file mode 100644 index 000000000..8fd6f59f4 --- /dev/null +++ b/pkg/envoy/resource/compose_namespace.go @@ -0,0 +1,44 @@ +package resource + +import ( + "fmt" + "strconv" + + "github.com/cortezaproject/corteza-server/compose/types" +) + +type ( + ComposeNamespace struct { + *base + Res *types.Namespace + } +) + +func NewComposeNamespace(ns *types.Namespace) *ComposeNamespace { + r := &ComposeNamespace{base: &base{}} + r.SetResourceType("compose:namespace") + r.Res = ns + + if ns.Slug != "" { + r.AddIdentifier(ns.Slug) + } + if ns.Name != "" { + r.AddIdentifier(ns.Name) + } + if ns.ID > 0 { + r.AddIdentifier(strconv.FormatUint(ns.ID, 10)) + } + return r +} + +func (m *ComposeNamespace) SearchQuery() types.NamespaceFilter { + f := types.NamespaceFilter{Query: ""} + + f.Slug = m.Res.Slug + f.Name = m.Res.Name + if m.Res.ID > 0 { + f.Query = fmt.Sprintf("namespaceID=%d", m.Res.ID) + } + + return f +} diff --git a/pkg/envoy/resource/compose_record_set.go b/pkg/envoy/resource/compose_record_set.go new file mode 100644 index 000000000..f833d6341 --- /dev/null +++ b/pkg/envoy/resource/compose_record_set.go @@ -0,0 +1,23 @@ +package resource + +import ( + "github.com/cortezaproject/corteza-server/compose/types" +) + +type ( + ComposeRecordSet struct { + *base + + Walk Walker + } + + Walker func(r *types.Record) error +) + +// @todo add record provider +func NewComposeRecordSet() *ComposeRecordSet { + r := &ComposeRecordSet{base: &base{}} + r.SetResourceType("compose:record") + + return r +} diff --git a/pkg/envoy/resource/types.go b/pkg/envoy/resource/types.go new file mode 100644 index 000000000..23cf6c2d2 --- /dev/null +++ b/pkg/envoy/resource/types.go @@ -0,0 +1,67 @@ +package resource + +type ( + Interface interface { + Identifiers() Identifiers + ResourceType() string + Refs() RefSet + } + + RefSet []*Ref + Ref struct { + // @todo check with Denis regarding strings here (the cdocs comment) + // @todo should this become node type instead? + ResourceType string + Identifiers Identifiers + } + + Identifiers map[string]bool +) + +func (ri Identifiers) Add(ii ...string) Identifiers { + for _, i := range ii { + if len(i) > 0 { + ri[i] = true + } + } + + return ri +} + +func (ri Identifiers) Remove(ii ...string) Identifiers { + for _, i := range ii { + delete(ri, i) + } + + return ri +} + +func (ri Identifiers) HasAny(ii ...string) bool { + for _, i := range ii { + if ri[i] { + return true + } + } + + return false +} + +func (ri Identifiers) StringSlice() []string { + ss := make([]string, 0, len(ri)) + for k := range ri { + ss = append(ss, k) + } + return ss +} + +func (ss RefSet) FilterByResourceType(rt string) RefSet { + rr := make(RefSet, 0, len(ss)) + + for _, s := range ss { + if s.ResourceType == rt { + rr = append(rr, s) + } + } + + return rr +}