Skip to content

Commit

Permalink
mark MapperContainer as deprecated
Browse files Browse the repository at this point in the history
  • Loading branch information
schultek committed Dec 25, 2023
1 parent 6aa2c07 commit 266f3ee
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 20 deletions.
2 changes: 1 addition & 1 deletion examples/fic_mappable/lib/fic_mappers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'package:fast_immutable_collections/fast_immutable_collections.dart';

void initializeFICMappers() {
// This makes all mappers work with immutable collections
MapperContainer.defaults.useAll([
MapperContainer.globals.useAll([
// mapper for immutable lists
SerializableMapper<IList, Object>.arg1(
decode: IList.fromJson,
Expand Down
2 changes: 1 addition & 1 deletion examples/fic_mappable/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,5 @@ void main() {
assert(a3.list.length == 2 && a3.list[1].str == 'tom');

var m = IMap({"value": 1000});
assert(MapperContainer.defaults.toJson(m) == '{"value":1000}');
assert(MapperContainer.globals.toJson(m) == '{"value":1000}');
}
2 changes: 1 addition & 1 deletion packages/dart_mappable/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,6 @@ void main() {
print(whatAmI.runtimeType); // Box<Confetti>

// also works with lists and sets
List<int> numbers = MapperContainer().fromJson('[2, 4, 105]');
List<int> numbers = MapperContainer.globals.fromJson('[2, 4, 105]');
print(numbers); // [2, 4, 105]
}
14 changes: 14 additions & 0 deletions packages/dart_mappable/lib/src/mapper_container.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// ignore_for_file: deprecated_member_use_from_same_package

import 'dart:convert';

import 'package:collection/collection.dart';
Expand All @@ -8,6 +10,12 @@ import 'package:type_plus/type_plus.dart' hide typeOf;

import '../dart_mappable.dart';

const _containerIssueLink =
'https://github.com/schultek/dart_mappable/issues/159';
const _containerDeprecationMessage =
'If you are using this check out the following issue for discussion: $_containerIssueLink. '
'May be removed in a future version.';

/// Additional options to be passed to [MapperContainer.toValue].
///
/// {@category Generics}
Expand Down Expand Up @@ -75,6 +83,7 @@ class DecodingOptions {
/// {@category Mapper Container}
@sealed
abstract class MapperContainer {
@Deprecated(_containerDeprecationMessage)
factory MapperContainer({
Set<MapperBase>? mappers,
Set<MapperContainer>? linked,
Expand All @@ -85,6 +94,9 @@ abstract class MapperContainer {
/// including all primitives, List, Set, Map and DateTime.
///
/// All other container will automatically be linked to this container.
@Deprecated(
'Use `MapperContainer.globals` instead. See $_containerIssueLink. '
'May be removed in a future version.')
static final MapperContainer defaults = _MapperContainerBase._({
PrimitiveMapper<Object>((v) => v, dynamic),
PrimitiveMapper<Object>((v) => v, Object),
Expand Down Expand Up @@ -170,9 +182,11 @@ abstract class MapperContainer {
List<MapperBase> getAll();

/// Links another container to this container.
@Deprecated(_containerDeprecationMessage)
void link(MapperContainer container);

/// Links a list of containers to this container.
@Deprecated(_containerDeprecationMessage)
void linkAll(Iterable<MapperContainer> containers);
}

Expand Down
2 changes: 2 additions & 0 deletions packages/dart_mappable/test/container/container_test.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// ignore_for_file: deprecated_member_use_from_same_package

import 'package:dart_mappable/dart_mappable.dart';
import 'package:test/test.dart';

Expand Down
33 changes: 17 additions & 16 deletions packages/dart_mappable/test/custom_mapper/custom_mapper_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -93,64 +93,65 @@ class EmptyMapper extends MapperBase<Uint8List> {}
void main() {
group('Custom Mappers', () {
test('Simple Custom Mapper', () {
var container = MapperContainer(mappers: {PrivateClassMapper()});
MapperContainer.globals.use(PrivateClassMapper());

MyPrivateClass c = container.fromValue('test');
MyPrivateClass c = MapperContainer.globals.fromValue('test');
expect(c.value, equals('test'));

var s = container.toValue(c);
var s = MapperContainer.globals.toValue(c);
expect(s, equals('test'));

expect(container.isEqual(c, MyPrivateClass('test')), isTrue);
expect(
MapperContainer.globals.isEqual(c, MyPrivateClass('test')), isTrue);
});

test('Generic custom Mapper', () {
var container = MapperContainer(mappers: {GenericBoxMapper()});
MapperContainer.globals.use(GenericBoxMapper());

GenericBox<int> box = container.fromValue('2');
GenericBox<int> box = MapperContainer.globals.fromValue('2');
expect(box.content, equals(2));

var json = container.toJson(box);
var json = MapperContainer.globals.toJson(box);
expect(json, equals('2'));
});

test('Custom Uri Mapper', () {
var container = MapperContainer(mappers: {UriMapper()});
MapperContainer.globals.use(UriMapper());

var uri = Uri(
scheme: 'https',
host: 'example.com',
path: 'some/path',
query: 'key=value');

var encoded = container.toValue(uri);
var encoded = MapperContainer.globals.toValue(uri);
expect(encoded, equals('https://example.com/some/path?key=value'));

var decoded = container.fromValue<Uri>(encoded);
var decoded = MapperContainer.globals.fromValue<Uri>(encoded);
expect(decoded, equals(uri));
});

test('Empty mapper', () {
var container = MapperContainer(mappers: {EmptyMapper()});
MapperContainer.globals.use(EmptyMapper());

expect(
() => container.fromValue<Uint8List>([]),
() => MapperContainer.globals.fromValue<Uint8List>([]),
throwsMapperException(MapperException.chain(
MapperMethod.decode,
'(Uint8List)',
MapperException.unsupportedMethod(MapperMethod.decode, Uint8List))),
);

expect(
() => container.toValue<Uint8List>(Uint8List(0)),
() => MapperContainer.globals.toValue<Uint8List>(Uint8List(0)),
throwsMapperException(MapperException.chain(
MapperMethod.encode,
'(Uint8List)',
MapperException.unsupportedMethod(MapperMethod.encode, Uint8List))),
);

expect(
() => container.asString(Uint8List(0)),
() => MapperContainer.globals.asString(Uint8List(0)),
throwsMapperException(MapperException.chain(
MapperMethod.stringify,
"(Instance of 'Uint8List')",
Expand All @@ -159,13 +160,13 @@ void main() {
);

expect(
() => container.isEqual(Uint8List(0), Uint8List(0)),
() => MapperContainer.globals.isEqual(Uint8List(0), Uint8List(0)),
throwsMapperException(MapperException.chain(MapperMethod.equals, '[[]]',
MapperException.unsupportedMethod(MapperMethod.equals, Uint8List))),
);

expect(
() => container.hash(Uint8List(0)),
() => MapperContainer.globals.hash(Uint8List(0)),
throwsMapperException(MapperException.chain(MapperMethod.hash, '[[]]',
MapperException.unsupportedMethod(MapperMethod.hash, Uint8List))),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class Item with ItemMappable {
}

void main() {
var m = MapperContainer.defaults;
var m = MapperContainer.globals;
group('Mapping of primitive types', () {
test('Mapping of strings', () {
expect(m.fromValue<String>('abc'), equals('abc'));
Expand Down

0 comments on commit 266f3ee

Please sign in to comment.