3
0

Implement constraint checking for messaging events

This commit is contained in:
Denis Arh 2020-01-10 10:00:35 +01:00
parent 8cb2c4c03d
commit c944402352
10 changed files with 200 additions and 53 deletions

View File

@ -1,15 +1,27 @@
package event
import "github.com/cortezaproject/corteza-server/pkg/eventbus"
import (
"github.com/cortezaproject/corteza-server/messaging/types"
"github.com/cortezaproject/corteza-server/pkg/eventbus"
)
// Match returns false if given conditions do not match event & resource internals
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
// All should match (return true):
// constraint#1 AND constraint#2 AND constraint#3 ...
//
// When there are multiple values, Match() can decide how to treat them (OR, AND...)
return true
return channelMatch(res.channel, c, false)
}
// Handles channel matchers
//
// This *match() fn uses a 3rd param to allow matcher chaining (see commands, channels)
func channelMatch(r *types.Channel, c eventbus.ConstraintMatcher, def bool) bool {
switch c.Name() {
case "channel", "channel.name":
return r != nil && c.Match(r.Name)
case "channel.topic":
return r != nil && c.Match(r.Topic)
case "channel.type":
return r != nil && c.Match(r.Type.String())
}
return def
}

View File

@ -1,15 +1,21 @@
package event
import "github.com/cortezaproject/corteza-server/pkg/eventbus"
import (
"github.com/cortezaproject/corteza-server/messaging/types"
"github.com/cortezaproject/corteza-server/pkg/eventbus"
)
// Match returns false if given conditions do not match event & resource internals
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
// All should match (return true):
// constraint#1 AND constraint#2 AND constraint#3 ...
//
// When there are multiple values, Match() can decide how to treat them (OR, AND...)
return true
return channelMatch(res.channel, c, channelMemberMatch(res.member, c))
}
// Handles channel member matchers
func channelMemberMatch(r *types.ChannelMember, c eventbus.ConstraintMatcher) bool {
switch c.Name() {
case "channel-member.type":
return c.Match(string(r.Type))
}
return false
}

View File

@ -0,0 +1,27 @@
package event
import (
"github.com/cortezaproject/corteza-server/messaging/types"
"github.com/cortezaproject/corteza-server/pkg/eventbus"
"testing"
"github.com/stretchr/testify/assert"
)
func TestChannelMemberMatching(t *testing.T) {
var (
a = assert.New(t)
res = &channelMemberBase{
channel: &types.Channel{Name: "ChanChan"},
member: &types.ChannelMember{Type: types.ChannelMembershipTypeOwner},
}
cOwn = eventbus.MustMakeConstraint("channel-member.type", "eq", "owner")
cChn = eventbus.MustMakeConstraint("channel", "eq", "ChanChan")
)
a.True(channelMemberMatch(res.member, cOwn))
a.True(res.Match(cOwn))
a.True(res.Match(cChn))
}

View File

@ -1,15 +1,21 @@
package event
import "github.com/cortezaproject/corteza-server/pkg/eventbus"
import (
"github.com/cortezaproject/corteza-server/messaging/types"
"github.com/cortezaproject/corteza-server/pkg/eventbus"
)
// Match returns false if given conditions do not match event & resource internals
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
// All should match (return true):
// constraint#1 AND constraint#2 AND constraint#3 ...
//
// When there are multiple values, Match() can decide how to treat them (OR, AND...)
return true
return channelMatch(res.channel, c, commandMatch(res.command, c))
}
// Handles command matchers
func commandMatch(r *types.Command, c eventbus.ConstraintMatcher) bool {
switch c.Name() {
case "command", "command.name":
return r != nil && c.Match(r.Name)
}
return false
}

View File

@ -0,0 +1,30 @@
package event
import (
"github.com/cortezaproject/corteza-server/messaging/types"
"github.com/cortezaproject/corteza-server/pkg/eventbus"
"testing"
"github.com/stretchr/testify/assert"
)
func TestCommandMatching(t *testing.T) {
var (
a = assert.New(t)
res = &commandBase{
channel: &types.Channel{Name: "ChanChan"},
command: &types.Command{Name: "fooCommand"},
}
cFoo = eventbus.MustMakeConstraint("command", "eq", "fooCommand")
cBar = eventbus.MustMakeConstraint("command", "eq", "barCommand")
cChn = eventbus.MustMakeConstraint("channel", "eq", "ChanChan")
)
a.True(commandMatch(res.command, cFoo))
a.False(commandMatch(res.command, cBar))
a.True(res.Match(cFoo))
a.False(res.Match(cBar))
a.True(res.Match(cChn))
}

