You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
tree: Allow array from map and map from array (#22036)
## Description
Allow constructing ArrayNodes from Maps and MapNodes from arrays when
unambiguous.
Also removes some no longer needed shallow copies in the map and array
constructors .
See changeset for details.
Allow constructing ArrayNodes from Maps and MapNodes from arrays when unambiguous.
7
+
8
+
Since the types for ArrayNodes and MapNodes indicate they can be constructed from iterables,
9
+
it should work, even if those iterables are themselves arrays or maps.
10
+
To avoid this being a breaking change, a priority system was introduced.
11
+
ArrayNodes will only be implicitly constructable from JavaScript Map objects in contexts where no MapNodes are allowed.
12
+
Similarly MapNodes will only be implicitly constructable from JavaScript Array objects in contexts where no ArrayNodes are allowed.
13
+
14
+
In practice, the main case in which this is likely to matter is when implicitly constructing a map node. If you provide an array of key value pairs, this now works instead of erroring, as long as no ArrayNode is valid at that location in the tree.
// current typing (of schema based constructors and thus implicit node construction)
560
+
// Typing (of schema based constructors and thus implicit node construction)
534
561
// allows iterables for constructing maps and arrays.
535
-
// Some current users of this API may have unions of maps and arrays,
562
+
// Some users of this API may have unions of maps and arrays,
536
563
// and rely on Arrays ending up as array nodes and maps as Map nodes,
537
564
// despite both being iterable and thus compatible with both.
538
-
// In the future, a better solution could be a priority based system where an array would be parsed as an array when unioned with a map,
565
+
// This uses a priority based system where an array would be parsed as an array when unioned with a map,
539
566
// but if in a map only context, could still be used as a map.
540
-
// Then this method would return a quality of fit, not just a boolean.
541
-
// For now, special case map and array before checking iterable to avoid regressing the union of map and array case.
542
567
543
568
if(datainstanceofMap){
544
-
returnschema.kind===NodeKind.Map;
569
+
switch(schema.kind){
570
+
caseNodeKind.Map:
571
+
returnCompatibilityLevel.Normal;
572
+
caseNodeKind.Array:
573
+
// Maps are iterable, so type checking does allow constructing an ArrayNode from a map if the array's type is an array that includes the key and value types of the map.
574
+
returnCompatibilityLevel.Low;
575
+
default:
576
+
returnCompatibilityLevel.None;
577
+
}
545
578
}
546
579
547
580
if(isReadonlyArray(data)){
548
-
returnschema.kind===NodeKind.Array;
581
+
switch(schema.kind){
582
+
caseNodeKind.Array:
583
+
returnCompatibilityLevel.Normal;
584
+
caseNodeKind.Map:
585
+
// Arrays are iterable, so type checking does allow constructing an array from a MapNode from an if the array's type is key values pairs for the map.
0 commit comments