From 110253b646ccabc340abe5b1c9f3b66b26cc512d Mon Sep 17 00:00:00 2001 From: Josh Liburdi Date: Mon, 17 Apr 2023 16:42:01 -0700 Subject: [PATCH] feat: add gt, lt (#98) --- 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) {