Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions condition/number_equal_to.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,29 @@ func (insp *numberEqualTo) Inspect(ctx context.Context, msg *message.Message) (o
if msg.IsControl() {
return false, nil
}
compare := insp.conf.Value

if insp.conf.Object.SourceKey == "" {
f, err := strconv.ParseFloat(string(msg.Data()), 64)
if err != nil {
return false, err
}

return insp.match(f), nil
return insp.match(f, compare), nil
}

target := msg.GetValue(insp.conf.Object.TargetKey)

if target.Exists() {
compare = target.Float()
}

v := msg.GetValue(insp.conf.Object.SourceKey)
return insp.match(v.Float()), nil
return insp.match(v.Float(), compare), nil
}

func (c *numberEqualTo) match(f float64) bool {
return f == c.conf.Value
func (c *numberEqualTo) match(f float64, t float64) bool {
return f == t
}

func (c *numberEqualTo) String() string {
Expand Down
27 changes: 27 additions & 0 deletions condition/number_equal_to_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,33 @@ var numberEqualToTests = []struct {
[]byte(`1.4`),
true,
},
{
"pass",
config.Config{
Settings: map[string]interface{}{
"object": map[string]interface{}{
"source_key": "foo",
"target_key": "bar",
},
},
},
[]byte(`{"foo": 10, "bar": 10}`),
true,
},
{
"fail",
config.Config{
Settings: map[string]interface{}{
"object": map[string]interface{}{
"source_key": "foo",
"target_key": "bar",
},
"value": 100,
},
},
[]byte(`{"foo": 100, "bar": 200}`),
false,
},
}

func TestNumberEqualTo(t *testing.T) {
Expand Down
16 changes: 12 additions & 4 deletions condition/number_greater_than.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,29 @@ func (insp *numberGreaterThan) Inspect(ctx context.Context, msg *message.Message
return false, nil
}

compare := insp.conf.Value

if insp.conf.Object.SourceKey == "" {
f, err := strconv.ParseFloat(string(msg.Data()), 64)
if err != nil {
return false, err
}

return insp.match(f), nil
return insp.match(f, compare), nil
}

target := msg.GetValue(insp.conf.Object.TargetKey)

if target.Exists() {
compare = target.Float()
}

v := msg.GetValue(insp.conf.Object.SourceKey)
return insp.match(v.Float()), nil
return insp.match(v.Float(), compare), nil
}

func (c *numberGreaterThan) match(f float64) bool {
return f > c.conf.Value
func (c *numberGreaterThan) match(f float64, t float64) bool {
return f > t
}

func (c *numberGreaterThan) String() string {
Expand Down
27 changes: 27 additions & 0 deletions condition/number_greater_than_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,33 @@ var numberGreaterThanTests = []struct {
[]byte(`1`),
false,
},
{
"pass",
config.Config{
Settings: map[string]interface{}{
"object": map[string]interface{}{
"source_key": "foo",
"target_key": "bar",
},
},
},
[]byte(`{"foo": 100, "bar": 10}`),
true,
},
{
"fail",
config.Config{
Settings: map[string]interface{}{
"object": map[string]interface{}{
"source_key": "foo",
"target_key": "bar",
},
"value": 10,
},
},
[]byte(`{"foo": 100, "bar": 2000}`),
false,
},
}

func TestNumberGreaterThan(t *testing.T) {
Expand Down
17 changes: 13 additions & 4 deletions condition/number_less_than.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,29 @@ func (insp *numberLessThan) Inspect(ctx context.Context, msg *message.Message) (
return false, nil
}

compare := insp.conf.Value

if insp.conf.Object.SourceKey == "" {
f, err := strconv.ParseFloat(string(msg.Data()), 64)
if err != nil {
return false, err
}

return insp.match(f), nil
return insp.match(f, compare), nil
}

target := msg.GetValue(insp.conf.Object.TargetKey)

if target.Exists() {
compare = target.Float()
}

v := msg.GetValue(insp.conf.Object.SourceKey)
return insp.match(v.Float()), nil
return insp.match(v.Float(), compare), nil
}

func (c *numberLessThan) match(f float64) bool {
return f < c.conf.Value
func (c *numberLessThan) match(f float64, t float64) bool {
return f < t
}

func (c *numberLessThan) String() string {
Expand Down
27 changes: 27 additions & 0 deletions condition/number_less_than_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,33 @@ var numberLessThanTests = []struct {
[]byte(`1`),
true,
},
{
"pass",
config.Config{
Settings: map[string]interface{}{
"object": map[string]interface{}{
"source_key": "foo",
"target_key": "bar",
},
},
},
[]byte(`{"foo": 10, "bar": 100}`),
true,
},
{
"fail",
config.Config{
Settings: map[string]interface{}{
"object": map[string]interface{}{
"source_key": "foo",
"target_key": "bar",
},
"value": 10,
},
},
[]byte(`{"foo": 2, "bar": 1}`),
false,
},
}

func TestNumberLessThan(t *testing.T) {
Expand Down
12 changes: 10 additions & 2 deletions condition/string_equal_to.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,20 @@ func (insp *stringEqualTo) Inspect(ctx context.Context, msg *message.Message) (o
return false, nil
}

compare := insp.b

if insp.conf.Object.SourceKey == "" {
return bytes.Equal(msg.Data(), insp.b), nil
return bytes.Equal(msg.Data(), compare), nil
}

target := msg.GetValue(insp.conf.Object.TargetKey)

if target.Exists() {
compare = target.Bytes()
}

value := msg.GetValue(insp.conf.Object.SourceKey)
return bytes.Equal(value.Bytes(), insp.b), nil
return bytes.Equal(value.Bytes(), compare), nil
}

func (c *stringEqualTo) String() string {
Expand Down
27 changes: 27 additions & 0 deletions condition/string_equal_to_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,33 @@ var stringEqualToTests = []struct {
[]byte("\"\""),
true,
},
{
"pass",
config.Config{
Settings: map[string]interface{}{
"object": map[string]interface{}{
"source_key": "foo",
"target_key": "bar",
},
},
},
[]byte(`{"foo":"abc", "bar":"abc"}`),
true,
},
{
"fail",
config.Config{
Settings: map[string]interface{}{
"object": map[string]interface{}{
"source_key": "foo",
"target_key": "bar",
},
"value": "abc",
},
},
[]byte(`{"foo":"abc", "bar":"def"}`),
false,
},
}

func TestStringEqualTo(t *testing.T) {
Expand Down
12 changes: 10 additions & 2 deletions condition/string_greater_than.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,20 @@ func (insp *stringGreaterThan) Inspect(ctx context.Context, msg *message.Message
return false, nil
}

compare := insp.b

if insp.conf.Object.SourceKey == "" {
return bytes.Compare(msg.Data(), insp.b) > 0, nil
return bytes.Compare(msg.Data(), compare) > 0, nil
}

target := msg.GetValue(insp.conf.Object.TargetKey)

if target.Exists() {
compare = target.Bytes()
}

value := msg.GetValue(insp.conf.Object.SourceKey)
return bytes.Compare(value.Bytes(), insp.b) > 0, nil
return bytes.Compare(value.Bytes(), compare) > 0, nil
}

func (c *stringGreaterThan) String() string {
Expand Down
27 changes: 27 additions & 0 deletions condition/string_greater_than_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,33 @@ var stringGreaterThanTests = []struct {
[]byte(`2023-01-01T00:00:00Z`),
true,
},
{
"pass",
config.Config{
Settings: map[string]interface{}{
"object": map[string]interface{}{
"source_key": "foo",
"target_key": "bar",
},
},
},
[]byte(`{"foo":"2023-01-01T00:00:00Z", "bar":"2022-01-01T00:00:00Z"}`),
true,
},
{
"fail",
config.Config{
Settings: map[string]interface{}{
"object": map[string]interface{}{
"source_key": "foo",
"target_key": "bar",
},
"value": "greetings",
},
},
[]byte(`{"foo":"hello", "bar":"world"}`),
false,
},
}

func TestStringGreaterThan(t *testing.T) {
Expand Down
11 changes: 9 additions & 2 deletions condition/string_less_than.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,19 @@ func (insp *stringLessThan) Inspect(ctx context.Context, msg *message.Message) (
return false, nil
}

compare := insp.b

if insp.conf.Object.SourceKey == "" {
return bytes.Compare(msg.Data(), insp.b) < 0, nil
return bytes.Compare(msg.Data(), compare) < 0, nil
}
target := msg.GetValue(insp.conf.Object.TargetKey)

if target.Exists() {
compare = target.Bytes()
}

value := msg.GetValue(insp.conf.Object.SourceKey)
return bytes.Compare(value.Bytes(), insp.b) < 0, nil
return bytes.Compare(value.Bytes(), compare) < 0, nil
}

func (c *stringLessThan) String() string {
Expand Down
27 changes: 27 additions & 0 deletions condition/string_less_than_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,33 @@ var stringLessThanTests = []struct {
[]byte(`2023-01-01T00:00:00Z`),
true,
},
{
"pass",
config.Config{
Settings: map[string]interface{}{
"object": map[string]interface{}{
"source_key": "foo",
"target_key": "bar",
},
},
},
[]byte(`{"foo":"2022-01-01T00:00:00Z", "bar":"2023-01-01T00:00:00Z"}`),
true,
},
{
"fail",
config.Config{
Settings: map[string]interface{}{
"object": map[string]interface{}{
"source_key": "foo",
"target_key": "bar",
},
"value": "2025-01-01",
},
},
[]byte(`{"foo":"2024-01-01T00:00:00Z", "bar":"2023-01-01"}`),
false,
},
}

func TestStringLessThan(t *testing.T) {
Expand Down