From 63629ffbf6607c7849fd8f0acf3d6573a7ba71ef Mon Sep 17 00:00:00 2001 From: Josh Liburdi Date: Mon, 17 Apr 2023 18:34:10 +0000 Subject: [PATCH] feat: add gt, lt --- condition/strings.go | 8 +++++++ condition/strings_test.go | 50 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/condition/strings.go b/condition/strings.go index d83458ad..d4526c40 100644 --- a/condition/strings.go +++ b/condition/strings.go @@ -29,6 +29,10 @@ type inspStringsOptions struct { // - starts_with // // - ends_with + // + // - greater_than + // + // - less_than Type string `json:"type"` // Expression is a substring used during inspection. Expression string `json:"expression"` @@ -59,6 +63,10 @@ func (c inspStrings) Inspect(ctx context.Context, capsule config.Capsule) (outpu matched = strings.HasPrefix(check, c.Options.Expression) case "ends_with": matched = strings.HasSuffix(check, c.Options.Expression) + case "greater_than": + matched = strings.Compare(check, c.Options.Expression) > 0 + case "less_than": + matched = strings.Compare(check, c.Options.Expression) < 0 default: return false, fmt.Errorf("condition: strings: type %s: %v", c.Options.Type, errors.ErrInvalidType) } diff --git a/condition/strings_test.go b/condition/strings_test.go index 99704c1e..8581a98b 100644 --- a/condition/strings_test.go +++ b/condition/strings_test.go @@ -171,6 +171,56 @@ var stringsTests = []struct { []byte(``), true, }, + { + "pass", + inspStrings{ + Options: inspStringsOptions{ + Type: "greater_than", + Expression: "a", + }, + }, + []byte("b"), + true, + }, + { + "pass", + inspStrings{ + Options: inspStringsOptions{ + Type: "less_than", + Expression: "c", + }, + }, + []byte("b"), + true, + }, + { + "pass", + inspStrings{ + condition: condition{ + Key: "a", + }, + Options: inspStringsOptions{ + Type: "greater_than", + Expression: "2022-01-01T00:00:00Z", + }, + }, + []byte(`{"a":"2023-01-01T00:00:00Z"}`), + true, + }, + { + "pass", + inspStrings{ + condition: condition{ + Key: "a", + }, + Options: inspStringsOptions{ + Type: "less_than", + Expression: "2024-01", + }, + }, + []byte(`{"a":"2023-01-01T00:00:00Z"}`), + true, + }, } func TestStrings(t *testing.T) {