Skip to content

Commit 5339c8a

Browse files
committed
fix: primitive coercion logic for primitive fields
I don't even know how this went unnoticed for so long. Well it's fixed now. Refs: #23
1 parent 8b34db2 commit 5339c8a

File tree

4 files changed

+44
-35
lines changed

4 files changed

+44
-35
lines changed

packages/dogs_core/lib/src/structure/native.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,7 @@ class StructureNativeSerialization<T> extends NativeSerializerMode<T> with TypeC
194194
if (fieldType.isAssignable(mapValue)) {
195195
args.add(mapValue);
196196
} else {
197-
if (fieldType.isAssignable(mapValue)) return fieldType;
198-
return engine.codec.primitiveCoercion.coerce(fieldType, fieldType, fieldName);
197+
args.add(engine.codec.primitiveCoercion.coerce(fieldType, mapValue, fieldName));
199198
}
200199
}
201200
} on DogFieldSerializerException {

smoke/test0/lib/models.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,4 +278,4 @@ class DeepPolymorphic with Dataclass<DeepPolymorphic> {
278278
}
279279
});
280280
}
281-
}
281+
}

smoke/test0/lib/parts/models.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,12 @@ void testModels() {
9797
expect(classMap.containsKey("a"), false);
9898
expect(classMap.containsKey("b"), false);
9999
});
100+
101+
test("Structure Coercion", () {
102+
final parsed = dogs.fromNative<CoerceTestModel>(CoerceTestModel.variant0Input);
103+
final expected = CoerceTestModel.variant0();
104+
expect(parsed.a, expected.a);
105+
});
100106
}
101107

