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

fix serialization equality #184

Merged
merged 2 commits into from
Apr 2, 2024
Merged
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
8 changes: 2 additions & 6 deletions examples/fic_mappable/lib/main.mapper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,7 @@ mixin AMappable {

@override
bool operator ==(Object other) {
return identical(this, other) ||
(runtimeType == other.runtimeType &&
AMapper.ensureInitialized().isValueEqual(this as A, other));
return AMapper.ensureInitialized().equalsValue(this as A, other);
}

@override
Expand Down Expand Up @@ -158,9 +156,7 @@ mixin BMappable {

@override
bool operator ==(Object other) {
return identical(this, other) ||
(runtimeType == other.runtimeType &&
BMapper.ensureInitialized().isValueEqual(this as B, other));
return BMapper.ensureInitialized().equalsValue(this as B, other);
}

@override
Expand Down
6 changes: 2 additions & 4 deletions examples/package_comparison/lib/comparisons/basic.mapper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,8 @@ mixin PersonCMappable {

@override
bool operator ==(Object other) {
return identical(this, other) ||
(runtimeType == other.runtimeType &&
PersonCMapper.ensureInitialized()
.isValueEqual(this as PersonC, other));
return PersonCMapper.ensureInitialized()
.equalsValue(this as PersonC, other);
}

@override
Expand Down
11 changes: 3 additions & 8 deletions examples/package_comparison/lib/comparisons/generic.mapper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,7 @@ mixin BoxCMappable<T> {

@override
bool operator ==(Object other) {
return identical(this, other) ||
(runtimeType == other.runtimeType &&
BoxCMapper.ensureInitialized()
.isValueEqual(this as BoxC<T>, other));
return BoxCMapper.ensureInitialized().equalsValue(this as BoxC<T>, other);
}

@override
Expand Down Expand Up @@ -166,10 +163,8 @@ mixin ContentCMappable {

@override
bool operator ==(Object other) {
return identical(this, other) ||
(runtimeType == other.runtimeType &&
ContentCMapper.ensureInitialized()
.isValueEqual(this as ContentC, other));
return ContentCMapper.ensureInitialized()
.equalsValue(this as ContentC, other);
}

@override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,7 @@ mixin CatBMappable {

@override
bool operator ==(Object other) {
return identical(this, other) ||
(runtimeType == other.runtimeType &&
CatBMapper.ensureInitialized().isValueEqual(this as CatB, other));
return CatBMapper.ensureInitialized().equalsValue(this as CatB, other);
}

@override
Expand Down Expand Up @@ -234,9 +232,7 @@ mixin DogBMappable {

@override
bool operator ==(Object other) {
return identical(this, other) ||
(runtimeType == other.runtimeType &&
DogBMapper.ensureInitialized().isValueEqual(this as DogB, other));
return DogBMapper.ensureInitialized().equalsValue(this as DogB, other);
}

@override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,8 @@ mixin PersonBMappable {

@override
bool operator ==(Object other) {
return identical(this, other) ||
(runtimeType == other.runtimeType &&
PersonBMapper.ensureInitialized()
.isValueEqual(this as PersonB, other));
return PersonBMapper.ensureInitialized()
.equalsValue(this as PersonB, other);
}

@override
Expand Down
4 changes: 1 addition & 3 deletions examples/polymorph_copywith/lib/models/cat.mapper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,7 @@ mixin CatMappable {

@override
bool operator ==(Object other) {
return identical(this, other) ||
(runtimeType == other.runtimeType &&
CatMapper.ensureInitialized().isValueEqual(this as Cat, other));
return CatMapper.ensureInitialized().equalsValue(this as Cat, other);
}

@override
Expand Down
4 changes: 1 addition & 3 deletions examples/polymorph_copywith/lib/models/dog.mapper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,7 @@ mixin DogMappable {

@override
bool operator ==(Object other) {
return identical(this, other) ||
(runtimeType == other.runtimeType &&
DogMapper.ensureInitialized().isValueEqual(this as Dog, other));
return DogMapper.ensureInitialized().equalsValue(this as Dog, other);
}

@override
Expand Down
5 changes: 1 addition & 4 deletions examples/polymorph_copywith/lib/models/person.mapper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,7 @@ mixin PersonMappable {

@override
bool operator ==(Object other) {
return identical(this, other) ||
(runtimeType == other.runtimeType &&
PersonMapper.ensureInitialized()
.isValueEqual(this as Person, other));
return PersonMapper.ensureInitialized().equalsValue(this as Person, other);
}

@override
Expand Down
4 changes: 1 addition & 3 deletions examples/polymorph_copywith/lib/models/zoo.mapper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,7 @@ mixin ZooMappable<T extends Animal> {

@override
bool operator ==(Object other) {
return identical(this, other) ||
(runtimeType == other.runtimeType &&
ZooMapper.ensureInitialized().isValueEqual(this as Zoo<T>, other));
return ZooMapper.ensureInitialized().equalsValue(this as Zoo<T>, other);
}

@override
Expand Down
19 changes: 5 additions & 14 deletions packages/dart_mappable/example/lib/main.mapper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,7 @@ mixin PersonMappable {

@override
bool operator ==(Object other) {
return identical(this, other) ||
(runtimeType == other.runtimeType &&
PersonMapper.ensureInitialized()
.isValueEqual(this as Person, other));
return PersonMapper.ensureInitialized().equalsValue(this as Person, other);
}

@override
Expand Down Expand Up @@ -230,9 +227,7 @@ mixin CarMappable {

@override
bool operator ==(Object other) {
return identical(this, other) ||
(runtimeType == other.runtimeType &&
CarMapper.ensureInitialized().isValueEqual(this as Car, other));
return CarMapper.ensureInitialized().equalsValue(this as Car, other);
}

@override
Expand Down Expand Up @@ -333,9 +328,7 @@ mixin BoxMappable<T> {

@override
bool operator ==(Object other) {
return identical(this, other) ||
(runtimeType == other.runtimeType &&
BoxMapper.ensureInitialized().isValueEqual(this as Box<T>, other));
return BoxMapper.ensureInitialized().equalsValue(this as Box<T>, other);
}

@override
Expand Down Expand Up @@ -431,10 +424,8 @@ mixin ConfettiMappable {

@override
bool operator ==(Object other) {
return identical(this, other) ||
(runtimeType == other.runtimeType &&
ConfettiMapper.ensureInitialized()
.isValueEqual(this as Confetti, other));
return ConfettiMapper.ensureInitialized()
.equalsValue(this as Confetti, other);
}

@override
Expand Down
1 change: 0 additions & 1 deletion packages/dart_mappable/lib/dart_mappable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ export 'src/hooks/string_hooks.dart';
export 'src/hooks/unmapped_properties_hook.dart';
export 'src/mapper_container.dart';
export 'src/mapper_exception.dart';
export 'src/mapper_utils.dart' show MapperUtils;
export 'src/mappers/class_mapper.dart';
export 'src/mappers/default_mappers.dart';
export 'src/mappers/interface_mapper.dart';
Expand Down
2 changes: 1 addition & 1 deletion packages/dart_mappable/lib/src/mapper_container.dart
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ class _MapperContainerBase implements MapperContainer, TypeProvider {
}
var mapper = _mapperFor(value);
if (mapper != null) {
return mapper.isValueEqual(value, other, this);
return mapper.equalsValue(value, other, this);
} else {
return value == other;
}
Expand Down
77 changes: 4 additions & 73 deletions packages/dart_mappable/lib/src/mapper_utils.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import 'package:type_plus/type_plus.dart';

import 'annotations.dart';
import 'mapper_container.dart';
import 'mapper_exception.dart';
import 'mappers/mapper_base.dart';
import 'mappers/mapping_context.dart';

/// {@nodoc}
Expand All @@ -19,14 +16,12 @@ extension DecodingUtil on DecodingContext {
value = hook.beforeDecode(value);
}

if (value is! T) {
if (value != null) {
value = container.fromValue<T>(value, options);
} else {
throw MapperException.missingParameter(key);
}
if (value == null && value is! T) {
throw MapperException.missingParameter(key);
}

value = container.fromValue<T>(value, options);

if (hook != null) {
value = hook.afterDecode(value);
}
Expand Down Expand Up @@ -78,67 +73,3 @@ extension TypeCheck<T> on T {
}
}
}

extension MapperUtils<T extends Object> on MapperBase<T> {
bool isValueEqual(T? value, Object? other, [MapperContainer? container]) {
if (value == null) {
return other == null;
}
try {
if (!isFor(other)) return false;
var context = MappingContext(
container: container,
args: () => value.runtimeType.args
.map((t) => t == UnresolvedType ? dynamic : t)
.toList(),
);
return equals(value, other as T, context);
} catch (e, stacktrace) {
Error.throwWithStackTrace(
MapperException.chain(MapperMethod.equals, '[$value]', e),
stacktrace,
);
}
}

int hashValue(T? value, [MapperContainer? container]) {
if (value == null) {
return value.hashCode;
}
try {
var context = MappingContext(
container: container,
args: () => value.runtimeType.args
.map((t) => t == UnresolvedType ? dynamic : t)
.toList(),
);
return hash(value, context);
} catch (e, stacktrace) {
Error.throwWithStackTrace(
MapperException.chain(MapperMethod.hash, '[$value]', e),
stacktrace,
);
}
}

String stringifyValue(T? value, [MapperContainer? container]) {
if (value == null) {
return value.toString();
}
try {
var context = MappingContext(
container: container,
args: () => value.runtimeType.args
.map((t) => t == UnresolvedType ? dynamic : t)
.toList(),
);
return stringify(value, context);
} catch (e, stacktrace) {
Error.throwWithStackTrace(
MapperException.chain(MapperMethod.stringify,
'(Instance of \'${value.runtimeType}\')', e),
stacktrace,
);
}
}
}
3 changes: 3 additions & 0 deletions packages/dart_mappable/lib/src/mappers/class_mapper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,9 @@ abstract class ClassMapperBase<T extends Object>

@override
bool equals(T value, T other, MappingContext context) {
if (value.runtimeType.base != other.runtimeType.base) {
return false;
}
return _members.every((f) {
return context.container.isEqual(f.get(value), f.get(other));
});
Expand Down
Loading
Loading