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 support for Duration fields with default values #1172

Merged
merged 1 commit into from Jul 8, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions json_serializable/CHANGELOG.md
@@ -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
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
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
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;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was the only use of commonNullPrefix – just inlined it here!

}
}
2 changes: 1 addition & 1 deletion json_serializable/pubspec.yaml
@@ -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
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.

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.

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
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
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.