Skip to content

Commit

Permalink
add starts-with, ends-with operators
Browse files Browse the repository at this point in the history
  • Loading branch information
Larry Hitchon committed Jul 23, 2018
1 parent f0592ea commit 599175e
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 4 deletions.
24 changes: 24 additions & 0 deletions assertion/contains.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,27 @@ func doesNotContain(data interface{}, key, value string) (MatchResult, error) {
}
return matches()
}

func startsWith(data interface{}, key, prefix string) (MatchResult, error) {
switch v := data.(type) {
case string:
if strings.HasPrefix(v, prefix) {
return matches()
}
return doesNotMatch("%v does not start with %v", key, prefix)
default:
return doesNotMatch("%v is not a string %v", key, prefix)
}
}

func endsWith(data interface{}, key, suffix string) (MatchResult, error) {
switch v := data.(type) {
case string:
if strings.HasSuffix(v, suffix) {
return matches()
}
return doesNotMatch("%v does not end with %v", key, suffix)
default:
return doesNotMatch("%v is not a string %v", key, suffix)
}
}
4 changes: 4 additions & 0 deletions assertion/match.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ func isMatch(data interface{}, expression Expression) (MatchResult, error) {
return doesNotContain(data, key, value)
case "does-not-contain":
return doesNotContain(data, key, value)
case "starts-with":
return startsWith(data, key, value)
case "ends-with":
return endsWith(data, key, value)
case "regex":
re, err := regexp.Compile(value)
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions assertion/match_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ func TestIsMatch(t *testing.T) {
"isNotTrue": {"false", "is-true", "", "", false},
"isFalse": {"false", "is-false", "", "", true},
"isNotFalse": {"100", "is-false", "", "", false},
"startsWithTrue": {"FooBar", "starts-with", "Foo", "", true},
"startsWithFalse": {"FooBar", "starts-with", "Bar", "", false},
"endsWithTrue": {"FooBar", "ends-with", "Bar", "", true},
"endsWithFalse": {"FooBar", "ends-with", "Foo", "", false},
}
for k, tc := range testCases {
var m MatchResult
Expand Down
48 changes: 44 additions & 4 deletions docs/operations.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,27 @@

| Operation | Description |
|-----------------------------------|----------------|
| [and](#and) | And |
| [absent](#absent) | Absent |
| [and](#and) | And |
| [contains](#contains) | Contains |
| [does-not-contain](#does-not-contain) | Does Not Contain |
| [end-with](#end-with) | Ends With |
| [eq](#eq) | Equal |
| [empty](#empty) | Empty |
| [every](#every) | Every |
| [eq](#eq) | Equal |
| [has-properties](#has-properties) | Has Properties |
| [in](#in) | In |
| [is-false](#is-false) | Is False |
| [is-true](#is-true) | Is True |
| [has-properties](#has-properties) | Has Properties |
| [ne](#ne) | Not equal |
| [none](#none) | None |
| [not](#not) | Not |
| [does-not-contain](#does-not-contain) | Does Not Contain |
| [not-empty](#not-empty) | Not Empty |
| [not-in](#not-in) | Not In |
| [or](#or) | Or |
| [present](#present) | Present |
| [regex](#regex) | Regex |
| [starts-with](#starts-with) | Starts With |
| [some](#some) | Some |
| [xor](#xor) | Xor |

Expand Down Expand Up @@ -349,3 +351,41 @@ Example:
op: is-false
...
```

## starts-with

Check that a string value starts with a value

Example:

```
...
- id: NAME_PREFIX
message: Name should have a certain prefix
severity: FAILURE
resource: some_resource
assertions:
- key: name
op: starts-with
value: Foo
...
```

## ends-with

Check that a string value ends with a value

Example:

```
...
- id: NAME_SUFFIX
message: Name should have a certain suffix
severity: FAILURE
resource: some_resource
assertions:
- key: name
op: ends-with
value: Resource
...
```

0 comments on commit 599175e

Please sign in to comment.