-
-
Notifications
You must be signed in to change notification settings - Fork 626
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cache): handle full blown cyclical pointers in normalization
- Loading branch information
Showing
4 changed files
with
360 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,62 @@ | ||
import 'dart:collection'; | ||
|
||
typedef Transform = Object Function(Object node); | ||
typedef SideEffect = void Function( | ||
Object transformResult, | ||
Object node, | ||
Traversal traversal, | ||
); | ||
|
||
Map<String, Object> traverseValues( | ||
Map<String, Object> node, | ||
Transform transform, | ||
) { | ||
return node.map<String, Object>( | ||
(String key, Object value) => MapEntry<String, Object>( | ||
key, | ||
traverse(value, transform), | ||
), | ||
); | ||
} | ||
class Traversal { | ||
Traversal( | ||
this.transform, { | ||
this.transformSideEffect, | ||
this.seenObjects, | ||
}) { | ||
seenObjects ??= HashSet<int>(); | ||
} | ||
|
||
Transform transform; | ||
|
||
// Attempts to apply the transform to every leaf of the data structure recursively. | ||
// Stops recursing when a node is transformed (returns non-null) | ||
Object traverse(Object node, Transform transform) { | ||
final Object transformed = transform(node); | ||
if (transformed != null) { | ||
return transformed; | ||
/// An optional side effect to call when a node is transformed. | ||
SideEffect transformSideEffect; | ||
HashSet<int> seenObjects; | ||
|
||
bool hasAlreadySeen(Object node) { | ||
final bool wasAdded = seenObjects.add(node.hashCode); | ||
return !wasAdded; | ||
} | ||
|
||
if (node is List<Object>) { | ||
return node | ||
.map<Object>((Object node) => traverse(node, transform)) | ||
.toList(); | ||
/// Traverse only the values of the given map | ||
Map<String, Object> traverseValues(Map<String, Object> node) { | ||
return node.map<String, Object>( | ||
(String key, Object value) => MapEntry<String, Object>( | ||
key, | ||
traverse(value), | ||
), | ||
); | ||
} | ||
if (node is Map<String, Object>) { | ||
return traverseValues(node, transform); | ||
|
||
// Attempts to apply the transform to every leaf of the data structure recursively. | ||
// Stops recursing when a node is transformed (returns non-null) | ||
Object traverse(Object node) { | ||
final Object transformed = transform(node); | ||
if (hasAlreadySeen(node)) { | ||
return transformed ?? node; | ||
} | ||
if (transformed != null) { | ||
if (transformSideEffect != null) { | ||
transformSideEffect(transformed, node, this); | ||
} | ||
return transformed; | ||
} | ||
|
||
if (node is List<Object>) { | ||
return node.map<Object>((Object node) => traverse(node)).toList(); | ||
} | ||
if (node is Map<String, Object>) { | ||
return traverseValues(node); | ||
} | ||
return node; | ||
} | ||
return node; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.