@@ -163,24 +163,21 @@ pub fn (a Any) default_to(value Any) Any {
163163}
164164
165165// value queries a value from the map.
166- // `key` should be in "dotted" form (`a.b.c`).
167- // `key` supports quoted keys like `a."b.c"`.
166+ // `key` supports a small query syntax scheme:
167+ // Maps can be queried in "dotted" form e.g. `a.b.c`.
168+ // quoted keys are supported as `a."b.c"` or `a.'b.c'`.
169+ // Arrays can be queried with `a[0].b[1].[2]`.
168170pub fn (m map[string]Any) value (key string ) Any {
169- key_split := parse_dotted_key (key) or { return Any (Null{}) }
170- return m.value_ (key_split)
171+ return Any (m).value (key)
171172}
172173
173- fn (m map[string]Any) value_ (key []string ) Any {
174- value := m[key[0 ]] or { return Any (Null{}) }
175- // `match` isn't currently very suitable for these types of sum type constructs...
176- if value is map [string ]Any {
177- if key.len < = 1 {
178- return value
179- }
180- nm := (value as map [string ]Any)
181- return nm.value_ (key[1 ..])
182- }
183- return value
174+ // value queries a value from the array.
175+ // `key` supports a small query syntax scheme:
176+ // The array can be queried with `[0].b[1].[2]`.
177+ // Maps can be queried in "dotted" form e.g. `a.b.c`.
178+ // quoted keys are supported as `a."b.c"` or `a.'b.c'`.
179+ pub fn (a []Any) value (key string ) Any {
180+ return Any (a).value (key)
184181}
185182
186183pub fn (a []Any) as_strings () []string {
@@ -190,3 +187,42 @@ pub fn (a []Any) as_strings() []string {
190187 }
191188 return sa
192189}
190+
191+ // value queries a value from the `Any` type.
192+ // `key` supports a small query syntax scheme:
193+ // Maps can be queried in "dotted" form e.g. `a.b.c`.
194+ // quoted keys are supported as `a."b.c"` or `a.'b.c'`.
195+ // Arrays can be queried with `a[0].b[1].[2]`.
196+ pub fn (a Any) value (key string ) Any {
197+ key_split := parse_dotted_key (key) or { return Any (Null{}) }
198+ return a.value_ (a, key_split)
199+ }
200+
201+ // value_ returns the `Any` value found at `key`.
202+ fn (a Any) value_ (value Any, key []string ) Any {
203+ assert key.len > 0
204+ mut any_value := Any (Null{})
205+ k , index := parse_array_key (key[0 ])
206+ if k == '' {
207+ arr := value as []Any
208+ any_value = arr[index] or { return Any (Null{}) }
209+ }
210+ if value is map [string ]Any {
211+ any_value = value[k] or { return Any (Null{}) }
212+ if index > - 1 {
213+ arr := any_value as []Any
214+ any_value = arr[index] or { return Any (Null{}) }
215+ }
216+ }
217+ if key.len < = 1 {
218+ return any_value
219+ }
220+ match any_value {
221+ map [string ]Any, []Any {
222+ return a.value_ (any_value, key[1 ..])
223+ }
224+ else {
225+ return value
226+ }
227+ }
228+ }
0 commit comments