3
0

Extend record constraints to support cr/up/de-at and values matching

New record contraints now support:
 - record.updatedAt
 - record.createdAt
 - record.deletedAt
 - record.values.<field-name>
This commit is contained in:
Denis Arh
2020-03-20 12:38:37 +01:00
parent 845dc2c4de
commit 0f659fdeae
2 changed files with 68 additions and 4 deletions

View File

@@ -1,8 +1,39 @@
package event
import "github.com/cortezaproject/corteza-server/pkg/eventbus"
import (
"github.com/cortezaproject/corteza-server/compose/types"
"github.com/cortezaproject/corteza-server/pkg/eventbus"
"strings"
"time"
)
const (
recordMatchValues = "record.values."
)
// Match returns false if given conditions do not match event & resource internals
func (res recordBase) Match(c eventbus.ConstraintMatcher) bool {
return namespaceMatch(res.namespace, c, moduleMatch(res.module, c, false))
return recordMatch(res.record, c, namespaceMatch(res.namespace, c, moduleMatch(res.module, c, false)))
}
func recordMatch(r *types.Record, c eventbus.ConstraintMatcher, def bool) bool {
switch c.Name() {
case "record.updatedAt":
return c.Match(r.UpdatedAt.Format(time.RFC3339))
case "record.createdAt":
return c.Match(r.CreatedAt.Format(time.RFC3339))
case "record.deletedAt":
return c.Match(r.DeletedAt.Format(time.RFC3339))
}
if strings.HasPrefix(c.Name(), recordMatchValues) {
fieldName := c.Name()[len(recordMatchValues):]
for _, v := range r.Values.FilterByName(fieldName) {
if c.Match(v.Value) {
return true
}
}
}
return def
}

View File

@@ -1,11 +1,11 @@
package event
import (
"fmt"
"github.com/cortezaproject/corteza-server/compose/types"
"github.com/cortezaproject/corteza-server/pkg/eventbus"
"testing"
"github.com/stretchr/testify/assert"
"testing"
)
func TestRecordMatching(t *testing.T) {
@@ -23,3 +23,36 @@ func TestRecordMatching(t *testing.T) {
a.True(res.Match(cMod))
a.True(res.Match(cNms))
}
func TestRecordMatchValues(t *testing.T) {
var (
rec = &types.Record{
Values: types.RecordValueSet{
&types.RecordValue{Name: "fld1", Value: "val1"},
&types.RecordValue{Name: "fld2", Value: "val2"},
},
}
res = &recordBase{record: rec}
cases = []struct {
match bool
name string
op string
v string
}{
{true, "fld1", "eq", "val1"},
{false, "fld2", "eq", "val1"},
{false, "fld1", "!=", "val1"},
}
)
for _, c := range cases {
t.Run(
fmt.Sprintf("(%s %s %s) == %v", c.name, c.op, c.v, c.match),
func(t *testing.T) {
assert.Equal(t, c.match, res.Match(eventbus.MustMakeConstraint("record.values."+c.name, c.op, c.v)))
},
)
}
}