Skip to content

Commit c7ce9bd

Browse files
lahabanaGuaris
andauthored
feat(event-gateway): add expression examples and info about the context (#3801)
* feat(event-gateway): add expression examples and info about the context Add what is available when and some basic examples Fix #3409 Signed-off-by: Charly Molter <[email protected]> * fix lint * table formatting --------- Signed-off-by: Charly Molter <[email protected]> Co-authored-by: Angel <[email protected]>
1 parent d901c7e commit c7ce9bd

File tree

1 file changed

+110
-5
lines changed

1 file changed

+110
-5
lines changed

app/event-gateway/expressions.md

Lines changed: 110 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ In {{site.event_gateway}}, you can use a policy's condition field to determine w
2828
For example, you can create a condition that selects all topics that end with the suffix `my_suffix`:
2929

3030
```json
31-
"condition": "context.topic.name.endsWith('my_suffix')"
31+
{"condition": "context.topic.name.endsWith('my_suffix')"}
3232
```
3333

3434
Conditions must be between 1 and 1000 characters long.
@@ -65,10 +65,115 @@ rows:
6565

6666
{{site.event_gateway}} supports the following string functions in conditional fields:
6767

68-
* `includes`: Performs a case-sensitive search to determine whether a given string may be found within this string, as defined in the [JavaScript standard](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes).
69-
* `startsWith`: Determines whether the string begins with the characters of a specified string, [equivalent to the JavaScript standard function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/startsWith).
70-
* `endsWith`: Determines whether the string ends with the characters of a specified string, [equivalent to the JavaScript standard function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/startsWith).
68+
* `includes`: Performs a case-sensitive search to determine whether a given string may be found within this string, as
69+
defined in
70+
the [JavaScript standard](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes).
71+
* `startsWith`: Determines whether the string begins with the characters of a specified
72+
string, [equivalent to the JavaScript standard function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/startsWith).
73+
* `endsWith`: Determines whether the string ends with the characters of a specified
74+
string, [equivalent to the JavaScript standard function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/startsWith).
7175
* `substring`: Returns the part of this string from the start index up to and excluding the end index.
72-
* `match`: Retrieves the result of matching this string against an [RE2 regular expression](https://github.com/google/re2/wiki/syntax) string.
76+
* `match`: Retrieves the result of matching this string against
77+
an [RE2 regular expression](https://github.com/google/re2/wiki/syntax) string.
7378

79+
### Supported fields
7480

81+
Depending on where an expression is authored the fields available in the context vary.
82+
In this section we state for each area how the context varies.
83+
The source of truth for these is the `x-expression` field in
84+
the [API specification](/api/konnect/event-gateway/v1/).
85+
86+
{% table %}
87+
columns:
88+
- title: Variable
89+
key: variable
90+
- title: Type
91+
key: type
92+
- title: Description
93+
key: description
94+
- title: Availability
95+
key: availability
96+
- title: Example
97+
key: example
98+
rows:
99+
- variable: "`context.auth.type`"
100+
type: "`string`"
101+
description: |
102+
The type of authentication used
103+
availability: |
104+
Cluster, Produce and Consume policies
105+
example: |
106+
`context.auth.type == 'anonymous'`
107+
- variable: "`context.auth.principal.name`"
108+
type: "`string`"
109+
description: |
110+
The name of the principal for this connection
111+
availability: |
112+
Cluster, Produce and Consume policies
113+
example: |
114+
`context.auth.principal.name == 'user1'`
115+
- variable: "`context.topic.name`"
116+
type: "`string`"
117+
description: |
118+
The name of the topic of the record
119+
availability: |
120+
Produce and Consume policies
121+
example: |
122+
`context.topic.name == 'my-ns.my-topic'`
123+
- variable: "`record.headers`"
124+
type: "`map<string, string>`"
125+
description: |
126+
The headers of the record
127+
availability: |
128+
Produce and Consume policies
129+
example: |
130+
`record.headers['skip-record'] == 'true'`
131+
- variable: "`record.value.content`"
132+
type: "`map<string, string>`"
133+
description: |
134+
The value of the record. Deep fields can be accessed using json object notation
135+
availability: |
136+
Produce and Consume policies used as children of Schema Validation
137+
example: |
138+
`record.value.content['sub.other'] == 3`
139+
- variable: "`record.value.validated`"
140+
type: "`boolean`"
141+
description: |
142+
Whether record validation succeeded or not
143+
availability: |
144+
Produce and Consume policies used as children of Schema Validation
145+
example: |
146+
`record.value.validated == true`
147+
{% endtable %}
148+
149+
### Example expressions
150+
151+
152+
Don't apply a policy if a record has a `x-restricted=true` header and user is not admin:
153+
154+
```sh
155+
context.topic.name == 'filterdemo' && record.headers['x-restricted'] == 'true' && context.auth.principal.name != 'admin'
156+
```
157+
158+
Apply a policy only for `user1` and `user2`:
159+
```sh
160+
context.auth.principal.name == 'user1' || context.auth.principal.name == 'user2'
161+
```
162+
163+
Apply a policy only for topics that start with `my-prefix`:
164+
165+
```sh
166+
context.topic.name.startsWith('my-prefix')
167+
```
168+
169+
Apply a policy if a header is present regardless of the value:
170+
171+
```sh
172+
'x-optional-header' in record.headers
173+
```
174+
175+
Apply a policy if the topic is `filterdemo` and that the record content has a field `foo` equal to `bar` and a sub field `sub.other` equal to 3.
176+
177+
```sh
178+
context.topic.name == 'filterdemo' && record.value.content['foo'] == 'bar' || record.value.content['sub.other'] == 3
179+
```

0 commit comments

Comments
 (0)