Improve eventbus & constraints handling

Constraints are now preparsed into active structs
that can be used for matching
This commit is contained in:
Denis Arh
2020-01-18 07:05:35 +01:00
parent ded83ff5db
commit abf4d106bb
45 changed files with 1136 additions and 867 deletions
+44 -1
View File
@@ -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
}
+2 -45
View File
@@ -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
}
+23
View File
@@ -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
}
+7 -28
View File
@@ -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
}
+47
View File
@@ -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
}
+2 -49
View File
@@ -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
}
+43
View File
@@ -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
}
+2 -45
View File
@@ -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
}
+47
View File
@@ -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
}
+2 -49
View File
@@ -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
}
+51
View File
@@ -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
}
+2 -53
View File
@@ -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
}
+43
View File
@@ -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
}
+2 -45
View File
@@ -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
}
@@ -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
}
+2 -51
View File
@@ -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
}
+37
View File
@@ -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
}
+2 -39
View File
@@ -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
}
+9
View File
@@ -0,0 +1,9 @@
package event
type (
constraint interface {
Name() string
Values() []string
Match(value string) bool
}
)
+53
View File
@@ -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
}
+2 -55
View File
@@ -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
}
+23
View File
@@ -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
}
+6 -28
View File
@@ -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
}
+8 -6
View File
@@ -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
+168
View File
@@ -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
}
+98
View File
@@ -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"))
}
+1 -1
View File
@@ -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 {
+10 -10
View File
@@ -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,
+3 -15
View File
@@ -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)
}
}
+43
View File
@@ -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
}
+2 -45
View File
@@ -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
}
+49
View File
@@ -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
}
+2 -51
View File
@@ -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
}
+39
View File
@@ -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
}
+2 -41
View File
@@ -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
}
+43
View File
@@ -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
}
+2 -45
View File
@@ -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
}
+49
View File
@@ -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
}
+2 -51
View File
@@ -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
}
+39
View File
@@ -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
}
+2 -41
View File
@@ -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
}
+23
View File
@@ -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
}
+6 -28
View File
@@ -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
}
+43
View File
@@ -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
}
+2 -45
View File
@@ -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
}