Skip to content

Commit a9dc160

Browse files
authored
Improve the output of the nullable enum encode logic (#204)
1 parent 6bde0e9 commit a9dc160

8 files changed

+22
-25
lines changed

json_serializable/lib/src/type_helpers/enum_helper.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ class EnumHelper extends TypeHelper {
1919
return null;
2020
}
2121

22-
return commonNullPrefix(
23-
context.nullable, expression, "$expression.toString().split('.')[1]");
22+
var nullableLiteral = context.nullable ? '?' : '';
23+
24+
return '$expression$nullableLiteral.toString()'
25+
"$nullableLiteral.split('.')$nullableLiteral.last";
2426
}
2527

2628
@override

json_serializable/test/default_value/default_value.checked.g.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ abstract class _$DefaultValueSerializerMixin {
7575
'fieldListSimple': fieldListSimple,
7676
'fieldMapSimple': fieldMapSimple,
7777
'fieldMapListString': fieldMapListString,
78-
'fieldEnum':
79-
fieldEnum == null ? null : fieldEnum.toString().split('.')[1]
78+
'fieldEnum': fieldEnum?.toString()?.split('.')?.last
8079
};
8180
}

json_serializable/test/default_value/default_value.g.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ abstract class _$DefaultValueSerializerMixin {
5656
'fieldListSimple': fieldListSimple,
5757
'fieldMapSimple': fieldMapSimple,
5858
'fieldMapListString': fieldMapListString,
59-
'fieldEnum':
60-
fieldEnum == null ? null : fieldEnum.toString().split('.')[1]
59+
'fieldEnum': fieldEnum?.toString()?.split('.')?.last
6160
};
6261
}

json_serializable/test/test_files/json_test_example.g.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ abstract class _$PersonSerializerMixin {
3737
'middleName': middleName,
3838
'lastName': lastName,
3939
'dateOfBirth': dateOfBirth?.toIso8601String(),
40-
r'$house': house == null ? null : house.toString().split('.')[1],
40+
r'$house': house?.toString()?.split('.')?.last,
4141
'order': order,
42-
'houseMap': houseMap?.map((k, e) =>
43-
new MapEntry(k, e == null ? null : e.toString().split('.')[1]))
42+
'houseMap': houseMap
43+
?.map((k, e) => new MapEntry(k, e?.toString()?.split('.')?.last))
4444
};
4545
}
4646

@@ -66,7 +66,7 @@ abstract class _$OrderSerializerMixin {
6666
Map<String, dynamic> toJson() => <String, dynamic>{
6767
'count': count,
6868
'isRushed': isRushed,
69-
'category': category.toString().split('.')[1],
69+
'category': category.toString().split('.').last,
7070
'items': items,
7171
'platform': platform,
7272
'altPlatforms': altPlatforms

json_serializable/test/test_files/json_test_example.non_nullable.g.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ abstract class _$PersonSerializerMixin {
3333
'middleName': middleName,
3434
'lastName': lastName,
3535
'dateOfBirth': dateOfBirth.toIso8601String(),
36-
r'$house': house.toString().split('.')[1],
36+
r'$house': house.toString().split('.').last,
3737
'order': order,
38-
'houseMap':
39-
houseMap.map((k, e) => new MapEntry(k, e.toString().split('.')[1]))
38+
'houseMap': houseMap
39+
.map((k, e) => new MapEntry(k, e.toString().split('.').last))
4040
};
4141
}
4242

@@ -60,7 +60,7 @@ abstract class _$OrderSerializerMixin {
6060
Map<String, dynamic> toJson() => <String, dynamic>{
6161
'count': count,
6262
'isRushed': isRushed,
63-
'category': category.toString().split('.')[1],
63+
'category': category.toString().split('.').last,
6464
'items': items,
6565
'platform': platform,
6666
'altPlatforms': altPlatforms

json_serializable/test/test_files/json_test_example.non_nullable.wrapped.g.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,12 @@ class _$PersonJsonMapWrapper extends $JsonMapWrapper {
5959
case 'dateOfBirth':
6060
return _v.dateOfBirth.toIso8601String();
6161
case r'$house':
62-
return _v.house.toString().split('.')[1];
62+
return _v.house.toString().split('.').last;
6363
case 'order':
6464
return _v.order;
6565
case 'houseMap':
6666
return $wrapMap<String, House>(
67-
_v.houseMap, (e) => e.toString().split('.')[1]);
67+
_v.houseMap, (e) => e.toString().split('.').last);
6868
}
6969
}
7070
return null;
@@ -114,7 +114,7 @@ class _$OrderJsonMapWrapper extends $JsonMapWrapper {
114114
case 'isRushed':
115115
return _v.isRushed;
116116
case 'category':
117-
return _v.category.toString().split('.')[1];
117+
return _v.category.toString().split('.').last;
118118
case 'items':
119119
return _v.items;
120120
case 'platform':

json_serializable/test/test_files/json_test_example.wrapped.g.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,12 @@ class _$PersonJsonMapWrapper extends $JsonMapWrapper {
6363
case 'dateOfBirth':
6464
return _v.dateOfBirth?.toIso8601String();
6565
case r'$house':
66-
return _v.house == null ? null : _v.house.toString().split('.')[1];
66+
return _v.house?.toString()?.split('.')?.last;
6767
case 'order':
6868
return _v.order;
6969
case 'houseMap':
70-
return $wrapMapHandleNull<String, House>(_v.houseMap,
71-
(e) => e == null ? null : e.toString().split('.')[1]);
70+
return $wrapMapHandleNull<String, House>(
71+
_v.houseMap, (e) => e?.toString()?.split('.')?.last);
7272
}
7373
}
7474
return null;
@@ -120,7 +120,7 @@ class _$OrderJsonMapWrapper extends $JsonMapWrapper {
120120
case 'isRushed':
121121
return _v.isRushed;
122122
case 'category':
123-
return _v.category.toString().split('.')[1];
123+
return _v.category.toString().split('.').last;
124124
case 'items':
125125
return _v.items;
126126
case 'platform':

json_serializable/test/yaml/build_config.g.dart

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,7 @@ abstract class _$BuilderSerializerMixin {
8383
writeNotNull('is_optional', isOptional);
8484
writeNotNull('auto_apply', autoApply == null ? null : _toJson(autoApply));
8585
writeNotNull(
86-
'defaultEnumTest',
87-
defaultEnumTest == null
88-
? null
89-
: defaultEnumTest.toString().split('.')[1]);
86+
'defaultEnumTest', defaultEnumTest?.toString()?.split('.')?.last);
9087
val['builder_factories'] = builderFactories;
9188
writeNotNull('applies_builders', appliesBuilders);
9289
writeNotNull('required_inputs', requiredInputs);

0 commit comments

Comments
 (0)