102108
void testSingleModel<T>(T Function() a, T Function() b) => group("$T", () {

smoke/test0/lib/special.dart

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import 'package:dogs_core/dogs_validation.dart';
2222
import 'models.dart';
2323

2424
class ConvertableA {
25-
2625
int a;
2726
int b;
2827

@@ -31,38 +30,35 @@ class ConvertableA {
3130
@override
3231
bool operator ==(Object other) =>
3332
identical(this, other) ||
34-
other is ConvertableA &&
35-
runtimeType == other.runtimeType &&
36-
a == other.a &&
37-
b == other.b;
33+
other is ConvertableA && runtimeType == other.runtimeType && a == other.a && b == other.b;
3834

3935
@override
4036
int get hashCode => a.hashCode ^ b.hashCode;
4137
}
4238

4339
@linkSerializer
44-
class ConvertableAConverter extends DogConverter<ConvertableA> with OperationMapMixin<ConvertableA> {
45-
40+
class ConvertableAConverter extends DogConverter<ConvertableA>
41+
with OperationMapMixin<ConvertableA> {
4642
@override
4743
Map<Type, OperationMode<ConvertableA> Function()> get modes => {
48-
NativeSerializerMode: () => NativeSerializerMode.create(
49-
serializer: (value, engine) => [value.a, value.b],
50-
deserializer: (value, engine) => ConvertableA(value[0], value[1]),
51-
),
52-
};
44+
NativeSerializerMode: () => NativeSerializerMode.create(
45+
serializer: (value, engine) => [value.a, value.b],
46+
deserializer: (value, engine) => ConvertableA(value[0], value[1]),
47+
),
48+
};
5349

54-
ConvertableAConverter() : super(
55-
struct: DogStructure<ConvertableA>.synthetic("ConvertableA")
56-
);
50+
ConvertableAConverter() : super(struct: DogStructure<ConvertableA>.synthetic("ConvertableA"));
5751
}
5852

5953
@serializable
6054
enum EnumA {
61-
a,b,c,longNameValue;
55+
a,
56+
b,
57+
c,
58+
longNameValue;
6259
}
6360

6461
abstract class CustomBase {
65-
6662
// This should also be copied!
6763
@PropertyName("_id")
6864
final String id;
@@ -73,7 +69,6 @@ abstract class CustomBase {
7369
}
7470

7571
abstract class SecondLevelBase extends CustomBase {
76-
7772
@LengthRange(max: 100)
7873
final String name;
7974

@@ -85,7 +80,6 @@ abstract class SecondLevelBase extends CustomBase {
8580

8681
@serializable
8782
class CustomBaseImpl extends SecondLevelBase with Dataclass<CustomBaseImpl> {
88-
8983
final String tag;
9084

9185
CustomBaseImpl({
@@ -101,12 +95,10 @@ class CustomBaseImpl extends SecondLevelBase with Dataclass<CustomBaseImpl> {
10195
factory CustomBaseImpl.variant1() {
10296
return CustomBaseImpl(id: "id1", name: "Helga", tag: "tag");
10397
}
104-
10598
}
10699

107100
@serializable
108101
class InitializersModel with Dataclass<InitializersModel> {
109-
110102
final String id;
111103

112104
InitializersModel(String? id) : id = id ?? "default";
@@ -121,8 +113,7 @@ class InitializersModel with Dataclass<InitializersModel> {
121113
}
122114

123115
@serializable
124-
class ConstructorBodyModel with Dataclass<ConstructorBodyModel>{
125-
116+
class ConstructorBodyModel with Dataclass<ConstructorBodyModel> {
126117
late String id;
127118
late String data;
128119

@@ -142,11 +133,10 @@ class ConstructorBodyModel with Dataclass<ConstructorBodyModel>{
142133

143134
@serializable
144135
class GetterModel with Dataclass<GetterModel> {
145-
146136
late String id;
147137
String? _buffer;
148138

149-
GetterModel(String? id, String data) {
139+
GetterModel(String? id, String data) {
150140
this.id = id ?? "default";
151141
_buffer = data;
152142
}
@@ -165,7 +155,6 @@ class GetterModel with Dataclass<GetterModel> {
165155

166156
@serializable
167157
class DefaultValueModel with Dataclass<DefaultValueModel> {
168-
169158
@DefaultValue("default")
170159
String a;
171160

@@ -190,13 +179,14 @@ class DefaultValueModel with Dataclass<DefaultValueModel> {
190179
}
191180

192181
static int bSupplier() => 420;
193-
static ModelA cSupplier() => ModelA.variant0();
194182

183+
static ModelA cSupplier() => ModelA.variant0();
195184
}
196185

197186
@Serializable(serialName: "MyCustomSerialName")
198187
class CustomSerialName {
199188
String value;
189+
200190
CustomSerialName(this.value);
201191
}
202192

@@ -263,7 +253,6 @@ enum EnumB {
263253
c;
264254
}
265255

266-
267256
@serializable
268257
class CombinedEnumTestModel with Dataclass<CombinedEnumTestModel> {
269258
EnumA enumA;
@@ -283,11 +272,28 @@ class CombinedEnumTestModel with Dataclass<CombinedEnumTestModel> {
283272
}
284273
}
285274

286-
class CustomList<E> extends ListBase<E>{
275+
@serializable
276+
class CoerceTestModel with Dataclass<CoerceTestModel> {
277+
final int a;
278+
final double b;
279+
280+
CoerceTestModel(this.a, this.b);
287281

282+
static CoerceTestModel variant0() {
283+
return CoerceTestModel(42, 3);
284+
}
285+
286+
static Map<String,dynamic> variant0Input = {
287+
"a": 42.2,
288+
"b": 3
289+
};
290+
}
291+
292+
class CustomList<E> extends ListBase<E> {
288293
List<E> backing = [];
289294

290295
CustomList();
296+
291297
CustomList.from(this.backing);
292298

293299
@override
@@ -311,6 +317,4 @@ class CustomList<E> extends ListBase<E>{
311317

312318
@dogsLinked
313319
final customListConverter = TreeBaseConverterFactory.createIterableFactory<CustomList>(
314-
wrap: <T>(entries) => CustomList<T>.from(entries.toList()),
315-
unwrap: <T>(list) => list
316-
);
320+
wrap: <T>(entries) => CustomList<T>.from(entries.toList()), unwrap: <T>(list) => list);

0 commit comments

Comments
 (0)