-
Notifications
You must be signed in to change notification settings - Fork 1.6k
docs: add array.concat and array.slice examples to array.mdx #8669
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| { | ||
| "showInput": true, | ||
| "showData": false, | ||
| "showTitles": false, | ||
| "titleSize": 4 | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| {} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| { | ||
| "metadata": { | ||
| "labels": { | ||
| "app": "web", | ||
| "owner": "platform-team" | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| <!-- markdownlint-disable MD041 --> | ||
|
|
||
| `array.concat` joins two arrays in order. A common use is building a complete | ||
| validation list by merging a shared base set of rules with environment-specific | ||
| additions, then iterating the result in a single `deny` rule. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| { | ||
| "base_required_labels": [ | ||
| "app", | ||
| "owner" | ||
| ], | ||
| "deny": [ | ||
| "missing required label: \"cost-center\"", | ||
| "missing required label: \"environment\"" | ||
| ], | ||
| "env_required_labels": [ | ||
| "environment", | ||
| "cost-center" | ||
| ], | ||
| "required_labels": [ | ||
| "app", | ||
| "owner", | ||
| "environment", | ||
| "cost-center" | ||
| ] | ||
| } |
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider reducing the complexity of this rule, as the important part ( |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| package play | ||
|
|
||
| # Merge base required labels with environment-specific ones. | ||
| base_required_labels := ["app", "owner"] | ||
|
|
||
| env_required_labels := ["environment", "cost-center"] | ||
|
|
||
| required_labels := array.concat(base_required_labels, env_required_labels) | ||
|
|
||
| # Deny a resource whose metadata is missing any required label. | ||
| deny contains msg if { | ||
| some label in required_labels | ||
| not input.metadata.labels[label] | ||
| msg := sprintf("missing required label: %q", [label]) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Merging base and environment label requirements |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| { | ||
| "showInput": true, | ||
| "showData": false, | ||
| "showTitles": false, | ||
| "titleSize": 4 | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| {} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| { | ||
| "path": "/api/v2/users/123" | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| <!-- markdownlint-disable MD041 --> | ||
|
|
||
| `array.slice` returns the sub-array from index `start` (inclusive) to `stop` | ||
| (exclusive). A common use is extracting a fixed-length prefix from a | ||
| variable-length path after splitting it into segments, so the routing logic | ||
| stays independent of how deep the path goes. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| { | ||
| "api_version": "v2", | ||
| "deny": [ | ||
| "unsupported API version \"v2\"; only v1 is allowed" | ||
| ], | ||
| "path_segments": [ | ||
| "api", | ||
| "v2", | ||
| "users", | ||
| "123" | ||
| ], | ||
| "route_prefix": [ | ||
| "api", | ||
| "v2" | ||
| ] | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,17 @@ | ||||||||||||||||
| package play | ||||||||||||||||
|
|
||||||||||||||||
| # Split a request path into segments. | ||||||||||||||||
| # e.g. "/api/v1/users/123" → ["api", "v1", "users", "123"] | ||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This arrow won't transition well when the user open's the example in the playground. Consider not using any "special" characters. |
||||||||||||||||
| path_segments := split(trim_left(input.path, "/"), "/") | ||||||||||||||||
|
|
||||||||||||||||
| # The API version is always the second segment (index 1). | ||||||||||||||||
| api_version := path_segments[1] | ||||||||||||||||
|
|
||||||||||||||||
| # The first two segments identify the route, regardless of how long the path is. | ||||||||||||||||
| route_prefix := array.slice(path_segments, 0, 2) | ||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar to the other example policy, I wonder if this policy isn't a bit too complicated for its intended purpose. |
||||||||||||||||
|
|
||||||||||||||||
| # Deny requests that do not target a supported API version. | ||||||||||||||||
| deny contains msg if { | ||||||||||||||||
| api_version != "v1" | ||||||||||||||||
| msg := sprintf("unsupported API version %q; only v1 is allowed", [api_version]) | ||||||||||||||||
| } | ||||||||||||||||
|
Comment on lines
+14
to
+17
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the final example policies end up using generated strings, consider using string-templates instead of
Suggested change
|
||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Extracting a route prefix from a request path |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this'll be injected into the content in
array.mdx, the reader will experience a bit of a hiccup in the text. E.g.:Consider dropping the redundant text from here to make it read a bit better. Same for
array.slice.