Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nullsafety #12

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
language: dart
dart:
- stable
- beta

jobs:
include:
Expand Down
71 changes: 35 additions & 36 deletions lib/cast.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,41 +58,41 @@ class AnyCast extends Cast<dynamic> {
class IntCast extends Cast<core.int> {
const IntCast();
core.int _cast(dynamic from, core.String context, dynamic key) =>
from is core.int
? from
: throw new FailedCast(context, key, "$from is not an int");
from is core.int
? from
: throw FailedCast(context, key, "$from is not an int");
}

class DoubleCast extends Cast<core.double> {
const DoubleCast();
core.double _cast(dynamic from, core.String context, dynamic key) =>
from is core.double
? from
: throw new FailedCast(context, key, "$from is not an double");
from is core.double
? from
: throw FailedCast(context, key, "$from is not an double");
}

class StringCast extends Cast<core.String> {
const StringCast();
core.String _cast(dynamic from, core.String context, dynamic key) =>
from is core.String
? from
: throw new FailedCast(context, key, "$from is not a String");
from is core.String
? from
: throw FailedCast(context, key, "$from is not a String");
}

class BoolCast extends Cast<core.bool> {
const BoolCast();
core.bool _cast(dynamic from, core.String context, dynamic key) =>
from is core.bool
? from
: throw new FailedCast(context, key, "$from is not a bool");
from is core.bool
? from
: throw FailedCast(context, key, "$from is not a bool");
}

class Map<K, V> extends Cast<core.Map<K, V>> {
final Cast<K> _key;
final Cast<V> _value;
const Map(Cast<K> key, Cast<V> value)
: _key = key,
_value = value;
: _key = key,
_value = value;
core.Map<K, V> _cast(dynamic from, core.String context, dynamic key) {
if (from is core.Map) {
var result = <K, V>{};
Expand All @@ -102,33 +102,33 @@ class Map<K, V> extends Cast<core.Map<K, V>> {
}
return result;
}
return throw new FailedCast(context, key, "not a map");
return throw FailedCast(context, key, "not a map");
}
}

class StringMap<V> extends Cast<core.Map<core.String, V>> {
final Cast<V> _value;
const StringMap(Cast<V> value) : _value = value;
core.Map<core.String, V> _cast(
dynamic from, core.String context, dynamic key) {
dynamic from, core.String context, dynamic key) {
if (from is core.Map) {
var result = <core.String, V>{};
for (core.String key in from.keys) {
for (core.String key in from.keys as core.Iterable<core.String>) {
result[key] = _value._cast(from[key], "map entry", key);
}
return result;
}
return throw new FailedCast(context, key, "not a map");
return throw FailedCast(context, key, "not a map");
}
}

class List<E> extends Cast<core.List<E>> {
class List<E> extends Cast<core.List<E?>> {
final Cast<E> _entry;
const List(Cast<E> entry) : _entry = entry;
core.List<E> _cast(dynamic from, core.String context, dynamic key) {
core.List<E?> _cast(dynamic from, core.String context, dynamic key) {
if (from is core.List) {
var length = from.length;
var result = core.List<E>(length);
var result = core.List<E?>.filled(length, null);
for (core.int i = 0; i < length; ++i) {
if (from[i] != null) {
result[i] = _entry._cast(from[i], "list entry", i);
Expand All @@ -138,37 +138,36 @@ class List<E> extends Cast<core.List<E>> {
}
return result;
}
return throw new FailedCast(context, key, "not a list");
return throw FailedCast(context, key, "not a list");
}
}

class Keyed<K, V> extends Cast<core.Map<K, V>> {
class Keyed<K, V> extends Cast<core.Map<K, V?>> {
Iterable<K> get keys => _map.keys;
final core.Map<K, Cast<V>> _map;
const Keyed(core.Map<K, Cast<V>> map)
: _map = map;
core.Map<K, V> _cast(dynamic from, core.String context, dynamic key) {
core.Map<K, V> result = {};
const Keyed(core.Map<K, Cast<V>> map) : _map = map;
core.Map<K, V?> _cast(dynamic from, core.String context, dynamic key) {
core.Map<K, V?> result = {};
if (from is core.Map) {
for (K key in from.keys) {
for (K key in from.keys as core.Iterable<K>) {
if (_map.containsKey(key)) {
result[key] = _map[key]._cast(from[key], "map entry", key);
result[key] = _map[key]!._cast(from[key], "map entry", key);
} else {
result[key] = from[key];
}
}
return result;
}
return throw new FailedCast(context, key, "not a map");
return throw FailedCast(context, key, "not a map");
}
}

class OneOf<S, T> extends Cast<dynamic> {
final Cast<S> _left;
final Cast<T> _right;
const OneOf(Cast<S> left, Cast<T> right)
: _left = left,
_right = right;
: _left = left,
_right = right;
dynamic _cast(dynamic from, core.String context, dynamic key) {
try {
return _left._cast(from, context, key);
Expand All @@ -182,10 +181,10 @@ class Apply<S, T> extends Cast<T> {
final Cast<S> _first;
final T Function(S) _transform;
const Apply(T Function(S) transform, Cast<S> first)
: _transform = transform,
_first = first;
: _transform = transform,
_first = first;
T _cast(dynamic from, core.String context, dynamic key) =>
_transform(_first._cast(from, context, key));
_transform(_first._cast(from, context, key));
}

class Future<E> extends Cast<async.Future<E>> {
Expand All @@ -195,7 +194,7 @@ class Future<E> extends Cast<async.Future<E>> {
if (from is async.Future) {
return from.then(_value.cast);
}
return throw new FailedCast(context, key, "not a Future");
return throw FailedCast(context, key, "not a Future");
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/codable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ library codable;

export 'src/coding.dart';
export 'src/list.dart';
export 'src/keyed_archive.dart';
export 'src/keyed_archive.dart';
4 changes: 2 additions & 2 deletions lib/src/codable.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'package:codable/src/resolver.dart';
import 'package:conduit_codable/src/resolver.dart';

abstract class Referencable {
void resolveOrThrow(ReferenceResolver resolver);
}
}
11 changes: 5 additions & 6 deletions lib/src/coding.dart
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import 'package:codable/src/keyed_archive.dart';
import 'package:conduit_codable/src/keyed_archive.dart';
import 'package:meta/meta.dart';
import 'package:codable/cast.dart' as cast;
import 'package:conduit_codable/cast.dart' as cast;

/// A base class for encodable and decodable objects.
///
/// Types that can read or write their values to a document should extend this abstract class.
/// By overriding [decode] and [encode], an instance of this type will read or write its values
/// into a data container that can be transferred into formats like JSON or YAML.
abstract class Coding {
Uri referenceURI;
Map<String, cast.Cast<dynamic>> get castMap => null;
Uri? referenceURI;
Map<String, cast.Cast<dynamic>>? get castMap => null;

@mustCallSuper
void decode(KeyedArchive object) {
Expand All @@ -19,5 +19,4 @@ abstract class Coding {

// would prefer to write referenceURI to object here, but see note in KeyedArchive._encodedObject
void encode(KeyedArchive object);

}
}
Loading