Sentinel
Import: collection/maps
The collection/maps
import provides helpers for working with maps.
get
get(object, path[, default])
Get the value from the provided object using the path. When the path is invalid or the object doesn't contain a value at the path, then the default is returned. The default return value is undefined.
Arguments
Name | Description |
---|---|
object | the map to perform the get against. |
path | a path to a key within object to retreive the value |
default | an optional value that will return if the path does not exist or returns undefined |
Examples
Get a value from a simple object:
Sentinel Playground
Loading the playground...
Press "Run" to get policy output
Get a nested value from a complex object:
Sentinel Playground
Loading the playground...
Press "Run" to get policy output
Get a list of values using splat:
Sentinel Playground
Loading the playground...
Press "Run" to get policy output
Get an invalid path, providing a default:
Sentinel Playground
Loading the playground...
Press "Run" to get policy output
Get an invalid path, not providing a default:
Sentinel Playground
Loading the playground...
Press "Run" to get policy output
has
has(object, path)
Return a boolean value if the object has the path within its structure.
Arguments
Name | Description |
---|---|
object | the map to perform the has against. |
path | a path to a key within object to determine if it exists |
Examples
An object has a valid key:
Sentinel Playground
Loading the playground...
Press "Run" to get policy output
An object does not have a valid key:
Sentinel Playground
Loading the playground...
Press "Run" to get policy output
has
pick(object, paths)
Return a map consisting of each value found in object from the paths provided. The original structure of the map is maintained, as seen in the below examples.
Arguments
Name | Description |
---|---|
object | the map to perform the has against. |
paths | a list of paths, each used to select a value from the object. |
Examples
Picking a single value:
Sentinel Playground
Loading the playground...
Press "Run" to get policy output
Picking multiple values:
Sentinel Playground
Loading the playground...
Press "Run" to get policy output
Picking deep values:
Sentinel Playground
Loading the playground...
Press "Run" to get policy output
set
set(object, path, value)
Return a new map, assigning the provided value to the provided path. It will not modify the provided map in place.
Arguments
Name | Description |
---|---|
object | the map to perform the set against. |
path | the path to assign value |
value | the value to be assigned |
Examples
Set a value on a simple path:
Sentinel Playground
Loading the playground...
Press "Run" to get policy output
Set a value on a deep path:
Sentinel Playground
Loading the playground...
Press "Run" to get policy output
Set a value on an index in a list:
Sentinel Playground
Loading the playground...
Press "Run" to get policy output
Set a value on every key within a list key:
Sentinel Playground
Loading the playground...
Press "Run" to get policy output
Paths
Map helpers often receive a path argument that allows for looking up a nested
key. Generally speaking, a path is a series of keys separated by .
, however there
are some additional capabilites that need to be explained.
Lists
When traversing a nested map that contains a list, a specific index can be retrieved by providing the index as the part of the path.
In the following code sample, the path will first enter the key "foo"
within the
map, followed by entering the first index of the list.
path = "foo.0"
object = {"foo": [1]}
Splat
To provide advanced capabilities when using paths, you can also use the splat (*
)
operator to iterate through all elements in a list, with all parts of the path
following the splat occuring on each entry.
In the following code sample, the path will first enter the key "foo"
within the map.
It will then enter each item in the list, entering the "bar"
key for each nested object.
path = "foo.*.bar"
object = {"foo": [{"bar": 1}, {"bar": 2}]}