Skip to content

Commit

Permalink
Fix support for Duration fields with default values (#1172)
Browse files Browse the repository at this point in the history
Fixes #1170

`Duration` was overlooked when supporting default values in constructors. That's now fixed!
  • Loading branch information
kevmoo authored Jul 8, 2022
1 parent decbda1 commit f50eb99
Show file tree
Hide file tree
Showing 13 changed files with 40 additions and 18 deletions.
5 changes: 5 additions & 0 deletions json_serializable/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 6.3.1

- Fixed support for `Duration` fields with default values.
([#1170](https://github.com/google/json_serializable.dart/issues/1170))

## 6.3.0

- Added support for generating `_$ExampleFieldMap`, which can be used by other
Expand Down
10 changes: 0 additions & 10 deletions json_serializable/lib/src/type_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/type.dart';

import 'type_helpers/config_types.dart';
import 'utils.dart';

/// Context information provided in calls to [TypeHelper.serialize] and
/// [TypeHelper.deserialize].
Expand Down Expand Up @@ -85,12 +84,3 @@ abstract class TypeHelper<T extends TypeHelperContext> {
bool defaultProvided,
);
}

Object commonNullPrefix(
bool nullable,
String expression,
Object unsafeExpression,
) =>
nullable
? ifNullOrElse(expression, 'null', '$unsafeExpression')
: unsafeExpression;
8 changes: 4 additions & 4 deletions json_serializable/lib/src/type_helpers/duration_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:analyzer/dart/element/type.dart';
import 'package:source_gen/source_gen.dart' show TypeChecker;
import 'package:source_helper/source_helper.dart';

import '../default_container.dart';
import '../type_helper.dart';

class DurationHelper extends TypeHelper {
Expand Down Expand Up @@ -33,7 +34,7 @@ class DurationHelper extends TypeHelper {
}

@override
String? deserialize(
Object? deserialize(
DartType targetType,
String expression,
TypeHelperContext context,
Expand All @@ -43,11 +44,10 @@ class DurationHelper extends TypeHelper {
return null;
}

return commonNullPrefix(
targetType.isNullableType,
return DefaultContainer(
expression,
'Duration(microseconds: $expression as int)',
).toString();
);
}
}

Expand Down
7 changes: 4 additions & 3 deletions json_serializable/lib/src/type_helpers/to_from_string.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import 'package:analyzer/dart/element/type.dart';
import 'package:source_gen/source_gen.dart';

import '../type_helper.dart';
import '../utils.dart';

final bigIntString = ToFromStringHelper(
'BigInt.parse',
Expand Down Expand Up @@ -76,7 +76,8 @@ class ToFromStringHelper {

final parseParam = isString ? expression : '$expression as String';

return commonNullPrefix(nullable, expression, '$_parse($parseParam)')
.toString();
final output = '$_parse($parseParam)';

return nullable ? ifNullOrElse(expression, 'null', output) : output;
}
}
2 changes: 1 addition & 1 deletion json_serializable/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: json_serializable
version: 6.3.0
version: 6.3.1
description: >-
Automatically generate code for converting to and from JSON by annotating
Dart classes.
Expand Down
3 changes: 3 additions & 0 deletions json_serializable/test/default_value/default_value.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ class DefaultValue implements dvi.DefaultValue {
})
Map<String, List<String>> fieldMapListString;

Duration durationField;

@JsonKey(defaultValue: Greek.beta)
Greek fieldEnum;

Expand All @@ -83,6 +85,7 @@ class DefaultValue implements dvi.DefaultValue {
this.fieldMapSimple,
this.fieldMapListString,
this.fieldEnum, {
this.durationField = Duration.zero,
this.constClass = const ConstClass('value'),
this.valueFromConverter = const ConstClass('value'),
this.valueFromFunction = const ConstClass('value'),
Expand Down
4 changes: 4 additions & 0 deletions json_serializable/test/default_value/default_value.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ class DefaultValue implements dvi.DefaultValue {
})
Map<String, List<String>> fieldMapListString;

Duration durationField;

@JsonKey(defaultValue: Greek.beta)
Greek fieldEnum;

Expand All @@ -86,6 +88,7 @@ class DefaultValue implements dvi.DefaultValue {
this.fieldMapSimple,
this.fieldMapListString,
this.fieldEnum, {
this.durationField = Duration.zero,
this.constClass = const ConstClass('value'),
this.valueFromConverter = const ConstClass('value'),
this.valueFromFunction = const ConstClass('value'),
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ abstract class DefaultValue {

Map<String, List<String>> get fieldMapListString;

Duration get durationField;

Greek get fieldEnum;

ConstClass get constClass;
Expand Down
2 changes: 2 additions & 0 deletions json_serializable/test/default_value/default_value_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const _defaultInstance = {
'fieldMapListString': {
'root': ['child']
},
'durationField': 0,
'fieldEnum': 'beta',
'constClass': {'field': 'value'},
'valueFromConverter': 'value',
Expand All @@ -44,6 +45,7 @@ const _otherValues = {
'fieldMapListString': {
'root2': ['alpha']
},
'durationField': 1,
'fieldEnum': 'delta',
'constClass': {'field': 'otherValue'},
'valueFromConverter': 'otherValue',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ class DefaultValueImplicit implements dvi.DefaultValue {

final Map<String, List<String>> fieldMapListString;

final Duration durationField;

final Greek fieldEnum;

final ConstClass constClass;
Expand All @@ -59,6 +61,7 @@ class DefaultValueImplicit implements dvi.DefaultValue {
'root': ['child']
},
this.fieldEnum = Greek.beta,
this.durationField = const Duration(),
this.constClass = const ConstClass('value'),
this.valueFromConverter = const ConstClass('value'),
this.valueFromFunction = const ConstClass('value'),
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f50eb99

Please sign in to comment.