diff --git a/codegen/v2/events.gen.go.tpl b/codegen/v2/events.gen.go.tpl index 89235dd52..a4bdc71ee 100644 --- a/codegen/v2/events.gen.go.tpl +++ b/codegen/v2/events.gen.go.tpl @@ -9,8 +9,11 @@ package {{ .Package }} // {{ .Command }} // -{{ if .Imports }} +{{ if or .Imports $.Events.Properties }} import ( +{{ if $.Events.Properties }} + "encoding/json" +{{ end }} {{ range $i, $import := .Imports }} "{{ $import }}" {{ end }} @@ -96,3 +99,43 @@ func (res {{ camelCase $.ResourceIdent "base" }}) {{ camelCase "" $p.Name }}() { return res.{{ $p.Name }} } {{ end }} + + +// Encode internal data to be passed as event params & arguments to triggered Corredor script +func (res {{ camelCase .ResourceIdent "base" }}) Encode() (args map[string][]byte, err error) { + {{- if $.Events.Properties }} + args = make(map[string][]byte) + + {{ range $prop := $.Events.Properties }} + if args["{{ $prop.Name }}"], err = json.Marshal(res.{{ $prop.Name }}); err != nil { + return nil, err + } + {{ end }} + {{ else }} + // Handle argument encoding + {{ end -}} + return +} + +// Decode return values from Corredor script into struct props +func (res *{{ camelCase .ResourceIdent "base" }}) Decode(results map[string][]byte)( err error) { + {{- if $.Events.Result }} + if r, ok := results["result"]; ok && len(results) == 1 { + if err = json.Unmarshal(r, res.{{ $.Events.Result }}); err != nil { + return + } + } + {{ end -}} + + {{- range $prop := $.Events.Properties }} + {{- if not $prop.Immutable }} + if r, ok := results["{{ $prop.Name }}"]; ok && len(results) == 1 { + if err = json.Unmarshal(r, res.{{ $prop.Name }}); err != nil { + return + } + } + {{ end -}} + {{ end -}} + + return +} diff --git a/codegen/v2/events.go.tpl b/codegen/v2/events.go.tpl index b0bcf64ef..31c28f502 100644 --- a/codegen/v2/events.go.tpl +++ b/codegen/v2/events.go.tpl @@ -1,13 +1,9 @@ package {{ .Package }} -{{ if $.Events.Properties }} -import ( - "encoding/json" -) -{{ end }} +import "github.com/cortezaproject/corteza-server/pkg/eventbus" // Match returns false if given conditions do not match event & resource internals -func (res {{ camelCase .ResourceIdent "base" }}) Match(name string, op string, values ...string) bool { +func (res {{ camelCase .ResourceIdent "base" }}) Match(c constraint) bool { // By default we match no mather what kind of constraints we receive // // Function will be called multiple times - once for every trigger constraint @@ -17,42 +13,3 @@ func (res {{ camelCase .ResourceIdent "base" }}) Match(name string, op string, v // When there are multiple values, Match() can decide how to treat them (OR, AND...) return true } - -// Encode internal data to be passed as event params & arguments to triggered Corredor script -func (res {{ camelCase .ResourceIdent "base" }}) Encode() (args map[string][]byte, err error) { - {{- if $.Events.Properties }} - args = make(map[string][]byte) - - {{ range $prop := $.Events.Properties }} - if args["{{ $prop.Name }}"], err = json.Marshal(res.{{ $prop.Name }}); err != nil { - return nil, err - } - {{ end }} - {{ else }} - // Handle argument encoding - {{ end -}} - return -} - -// Decode return values from Corredor script into struct props -func (res *{{ camelCase .ResourceIdent "base" }}) Decode(results map[string][]byte)( err error) { - {{- if $.Events.Result }} - if r, ok := results["result"]; ok && len(results) == 1 { - if err = json.Unmarshal(r, res.{{ $.Events.Result }}); err != nil { - return - } - } - {{ end -}} - - {{- range $prop := $.Events.Properties }} - {{- if not $prop.Immutable }} - if r, ok := results["{{ $prop.Name }}"]; ok && len(results) == 1 { - if err = json.Unmarshal(r, res.{{ $prop.Name }}); err != nil { - return - } - } - {{ end -}} - {{ end -}} - - return -} diff --git a/compose/service/event/compose.gen.go b/compose/service/event/compose.gen.go index cdeab6c82..09be80439 100644 --- a/compose/service/event/compose.gen.go +++ b/compose/service/event/compose.gen.go @@ -10,6 +10,8 @@ package event // import ( + "encoding/json" + "github.com/cortezaproject/corteza-server/pkg/auth" ) @@ -111,3 +113,24 @@ func (res *composeBase) SetInvoker(argInvoker auth.Identifiable) { func (res composeBase) Invoker() auth.Identifiable { return res.invoker } + +// Encode internal data to be passed as event params & arguments to triggered Corredor script +func (res composeBase) Encode() (args map[string][]byte, err error) { + args = make(map[string][]byte) + + if args["invoker"], err = json.Marshal(res.invoker); err != nil { + return nil, err + } + + return +} + +// Decode return values from Corredor script into struct props +func (res *composeBase) Decode(results map[string][]byte) (err error) { + if r, ok := results["invoker"]; ok && len(results) == 1 { + if err = json.Unmarshal(r, res.invoker); err != nil { + return + } + } + return +} diff --git a/compose/service/event/compose.go b/compose/service/event/compose.go index ab8d31d16..0b3dfbcc5 100644 --- a/compose/service/event/compose.go +++ b/compose/service/event/compose.go @@ -1,23 +1,22 @@ package event import ( - "encoding/json" - + "github.com/cortezaproject/corteza-server/pkg/eventbus" "github.com/cortezaproject/corteza-server/pkg/scheduler" ) // Match returns false if given conditions do not match event & resource internals -func (res composeOnInterval) Match(_ string, _ string, values ...string) bool { - return scheduler.OnInterval(values...) +func (res composeOnInterval) Match(c eventbus.ConstraintMatcher) bool { + return scheduler.OnInterval(c.Values()...) } // Match returns false if given conditions do not match event & resource internals -func (res composeOnTimestamp) Match(_ string, _ string, values ...string) bool { - return scheduler.OnTimestamp(values...) +func (res composeOnTimestamp) Match(c eventbus.ConstraintMatcher) bool { + return scheduler.OnTimestamp(c.Values()...) } // Match returns false if given conditions do not match event & resource internals -func (res composeBase) Match(name string, op string, values ...string) bool { +func (res composeBase) Match(c eventbus.ConstraintMatcher) bool { // By default we match no mather what kind of constraints we receive // // Function will be called multiple times - once for every trigger constraint @@ -25,26 +24,6 @@ func (res composeBase) Match(name string, op string, values ...string) bool { // constraint#1 AND constraint#2 AND constraint#3 ... // // When there are multiple values, Match() can decide how to treat them (OR, AND...) + return true } - -// Encode internal data to be passed as event params & arguments to triggered Corredor script -func (res composeBase) Encode() (args map[string][]byte, err error) { - args = make(map[string][]byte) - - if args["invoker"], err = json.Marshal(res.invoker); err != nil { - return nil, err - } - - return -} - -// Decode return values from Corredor script into struct props -func (res *composeBase) Decode(results map[string][]byte) (err error) { - if r, ok := results["invoker"]; ok && len(results) == 1 { - if err = json.Unmarshal(r, res.invoker); err != nil { - return - } - } - return -} diff --git a/compose/service/event/module.gen.go b/compose/service/event/module.gen.go index 1a0fe6285..e52c7bcd6 100644 --- a/compose/service/event/module.gen.go +++ b/compose/service/event/module.gen.go @@ -10,6 +10,8 @@ package event // import ( + "encoding/json" + "github.com/cortezaproject/corteza-server/compose/types" "github.com/cortezaproject/corteza-server/pkg/auth" @@ -292,3 +294,48 @@ func (res *moduleBase) SetInvoker(argInvoker auth.Identifiable) { func (res moduleBase) Invoker() auth.Identifiable { return res.invoker } + +// Encode internal data to be passed as event params & arguments to triggered Corredor script +func (res moduleBase) Encode() (args map[string][]byte, err error) { + args = make(map[string][]byte) + + if args["module"], err = json.Marshal(res.module); err != nil { + return nil, err + } + + if args["oldModule"], err = json.Marshal(res.oldModule); err != nil { + return nil, err + } + + if args["namespace"], err = json.Marshal(res.namespace); err != nil { + return nil, err + } + + if args["invoker"], err = json.Marshal(res.invoker); err != nil { + return nil, err + } + + return +} + +// Decode return values from Corredor script into struct props +func (res *moduleBase) Decode(results map[string][]byte) (err error) { + if r, ok := results["result"]; ok && len(results) == 1 { + if err = json.Unmarshal(r, res.module); err != nil { + return + } + } + + if r, ok := results["module"]; ok && len(results) == 1 { + if err = json.Unmarshal(r, res.module); err != nil { + return + } + } + + if r, ok := results["invoker"]; ok && len(results) == 1 { + if err = json.Unmarshal(r, res.invoker); err != nil { + return + } + } + return +} diff --git a/compose/service/event/module.go b/compose/service/event/module.go index baaf212a2..ac840862a 100644 --- a/compose/service/event/module.go +++ b/compose/service/event/module.go @@ -1,11 +1,9 @@ package event -import ( - "encoding/json" -) +import "github.com/cortezaproject/corteza-server/pkg/eventbus" // Match returns false if given conditions do not match event & resource internals -func (res moduleBase) Match(name string, op string, values ...string) bool { +func (res moduleBase) Match(c eventbus.ConstraintMatcher) bool { // By default we match no mather what kind of constraints we receive // // Function will be called multiple times - once for every trigger constraint @@ -15,48 +13,3 @@ func (res moduleBase) Match(name string, op string, values ...string) bool { // When there are multiple values, Match() can decide how to treat them (OR, AND...) return true } - -// Encode internal data to be passed as event params & arguments to triggered Corredor script -func (res moduleBase) Encode() (args map[string][]byte, err error) { - args = make(map[string][]byte) - - if args["module"], err = json.Marshal(res.module); err != nil { - return nil, err - } - - if args["oldModule"], err = json.Marshal(res.oldModule); err != nil { - return nil, err - } - - if args["namespace"], err = json.Marshal(res.namespace); err != nil { - return nil, err - } - - if args["invoker"], err = json.Marshal(res.invoker); err != nil { - return nil, err - } - - return -} - -// Decode return values from Corredor script into struct props -func (res *moduleBase) Decode(results map[string][]byte) (err error) { - if r, ok := results["result"]; ok && len(results) == 1 { - if err = json.Unmarshal(r, res.module); err != nil { - return - } - } - - if r, ok := results["module"]; ok && len(results) == 1 { - if err = json.Unmarshal(r, res.module); err != nil { - return - } - } - - if r, ok := results["invoker"]; ok && len(results) == 1 { - if err = json.Unmarshal(r, res.invoker); err != nil { - return - } - } - return -} diff --git a/compose/service/event/namespace.gen.go b/compose/service/event/namespace.gen.go index 58881c7ae..7a10a010c 100644 --- a/compose/service/event/namespace.gen.go +++ b/compose/service/event/namespace.gen.go @@ -10,6 +10,8 @@ package event // import ( + "encoding/json" + "github.com/cortezaproject/corteza-server/compose/types" "github.com/cortezaproject/corteza-server/pkg/auth" @@ -270,3 +272,44 @@ func (res *namespaceBase) SetInvoker(argInvoker auth.Identifiable) { func (res namespaceBase) Invoker() auth.Identifiable { return res.invoker } + +// Encode internal data to be passed as event params & arguments to triggered Corredor script +func (res namespaceBase) Encode() (args map[string][]byte, err error) { + args = make(map[string][]byte) + + if args["namespace"], err = json.Marshal(res.namespace); err != nil { + return nil, err + } + + if args["oldNamespace"], err = json.Marshal(res.oldNamespace); err != nil { + return nil, err + } + + if args["invoker"], err = json.Marshal(res.invoker); err != nil { + return nil, err + } + + return +} + +// Decode return values from Corredor script into struct props +func (res *namespaceBase) Decode(results map[string][]byte) (err error) { + if r, ok := results["result"]; ok && len(results) == 1 { + if err = json.Unmarshal(r, res.namespace); err != nil { + return + } + } + + if r, ok := results["namespace"]; ok && len(results) == 1 { + if err = json.Unmarshal(r, res.namespace); err != nil { + return + } + } + + if r, ok := results["invoker"]; ok && len(results) == 1 { + if err = json.Unmarshal(r, res.invoker); err != nil { + return + } + } + return +} diff --git a/compose/service/event/namespace.go b/compose/service/event/namespace.go index ce1a68502..ca8db1cbf 100644 --- a/compose/service/event/namespace.go +++ b/compose/service/event/namespace.go @@ -1,11 +1,9 @@ package event -import ( - "encoding/json" -) +import "github.com/cortezaproject/corteza-server/pkg/eventbus" // Match returns false if given conditions do not match event & resource internals -func (res namespaceBase) Match(name string, op string, values ...string) bool { +func (res namespaceBase) Match(c eventbus.ConstraintMatcher) bool { // By default we match no mather what kind of constraints we receive // // Function will be called multiple times - once for every trigger constraint @@ -15,44 +13,3 @@ func (res namespaceBase) Match(name string, op string, values ...string) bool { // When there are multiple values, Match() can decide how to treat them (OR, AND...) return true } - -// Encode internal data to be passed as event params & arguments to triggered Corredor script -func (res namespaceBase) Encode() (args map[string][]byte, err error) { - args = make(map[string][]byte) - - if args["namespace"], err = json.Marshal(res.namespace); err != nil { - return nil, err - } - - if args["oldNamespace"], err = json.Marshal(res.oldNamespace); err != nil { - return nil, err - } - - if args["invoker"], err = json.Marshal(res.invoker); err != nil { - return nil, err - } - - return -} - -// Decode return values from Corredor script into struct props -func (res *namespaceBase) Decode(results map[string][]byte) (err error) { - if r, ok := results["result"]; ok && len(results) == 1 { - if err = json.Unmarshal(r, res.namespace); err != nil { - return - } - } - - if r, ok := results["namespace"]; ok && len(results) == 1 { - if err = json.Unmarshal(r, res.namespace); err != nil { - return - } - } - - if r, ok := results["invoker"]; ok && len(results) == 1 { - if err = json.Unmarshal(r, res.invoker); err != nil { - return - } - } - return -} diff --git a/compose/service/event/page.gen.go b/compose/service/event/page.gen.go index a819118f9..5d17d47e6 100644 --- a/compose/service/event/page.gen.go +++ b/compose/service/event/page.gen.go @@ -10,6 +10,8 @@ package event // import ( + "encoding/json" + "github.com/cortezaproject/corteza-server/compose/types" "github.com/cortezaproject/corteza-server/pkg/auth" @@ -292,3 +294,48 @@ func (res *pageBase) SetInvoker(argInvoker auth.Identifiable) { func (res pageBase) Invoker() auth.Identifiable { return res.invoker } + +// Encode internal data to be passed as event params & arguments to triggered Corredor script +func (res pageBase) Encode() (args map[string][]byte, err error) { + args = make(map[string][]byte) + + if args["page"], err = json.Marshal(res.page); err != nil { + return nil, err + } + + if args["oldPage"], err = json.Marshal(res.oldPage); err != nil { + return nil, err + } + + if args["namespace"], err = json.Marshal(res.namespace); err != nil { + return nil, err + } + + if args["invoker"], err = json.Marshal(res.invoker); err != nil { + return nil, err + } + + return +} + +// Decode return values from Corredor script into struct props +func (res *pageBase) Decode(results map[string][]byte) (err error) { + if r, ok := results["result"]; ok && len(results) == 1 { + if err = json.Unmarshal(r, res.page); err != nil { + return + } + } + + if r, ok := results["page"]; ok && len(results) == 1 { + if err = json.Unmarshal(r, res.page); err != nil { + return + } + } + + if r, ok := results["invoker"]; ok && len(results) == 1 { + if err = json.Unmarshal(r, res.invoker); err != nil { + return + } + } + return +} diff --git a/compose/service/event/page.go b/compose/service/event/page.go index 66aefb99c..dbc47f76e 100644 --- a/compose/service/event/page.go +++ b/compose/service/event/page.go @@ -1,11 +1,9 @@ package event -import ( - "encoding/json" -) +import "github.com/cortezaproject/corteza-server/pkg/eventbus" // Match returns false if given conditions do not match event & resource internals -func (res pageBase) Match(name string, op string, values ...string) bool { +func (res pageBase) Match(c eventbus.ConstraintMatcher) bool { // By default we match no mather what kind of constraints we receive // // Function will be called multiple times - once for every trigger constraint @@ -15,48 +13,3 @@ func (res pageBase) Match(name string, op string, values ...string) bool { // When there are multiple values, Match() can decide how to treat them (OR, AND...) return true } - -// Encode internal data to be passed as event params & arguments to triggered Corredor script -func (res pageBase) Encode() (args map[string][]byte, err error) { - args = make(map[string][]byte) - - if args["page"], err = json.Marshal(res.page); err != nil { - return nil, err - } - - if args["oldPage"], err = json.Marshal(res.oldPage); err != nil { - return nil, err - } - - if args["namespace"], err = json.Marshal(res.namespace); err != nil { - return nil, err - } - - if args["invoker"], err = json.Marshal(res.invoker); err != nil { - return nil, err - } - - return -} - -// Decode return values from Corredor script into struct props -func (res *pageBase) Decode(results map[string][]byte) (err error) { - if r, ok := results["result"]; ok && len(results) == 1 { - if err = json.Unmarshal(r, res.page); err != nil { - return - } - } - - if r, ok := results["page"]; ok && len(results) == 1 { - if err = json.Unmarshal(r, res.page); err != nil { - return - } - } - - if r, ok := results["invoker"]; ok && len(results) == 1 { - if err = json.Unmarshal(r, res.invoker); err != nil { - return - } - } - return -} diff --git a/compose/service/event/record.gen.go b/compose/service/event/record.gen.go index 626d23e09..e20fb1114 100644 --- a/compose/service/event/record.gen.go +++ b/compose/service/event/record.gen.go @@ -10,6 +10,8 @@ package event // import ( + "encoding/json" + "github.com/cortezaproject/corteza-server/compose/types" "github.com/cortezaproject/corteza-server/pkg/auth" @@ -314,3 +316,52 @@ func (res *recordBase) SetInvoker(argInvoker auth.Identifiable) { func (res recordBase) Invoker() auth.Identifiable { return res.invoker } + +// Encode internal data to be passed as event params & arguments to triggered Corredor script +func (res recordBase) Encode() (args map[string][]byte, err error) { + args = make(map[string][]byte) + + if args["record"], err = json.Marshal(res.record); err != nil { + return nil, err + } + + if args["oldRecord"], err = json.Marshal(res.oldRecord); err != nil { + return nil, err + } + + if args["module"], err = json.Marshal(res.module); err != nil { + return nil, err + } + + if args["namespace"], err = json.Marshal(res.namespace); err != nil { + return nil, err + } + + if args["invoker"], err = json.Marshal(res.invoker); err != nil { + return nil, err + } + + return +} + +// Decode return values from Corredor script into struct props +func (res *recordBase) Decode(results map[string][]byte) (err error) { + if r, ok := results["result"]; ok && len(results) == 1 { + if err = json.Unmarshal(r, res.record); err != nil { + return + } + } + + if r, ok := results["record"]; ok && len(results) == 1 { + if err = json.Unmarshal(r, res.record); err != nil { + return + } + } + + if r, ok := results["invoker"]; ok && len(results) == 1 { + if err = json.Unmarshal(r, res.invoker); err != nil { + return + } + } + return +} diff --git a/compose/service/event/record.go b/compose/service/event/record.go index ade7a570b..4291f3c36 100644 --- a/compose/service/event/record.go +++ b/compose/service/event/record.go @@ -1,11 +1,9 @@ package event -import ( - "encoding/json" -) +import "github.com/cortezaproject/corteza-server/pkg/eventbus" // Match returns false if given conditions do not match event & resource internals -func (res recordBase) Match(name string, op string, values ...string) bool { +func (res recordBase) Match(c eventbus.ConstraintMatcher) bool { // By default we match no mather what kind of constraints we receive // // Function will be called multiple times - once for every trigger constraint @@ -15,52 +13,3 @@ func (res recordBase) Match(name string, op string, values ...string) bool { // When there are multiple values, Match() can decide how to treat them (OR, AND...) return true } - -// Encode internal data to be passed as event params & arguments to triggered Corredor script -func (res recordBase) Encode() (args map[string][]byte, err error) { - args = make(map[string][]byte) - - if args["record"], err = json.Marshal(res.record); err != nil { - return nil, err - } - - if args["oldRecord"], err = json.Marshal(res.oldRecord); err != nil { - return nil, err - } - - if args["module"], err = json.Marshal(res.module); err != nil { - return nil, err - } - - if args["namespace"], err = json.Marshal(res.namespace); err != nil { - return nil, err - } - - if args["invoker"], err = json.Marshal(res.invoker); err != nil { - return nil, err - } - - return -} - -// Decode return values from Corredor script into struct props -func (res *recordBase) Decode(results map[string][]byte) (err error) { - if r, ok := results["result"]; ok && len(results) == 1 { - if err = json.Unmarshal(r, res.record); err != nil { - return - } - } - - if r, ok := results["record"]; ok && len(results) == 1 { - if err = json.Unmarshal(r, res.record); err != nil { - return - } - } - - if r, ok := results["invoker"]; ok && len(results) == 1 { - if err = json.Unmarshal(r, res.invoker); err != nil { - return - } - } - return -} diff --git a/messaging/service/event/channel.gen.go b/messaging/service/event/channel.gen.go index 5cc733ffb..9cab8f1b8 100644 --- a/messaging/service/event/channel.gen.go +++ b/messaging/service/event/channel.gen.go @@ -10,6 +10,8 @@ package event // import ( + "encoding/json" + "github.com/cortezaproject/corteza-server/messaging/types" "github.com/cortezaproject/corteza-server/pkg/auth" @@ -270,3 +272,44 @@ func (res *channelBase) SetInvoker(argInvoker auth.Identifiable) { func (res channelBase) Invoker() auth.Identifiable { return res.invoker } + +// Encode internal data to be passed as event params & arguments to triggered Corredor script +func (res channelBase) Encode() (args map[string][]byte, err error) { + args = make(map[string][]byte) + + if args["channel"], err = json.Marshal(res.channel); err != nil { + return nil, err + } + + if args["oldChannel"], err = json.Marshal(res.oldChannel); err != nil { + return nil, err + } + + if args["invoker"], err = json.Marshal(res.invoker); err != nil { + return nil, err + } + + return +} + +// Decode return values from Corredor script into struct props +func (res *channelBase) Decode(results map[string][]byte) (err error) { + if r, ok := results["result"]; ok && len(results) == 1 { + if err = json.Unmarshal(r, res.channel); err != nil { + return + } + } + + if r, ok := results["channel"]; ok && len(results) == 1 { + if err = json.Unmarshal(r, res.channel); err != nil { + return + } + } + + if r, ok := results["invoker"]; ok && len(results) == 1 { + if err = json.Unmarshal(r, res.invoker); err != nil { + return + } + } + return +} diff --git a/messaging/service/event/channel.go b/messaging/service/event/channel.go index cdd7741d7..70bf2ebe5 100644 --- a/messaging/service/event/channel.go +++ b/messaging/service/event/channel.go @@ -1,11 +1,9 @@ package event -import ( - "encoding/json" -) +import "github.com/cortezaproject/corteza-server/pkg/eventbus" // Match returns false if given conditions do not match event & resource internals -func (res channelBase) Match(name string, op string, values ...string) bool { +func (res channelBase) Match(c eventbus.ConstraintMatcher) bool { // By default we match no mather what kind of constraints we receive // // Function will be called multiple times - once for every trigger constraint @@ -15,44 +13,3 @@ func (res channelBase) Match(name string, op string, values ...string) bool { // When there are multiple values, Match() can decide how to treat them (OR, AND...) return true } - -// Encode internal data to be passed as event params & arguments to triggered Corredor script -func (res channelBase) Encode() (args map[string][]byte, err error) { - args = make(map[string][]byte) - - if args["channel"], err = json.Marshal(res.channel); err != nil { - return nil, err - } - - if args["oldChannel"], err = json.Marshal(res.oldChannel); err != nil { - return nil, err - } - - if args["invoker"], err = json.Marshal(res.invoker); err != nil { - return nil, err - } - - return -} - -// Decode return values from Corredor script into struct props -func (res *channelBase) Decode(results map[string][]byte) (err error) { - if r, ok := results["result"]; ok && len(results) == 1 { - if err = json.Unmarshal(r, res.channel); err != nil { - return - } - } - - if r, ok := results["channel"]; ok && len(results) == 1 { - if err = json.Unmarshal(r, res.channel); err != nil { - return - } - } - - if r, ok := results["invoker"]; ok && len(results) == 1 { - if err = json.Unmarshal(r, res.invoker); err != nil { - return - } - } - return -} diff --git a/messaging/service/event/channel_member.gen.go b/messaging/service/event/channel_member.gen.go index 64f71d7ff..277863468 100644 --- a/messaging/service/event/channel_member.gen.go +++ b/messaging/service/event/channel_member.gen.go @@ -10,6 +10,8 @@ package event // import ( + "encoding/json" + "github.com/cortezaproject/corteza-server/messaging/types" "github.com/cortezaproject/corteza-server/pkg/auth" @@ -306,3 +308,50 @@ func (res *channelMemberBase) SetInvoker(argInvoker auth.Identifiable) { func (res channelMemberBase) Invoker() auth.Identifiable { return res.invoker } + +// Encode internal data to be passed as event params & arguments to triggered Corredor script +func (res channelMemberBase) Encode() (args map[string][]byte, err error) { + args = make(map[string][]byte) + + if args["member"], err = json.Marshal(res.member); err != nil { + return nil, err + } + + if args["channel"], err = json.Marshal(res.channel); err != nil { + return nil, err + } + + if args["invoker"], err = json.Marshal(res.invoker); err != nil { + return nil, err + } + + return +} + +// Decode return values from Corredor script into struct props +func (res *channelMemberBase) Decode(results map[string][]byte) (err error) { + if r, ok := results["result"]; ok && len(results) == 1 { + if err = json.Unmarshal(r, res.member); err != nil { + return + } + } + + if r, ok := results["member"]; ok && len(results) == 1 { + if err = json.Unmarshal(r, res.member); err != nil { + return + } + } + + if r, ok := results["channel"]; ok && len(results) == 1 { + if err = json.Unmarshal(r, res.channel); err != nil { + return + } + } + + if r, ok := results["invoker"]; ok && len(results) == 1 { + if err = json.Unmarshal(r, res.invoker); err != nil { + return + } + } + return +} diff --git a/messaging/service/event/channel_member.go b/messaging/service/event/channel_member.go index 0ef8d5a04..abe0349e2 100644 --- a/messaging/service/event/channel_member.go +++ b/messaging/service/event/channel_member.go @@ -1,11 +1,9 @@ package event -import ( - "encoding/json" -) +import "github.com/cortezaproject/corteza-server/pkg/eventbus" // Match returns false if given conditions do not match event & resource internals -func (res channelMemberBase) Match(name string, op string, values ...string) bool { +func (res channelMemberBase) Match(c eventbus.ConstraintMatcher) bool { // By default we match no mather what kind of constraints we receive // // Function will be called multiple times - once for every trigger constraint @@ -15,50 +13,3 @@ func (res channelMemberBase) Match(name string, op string, values ...string) boo // When there are multiple values, Match() can decide how to treat them (OR, AND...) return true } - -// Encode internal data to be passed as event params & arguments to triggered Corredor script -func (res channelMemberBase) Encode() (args map[string][]byte, err error) { - args = make(map[string][]byte) - - if args["member"], err = json.Marshal(res.member); err != nil { - return nil, err - } - - if args["channel"], err = json.Marshal(res.channel); err != nil { - return nil, err - } - - if args["invoker"], err = json.Marshal(res.invoker); err != nil { - return nil, err - } - - return -} - -// Decode return values from Corredor script into struct props -func (res *channelMemberBase) Decode(results map[string][]byte) (err error) { - if r, ok := results["result"]; ok && len(results) == 1 { - if err = json.Unmarshal(r, res.member); err != nil { - return - } - } - - if r, ok := results["member"]; ok && len(results) == 1 { - if err = json.Unmarshal(r, res.member); err != nil { - return - } - } - - if r, ok := results["channel"]; ok && len(results) == 1 { - if err = json.Unmarshal(r, res.channel); err != nil { - return - } - } - - if r, ok := results["invoker"]; ok && len(results) == 1 { - if err = json.Unmarshal(r, res.invoker); err != nil { - return - } - } - return -} diff --git a/messaging/service/event/command.gen.go b/messaging/service/event/command.gen.go index 3f99b13ef..d62c71cab 100644 --- a/messaging/service/event/command.gen.go +++ b/messaging/service/event/command.gen.go @@ -10,6 +10,8 @@ package event // import ( + "encoding/json" + "github.com/cortezaproject/corteza-server/messaging/types" "github.com/cortezaproject/corteza-server/pkg/auth" @@ -89,3 +91,38 @@ func (res *commandBase) SetInvoker(argInvoker auth.Identifiable) { func (res commandBase) Invoker() auth.Identifiable { return res.invoker } + +// Encode internal data to be passed as event params & arguments to triggered Corredor script +func (res commandBase) Encode() (args map[string][]byte, err error) { + args = make(map[string][]byte) + + if args["command"], err = json.Marshal(res.command); err != nil { + return nil, err + } + + if args["channel"], err = json.Marshal(res.channel); err != nil { + return nil, err + } + + if args["invoker"], err = json.Marshal(res.invoker); err != nil { + return nil, err + } + + return +} + +// Decode return values from Corredor script into struct props +func (res *commandBase) Decode(results map[string][]byte) (err error) { + if r, ok := results["result"]; ok && len(results) == 1 { + if err = json.Unmarshal(r, res.command); err != nil { + return + } + } + + if r, ok := results["invoker"]; ok && len(results) == 1 { + if err = json.Unmarshal(r, res.invoker); err != nil { + return + } + } + return +} diff --git a/messaging/service/event/command.go b/messaging/service/event/command.go index a307cd586..2a68a645a 100644 --- a/messaging/service/event/command.go +++ b/messaging/service/event/command.go @@ -1,11 +1,9 @@ package event -import ( - "encoding/json" -) +import "github.com/cortezaproject/corteza-server/pkg/eventbus" // Match returns false if given conditions do not match event & resource internals -func (res commandBase) Match(name string, op string, values ...string) bool { +func (res commandBase) Match(c eventbus.ConstraintMatcher) bool { // By default we match no mather what kind of constraints we receive // // Function will be called multiple times - once for every trigger constraint @@ -15,38 +13,3 @@ func (res commandBase) Match(name string, op string, values ...string) bool { // When there are multiple values, Match() can decide how to treat them (OR, AND...) return true } - -// Encode internal data to be passed as event params & arguments to triggered Corredor script -func (res commandBase) Encode() (args map[string][]byte, err error) { - args = make(map[string][]byte) - - if args["command"], err = json.Marshal(res.command); err != nil { - return nil, err - } - - if args["channel"], err = json.Marshal(res.channel); err != nil { - return nil, err - } - - if args["invoker"], err = json.Marshal(res.invoker); err != nil { - return nil, err - } - - return -} - -// Decode return values from Corredor script into struct props -func (res *commandBase) Decode(results map[string][]byte) (err error) { - if r, ok := results["result"]; ok && len(results) == 1 { - if err = json.Unmarshal(r, res.command); err != nil { - return - } - } - - if r, ok := results["invoker"]; ok && len(results) == 1 { - if err = json.Unmarshal(r, res.invoker); err != nil { - return - } - } - return -} diff --git a/messaging/service/event/constraint.go b/messaging/service/event/constraint.go new file mode 100644 index 000000000..84fc6cd96 --- /dev/null +++ b/messaging/service/event/constraint.go @@ -0,0 +1,9 @@ +package event + +type ( + constraint interface { + Name() string + Values() []string + Match(value string) bool + } +) diff --git a/messaging/service/event/message.gen.go b/messaging/service/event/message.gen.go index 1306d72f6..94d0f2974 100644 --- a/messaging/service/event/message.gen.go +++ b/messaging/service/event/message.gen.go @@ -10,6 +10,8 @@ package event // import ( + "encoding/json" + "github.com/cortezaproject/corteza-server/messaging/types" "github.com/cortezaproject/corteza-server/pkg/auth" @@ -299,3 +301,54 @@ func (res *messageBase) SetInvoker(argInvoker auth.Identifiable) { func (res messageBase) Invoker() auth.Identifiable { return res.invoker } + +// Encode internal data to be passed as event params & arguments to triggered Corredor script +func (res messageBase) Encode() (args map[string][]byte, err error) { + args = make(map[string][]byte) + + if args["message"], err = json.Marshal(res.message); err != nil { + return nil, err + } + + if args["oldMessage"], err = json.Marshal(res.oldMessage); err != nil { + return nil, err + } + + if args["channel"], err = json.Marshal(res.channel); err != nil { + return nil, err + } + + if args["invoker"], err = json.Marshal(res.invoker); err != nil { + return nil, err + } + + return +} + +// Decode return values from Corredor script into struct props +func (res *messageBase) Decode(results map[string][]byte) (err error) { + if r, ok := results["result"]; ok && len(results) == 1 { + if err = json.Unmarshal(r, res.message); err != nil { + return + } + } + + if r, ok := results["message"]; ok && len(results) == 1 { + if err = json.Unmarshal(r, res.message); err != nil { + return + } + } + + if r, ok := results["channel"]; ok && len(results) == 1 { + if err = json.Unmarshal(r, res.channel); err != nil { + return + } + } + + if r, ok := results["invoker"]; ok && len(results) == 1 { + if err = json.Unmarshal(r, res.invoker); err != nil { + return + } + } + return +} diff --git a/messaging/service/event/message.go b/messaging/service/event/message.go index d6ce9c4f6..e0ad33b8c 100644 --- a/messaging/service/event/message.go +++ b/messaging/service/event/message.go @@ -1,11 +1,9 @@ package event -import ( - "encoding/json" -) +import "github.com/cortezaproject/corteza-server/pkg/eventbus" // Match returns false if given conditions do not match event & resource internals -func (res messageBase) Match(name string, op string, values ...string) bool { +func (res messageBase) Match(c eventbus.ConstraintMatcher) bool { // By default we match no mather what kind of constraints we receive // // Function will be called multiple times - once for every trigger constraint @@ -15,54 +13,3 @@ func (res messageBase) Match(name string, op string, values ...string) bool { // When there are multiple values, Match() can decide how to treat them (OR, AND...) return true } - -// Encode internal data to be passed as event params & arguments to triggered Corredor script -func (res messageBase) Encode() (args map[string][]byte, err error) { - args = make(map[string][]byte) - - if args["message"], err = json.Marshal(res.message); err != nil { - return nil, err - } - - if args["oldMessage"], err = json.Marshal(res.oldMessage); err != nil { - return nil, err - } - - if args["channel"], err = json.Marshal(res.channel); err != nil { - return nil, err - } - - if args["invoker"], err = json.Marshal(res.invoker); err != nil { - return nil, err - } - - return -} - -// Decode return values from Corredor script into struct props -func (res *messageBase) Decode(results map[string][]byte) (err error) { - if r, ok := results["result"]; ok && len(results) == 1 { - if err = json.Unmarshal(r, res.message); err != nil { - return - } - } - - if r, ok := results["message"]; ok && len(results) == 1 { - if err = json.Unmarshal(r, res.message); err != nil { - return - } - } - - if r, ok := results["channel"]; ok && len(results) == 1 { - if err = json.Unmarshal(r, res.channel); err != nil { - return - } - } - - if r, ok := results["invoker"]; ok && len(results) == 1 { - if err = json.Unmarshal(r, res.invoker); err != nil { - return - } - } - return -} diff --git a/messaging/service/event/messaging.gen.go b/messaging/service/event/messaging.gen.go index 4e089bf95..7b3b359e2 100644 --- a/messaging/service/event/messaging.gen.go +++ b/messaging/service/event/messaging.gen.go @@ -10,6 +10,8 @@ package event // import ( + "encoding/json" + "github.com/cortezaproject/corteza-server/pkg/auth" ) @@ -111,3 +113,24 @@ func (res *messagingBase) SetInvoker(argInvoker auth.Identifiable) { func (res messagingBase) Invoker() auth.Identifiable { return res.invoker } + +// Encode internal data to be passed as event params & arguments to triggered Corredor script +func (res messagingBase) Encode() (args map[string][]byte, err error) { + args = make(map[string][]byte) + + if args["invoker"], err = json.Marshal(res.invoker); err != nil { + return nil, err + } + + return +} + +// Decode return values from Corredor script into struct props +func (res *messagingBase) Decode(results map[string][]byte) (err error) { + if r, ok := results["invoker"]; ok && len(results) == 1 { + if err = json.Unmarshal(r, res.invoker); err != nil { + return + } + } + return +} diff --git a/messaging/service/event/messaging.go b/messaging/service/event/messaging.go index 95d11bfbf..3120f81da 100644 --- a/messaging/service/event/messaging.go +++ b/messaging/service/event/messaging.go @@ -1,23 +1,22 @@ package event import ( - "encoding/json" - + "github.com/cortezaproject/corteza-server/pkg/eventbus" "github.com/cortezaproject/corteza-server/pkg/scheduler" ) // Match returns false if given conditions do not match event & resource internals -func (res messagingOnInterval) Match(_ string, _ string, values ...string) bool { - return scheduler.OnInterval(values...) +func (res messagingOnInterval) Match(c eventbus.ConstraintMatcher) bool { + return scheduler.OnInterval(c.Values()...) } // Match returns false if given conditions do not match event & resource internals -func (res messagingOnTimestamp) Match(_ string, _ string, values ...string) bool { - return scheduler.OnTimestamp(values...) +func (res messagingOnTimestamp) Match(c eventbus.ConstraintMatcher) bool { + return scheduler.OnTimestamp(c.Values()...) } // Match returns false if given conditions do not match event & resource internals -func (res messagingBase) Match(name string, op string, values ...string) bool { +func (res messagingBase) Match(c eventbus.ConstraintMatcher) bool { // By default we match no mather what kind of constraints we receive // // Function will be called multiple times - once for every trigger constraint @@ -27,24 +26,3 @@ func (res messagingBase) Match(name string, op string, values ...string) bool { // When there are multiple values, Match() can decide how to treat them (OR, AND...) return true } - -// Encode internal data to be passed as event params & arguments to triggered Corredor script -func (res messagingBase) Encode() (args map[string][]byte, err error) { - args = make(map[string][]byte) - - if args["invoker"], err = json.Marshal(res.invoker); err != nil { - return nil, err - } - - return -} - -// Decode return values from Corredor script into struct props -func (res *messagingBase) Decode(results map[string][]byte) (err error) { - if r, ok := results["invoker"]; ok && len(results) == 1 { - if err = json.Unmarshal(r, res.invoker); err != nil { - return - } - } - return -} diff --git a/pkg/corredor/util.go b/pkg/corredor/util.go index 9b70d09d4..702f23136 100644 --- a/pkg/corredor/util.go +++ b/pkg/corredor/util.go @@ -3,6 +3,7 @@ package corredor import ( "encoding/json" "fmt" + "github.com/pkg/errors" "github.com/cortezaproject/corteza-server/pkg/eventbus" "github.com/cortezaproject/corteza-server/pkg/slice" @@ -59,6 +60,7 @@ func makeTriggerOpts(t *Trigger) (oo []eventbus.TriggerRegOp, err error) { if len(t.Events) == 0 { return nil, fmt.Errorf("can not generate trigger without at least one events") } + if len(t.Resources) == 0 { return nil, fmt.Errorf("can not generate trigger without at least one resource") } @@ -66,12 +68,12 @@ func makeTriggerOpts(t *Trigger) (oo []eventbus.TriggerRegOp, err error) { oo = append(oo, eventbus.On(t.Events...)) oo = append(oo, eventbus.For(t.Resources...)) - for i := range t.Constraints { - oo = append(oo, eventbus.Constraint( - t.Constraints[i].Name, - t.Constraints[i].Op, - t.Constraints[i].Value..., - )) + for _, raw := range t.Constraints { + if c, err := eventbus.ConstraintMaker(raw.Name, raw.Op, raw.Value...); err != nil { + return nil, errors.Wrap(err, "can not generate trigger") + } else { + oo = append(oo, eventbus.Constraint(c)) + } } return diff --git a/pkg/eventbus/constraints.go b/pkg/eventbus/constraints.go new file mode 100644 index 000000000..903d62e76 --- /dev/null +++ b/pkg/eventbus/constraints.go @@ -0,0 +1,168 @@ +package eventbus + +import ( + "errors" + "path" + "regexp" + "strings" +) + +type ( + mustBeEqual struct { + not bool + name string + values []string + } + + mustBeLike struct { + not bool + name string + values []string + } + + mustMatch struct { + not bool + name string + values []*regexp.Regexp + } + + ConstraintMatcher interface { + Name() string + Values() []string + Match(value string) bool + } + + constraintSet []ConstraintMatcher +) + +var ErrUnsupportedOp = errors.New("operator not supported") +var ErrUnsupportedName = errors.New("constraint name not supported") + +func (c mustBeEqual) Name() string { return c.name } +func (c mustBeLike) Name() string { return c.name } +func (c mustMatch) Name() string { return c.name } +func (c mustBeEqual) Values() []string { return c.values } +func (c mustBeLike) Values() []string { return c.values } +func (c mustMatch) Values() []string { return nil } + +// Converts raw ConstraintMatcher into one of the +// ConstraintMatcher handlers +func ConstraintMaker(name, op string, vv ...string) (ConstraintMatcher, error) { + switch strings.ToLower(op) { + case "", "eq", "=", "==", "===": + return MustBeEqual(name, vv...) + case "not eq", "ne", "!=", "!==": + return MustNotBeEqual(name, vv...) + case "like": + return MustBeLike(name, vv...) + case "not like": + return MustNotBeLike(name, vv...) + case "~": + return MustMatch(name, vv...) + case "!~": + return MustNotMatch(name, vv...) + default: + return nil, ErrUnsupportedOp + } +} + +func MustMakeConstraint(name, op string, vv ...string) ConstraintMatcher { + c, err := ConstraintMaker(name, op, vv...) + if err != nil { + panic(err) + } + + return c +} + +func MustBeEqual(name string, vv ...string) (ConstraintMatcher, error) { + return &mustBeEqual{name: name, values: vv}, nil +} + +func MustNotBeEqual(name string, vv ...string) (ConstraintMatcher, error) { + return &mustBeEqual{name: name, values: vv, not: true}, nil +} + +func (c mustBeEqual) Match(value string) bool { + for _, v := range c.values { + if value == v { + return !c.not + } + } + + return c.not +} + +func mustLikeMaker(name string, not bool, vv ...string) (*mustBeLike, error) { + var ( + m = &mustBeLike{ + name: name, + values: make([]string, len(vv)), + not: not, + } + ) + + for i, v := range vv { + v = strings.ReplaceAll(v, "%", "*") + v = strings.ReplaceAll(v, "_", "?") + m.values[i] = v + } + + return m, nil +} + +func MustBeLike(name string, vv ...string) (ConstraintMatcher, error) { + return &mustBeLike{name: name, values: vv}, nil +} + +func MustNotBeLike(name string, vv ...string) (ConstraintMatcher, error) { + return &mustBeLike{name: name, values: vv, not: true}, nil +} + +func (c mustBeLike) Match(value string) bool { + for _, v := range c.values { + if m, _ := path.Match(v, value); m { + return !c.not + } + } + + return c.not +} + +func mustMatchMaker(name string, not bool, vv ...string) (*mustMatch, error) { + var ( + m = &mustMatch{ + name: name, + values: make([]*regexp.Regexp, len(vv)), + not: not, + } + err error + ) + + for i, v := range vv { + if m.values[i], err = regexp.Compile(v); err != nil { + return nil, err + } + } + + return m, nil +} + +func MustMatch(name string, vv ...string) (ConstraintMatcher, error) { + return mustMatchMaker(name, false, vv...) +} + +func MustNotMatch(name string, vv ...string) (ConstraintMatcher, error) { + return mustMatchMaker(name, true, vv...) + +} + +func (c mustMatch) Match(value string) bool { + for _, v := range c.values { + if v.MatchString(value) { + return !c.not + } + } + + return c.not +} diff --git a/pkg/eventbus/constraints_test.go b/pkg/eventbus/constraints_test.go new file mode 100644 index 000000000..d51354eac --- /dev/null +++ b/pkg/eventbus/constraints_test.go @@ -0,0 +1,98 @@ +package eventbus + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestConstraintMaking(t *testing.T) { + var ( + a = assert.New(t) + c ConstraintMatcher + err error + ) + + c, err = ConstraintMaker("", "foo") + a.Nil(c) + a.Error(err) + + c, err = ConstraintMaker("", "eq") + a.NoError(err) + a.NotNil(c) + a.Implements((*ConstraintMatcher)(nil), c) + + a.NotNil(MustMakeConstraint("foo", "=", "bar")) +} + +func TestMustBeEqual(t *testing.T) { + var ( + a = assert.New(t) + c, err = MustBeEqual("", "foo") + ) + + a.NoError(err) + a.NotNil(c) + a.True(c.Match("foo")) + a.False(c.Match("bar")) +} + +func TestMustNotBeEqual(t *testing.T) { + var ( + a = assert.New(t) + c, err = MustNotBeEqual("", "foo") + ) + + a.NoError(err) + a.NotNil(c) + a.False(c.Match("foo")) + a.True(c.Match("bar")) +} + +func TestMustBeLike(t *testing.T) { + var ( + a = assert.New(t) + c, err = MustBeLike("", "foo*") + ) + + a.NoError(err) + a.NotNil(c) + a.True(c.Match("fooBAZ")) + a.False(c.Match("barBAZ")) +} + +func TestMustNotBeLike(t *testing.T) { + var ( + a = assert.New(t) + c, err = MustNotBeLike("", "foo*") + ) + + a.NoError(err) + a.NotNil(c) + a.False(c.Match("fooBAZ")) + a.True(c.Match("barBAZ")) +} + +func TestMustMatch(t *testing.T) { + var ( + a = assert.New(t) + c, err = MustMatch("", "foo.+") + ) + + a.NoError(err) + a.NotNil(c) + a.True(c.Match("fooBAZ")) + a.False(c.Match("barBAZ")) +} + +func TestMustNotMatch(t *testing.T) { + var ( + a = assert.New(t) + c, err = MustNotMatch("", "foo.*") + ) + + a.NoError(err) + a.NotNil(c) + a.False(c.Match("fooBAZ")) + a.True(c.Match("barBAZ")) +} diff --git a/pkg/eventbus/eventbus.go b/pkg/eventbus/eventbus.go index b8f32de0c..9d9c6fe6e 100644 --- a/pkg/eventbus/eventbus.go +++ b/pkg/eventbus/eventbus.go @@ -17,7 +17,7 @@ type ( // Match tests if given constraints match // event's internal values - Match(name string, op string, values ...string) bool + Match(ConstraintMatcher) bool } eventbus struct { diff --git a/pkg/eventbus/trigger_test.go b/pkg/eventbus/trigger_test.go index 7aa6fd5a5..07f8c0128 100644 --- a/pkg/eventbus/trigger_test.go +++ b/pkg/eventbus/trigger_test.go @@ -15,7 +15,7 @@ type ( mockEvent struct { rType string eType string - match func(name string, op string, values ...string) bool + match func(c ConstraintMatcher) bool identity auth.Identifiable } @@ -29,12 +29,12 @@ func (e mockEvent) EventType() string { return e.eType } -func (e mockEvent) Match(name string, op string, values ...string) bool { +func (e mockEvent) Match(c ConstraintMatcher) bool { if e.match == nil { return true } - return e.match(name, op, values...) + return e.match(c) } func (e *mockEvent) SetInvoker(identity auth.Identifiable) { @@ -68,22 +68,22 @@ func TestTrigger_Match(t *testing.T) { &mockEvent{rType: "foo", eType: "bar"}, true, }, - {"constraint match", - []TriggerRegOp{For("foo"), On("bar"), Constraint("baz", "", "baz")}, + {"ConstraintMatcher match", + []TriggerRegOp{For("foo"), On("bar"), Constraint(MustMakeConstraint("baz", "=", "baz"))}, &mockEvent{ rType: "foo", eType: "bar", - match: func(name string, op string, values ...string) bool { - return len(values) > 0 && name == values[0] + match: func(c ConstraintMatcher) bool { + return len(c.Values()) > 0 && c.Name() == c.Values()[0] }}, true, }, - {"constraint mismatch", - []TriggerRegOp{For("foo"), On("bar"), Constraint("baz", "", "baz")}, + {"ConstraintMatcher mismatch", + []TriggerRegOp{For("foo"), On("bar"), Constraint(MustMakeConstraint("baz", "=", "baz"))}, &mockEvent{ rType: "foo", eType: "bar", - match: func(name string, op string, values ...string) bool { + match: func(c ConstraintMatcher) bool { return false }}, false, diff --git a/pkg/eventbus/triggers.go b/pkg/eventbus/triggers.go index 1579b1423..634500119 100644 --- a/pkg/eventbus/triggers.go +++ b/pkg/eventbus/triggers.go @@ -8,14 +8,6 @@ import ( ) type ( - constraint struct { - name string - op string - value []string - } - - constraintSet []constraint - Handler func(ctx context.Context, ev Event) error trigger struct { @@ -56,7 +48,7 @@ func (t trigger) Match(re Event) bool { for _, c := range t.constraints { // Should match all constraints - if !re.Match(c.name, c.op, c.value...) { + if !re.Match(c) { return false } } @@ -104,13 +96,9 @@ func On(ee ...string) TriggerRegOp { } } -func Constraint(name, op string, vv ...string) TriggerRegOp { +func Constraint(c ConstraintMatcher) TriggerRegOp { return func(t *trigger) { - t.constraints = append(t.constraints, constraint{ - name: name, - op: op, - value: vv, - }) + t.constraints = append(t.constraints, c) } } diff --git a/system/service/event/application.gen.go b/system/service/event/application.gen.go index f341c816b..12a540850 100644 --- a/system/service/event/application.gen.go +++ b/system/service/event/application.gen.go @@ -10,6 +10,8 @@ package event // import ( + "encoding/json" + "github.com/cortezaproject/corteza-server/system/types" "github.com/cortezaproject/corteza-server/pkg/auth" @@ -270,3 +272,44 @@ func (res *applicationBase) SetInvoker(argInvoker auth.Identifiable) { func (res applicationBase) Invoker() auth.Identifiable { return res.invoker } + +// Encode internal data to be passed as event params & arguments to triggered Corredor script +func (res applicationBase) Encode() (args map[string][]byte, err error) { + args = make(map[string][]byte) + + if args["application"], err = json.Marshal(res.application); err != nil { + return nil, err + } + + if args["oldApplication"], err = json.Marshal(res.oldApplication); err != nil { + return nil, err + } + + if args["invoker"], err = json.Marshal(res.invoker); err != nil { + return nil, err + } + + return +} + +// Decode return values from Corredor script into struct props +func (res *applicationBase) Decode(results map[string][]byte) (err error) { + if r, ok := results["result"]; ok && len(results) == 1 { + if err = json.Unmarshal(r, res.application); err != nil { + return + } + } + + if r, ok := results["application"]; ok && len(results) == 1 { + if err = json.Unmarshal(r, res.application); err != nil { + return + } + } + + if r, ok := results["invoker"]; ok && len(results) == 1 { + if err = json.Unmarshal(r, res.invoker); err != nil { + return + } + } + return +} diff --git a/system/service/event/application.go b/system/service/event/application.go index b7f48a0db..f4a17ca02 100644 --- a/system/service/event/application.go +++ b/system/service/event/application.go @@ -1,11 +1,9 @@ package event -import ( - "encoding/json" -) +import "github.com/cortezaproject/corteza-server/pkg/eventbus" // Match returns false if given conditions do not match event & resource internals -func (res applicationBase) Match(name string, op string, values ...string) bool { +func (res applicationBase) Match(c eventbus.ConstraintMatcher) bool { // By default we match no mather what kind of constraints we receive // // Function will be called multiple times - once for every trigger constraint @@ -15,44 +13,3 @@ func (res applicationBase) Match(name string, op string, values ...string) bool // When there are multiple values, Match() can decide how to treat them (OR, AND...) return true } - -// Encode internal data to be passed as event params & arguments to triggered Corredor script -func (res applicationBase) Encode() (args map[string][]byte, err error) { - args = make(map[string][]byte) - - if args["application"], err = json.Marshal(res.application); err != nil { - return nil, err - } - - if args["oldApplication"], err = json.Marshal(res.oldApplication); err != nil { - return nil, err - } - - if args["invoker"], err = json.Marshal(res.invoker); err != nil { - return nil, err - } - - return -} - -// Decode return values from Corredor script into struct props -func (res *applicationBase) Decode(results map[string][]byte) (err error) { - if r, ok := results["result"]; ok && len(results) == 1 { - if err = json.Unmarshal(r, res.application); err != nil { - return - } - } - - if r, ok := results["application"]; ok && len(results) == 1 { - if err = json.Unmarshal(r, res.application); err != nil { - return - } - } - - if r, ok := results["invoker"]; ok && len(results) == 1 { - if err = json.Unmarshal(r, res.invoker); err != nil { - return - } - } - return -} diff --git a/system/service/event/auth.gen.go b/system/service/event/auth.gen.go index 1f6186038..6d0b261d1 100644 --- a/system/service/event/auth.gen.go +++ b/system/service/event/auth.gen.go @@ -10,6 +10,8 @@ package event // import ( + "encoding/json" + "github.com/cortezaproject/corteza-server/system/types" "github.com/cortezaproject/corteza-server/pkg/auth" @@ -190,3 +192,50 @@ func (res *authBase) SetInvoker(argInvoker auth.Identifiable) { func (res authBase) Invoker() auth.Identifiable { return res.invoker } + +// Encode internal data to be passed as event params & arguments to triggered Corredor script +func (res authBase) Encode() (args map[string][]byte, err error) { + args = make(map[string][]byte) + + if args["user"], err = json.Marshal(res.user); err != nil { + return nil, err + } + + if args["provider"], err = json.Marshal(res.provider); err != nil { + return nil, err + } + + if args["invoker"], err = json.Marshal(res.invoker); err != nil { + return nil, err + } + + return +} + +// Decode return values from Corredor script into struct props +func (res *authBase) Decode(results map[string][]byte) (err error) { + if r, ok := results["result"]; ok && len(results) == 1 { + if err = json.Unmarshal(r, res.user); err != nil { + return + } + } + + if r, ok := results["user"]; ok && len(results) == 1 { + if err = json.Unmarshal(r, res.user); err != nil { + return + } + } + + if r, ok := results["provider"]; ok && len(results) == 1 { + if err = json.Unmarshal(r, res.provider); err != nil { + return + } + } + + if r, ok := results["invoker"]; ok && len(results) == 1 { + if err = json.Unmarshal(r, res.invoker); err != nil { + return + } + } + return +} diff --git a/system/service/event/auth.go b/system/service/event/auth.go index b52c83d31..0ee6bf216 100644 --- a/system/service/event/auth.go +++ b/system/service/event/auth.go @@ -1,11 +1,9 @@ package event -import ( - "encoding/json" -) +import "github.com/cortezaproject/corteza-server/pkg/eventbus" // Match returns false if given conditions do not match event & resource internals -func (res authBase) Match(name string, op string, values ...string) bool { +func (res authBase) Match(c eventbus.ConstraintMatcher) bool { // By default we match no mather what kind of constraints we receive // // Function will be called multiple times - once for every trigger constraint @@ -15,50 +13,3 @@ func (res authBase) Match(name string, op string, values ...string) bool { // When there are multiple values, Match() can decide how to treat them (OR, AND...) return true } - -// Encode internal data to be passed as event params & arguments to triggered Corredor script -func (res authBase) Encode() (args map[string][]byte, err error) { - args = make(map[string][]byte) - - if args["user"], err = json.Marshal(res.user); err != nil { - return nil, err - } - - if args["provider"], err = json.Marshal(res.provider); err != nil { - return nil, err - } - - if args["invoker"], err = json.Marshal(res.invoker); err != nil { - return nil, err - } - - return -} - -// Decode return values from Corredor script into struct props -func (res *authBase) Decode(results map[string][]byte) (err error) { - if r, ok := results["result"]; ok && len(results) == 1 { - if err = json.Unmarshal(r, res.user); err != nil { - return - } - } - - if r, ok := results["user"]; ok && len(results) == 1 { - if err = json.Unmarshal(r, res.user); err != nil { - return - } - } - - if r, ok := results["provider"]; ok && len(results) == 1 { - if err = json.Unmarshal(r, res.provider); err != nil { - return - } - } - - if r, ok := results["invoker"]; ok && len(results) == 1 { - if err = json.Unmarshal(r, res.invoker); err != nil { - return - } - } - return -} diff --git a/system/service/event/mail.gen.go b/system/service/event/mail.gen.go index cca479be1..0f8241e11 100644 --- a/system/service/event/mail.gen.go +++ b/system/service/event/mail.gen.go @@ -10,6 +10,8 @@ package event // import ( + "encoding/json" + "github.com/cortezaproject/corteza-server/system/types" "github.com/cortezaproject/corteza-server/pkg/auth" @@ -140,3 +142,40 @@ func (res *mailBase) SetInvoker(argInvoker auth.Identifiable) { func (res mailBase) Invoker() auth.Identifiable { return res.invoker } + +// Encode internal data to be passed as event params & arguments to triggered Corredor script +func (res mailBase) Encode() (args map[string][]byte, err error) { + args = make(map[string][]byte) + + if args["request"], err = json.Marshal(res.request); err != nil { + return nil, err + } + + if args["invoker"], err = json.Marshal(res.invoker); err != nil { + return nil, err + } + + return +} + +// Decode return values from Corredor script into struct props +func (res *mailBase) Decode(results map[string][]byte) (err error) { + if r, ok := results["result"]; ok && len(results) == 1 { + if err = json.Unmarshal(r, res.request); err != nil { + return + } + } + + if r, ok := results["request"]; ok && len(results) == 1 { + if err = json.Unmarshal(r, res.request); err != nil { + return + } + } + + if r, ok := results["invoker"]; ok && len(results) == 1 { + if err = json.Unmarshal(r, res.invoker); err != nil { + return + } + } + return +} diff --git a/system/service/event/mail.go b/system/service/event/mail.go index 5abc38286..dc567afdc 100644 --- a/system/service/event/mail.go +++ b/system/service/event/mail.go @@ -1,11 +1,9 @@ package event -import ( - "encoding/json" -) +import "github.com/cortezaproject/corteza-server/pkg/eventbus" // Match returns false if given conditions do not match event & resource internals -func (res mailBase) Match(name string, op string, values ...string) bool { +func (res mailBase) Match(c eventbus.ConstraintMatcher) bool { // By default we match no mather what kind of constraints we receive // // Function will be called multiple times - once for every trigger constraint @@ -15,40 +13,3 @@ func (res mailBase) Match(name string, op string, values ...string) bool { // When there are multiple values, Match() can decide how to treat them (OR, AND...) return true } - -// Encode internal data to be passed as event params & arguments to triggered Corredor script -func (res mailBase) Encode() (args map[string][]byte, err error) { - args = make(map[string][]byte) - - if args["request"], err = json.Marshal(res.request); err != nil { - return nil, err - } - - if args["invoker"], err = json.Marshal(res.invoker); err != nil { - return nil, err - } - - return -} - -// Decode return values from Corredor script into struct props -func (res *mailBase) Decode(results map[string][]byte) (err error) { - if r, ok := results["result"]; ok && len(results) == 1 { - if err = json.Unmarshal(r, res.request); err != nil { - return - } - } - - if r, ok := results["request"]; ok && len(results) == 1 { - if err = json.Unmarshal(r, res.request); err != nil { - return - } - } - - if r, ok := results["invoker"]; ok && len(results) == 1 { - if err = json.Unmarshal(r, res.invoker); err != nil { - return - } - } - return -} diff --git a/system/service/event/role.gen.go b/system/service/event/role.gen.go index 464d59a5c..6ca558db7 100644 --- a/system/service/event/role.gen.go +++ b/system/service/event/role.gen.go @@ -10,6 +10,8 @@ package event // import ( + "encoding/json" + "github.com/cortezaproject/corteza-server/system/types" "github.com/cortezaproject/corteza-server/pkg/auth" @@ -270,3 +272,44 @@ func (res *roleBase) SetInvoker(argInvoker auth.Identifiable) { func (res roleBase) Invoker() auth.Identifiable { return res.invoker } + +// Encode internal data to be passed as event params & arguments to triggered Corredor script +func (res roleBase) Encode() (args map[string][]byte, err error) { + args = make(map[string][]byte) + + if args["role"], err = json.Marshal(res.role); err != nil { + return nil, err + } + + if args["oldRole"], err = json.Marshal(res.oldRole); err != nil { + return nil, err + } + + if args["invoker"], err = json.Marshal(res.invoker); err != nil { + return nil, err + } + + return +} + +// Decode return values from Corredor script into struct props +func (res *roleBase) Decode(results map[string][]byte) (err error) { + if r, ok := results["result"]; ok && len(results) == 1 { + if err = json.Unmarshal(r, res.role); err != nil { + return + } + } + + if r, ok := results["role"]; ok && len(results) == 1 { + if err = json.Unmarshal(r, res.role); err != nil { + return + } + } + + if r, ok := results["invoker"]; ok && len(results) == 1 { + if err = json.Unmarshal(r, res.invoker); err != nil { + return + } + } + return +} diff --git a/system/service/event/role.go b/system/service/event/role.go index 09ab46b4b..37f850b2e 100644 --- a/system/service/event/role.go +++ b/system/service/event/role.go @@ -1,11 +1,9 @@ package event -import ( - "encoding/json" -) +import "github.com/cortezaproject/corteza-server/pkg/eventbus" // Match returns false if given conditions do not match event & resource internals -func (res roleBase) Match(name string, op string, values ...string) bool { +func (res roleBase) Match(c eventbus.ConstraintMatcher) bool { // By default we match no mather what kind of constraints we receive // // Function will be called multiple times - once for every trigger constraint @@ -15,44 +13,3 @@ func (res roleBase) Match(name string, op string, values ...string) bool { // When there are multiple values, Match() can decide how to treat them (OR, AND...) return true } - -// Encode internal data to be passed as event params & arguments to triggered Corredor script -func (res roleBase) Encode() (args map[string][]byte, err error) { - args = make(map[string][]byte) - - if args["role"], err = json.Marshal(res.role); err != nil { - return nil, err - } - - if args["oldRole"], err = json.Marshal(res.oldRole); err != nil { - return nil, err - } - - if args["invoker"], err = json.Marshal(res.invoker); err != nil { - return nil, err - } - - return -} - -// Decode return values from Corredor script into struct props -func (res *roleBase) Decode(results map[string][]byte) (err error) { - if r, ok := results["result"]; ok && len(results) == 1 { - if err = json.Unmarshal(r, res.role); err != nil { - return - } - } - - if r, ok := results["role"]; ok && len(results) == 1 { - if err = json.Unmarshal(r, res.role); err != nil { - return - } - } - - if r, ok := results["invoker"]; ok && len(results) == 1 { - if err = json.Unmarshal(r, res.invoker); err != nil { - return - } - } - return -} diff --git a/system/service/event/role_member.gen.go b/system/service/event/role_member.gen.go index 900cc922d..c67835216 100644 --- a/system/service/event/role_member.gen.go +++ b/system/service/event/role_member.gen.go @@ -10,6 +10,8 @@ package event // import ( + "encoding/json" + "github.com/cortezaproject/corteza-server/system/types" "github.com/cortezaproject/corteza-server/pkg/auth" @@ -190,3 +192,50 @@ func (res *roleMemberBase) SetInvoker(argInvoker auth.Identifiable) { func (res roleMemberBase) Invoker() auth.Identifiable { return res.invoker } + +// Encode internal data to be passed as event params & arguments to triggered Corredor script +func (res roleMemberBase) Encode() (args map[string][]byte, err error) { + args = make(map[string][]byte) + + if args["user"], err = json.Marshal(res.user); err != nil { + return nil, err + } + + if args["role"], err = json.Marshal(res.role); err != nil { + return nil, err + } + + if args["invoker"], err = json.Marshal(res.invoker); err != nil { + return nil, err + } + + return +} + +// Decode return values from Corredor script into struct props +func (res *roleMemberBase) Decode(results map[string][]byte) (err error) { + if r, ok := results["result"]; ok && len(results) == 1 { + if err = json.Unmarshal(r, res.user); err != nil { + return + } + } + + if r, ok := results["user"]; ok && len(results) == 1 { + if err = json.Unmarshal(r, res.user); err != nil { + return + } + } + + if r, ok := results["role"]; ok && len(results) == 1 { + if err = json.Unmarshal(r, res.role); err != nil { + return + } + } + + if r, ok := results["invoker"]; ok && len(results) == 1 { + if err = json.Unmarshal(r, res.invoker); err != nil { + return + } + } + return +} diff --git a/system/service/event/role_member.go b/system/service/event/role_member.go index db0e0a60f..220cd894d 100644 --- a/system/service/event/role_member.go +++ b/system/service/event/role_member.go @@ -1,11 +1,9 @@ package event -import ( - "encoding/json" -) +import "github.com/cortezaproject/corteza-server/pkg/eventbus" // Match returns false if given conditions do not match event & resource internals -func (res roleMemberBase) Match(name string, op string, values ...string) bool { +func (res roleMemberBase) Match(c eventbus.ConstraintMatcher) bool { // By default we match no mather what kind of constraints we receive // // Function will be called multiple times - once for every trigger constraint @@ -15,50 +13,3 @@ func (res roleMemberBase) Match(name string, op string, values ...string) bool { // When there are multiple values, Match() can decide how to treat them (OR, AND...) return true } - -// Encode internal data to be passed as event params & arguments to triggered Corredor script -func (res roleMemberBase) Encode() (args map[string][]byte, err error) { - args = make(map[string][]byte) - - if args["user"], err = json.Marshal(res.user); err != nil { - return nil, err - } - - if args["role"], err = json.Marshal(res.role); err != nil { - return nil, err - } - - if args["invoker"], err = json.Marshal(res.invoker); err != nil { - return nil, err - } - - return -} - -// Decode return values from Corredor script into struct props -func (res *roleMemberBase) Decode(results map[string][]byte) (err error) { - if r, ok := results["result"]; ok && len(results) == 1 { - if err = json.Unmarshal(r, res.user); err != nil { - return - } - } - - if r, ok := results["user"]; ok && len(results) == 1 { - if err = json.Unmarshal(r, res.user); err != nil { - return - } - } - - if r, ok := results["role"]; ok && len(results) == 1 { - if err = json.Unmarshal(r, res.role); err != nil { - return - } - } - - if r, ok := results["invoker"]; ok && len(results) == 1 { - if err = json.Unmarshal(r, res.invoker); err != nil { - return - } - } - return -} diff --git a/system/service/event/sink.gen.go b/system/service/event/sink.gen.go index 05335cccf..d161e5513 100644 --- a/system/service/event/sink.gen.go +++ b/system/service/event/sink.gen.go @@ -10,6 +10,8 @@ package event // import ( + "encoding/json" + "github.com/cortezaproject/corteza-server/system/types" "github.com/cortezaproject/corteza-server/pkg/auth" @@ -86,3 +88,40 @@ func (res *sinkBase) SetInvoker(argInvoker auth.Identifiable) { func (res sinkBase) Invoker() auth.Identifiable { return res.invoker } + +// Encode internal data to be passed as event params & arguments to triggered Corredor script +func (res sinkBase) Encode() (args map[string][]byte, err error) { + args = make(map[string][]byte) + + if args["request"], err = json.Marshal(res.request); err != nil { + return nil, err + } + + if args["invoker"], err = json.Marshal(res.invoker); err != nil { + return nil, err + } + + return +} + +// Decode return values from Corredor script into struct props +func (res *sinkBase) Decode(results map[string][]byte) (err error) { + if r, ok := results["result"]; ok && len(results) == 1 { + if err = json.Unmarshal(r, res.request); err != nil { + return + } + } + + if r, ok := results["request"]; ok && len(results) == 1 { + if err = json.Unmarshal(r, res.request); err != nil { + return + } + } + + if r, ok := results["invoker"]; ok && len(results) == 1 { + if err = json.Unmarshal(r, res.invoker); err != nil { + return + } + } + return +} diff --git a/system/service/event/sink.go b/system/service/event/sink.go index 9a1a3ae5d..dd6723d48 100644 --- a/system/service/event/sink.go +++ b/system/service/event/sink.go @@ -1,11 +1,9 @@ package event -import ( - "encoding/json" -) +import "github.com/cortezaproject/corteza-server/pkg/eventbus" // Match returns false if given conditions do not match event & resource internals -func (res sinkBase) Match(name string, op string, values ...string) bool { +func (res sinkBase) Match(c eventbus.ConstraintMatcher) bool { // By default we match no mather what kind of constraints we receive // // Function will be called multiple times - once for every trigger constraint @@ -15,40 +13,3 @@ func (res sinkBase) Match(name string, op string, values ...string) bool { // When there are multiple values, Match() can decide how to treat them (OR, AND...) return true } - -// Encode internal data to be passed as event params & arguments to triggered Corredor script -func (res sinkBase) Encode() (args map[string][]byte, err error) { - args = make(map[string][]byte) - - if args["request"], err = json.Marshal(res.request); err != nil { - return nil, err - } - - if args["invoker"], err = json.Marshal(res.invoker); err != nil { - return nil, err - } - - return -} - -// Decode return values from Corredor script into struct props -func (res *sinkBase) Decode(results map[string][]byte) (err error) { - if r, ok := results["result"]; ok && len(results) == 1 { - if err = json.Unmarshal(r, res.request); err != nil { - return - } - } - - if r, ok := results["request"]; ok && len(results) == 1 { - if err = json.Unmarshal(r, res.request); err != nil { - return - } - } - - if r, ok := results["invoker"]; ok && len(results) == 1 { - if err = json.Unmarshal(r, res.invoker); err != nil { - return - } - } - return -} diff --git a/system/service/event/system.gen.go b/system/service/event/system.gen.go index 6d9fbdb5b..12dd602ea 100644 --- a/system/service/event/system.gen.go +++ b/system/service/event/system.gen.go @@ -10,6 +10,8 @@ package event // import ( + "encoding/json" + "github.com/cortezaproject/corteza-server/pkg/auth" ) @@ -111,3 +113,24 @@ func (res *systemBase) SetInvoker(argInvoker auth.Identifiable) { func (res systemBase) Invoker() auth.Identifiable { return res.invoker } + +// Encode internal data to be passed as event params & arguments to triggered Corredor script +func (res systemBase) Encode() (args map[string][]byte, err error) { + args = make(map[string][]byte) + + if args["invoker"], err = json.Marshal(res.invoker); err != nil { + return nil, err + } + + return +} + +// Decode return values from Corredor script into struct props +func (res *systemBase) Decode(results map[string][]byte) (err error) { + if r, ok := results["invoker"]; ok && len(results) == 1 { + if err = json.Unmarshal(r, res.invoker); err != nil { + return + } + } + return +} diff --git a/system/service/event/system.go b/system/service/event/system.go index 44f0a742f..646169a65 100644 --- a/system/service/event/system.go +++ b/system/service/event/system.go @@ -1,23 +1,22 @@ package event import ( - "encoding/json" - + "github.com/cortezaproject/corteza-server/pkg/eventbus" "github.com/cortezaproject/corteza-server/pkg/scheduler" ) // Match returns false if given conditions do not match event & resource internals -func (res systemOnInterval) Match(_ string, _ string, values ...string) bool { - return scheduler.OnInterval(values...) +func (res systemOnInterval) Match(c eventbus.ConstraintMatcher) bool { + return scheduler.OnInterval(c.Values()...) } // Match returns false if given conditions do not match event & resource internals -func (res systemOnTimestamp) Match(_ string, _ string, values ...string) bool { - return scheduler.OnTimestamp(values...) +func (res systemOnTimestamp) Match(c eventbus.ConstraintMatcher) bool { + return scheduler.OnTimestamp(c.Values()...) } // Match returns false if given conditions do not match event & resource internals -func (res systemBase) Match(name string, op string, values ...string) bool { +func (res systemBase) Match(c eventbus.ConstraintMatcher) bool { // By default we match no mather what kind of constraints we receive // // Function will be called multiple times - once for every trigger constraint @@ -27,24 +26,3 @@ func (res systemBase) Match(name string, op string, values ...string) bool { // When there are multiple values, Match() can decide how to treat them (OR, AND...) return true } - -// Encode internal data to be passed as event params & arguments to triggered Corredor script -func (res systemBase) Encode() (args map[string][]byte, err error) { - args = make(map[string][]byte) - - if args["invoker"], err = json.Marshal(res.invoker); err != nil { - return nil, err - } - - return -} - -// Decode return values from Corredor script into struct props -func (res *systemBase) Decode(results map[string][]byte) (err error) { - if r, ok := results["invoker"]; ok && len(results) == 1 { - if err = json.Unmarshal(r, res.invoker); err != nil { - return - } - } - return -} diff --git a/system/service/event/user.gen.go b/system/service/event/user.gen.go index 10cd3eec3..1f6c11c26 100644 --- a/system/service/event/user.gen.go +++ b/system/service/event/user.gen.go @@ -10,6 +10,8 @@ package event // import ( + "encoding/json" + "github.com/cortezaproject/corteza-server/system/types" "github.com/cortezaproject/corteza-server/pkg/auth" @@ -270,3 +272,44 @@ func (res *userBase) SetInvoker(argInvoker auth.Identifiable) { func (res userBase) Invoker() auth.Identifiable { return res.invoker } + +// Encode internal data to be passed as event params & arguments to triggered Corredor script +func (res userBase) Encode() (args map[string][]byte, err error) { + args = make(map[string][]byte) + + if args["user"], err = json.Marshal(res.user); err != nil { + return nil, err + } + + if args["oldUser"], err = json.Marshal(res.oldUser); err != nil { + return nil, err + } + + if args["invoker"], err = json.Marshal(res.invoker); err != nil { + return nil, err + } + + return +} + +// Decode return values from Corredor script into struct props +func (res *userBase) Decode(results map[string][]byte) (err error) { + if r, ok := results["result"]; ok && len(results) == 1 { + if err = json.Unmarshal(r, res.user); err != nil { + return + } + } + + if r, ok := results["user"]; ok && len(results) == 1 { + if err = json.Unmarshal(r, res.user); err != nil { + return + } + } + + if r, ok := results["invoker"]; ok && len(results) == 1 { + if err = json.Unmarshal(r, res.invoker); err != nil { + return + } + } + return +} diff --git a/system/service/event/user.go b/system/service/event/user.go index 5903ef831..ba52342a7 100644 --- a/system/service/event/user.go +++ b/system/service/event/user.go @@ -1,11 +1,9 @@ package event -import ( - "encoding/json" -) +import "github.com/cortezaproject/corteza-server/pkg/eventbus" // Match returns false if given conditions do not match event & resource internals -func (res userBase) Match(name string, op string, values ...string) bool { +func (res userBase) Match(c eventbus.ConstraintMatcher) bool { // By default we match no mather what kind of constraints we receive // // Function will be called multiple times - once for every trigger constraint @@ -15,44 +13,3 @@ func (res userBase) Match(name string, op string, values ...string) bool { // When there are multiple values, Match() can decide how to treat them (OR, AND...) return true } - -// Encode internal data to be passed as event params & arguments to triggered Corredor script -func (res userBase) Encode() (args map[string][]byte, err error) { - args = make(map[string][]byte) - - if args["user"], err = json.Marshal(res.user); err != nil { - return nil, err - } - - if args["oldUser"], err = json.Marshal(res.oldUser); err != nil { - return nil, err - } - - if args["invoker"], err = json.Marshal(res.invoker); err != nil { - return nil, err - } - - return -} - -// Decode return values from Corredor script into struct props -func (res *userBase) Decode(results map[string][]byte) (err error) { - if r, ok := results["result"]; ok && len(results) == 1 { - if err = json.Unmarshal(r, res.user); err != nil { - return - } - } - - if r, ok := results["user"]; ok && len(results) == 1 { - if err = json.Unmarshal(r, res.user); err != nil { - return - } - } - - if r, ok := results["invoker"]; ok && len(results) == 1 { - if err = json.Unmarshal(r, res.invoker); err != nil { - return - } - } - return -}