1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
//! All types related to finding a [`Kind`] nested into another one.
use crate::path::{BorrowedSegment, ValuePath};
use crate::value::Kind;
use std::borrow::Cow;
impl Kind {
/// Returns the type of a value that is retrieved from a certain path.
///
/// This has the same behavior as `Value::get`, including
/// the implicit conversion of "undefined" to "null.
///
/// If you want the type _without_ the implicit type conversion,
/// use `Kind::at_path` instead.
#[must_use]
#[allow(clippy::needless_pass_by_value)] // only references are implemented for `Path`
pub fn get<'a>(&self, path: impl ValuePath<'a>) -> Self {
self.at_path(path).upgrade_undefined()
}
/// This retrieves the `Kind` at a given path. There is a subtle difference
/// between this and `Kind::get` where this function does _not_ convert undefined to null.
/// It is viewing the type of a value in-place, before it is retrieved.
#[must_use]
#[allow(clippy::needless_pass_by_value)] // only references are implemented for `Path`
pub fn at_path<'a>(&self, path: impl ValuePath<'a>) -> Self {
self.get_recursive(path.segment_iter())
}
fn get_field(&self, field: Cow<'_, str>) -> Self {
self.as_object().map_or_else(Self::undefined, |object| {
let mut kind = object
.known()
.get(&field.into_owned().into())
.cloned()
.unwrap_or_else(|| object.unknown_kind());
if !self.is_exact() {
kind = kind.or_undefined();
}
kind
})
}
fn get_recursive<'a>(
&self,
mut iter: impl Iterator<Item = BorrowedSegment<'a>> + Clone,
) -> Self {
if self.is_never() {
// a terminating expression by definition can "never" resolve to a value
return Self::never();
}
match iter.next() {
Some(BorrowedSegment::Field(field)) => self.get_field(field).get_recursive(iter),
Some(BorrowedSegment::Index(mut index)) => {
if let Some(array) = self.as_array() {
if index < 0 {
let largest_known_index = array.known().keys().map(|i| i.to_usize()).max();
// The minimum size of the resulting array.
let len_required = -index as usize;
if array.unknown_kind().contains_any_defined() {
// The exact length is not known. We can't know for sure if the index
// will point to a known or unknown type, so the union of the unknown type
// plus any possible known type must be taken. Just the unknown type alone is not sufficient.
// The array may be larger, but this is the largest we can prove the array is from the type information.
let min_length = largest_known_index.map_or(0, |i| i + 1);
// We can prove the positive index won't be less than "min_index".
let min_index = (min_length as isize + index).max(0) as usize;
let can_underflow = (min_length as isize + index) < 0;
let mut kind = array.unknown_kind();
// We can prove the index won't underflow, so it cannot be "undefined".
// But only if the type can only be an array.
if self.is_exact() && !can_underflow {
kind.remove_undefined();
}
for (i, i_kind) in array.known() {
if i.to_usize() >= min_index {
kind.merge_keep(i_kind.clone(), false);
}
}
return kind.get_recursive(iter);
}
// There are no unknown indices, so we can determine the exact positive index.
let exact_len = largest_known_index.map_or(0, |x| x + 1);
if exact_len >= len_required {
// Make the index positive, then continue below.
index += exact_len as isize;
} else {
// Out of bounds index.
return Self::undefined();
}
}
debug_assert!(index >= 0, "negative indices already handled");
let index = index as usize;
let mut kind = array
.known()
.get(&index.into())
.cloned()
.unwrap_or_else(|| array.unknown_kind());
if !self.is_exact() {
kind = kind.or_undefined();
}
kind.get_recursive(iter)
} else {
Self::undefined()
}
}
Some(BorrowedSegment::Invalid) => {
// Value::get returns `None` in this case, which means the value is not defined.
Self::undefined()
}
None => self.clone(),
}
}
}
#[cfg(test)]
mod tests {
use crate::owned_value_path;
use crate::path::OwnedValuePath;
use std::collections::BTreeMap;
use super::*;
use crate::value::kind::Collection;
#[test]
#[allow(clippy::too_many_lines)]
fn test_at_path() {
struct TestCase {
kind: Kind,
path: OwnedValuePath,
want: Kind,
}
for (title, TestCase { kind, path, want }) in [
(
"get root",
TestCase {
kind: Kind::bytes(),
path: owned_value_path!(),
want: Kind::bytes(),
},
),
(
"get field from non-object",
TestCase {
kind: Kind::bytes(),
path: owned_value_path!("foo"),
want: Kind::undefined(),
},
),
(
"get field from object",
TestCase {
kind: Kind::object(BTreeMap::from([("a".into(), Kind::integer())])),
path: owned_value_path!("a"),
want: Kind::integer(),
},
),
(
"get field from maybe an object",
TestCase {
kind: Kind::object(BTreeMap::from([("a".into(), Kind::integer())])).or_null(),
path: owned_value_path!("a"),
want: Kind::integer().or_undefined(),
},
),
(
"get unknown from object with no unknown",
TestCase {
kind: Kind::object(BTreeMap::from([("a".into(), Kind::integer())])),
path: owned_value_path!("b"),
want: Kind::undefined(),
},
),
(
"get unknown from object with unknown",
TestCase {
kind: Kind::object(
Collection::from(BTreeMap::from([("a".into(), Kind::integer())]))
.with_unknown(Kind::bytes()),
),
path: owned_value_path!("b"),
want: Kind::bytes().or_undefined(),
},
),
(
"get unknown from object with null unknown",
TestCase {
kind: Kind::object(
Collection::from(BTreeMap::from([("a".into(), Kind::integer())]))
.with_unknown(Kind::null()),
),
path: owned_value_path!("b"),
want: Kind::null().or_undefined(),
},
),
(
"get nested field",
TestCase {
kind: Kind::object(
Collection::from(BTreeMap::from([(
"a".into(),
Kind::object(
Collection::from(BTreeMap::from([("b".into(), Kind::integer())]))
.with_unknown(Kind::null()),
),
)]))
.with_unknown(Kind::null()),
),
path: owned_value_path!("a", "b"),
want: Kind::integer(),
},
),
(
"get index from non-array",
TestCase {
kind: Kind::bytes(),
path: owned_value_path!(1),
want: Kind::undefined(),
},
),
(
"get index from array",
TestCase {
kind: Kind::array(BTreeMap::from([(0.into(), Kind::integer())])),
path: owned_value_path!(0),
want: Kind::integer(),
},
),
(
"get index from maybe array",
TestCase {
kind: Kind::array(BTreeMap::from([(0.into(), Kind::integer())])).or_bytes(),
path: owned_value_path!(0),
want: Kind::integer().or_undefined(),
},
),
(
"get unknown from array with no unknown",
TestCase {
kind: Kind::array(BTreeMap::from([(0.into(), Kind::integer())])),
path: owned_value_path!(1),
want: Kind::undefined(),
},
),
(
"get unknown from array with unknown",
TestCase {
kind: Kind::array(
Collection::from(BTreeMap::from([(0.into(), Kind::integer())]))
.with_unknown(Kind::bytes()),
),
path: owned_value_path!(1),
want: Kind::bytes().or_undefined(),
},
),
(
"get unknown from array with null unknown",
TestCase {
kind: Kind::array(
Collection::from(BTreeMap::from([(0.into(), Kind::integer())]))
.with_unknown(Kind::null()),
),
path: owned_value_path!(1),
want: Kind::null().or_undefined(),
},
),
(
"get nested index",
TestCase {
kind: Kind::array(
Collection::from(BTreeMap::from([(
0.into(),
Kind::array(
Collection::from(BTreeMap::from([(0.into(), Kind::integer())]))
.with_unknown(Kind::null()),
),
)]))
.with_unknown(Kind::null()),
),
path: owned_value_path!(0, 0),
want: Kind::integer(),
},
),
(
"out of bounds negative index",
TestCase {
kind: Kind::array(Collection::from(BTreeMap::from([(
0.into(),
Kind::integer(),
)]))),
path: owned_value_path!(-2),
want: Kind::undefined(),
},
),
(
"negative index no unknown",
TestCase {
kind: Kind::array(Collection::from(BTreeMap::from([(
0.into(),
Kind::integer(),
)]))),
path: owned_value_path!(-1),
want: Kind::integer(),
},
),
(
"negative index with unknown can't underflow",
TestCase {
kind: Kind::array(
Collection::from(BTreeMap::from([
(0.into(), Kind::integer()),
(1.into(), Kind::bytes()),
(2.into(), Kind::float()),
]))
.with_unknown(Kind::boolean()),
),
path: owned_value_path!(-2),
want: Kind::boolean().or_bytes().or_float(),
},
),
(
"negative index with unknown can't underflow (maybe array)",
TestCase {
kind: Kind::array(
Collection::from(BTreeMap::from([
(0.into(), Kind::integer()),
(1.into(), Kind::bytes()),
(2.into(), Kind::float()),
]))
.with_unknown(Kind::boolean()),
)
.or_null(),
path: owned_value_path!(-2),
want: Kind::boolean().or_bytes().or_float().or_undefined(),
},
),
(
"negative index with unknown can underflow",
TestCase {
kind: Kind::array(
Collection::from(BTreeMap::from([(0.into(), Kind::integer())]))
.with_unknown(Kind::boolean()),
),
path: owned_value_path!(-2),
want: Kind::boolean().or_integer().or_undefined(),
},
),
(
"negative index nested",
TestCase {
kind: Kind::array(
Collection::from(BTreeMap::from([(0.into(), Kind::integer())]))
.with_unknown(Kind::object(BTreeMap::from([(
"foo".into(),
Kind::bytes(),
)]))),
),
path: owned_value_path!(-2, "foo"),
want: Kind::bytes().or_undefined(),
},
),
(
"nested terminating expression",
TestCase {
kind: Kind::never(),
path: owned_value_path!(".foo.bar"),
want: Kind::never(),
},
),
] {
assert_eq!(kind.at_path(&path), want, "test: {title}");
}
}
}