View File

@ -1,9 +0,0 @@
package event
type (
constraint interface {
Name() string
Values() []string
Match(value string) bool
}
)

View File

@ -1,15 +1,23 @@
package event
import "github.com/cortezaproject/corteza-server/pkg/eventbus"
import (
"github.com/cortezaproject/corteza-server/messaging/types"
"github.com/cortezaproject/corteza-server/pkg/eventbus"
)
// Match returns false if given conditions do not match event & resource internals
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
// All should match (return true):
// constraint#1 AND constraint#2 AND constraint#3 ...
//
// When there are multiple values, Match() can decide how to treat them (OR, AND...)
return true
return channelMatch(res.channel, c, messageMatch(res.message, c))
}
// Handles message matchers
func messageMatch(r *types.Message, c eventbus.ConstraintMatcher) bool {
switch c.Name() {
case "message":
return c != nil && c.Match(r.Message)
case "message.type":
return c != nil && c.Match(string(r.Type))
}
return false
}

View File

@ -0,0 +1,28 @@
package event
import (
"github.com/cortezaproject/corteza-server/messaging/types"
"github.com/cortezaproject/corteza-server/pkg/eventbus"
"github.com/stretchr/testify/assert"
"testing"
)
func TestMessageMatching(t *testing.T) {
var (
a = assert.New(t)
res = &messageBase{
channel: &types.Channel{Name: "ChanChan"},
message: &types.Message{Message: "foo bar", Type: types.MessageTypeIlleism},
}
cMsg = eventbus.MustMakeConstraint("message", "like", "foo*")
cTyp = eventbus.MustMakeConstraint("message.type", "eq", "illeism")
cChn = eventbus.MustMakeConstraint("channel", "eq", "ChanChan")
)
a.True(messageMatch(res.message, cMsg))
a.True(res.Match(cMsg))
a.True(res.Match(cTyp))
a.True(res.Match(cChn))
}

View File

@ -7,22 +7,22 @@ import (
// Match returns false if given conditions do not match event & resource internals
func (res messagingOnInterval) Match(c eventbus.ConstraintMatcher) bool {
// @todo this could be flippled
// instead of passing around raw values as strings,
// constraint values could/should be preparsed when creating constraint
return scheduler.OnInterval(c.Values()...)
}
// Match returns false if given conditions do not match event & resource internals
func (res messagingOnTimestamp) Match(c eventbus.ConstraintMatcher) bool {
// @todo this could be flippled
// instead of passing around raw values as strings,
// constraint values could/should be preparsed when creating constraint
return scheduler.OnTimestamp(c.Values()...)
}
// Match returns false if given conditions do not match event & resource internals
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
// All should match (return true):
// constraint#1 AND constraint#2 AND constraint#3 ...
//
// When there are multiple values, Match() can decide how to treat them (OR, AND...)
return true
// No constraints are supported for messaging.
return false
}

View File

@ -0,0 +1,39 @@
package event
import (
"github.com/cortezaproject/corteza-server/pkg/eventbus"
"testing"
"github.com/stretchr/testify/assert"
)
func TestMessagingOnIntervalMatching(t *testing.T) {
var (
res = &messagingOnInterval{}
cInt = eventbus.MustMakeConstraint("", "", "* * * * *")
)
// Just make sure it runs
// not bothering with precise test setup (scheduler's tests cover all that
res.Match(cInt)
}
func TestMessagingOnTimestampMatching(t *testing.T) {
var (
a = assert.New(t)
res = &messagingOnInterval{}
cTStamp = eventbus.MustMakeConstraint("", "", "2000-01-01T00:00:00Z") // Y2k!
)
a.False(res.Match(cTStamp), "Year 2000?! How did we get here? Anyhow, happy New year!")
}
func TestMessagingMatching(t *testing.T) {
var (
a = assert.New(t)
res = &messagingBase{}
)
a.False(res.Match(eventbus.MustMakeConstraint("foo", "", "bar")))
}