Skip to content

Commit

Permalink
feat: add gt, lt (#98)
Browse files Browse the repository at this point in the history
  • Loading branch information
jshlbrd authored Apr 17, 2023
1 parent 9e7a6db commit 110253b
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
8 changes: 8 additions & 0 deletions condition/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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)
}
Expand Down
50 changes: 50 additions & 0 deletions condition/strings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 110253b

Please sign in to comment.