| 1 |
# Dictionaries and Dict Functions |
| 2 |
|
| 3 |
Sprig provides a key/value storage type called a `dict` (short for "dictionary", |
| 4 |
as in Python). A `dict` is an _unorder_ type. |
| 5 |
|
| 6 |
The key to a dictionary **must be a string**. However, the value can be any |
| 7 |
type, even another `dict` or `list`. |
| 8 |
|
| 9 |
Unlike `list`s, `dict`s are not immutable. The `set` and `unset` functions will |
| 10 |
modify the contents of a dictionary. |
| 11 |
|
| 12 |
## dict |
| 13 |
|
| 14 |
Creating dictionaries is done by calling the `dict` function and passing it a |
| 15 |
list of pairs. |
| 16 |
|
| 17 |
The following creates a dictionary with three items: |
| 18 |
|
| 19 |
``` |
| 20 |
$myDict := dict "name1" "value1" "name2" "value2" "name3" "value 3" |
| 21 |
``` |
| 22 |
|
| 23 |
## get |
| 24 |
|
| 25 |
Given a map and a key, get the value from the map. |
| 26 |
|
| 27 |
``` |
| 28 |
get $myDict "key1" |
| 29 |
``` |
| 30 |
|
| 31 |
The above returns `"value1"` |
| 32 |
|
| 33 |
Note that if the key is not found, this operation will simply return `""`. No error |
| 34 |
will be generated. |
| 35 |
|
| 36 |
## set |
| 37 |
|
| 38 |
Use `set` to add a new key/value pair to a dictionary. |
| 39 |
|
| 40 |
``` |
| 41 |
$_ := set $myDict "name4" "value4" |
| 42 |
``` |
| 43 |
|
| 44 |
Note that `set` _returns the dictionary_ (a requirement of Go template functions), |
| 45 |
so you may need to trap the value as done above with the `$_` assignment. |
| 46 |
|
| 47 |
## unset |
| 48 |
|
| 49 |
Given a map and a key, delete the key from the map. |
| 50 |
|
| 51 |
``` |
| 52 |
$_ := unset $myDict "name4" |
| 53 |
``` |
| 54 |
|
| 55 |
As with `set`, this returns the dictionary. |
| 56 |
|
| 57 |
Note that if the key is not found, this operation will simply return. No error |
| 58 |
will be generated. |
| 59 |
|
| 60 |
## hasKey |
| 61 |
|
| 62 |
The `hasKey` function returns `true` if the given dict contains the given key. |
| 63 |
|
| 64 |
``` |
| 65 |
hasKey $myDict "name1" |
| 66 |
``` |
| 67 |
|
| 68 |
If the key is not found, this returns `false`. |
| 69 |
|
| 70 |
## pluck |
| 71 |
|
| 72 |
The `pluck` function makes it possible to give one key and multiple maps, and |
| 73 |
get a list of all of the matches: |
| 74 |
|
| 75 |
``` |
| 76 |
pluck "name1" $myDict $myOtherDict |
| 77 |
``` |
| 78 |
|
| 79 |
The above will return a `list` containing every found value (`[value1 otherValue1]`). |
| 80 |
|
| 81 |
If the give key is _not found_ in a map, that map will not have an item in the |
| 82 |
list (and the length of the returned list will be less than the number of dicts |
| 83 |
in the call to `pluck`. |
| 84 |
|
| 85 |
If the key is _found_ but the value is an empty value, that value will be |
| 86 |
inserted. |
| 87 |
|
| 88 |
A common idiom in Sprig templates is to uses `pluck... | first` to get the first |
| 89 |
matching key out of a collection of dictionaries. |
| 90 |
|
| 91 |
## dig |
| 92 |
|
| 93 |
The `dig` function traverses a nested set of dicts, selecting keys from a list |
| 94 |
of values. It returns a default value if any of the keys are not found at the |
| 95 |
associated dict. |
| 96 |
|
| 97 |
``` |
| 98 |
dig "user" "role" "humanName" "guest" $dict |
| 99 |
``` |
| 100 |
|
| 101 |
Given a dict structured like |
| 102 |
``` |
| 103 |
{ |
| 104 |
user: { |
| 105 |
role: { |
| 106 |
humanName: "curator" |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
``` |
| 111 |
|
| 112 |
the above would return `"curator"`. If the dict lacked even a `user` field, |
| 113 |
the result would be `"guest"`. |
| 114 |
|
| 115 |
Dig can be very useful in cases where you'd like to avoid guard clauses, |
| 116 |
especially since Go's template package's `and` doesn't shortcut. For instance |
| 117 |
`and a.maybeNil a.maybeNil.iNeedThis` will always evaluate |
| 118 |
`a.maybeNil.iNeedThis`, and panic if `a` lacks a `maybeNil` field.) |
| 119 |
|
| 120 |
`dig` accepts its dict argument last in order to support pipelining. For instance: |
| 121 |
``` |
| 122 |
merge a b c | dig "one" "two" "three" "<missing>" |
| 123 |
``` |
| 124 |
|
| 125 |
## merge, mustMerge |
| 126 |
|
| 127 |
Merge two or more dictionaries into one, giving precedence to the dest dictionary: |
| 128 |
|
| 129 |
``` |
| 130 |
$newdict := merge $dest $source1 $source2 |
| 131 |
``` |
| 132 |
|
| 133 |
This is a deep merge operation but not a deep copy operation. Nested objects that |
| 134 |
are merged are the same instance on both dicts. If you want a deep copy along |
| 135 |
with the merge than use the `deepCopy` function along with merging. For example, |
| 136 |
|
| 137 |
``` |
| 138 |
deepCopy $source | merge $dest |
| 139 |
``` |
| 140 |
|
| 141 |
`mustMerge` will return an error in case of unsuccessful merge. |
| 142 |
|
| 143 |
## mergeOverwrite, mustMergeOverwrite |
| 144 |
|
| 145 |
Merge two or more dictionaries into one, giving precedence from **right to left**, effectively |
| 146 |
overwriting values in the dest dictionary: |
| 147 |
|
| 148 |
Given: |
| 149 |
|
| 150 |
``` |
| 151 |
dst: |
| 152 |
default: default |
| 153 |
overwrite: me |
| 154 |
key: true |
| 155 |
|
| 156 |
src: |
| 157 |
overwrite: overwritten |
| 158 |
key: false |
| 159 |
``` |
| 160 |
|
| 161 |
will result in: |
| 162 |
|
| 163 |
``` |
| 164 |
newdict: |
| 165 |
default: default |
| 166 |
overwrite: overwritten |
| 167 |
key: false |
| 168 |
``` |
| 169 |
|
| 170 |
``` |
| 171 |
$newdict := mergeOverwrite $dest $source1 $source2 |
| 172 |
``` |
| 173 |
|
| 174 |
This is a deep merge operation but not a deep copy operation. Nested objects that |
| 175 |
are merged are the same instance on both dicts. If you want a deep copy along |
| 176 |
with the merge than use the `deepCopy` function along with merging. For example, |
| 177 |
|
| 178 |
``` |
| 179 |
deepCopy $source | mergeOverwrite $dest |
| 180 |
``` |
| 181 |
|
| 182 |
`mustMergeOverwrite` will return an error in case of unsuccessful merge. |
| 183 |
|
| 184 |
## keys |
| 185 |
|
| 186 |
The `keys` function will return a `list` of all of the keys in one or more `dict` |
| 187 |
types. Since a dictionary is _unordered_, the keys will not be in a predictable order. |
| 188 |
They can be sorted with `sortAlpha`. |
| 189 |
|
| 190 |
``` |
| 191 |
keys $myDict | sortAlpha |
| 192 |
``` |
| 193 |
|
| 194 |
When supplying multiple dictionaries, the keys will be concatenated. Use the `uniq` |
| 195 |
function along with `sortAlpha` to get a unqiue, sorted list of keys. |
| 196 |
|
| 197 |
``` |
| 198 |
keys $myDict $myOtherDict | uniq | sortAlpha |
| 199 |
``` |
| 200 |
|
| 201 |
## pick |
| 202 |
|
| 203 |
The `pick` function selects just the given keys out of a dictionary, creating a |
| 204 |
new `dict`. |
| 205 |
|
| 206 |
``` |
| 207 |
$new := pick $myDict "name1" "name2" |
| 208 |
``` |
| 209 |
|
| 210 |
The above returns `{name1: value1, name2: value2}` |
| 211 |
|
| 212 |
## omit |
| 213 |
|
| 214 |
The `omit` function is similar to `pick`, except it returns a new `dict` with all |
| 215 |
the keys that _do not_ match the given keys. |
| 216 |
|
| 217 |
``` |
| 218 |
$new := omit $myDict "name1" "name3" |
| 219 |
``` |
| 220 |
|
| 221 |
The above returns `{name2: value2}` |
| 222 |
|
| 223 |
## values |
| 224 |
|
| 225 |
The `values` function is similar to `keys`, except it returns a new `list` with |
| 226 |
all the values of the source `dict` (only one dictionary is supported). |
| 227 |
|
| 228 |
``` |
| 229 |
$vals := values $myDict |
| 230 |
``` |
| 231 |
|
| 232 |
The above returns `list["value1", "value2", "value 3"]`. Note that the `values` |
| 233 |
function gives no guarantees about the result ordering- if you care about this, |
| 234 |
then use `sortAlpha`. |
| 235 |
|
| 236 |
## deepCopy, mustDeepCopy |
| 237 |
|
| 238 |
The `deepCopy` and `mustDeepCopy` functions takes a value and makes a deep copy |
| 239 |
of the value. This includes dicts and other structures. `deepCopy` panics |
| 240 |
when there is a problem while `mustDeepCopy` returns an error to the template |
| 241 |
system when there is an error. |
| 242 |
|
| 243 |
``` |
| 244 |
dict "a" 1 "b" 2 | deepCopy |
| 245 |
``` |
| 246 |
|
| 247 |
## A Note on Dict Internals |
| 248 |
|
| 249 |
A `dict` is implemented in Go as a `map[string]interface{}`. Go developers can |
| 250 |
pass `map[string]interface{}` values into the context to make them available |
| 251 |
to templates as `dict`s. |