Skip to content

Commit fb9b98a

Browse files
authored
Nnbd migration (#1)
* formatted code and replaced deprecated list method. * null safety migration. All unit tests are passing. Updated verson no. to 2.0.0 to reflect breaking change (nnbd) and changed name to conduit-codable as codable can no longer be used. * renamed to conduit_codable as we can't publish as codable * Removed unused !
1 parent ede7b25 commit fb9b98a

10 files changed

+142
-136
lines changed

lib/cast.dart

+22-19
Original file line numberDiff line numberDiff line change
@@ -60,31 +60,31 @@ class IntCast extends Cast<core.int> {
6060
core.int _cast(dynamic from, core.String context, dynamic key) =>
6161
from is core.int
6262
? from
63-
: throw new FailedCast(context, key, "$from is not an int");
63+
: throw FailedCast(context, key, "$from is not an int");
6464
}
6565

6666
class DoubleCast extends Cast<core.double> {
6767
const DoubleCast();
6868
core.double _cast(dynamic from, core.String context, dynamic key) =>
6969
from is core.double
7070
? from
71-
: throw new FailedCast(context, key, "$from is not an double");
71+
: throw FailedCast(context, key, "$from is not an double");
7272
}
7373

7474
class StringCast extends Cast<core.String> {
7575
const StringCast();
7676
core.String _cast(dynamic from, core.String context, dynamic key) =>
7777
from is core.String
7878
? from
79-
: throw new FailedCast(context, key, "$from is not a String");
79+
: throw FailedCast(context, key, "$from is not a String");
8080
}
8181

8282
class BoolCast extends Cast<core.bool> {
8383
const BoolCast();
8484
core.bool _cast(dynamic from, core.String context, dynamic key) =>
8585
from is core.bool
8686
? from
87-
: throw new FailedCast(context, key, "$from is not a bool");
87+
: throw FailedCast(context, key, "$from is not a bool");
8888
}
8989

9090
class Map<K, V> extends Cast<core.Map<K, V>> {
@@ -102,7 +102,7 @@ class Map<K, V> extends Cast<core.Map<K, V>> {
102102
}
103103
return result;
104104
}
105-
return throw new FailedCast(context, key, "not a map");
105+
return throw FailedCast(context, key, "not a map");
106106
}
107107
}
108108

@@ -113,12 +113,12 @@ class StringMap<V> extends Cast<core.Map<core.String, V>> {
113113
dynamic from, core.String context, dynamic key) {
114114
if (from is core.Map) {
115115
var result = <core.String, V>{};
116-
for (core.String key in from.keys) {
116+
for (core.String key in from.keys as core.Iterable<core.String>) {
117117
result[key] = _value._cast(from[key], "map entry", key);
118118
}
119119
return result;
120120
}
121-
return throw new FailedCast(context, key, "not a map");
121+
return throw FailedCast(context, key, "not a map");
122122
}
123123
}
124124

@@ -128,25 +128,28 @@ class List<E> extends Cast<core.List<E?>> {
128128
core.List<E?> _cast(dynamic from, core.String context, dynamic key) {
129129
if (from is core.List) {
130130
var length = from.length;
131-
var result = core.List<E?>.generate(
132-
length,
133-
(i) =>
134-
from[i] != null ? _entry._cast(from[i], "list entry", i) : null);
135-
131+
var result = core.List<E?>.filled(length, null);
132+
for (core.int i = 0; i < length; ++i) {
133+
if (from[i] != null) {
134+
result[i] = _entry._cast(from[i], "list entry", i);
135+
} else {
136+
result[i] = null;
137+
}
138+
}
136139
return result;
137140
}
138-
return throw new FailedCast(context, key, "not a list");
141+
return throw FailedCast(context, key, "not a list");
139142
}
140143
}
141144

