You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
`index(container, key)`
index is used to index into a map via a string key or an array via an integer index.
e.g. `index(map, "key")`
map: `map[string]any{"key": "value"}` -> `"value"`
`index(["first", "second"], 1)` -> `"second"`
r, err:= rule.Parse(`domain matches /example\.com$/ and port == 8080`)
46
47
if err != nil { /* ... */ }
47
-
48
+
48
49
// define input data
49
50
input:= rulekit.KV{
50
51
"domain": "example.com",
51
52
"port": 8080,
52
53
}
53
-
54
+
54
55
// evaluate the rule
55
56
result:= r.Eval(&rulekit.Ctx{KV: inputData})
56
57
@@ -75,49 +76,50 @@ When a rule is evaluated, it returns a `Result` struct containing:
75
76
-`EvaluatedRule`: The sub-rule that determined the returned value. Useful for debugging and understanding which part of a complex rule caused the result.
76
77
77
78
The Result also provides additional helper methods:
79
+
78
80
-`Pass()`: Returns true if the rule returns true/a non-zero value with no errors
79
81
-`Fail()`: Returns true if the rule returns false/a zero value with no errors
80
82
-`Ok()`: Returns true if the rule executed with no error
81
83
82
84
## Supported Operators
83
85
84
-
| Operator | Alias | Description |
85
-
|----------|--------------|-------------|
86
-
|`or`|`\|\|`| Logical OR |
87
-
|`and`|`&&`| Logical AND |
88
-
|`not`|`!`| Logical NOT |
89
-
|`()`|| Parentheses for grouping |
90
-
|`==`|`eq`| Equal to |
91
-
|`!=`|`ne`| Not equal to |
92
-
|`>`|`gt`| Greater than |
93
-
|`>=`|`ge`| Greater than or equal to |
94
-
|`<`|`lt`| Less than |
95
-
|`<=`|`le`| Less than or equal to |
96
-
|`contains`|| Check if a value contains another value |
97
-
|`in`|| Check if a value is contained within an array or an IP within a CIDR |
|`contains`|| Check if a value contains another value|
99
+
|`in`|| Check if a value is contained within an array or an IP within a CIDR |
100
+
|`matches`|| Match against a regular expression|
99
101
100
102
## Supported Types
101
103
102
104
### Basic values
103
105
104
-
| Type | Used As | Example | Description |
105
-
|------|---------|---------|-------------|
106
-
|**bool**| VALUE, FIELD |`true`| Valid values: `true`, `false`|
107
-
|**number**| VALUE, FIELD |`8080`| Integer or float. Parsed as either int64 or uint64 if out of range for int64, or float64 if float. |
108
-
|**string**| VALUE, FIELD |`"domain.com"`| A double-quoted string. Quotes may be escaped with a backslash: `"a string \"with\" quotes"`. Any quoted value is parsed as a string. |
109
-
|**IP address**| VALUE, FIELD |`192.168.1.1`, `2001:db8:3333:4444:cccc:dddd:eeee:ffff`| An IPv4, IPv6, or an IPv6 dual address. Maps to Go type: `net.IP`|
110
-
|**CIDR**| VALUE |`192.168.1.0/24`, `2001:db8:3333:4444:cccc:dddd:eeee:ffff/64`| An IPv4 or IPv6 CIDR block. Maps to Go type: `*net.IPNet`|
111
-
|**Hexadecimal string**| VALUE, FIELD |`12:34:56:78:ab` (MAC address), `504f5354` (hex string "POST") | A hexadecimal string, optionally separated by colons. |
112
-
|**Regex**| VALUE |`/example\.com$/`| A Go-style regular expression. Must be surrounded by forward slashes. May not be quoted with double quotes (otherwise it will be parsed as a string). Maps to Go type: `*regexp.Regexp`|
|**bool**| VALUE, FIELD |`true`| Valid values: `true`, `false`|
109
+
|**number**| VALUE, FIELD |`8080`| Integer or float. Parsed as either int64 or uint64 if out of range for int64, or float64 if float.|
110
+
|**string**| VALUE, FIELD |`"domain.com"`| A double-quoted string. Quotes may be escaped with a backslash: `"a string \"with\" quotes"`. Any quoted value is parsed as a string.|
111
+
|**IP address**| VALUE, FIELD |`192.168.1.1`, `2001:db8:3333:4444:cccc:dddd:eeee:ffff`| An IPv4, IPv6, or an IPv6 dual address. Maps to Go type: `net.IP`|
112
+
|**CIDR**| VALUE |`192.168.1.0/24`, `2001:db8:3333:4444:cccc:dddd:eeee:ffff/64`| An IPv4 or IPv6 CIDR block. Maps to Go type: `*net.IPNet`|
113
+
|**Hexadecimal string**| VALUE, FIELD |`12:34:56:78:ab` (MAC address), `504f5354` (hex string "POST") | A hexadecimal string, optionally separated by colons. |
114
+
|**Regex**| VALUE |`/example\.com$/`| A Go-style regular expression. Must be surrounded by forward slashes. May not be quoted with double quotes (otherwise it will be parsed as a string). Maps to Go type: `*regexp.Regexp`|
113
115
114
116
### Constructs
115
117
116
-
| Type | Used As | Example | Description |
117
-
|------|---------|---------|-------------|
118
-
|**Array**| VALUE |`[1, "string", true]`| An array of mixed value types. Can be used with most operators including `in` and `contains`. |
119
-
|**Function**| VALUE |`starts_with(url, "https://")`| A function call with optional arguments. Can be built-in or custom. |
120
-
|**Macro**| VALUE |`isValidRequest()`| A zero-argument function that encapsulates a predefined rule. |
|**Array**| VALUE |`[1, "string", true]`| An array of mixed value types. Can be used with most operators including `in` and `contains`. |
121
+
|**Function**| VALUE |`starts_with(url, "https://")`| A function call with optional arguments. Can be built-in or custom.|
122
+
|**Macro**| VALUE |`isValidRequest()`| A zero-argument function that encapsulates a predefined rule.|
121
123
122
124
## Macros
123
125
@@ -152,9 +154,10 @@ Functions can be called inside rules and used as value objects. Functions may ac
152
154
153
155
Rulekit comes with a built-in standard library of functions:
154
156
155
-
| Function | Description | Example |
156
-
|----------|-------------|---------|
157
-
|`starts_with(value, prefix)`| Checks if a value starts with the given prefix. Works with strings, numbers, and other types by converting them to strings. |`starts_with(url, "https://")`|
|`starts_with(value, prefix)`| Checks if a value starts with the given prefix. Works with strings, numbers, and other types by converting them to strings. |`starts_with(url, "https://")`|
160
+
|`index(container, key)`| Indexes into a map or slice |`index(["one", "two"], 0)` -> `"one"`|
0 commit comments