Skip to content

Commit 7bd5a8e

Browse files
committed
add some helper methods for core collections to simplify the transformer
1 parent 0ff552e commit 7bd5a8e

File tree

3 files changed

+431
-9
lines changed

3 files changed

+431
-9
lines changed

lib/json_auto_decode.dart

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,32 @@
11
import 'dart:collection';
2+
import 'dart:typed_data';
23

34
T jsonAutoDecode<T>(String encoded) =>
45
throw UnsupportedError('We needed a static type!');
56

7+
T jsonAutoDecodeFromBytes<T>(Uint8List bytes) =>
8+
throw UnsupportedError('We needed a static type!');
9+
10+
Iterable<T> convertIterable<S, T>(Iterable<S> json, T Function(S) converter,
11+
{Iterable<T> defaultValue}) =>
12+
json?.map((v) => converter(v)) ?? defaultValue;
13+
14+
List<T> convertList<S, T>(Iterable<S> json, T Function(S) converter,
15+
{List<T> defaultValue}) {
16+
if (json == null) return null;
17+
return [for (var item in json) converter(item)];
18+
}
19+
20+
Map<K2, V2> convertMap<K1, V1, K2, V2>(Map<K1, V1> json,
21+
K2 Function(K1) keyConverter, V2 Function(V1) valueConverter,
22+
{Map<K2, V2> defaultValue}) {
23+
if (json == null) return null;
24+
return {
25+
for (var entry in json.entries)
26+
keyConverter(entry.key): valueConverter(entry.value)
27+
};
28+
}
29+
630
/// Lazily converts one list into another list based on a conversion function.
731
///
832
/// Destructive on the original list for efficiency reasons.
@@ -52,12 +76,16 @@ class LazyList<T> extends ListBase<T> {
5276
}
5377
}
5478

55-
/// Lazily converts one map into another map based on a conversion function.
79+
/// Lazily converts the values of one map into another map based on a
80+
/// conversion function.
81+
///
82+
/// This does not support converting the keys because that would have to be
83+
/// eager.
5684
///
5785
/// Destructive on the original map for efficiency reasons.
58-
class LazyMap<V> extends MapBase<String, V> {
59-
final Map<String, dynamic> _original;
60-
final _converted = <String, V>{};
86+
class LazyMap<K, V> extends MapBase<K, V> {
87+
final Map<K, dynamic> _original;
88+
final _converted = <K, V>{};
6189
final V Function(dynamic) _converter;
6290

6391
LazyMap(this._original, this._converter);
@@ -69,13 +97,13 @@ class LazyMap<V> extends MapBase<String, V> {
6997
}
7098
if (_original.containsKey(key)) {
7199
var original = _original.remove(key);
72-
return _converted[key as String] = _converter(original);
100+
return _converted[key as K] = _converter(original);
73101
}
74102
return null;
75103
}
76104

77105
@override
78-
void operator []=(String key, V value) {
106+
void operator []=(K key, V value) {
79107
_converted[key] = value;
80108
_original.remove(key);
81109
}
@@ -87,7 +115,7 @@ class LazyMap<V> extends MapBase<String, V> {
87115
}
88116

89117
@override
90-
Iterable<String> get keys => _original.keys.followedBy(_converted.keys);
118+
Iterable<K> get keys => _original.keys.followedBy(_converted.keys);
91119

92120
@override
93121
V remove(Object key) {

0 commit comments

Comments
 (0)