142-
class Keyed<K, V> extends Cast<core.Map<K, V>> {
145+
class Keyed<K, V> extends Cast<core.Map<K, V?>> {
143146
Iterable<K> get keys => _map.keys;
144147
final core.Map<K, Cast<V>> _map;
145148
const Keyed(core.Map<K, Cast<V>> map) : _map = map;
146-
core.Map<K, V> _cast(dynamic from, core.String context, dynamic key) {
147-
core.Map<K, V> result = {};
149+
core.Map<K, V?> _cast(dynamic from, core.String context, dynamic key) {
150+
core.Map<K, V?> result = {};
148151
if (from is core.Map) {
149-
for (K key in from.keys) {
152+
for (K key in from.keys as core.Iterable<K>) {
150153
if (_map.containsKey(key)) {
151154
result[key] = _map[key]!._cast(from[key], "map entry", key);
152155
} else {
@@ -155,7 +158,7 @@ class Keyed<K, V> extends Cast<core.Map<K, V>> {
155158
}
156159
return result;
157160
}
158-
return throw new FailedCast(context, key, "not a map");
161+
return throw FailedCast(context, key, "not a map");
159162
}
160163
}
161164

@@ -191,7 +194,7 @@ class Future<E> extends Cast<async.Future<E>> {
191194
if (from is async.Future) {
192195
return from.then(_value.cast);
193196
}
194-
return throw new FailedCast(context, key, "not a Future");
197+
return throw FailedCast(context, key, "not a Future");
195198
}
196199
}
197200

lib/codable.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ library codable;
55

66
export 'src/coding.dart';
77
export 'src/list.dart';
8-
export 'src/keyed_archive.dart';
8+
export 'src/keyed_archive.dart';

lib/src/codable.dart

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import 'package:codable/src/resolver.dart';
1+
import 'package:conduit_codable/src/resolver.dart';
22

33
abstract class Referencable {
44
void resolveOrThrow(ReferenceResolver resolver);
5-
}
5+
}

lib/src/coding.dart

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import 'package:codable/src/keyed_archive.dart';
1+
import 'package:conduit_codable/src/keyed_archive.dart';
22
import 'package:meta/meta.dart';
3-
import 'package:codable/cast.dart' as cast;
3+
import 'package:conduit_codable/cast.dart' as cast;
44

55
/// A base class for encodable and decodable objects.
66
///

lib/src/keyed_archive.dart

+38-41
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
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';
77

88
/// A container for a dynamic data object that can be decoded into [Coding] objects.
99
///
@@ -36,10 +36,10 @@ class KeyedArchive extends Object
3636
/// If [allowReferences] is true, JSON Schema references will be traversed and decoded objects
3737
/// will contain values from the referenced object. This flag defaults to false.
3838
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);
4141
if (allowReferences) {
42-
archive.resolveOrThrow(new ReferenceResolver(archive));
42+
archive.resolveOrThrow(ReferenceResolver(archive));
4343
}
4444
return archive;
4545
}
@@ -52,17 +52,17 @@ class KeyedArchive extends Object
5252
///
5353
/// If [allowReferences] is true, JSON Schema references in the emitted document will be validated.
5454
/// 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({});
5858
root.encode(archive);
5959
if (allowReferences) {
60-
archive.resolveOrThrow(new ReferenceResolver(archive));
60+
archive.resolveOrThrow(ReferenceResolver(archive));
6161
}
6262
return archive.toPrimitive();
6363
}
6464

65-
KeyedArchive._empty();
65+
KeyedArchive._empty() : _map = Map<String, dynamic>();
6666

6767
/// Use [unarchive] instead.
6868
KeyedArchive(this._map) {
@@ -86,7 +86,7 @@ class KeyedArchive extends Object
8686
///
8787
Uri? referenceURI;
8888

89-
late Map<String, dynamic> _map;
89+
Map<String, dynamic> _map;
9090
Coding? _inflated;
9191
KeyedArchive? _objectReference;
9292

@@ -118,7 +118,7 @@ class KeyedArchive extends Object
118118
return;
119119
}
120120

121-
final caster = new cast.Keyed(schema);
121+
final caster = cast.Keyed(schema);
122122
_map = caster.cast(_map);
123123

124124
if (_objectReference != null) {
@@ -127,20 +127,20 @@ class KeyedArchive extends Object
127127
}
128128
}
129129

