1
1
import 'dart:collection' ;
2
- import 'package:codable /src/codable.dart' ;
3
- import 'package:codable /src/coding.dart' ;
4
- import 'package:codable /cast.dart' as cast;
5
- import 'package:codable /src/list.dart' ;
6
- import 'package:codable /src/resolver.dart' ;
2
+ import 'package:conduit_codable /src/codable.dart' ;
3
+ import 'package:conduit_codable /src/coding.dart' ;
4
+ import 'package:conduit_codable /cast.dart' as cast;
5
+ import 'package:conduit_codable /src/list.dart' ;
6
+ import 'package:conduit_codable /src/resolver.dart' ;
7
7
8
8
/// A container for a dynamic data object that can be decoded into [Coding] objects.
9
9
///
@@ -36,10 +36,10 @@ class KeyedArchive extends Object
36
36
/// If [allowReferences] is true, JSON Schema references will be traversed and decoded objects
37
37
/// will contain values from the referenced object. This flag defaults to false.
38
38
static KeyedArchive unarchive (Map <String , dynamic > data,
39
- {bool allowReferences: false }) {
40
- final archive = new KeyedArchive (data);
39
+ {bool allowReferences = false }) {
40
+ final archive = KeyedArchive (data);
41
41
if (allowReferences) {
42
- archive.resolveOrThrow (new ReferenceResolver (archive));
42
+ archive.resolveOrThrow (ReferenceResolver (archive));
43
43
}
44
44
return archive;
45
45
}
@@ -52,17 +52,17 @@ class KeyedArchive extends Object
52
52
///
53
53
/// If [allowReferences] is true, JSON Schema references in the emitted document will be validated.
54
54
/// Defaults to false.
55
- static Map <String , dynamic > archive (Coding root,
56
- {bool allowReferences: false }) {
57
- final archive = new KeyedArchive ({});
55
+ static Map <String ? , dynamic > archive (Coding root,
56
+ {bool allowReferences = false }) {
57
+ final archive = KeyedArchive ({});
58
58
root.encode (archive);
59
59
if (allowReferences) {
60
- archive.resolveOrThrow (new ReferenceResolver (archive));
60
+ archive.resolveOrThrow (ReferenceResolver (archive));
61
61
}
62
62
return archive.toPrimitive ();
63
63
}
64
64
65
- KeyedArchive ._empty ();
65
+ KeyedArchive ._empty () : _map = Map < String , dynamic >() ;
66
66
67
67
/// Use [unarchive] instead.
68
68
KeyedArchive (this ._map) {
@@ -86,7 +86,7 @@ class KeyedArchive extends Object
86
86
///
87
87
Uri ? referenceURI;
88
88
89
- late Map <String , dynamic > _map;
89
+ Map <String , dynamic > _map;
90
90
Coding ? _inflated;
91
91
KeyedArchive ? _objectReference;
92
92
@@ -118,7 +118,7 @@ class KeyedArchive extends Object
118
118
return ;
119
119
}
120
120
121
- final caster = new cast.Keyed (schema);
121
+ final caster = cast.Keyed (schema);
122
122
_map = caster.cast (_map);
123
123
124
124
if (_objectReference != null ) {
@@ -127,20 +127,20 @@ class KeyedArchive extends Object
127
127
}
128
128
}
129
129
130
- operator []= (String key, dynamic value) {
130
+ operator []= (String key, dynamic ? value) {
131
131
_map[key] = value;
132
132
}
133
133
134
- dynamic operator [](Object ? key) => _getValue (key as String ? );
134
+ dynamic ? operator [](Object ? key) => _getValue (key as String );
135
135
136
136
Iterable <String > get keys => _map.keys;
137
137
138
138
void clear () => _map.clear ();
139
139
140
140
dynamic remove (Object ? key) => _map.remove (key);
141
141
142
- Map <String , dynamic > toPrimitive () {
143
- final out = < String , dynamic > {};
142
+ Map <String ? , dynamic > toPrimitive () {
143
+ final out = < String ? , dynamic > {};
144
144
_map.forEach ((key, val) {
145
145
if (val is KeyedArchive ) {
146
146
out[key] = val.toPrimitive ();
@@ -167,12 +167,12 @@ class KeyedArchive extends Object
167
167
keys.forEach ((key) {
168
168
final val = _map[key];
169
169
if (val is Map ) {
170
- _map[key] = new KeyedArchive (caster.cast (val));
170
+ _map[key] = KeyedArchive (caster.cast (val));
171
171
} else if (val is List ) {
172
- _map[key] = new ListArchive .from (val);
172
+ _map[key] = ListArchive .from (val);
173
173
} else if (key == r"$ref" ) {
174
174
if (val is Map ) {
175
- _objectReference = val as KeyedArchive ;
175
+ _objectReference = val as KeyedArchive ? ;
176
176
} else {
177
177
referenceURI = Uri .parse (Uri .parse (val).fragment);
178
178
}
@@ -188,7 +188,7 @@ class KeyedArchive extends Object
188
188
if (referenceURI != null ) {
189
189
_objectReference = coder.resolve (referenceURI! );
190
190
if (_objectReference == null ) {
191
- throw new ArgumentError (
191
+ throw ArgumentError (
192
192
"Invalid document. Reference '#${referenceURI !.path }' does not exist in document." );
193
193
}
194
194
}
@@ -204,19 +204,17 @@ class KeyedArchive extends Object
204
204
205
205
/* decode */
206
206
207
- T ? _decodedObject <T extends Coding >(KeyedArchive ? raw, T inflate ()) {
207
+ T ? _decodedObject <T extends Coding ? >(KeyedArchive ? raw, T inflate ()) {
208
208
if (raw == null ) {
209
209
return null ;
210
210
}
211
211
212
212
if (raw._inflated == null ) {
213
213
raw._inflated = inflate ();
214
- if (raw._inflated != null ) {
215
- raw._inflated! .decode (raw);
216
- }
214
+ raw._inflated! .decode (raw);
217
215
}
218
216
219
- return raw._inflated as T ;
217
+ return raw._inflated as T ? ;
220
218
}
221
219
222
220
/// Returns the object associated by [key] .
@@ -228,7 +226,7 @@ class KeyedArchive extends Object
228
226
/// If this object is a reference to another object (via [referenceURI] ), this object's key-value
229
227
/// pairs will be searched first. If [key] is not found, the referenced object's key-values pairs are searched.
230
228
/// If no match is found, null is returned.
231
- T ? decode <T >(String ? key) {
229
+ T ? decode <T >(String key) {
232
230
var v = _getValue (key);
233
231
if (v == null ) {
234
232
return null ;
@@ -248,14 +246,14 @@ class KeyedArchive extends Object
248
246
/// [inflate] must create an empty instance of [T] . The value associated with [key]
249
247
/// must be a [KeyedArchive] (a [Map] ). The values of the associated object are read into
250
248
/// the empty instance of [T] .
251
- T ? decodeObject <T extends Coding >(String ? key, T inflate ()) {
249
+ T ? decodeObject <T extends Coding >(String key, T inflate ()) {
252
250
final val = _getValue (key);
253
251
if (val == null ) {
254
252
return null ;
255
253
}
256
254
257
255
if (val is ! KeyedArchive ) {
258
- throw new ArgumentError (
256
+ throw ArgumentError (
259
257
"Cannot decode key '$key ' into '$T ', because the value is not a Map. Actual value: '$val '." );
260
258
}
261
259
@@ -268,17 +266,17 @@ class KeyedArchive extends Object
268
266
/// must be a [ListArchive] (a [List] of [Map] ). For each element of the archived list,
269
267
/// [inflate] is invoked and each object in the archived list is decoded into
270
268
/// the instance of [T] .
271
- List <T ?>? decodeObjects <T extends Coding >(String key, T inflate ()) {
269
+ List <T ?>? decodeObjects <T extends Coding >(String key, T ? inflate ()) {
272
270
var val = _getValue (key);
273
271
if (val == null ) {
274
272
return null ;
275
273
}
276
274
if (val is ! List ) {
277
- throw new ArgumentError (
275
+ throw ArgumentError (
278
276
"Cannot decode key '$key ' as 'List<$T >', because value is not a List. Actual value: '$val '." );
279
277
}
280
278
281
- return val.map ((v) => _decodedObject (v, inflate)).toList ();
279
+ return val.map ((v) => _decodedObject (v, inflate)).toList (). cast < T ?>() ;
282
280
}
283
281
284
282
/// Returns a map of [T] s associated with [key] .
@@ -287,14 +285,14 @@ class KeyedArchive extends Object
287
285
/// must be a [KeyedArchive] (a [Map] ), where each value is a [T] .
288
286
/// For each key-value pair of the archived map, [inflate] is invoked and
289
287
/// each value is decoded into the instance of [T] .
290
- Map <String , T ?>? decodeObjectMap <T extends Coding >(String ? key, T inflate ()) {
288
+ Map <String , T ?>? decodeObjectMap <T extends Coding >(String key, T inflate ()) {
291
289
var v = _getValue (key);
292
290
if (v == null ) {
293
291
return null ;
294
292
}
295
293
296
294
if (v is ! Map <String , dynamic >) {
297
- throw new ArgumentError (
295
+ throw ArgumentError (
298
296
"Cannot decode key '$key ' as 'Map<String, $T >', because value is not a Map. Actual value: '$v '." );
299
297
}
300
298
@@ -304,7 +302,7 @@ class KeyedArchive extends Object
304
302
305
303
/* encode */
306
304
307
- Map <String , dynamic >? _encodedObject (Coding ? object) {
305
+ Map <String ? , dynamic >? _encodedObject (Coding ? object) {
308
306
if (object == null ) {
309
307
return null ;
310
308
}
@@ -314,7 +312,7 @@ class KeyedArchive extends Object
314
312
// they are currently not being emitted. the solution is probably tricky.
315
313
// letting encode run as normal would stack overflow when there is a cyclic
316
314
// reference between this object and another.
317
- var json = new KeyedArchive ._empty ()
315
+ var json = KeyedArchive ._empty ()
318
316
.._map = {}
319
317
..referenceURI = object.referenceURI;
320
318
if (json.referenceURI != null ) {
@@ -367,15 +365,14 @@ class KeyedArchive extends Object
367
365
return ;
368
366
}
369
367
370
- _map[key] =
371
- new ListArchive .from (value.map ((v) => _encodedObject (v)).toList ());
368
+ _map[key] = ListArchive .from (value.map ((v) => _encodedObject (v)).toList ());
372
369
}
373
370
374
371
/// Encodes map of [Coding] objects into this object for [key] .
375
372
///
376
373
/// This invokes [Coding.encode] on each value in [value] and adds the map of objects
377
374
/// to this archive for the key [key] .
378
- void encodeObjectMap <T extends Coding >(String key, Map <String , T ? >? value) {
375
+ void encodeObjectMap <T extends Coding ? >(String key, Map <String , T >? value) {
379
376
if (value == null ) {
380
377
return ;
381
378
}
0 commit comments