130-
operator []=(String key, dynamic value) {
130+
operator []=(String key, dynamic? value) {
131131
_map[key] = value;
132132
}
133133

134-
dynamic operator [](Object? key) => _getValue(key as String?);
134+
dynamic? operator [](Object? key) => _getValue(key as String);
135135

136136
Iterable<String> get keys => _map.keys;
137137

138138
void clear() => _map.clear();
139139

140140
dynamic remove(Object? key) => _map.remove(key);
141141

142-
Map<String, dynamic> toPrimitive() {
143-
final out = <String, dynamic>{};
142+
Map<String?, dynamic> toPrimitive() {
143+
final out = <String?, dynamic>{};
144144
_map.forEach((key, val) {
145145
if (val is KeyedArchive) {
146146
out[key] = val.toPrimitive();
@@ -167,12 +167,12 @@ class KeyedArchive extends Object
167167
keys.forEach((key) {
168168
final val = _map[key];
169169
if (val is Map) {
170-
_map[key] = new KeyedArchive(caster.cast(val));
170+
_map[key] = KeyedArchive(caster.cast(val));
171171
} else if (val is List) {
172-
_map[key] = new ListArchive.from(val);
172+
_map[key] = ListArchive.from(val);
173173
} else if (key == r"$ref") {
174174
if (val is Map) {
175-
_objectReference = val as KeyedArchive;
175+
_objectReference = val as KeyedArchive?;
176176
} else {
177177
referenceURI = Uri.parse(Uri.parse(val).fragment);
178178
}
@@ -188,7 +188,7 @@ class KeyedArchive extends Object
188188
if (referenceURI != null) {
189189
_objectReference = coder.resolve(referenceURI!);
190190
if (_objectReference == null) {
191-
throw new ArgumentError(
191+
throw ArgumentError(
192192
"Invalid document. Reference '#${referenceURI!.path}' does not exist in document.");
193193
}
194194
}
@@ -204,19 +204,17 @@ class KeyedArchive extends Object
204204

205205
/* decode */
206206

207-
T? _decodedObject<T extends Coding>(KeyedArchive? raw, T inflate()) {
207+
T? _decodedObject<T extends Coding?>(KeyedArchive? raw, T inflate()) {
208208
if (raw == null) {
209209
return null;
210210
}
211211

212212
if (raw._inflated == null) {
213213
raw._inflated = inflate();
214-
if (raw._inflated != null) {
215-
raw._inflated!.decode(raw);
216-
}
214+
raw._inflated!.decode(raw);
217215
}
218216

219-
return raw._inflated as T;
217+
return raw._inflated as T?;
220218
}
221219

222220
/// Returns the object associated by [key].
@@ -228,7 +226,7 @@ class KeyedArchive extends Object
228226
/// If this object is a reference to another object (via [referenceURI]), this object's key-value
229227
/// pairs will be searched first. If [key] is not found, the referenced object's key-values pairs are searched.
230228
/// If no match is found, null is returned.
231-
T? decode<T>(String? key) {
229+
T? decode<T>(String key) {
232230
var v = _getValue(key);
233231
if (v == null) {
234232
return null;
@@ -248,14 +246,14 @@ class KeyedArchive extends Object
248246
/// [inflate] must create an empty instance of [T]. The value associated with [key]
249247
/// must be a [KeyedArchive] (a [Map]). The values of the associated object are read into
250248
/// 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()) {
252250
final val = _getValue(key);
253251
if (val == null) {
254252
return null;
255253
}
256254

257255
if (val is! KeyedArchive) {
258-
throw new ArgumentError(
256+
throw ArgumentError(
259257
"Cannot decode key '$key' into '$T', because the value is not a Map. Actual value: '$val'.");
260258
}
261259

@@ -268,17 +266,17 @@ class KeyedArchive extends Object
268266
/// must be a [ListArchive] (a [List] of [Map]). For each element of the archived list,
269267
/// [inflate] is invoked and each object in the archived list is decoded into
270268
/// 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()) {
272270
var val = _getValue(key);
273271
if (val == null) {
274272
return null;
275273
}
276274
if (val is! List) {
277-
throw new ArgumentError(
275+
throw ArgumentError(
278276
"Cannot decode key '$key' as 'List<$T>', because value is not a List. Actual value: '$val'.");
279277
}
280278

281-
return val.map((v) => _decodedObject(v, inflate)).toList();
279+
return val.map((v) => _decodedObject(v, inflate)).toList().cast<T?>();
282280
}
283281

284282
/// Returns a map of [T]s associated with [key].
@@ -287,14 +285,14 @@ class KeyedArchive extends Object
287285
/// must be a [KeyedArchive] (a [Map]), where each value is a [T].
288286
/// For each key-value pair of the archived map, [inflate] is invoked and
289287
/// 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()) {
291289
var v = _getValue(key);
292290
if (v == null) {
293291
return null;
294292
}
295293

296294
if (v is! Map<String, dynamic>) {
297-
throw new ArgumentError(
295+
throw ArgumentError(
298296
"Cannot decode key '$key' as 'Map<String, $T>', because value is not a Map. Actual value: '$v'.");
299297
}
300298

@@ -304,7 +302,7 @@ class KeyedArchive extends Object
304302

305303
/* encode */
306304

307-
Map<String, dynamic>? _encodedObject(Coding? object) {
305+
Map<String?, dynamic>? _encodedObject(Coding? object) {
308306
if (object == null) {
309307
return null;
310308
}
@@ -314,7 +312,7 @@ class KeyedArchive extends Object
314312
// they are currently not being emitted. the solution is probably tricky.
315313
// letting encode run as normal would stack overflow when there is a cyclic
316314
// reference between this object and another.
317-
var json = new KeyedArchive._empty()
315+
var json = KeyedArchive._empty()
318316
.._map = {}
319317
..referenceURI = object.referenceURI;
320318
if (json.referenceURI != null) {
@@ -367,15 +365,14 @@ class KeyedArchive extends Object
367365
return;
368366
}
369367

370-
_map[key] =
371-
new ListArchive.from(value.map((v) => _encodedObject(v)).toList());
368+
_map[key] = ListArchive.from(value.map((v) => _encodedObject(v)).toList());
372369
}
373370

374371
/// Encodes map of [Coding] objects into this object for [key].
375372
///
376373
/// This invokes [Coding.encode] on each value in [value] and adds the map of objects
377374
/// 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) {
379376
if (value == null) {
380377
return;
381378
}

lib/src/list.dart

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import 'dart:collection';
22

3-
import 'package:codable/src/codable.dart';
4-
import 'package:codable/src/coding.dart';
5-
import 'package:codable/src/keyed_archive.dart';
6-
import 'package:codable/src/resolver.dart';
3+
import 'package:conduit_codable/src/codable.dart';
4+
import 'package:conduit_codable/src/coding.dart';
5+
import 'package:conduit_codable/src/keyed_archive.dart';
6+
import 'package:conduit_codable/src/resolver.dart';
77

88
/// A list of values in a [KeyedArchive].
99
///

0 commit comments

Comments
 (0)