Skip to content

Add support for enums #29

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

Merged
merged 2 commits into from
Jul 27, 2017
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.2.2

* Enable support for `enum` values.

## 0.2.1

* Upgrade to `package:source_gen` v0.7.0
Expand Down
2 changes: 2 additions & 0 deletions lib/src/json_serializable_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import 'package:source_gen/source_gen.dart';
import 'json_serializable.dart';
import 'type_helper.dart';
import 'type_helpers/date_time_helper.dart';
import 'type_helpers/enum_helper.dart';
import 'type_helpers/iterable_helper.dart';
import 'type_helpers/json_helper.dart';
import 'type_helpers/map_helper.dart';
Expand All @@ -24,6 +25,7 @@ class JsonSerializableGenerator
static const _coreHelpers = const [
const IterableHelper(),
const MapHelper(),
const EnumHelper(),
const ValueHelper()
];

Expand Down
11 changes: 3 additions & 8 deletions lib/src/type_helpers/date_time_helper.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:analyzer/dart/element/type.dart';
import 'package:source_gen/source_gen.dart' show TypeChecker;
import '../type_helper.dart';
import 'type_helper_utils.dart';

class DateTimeHelper extends TypeHelper {
const DateTimeHelper();
Expand Down Expand Up @@ -28,14 +29,8 @@ class DateTimeHelper extends TypeHelper {
return null;
}

var buffer = new StringBuffer();

if (nullable) {
buffer.write("$expression == null ? null : ");
}

buffer.write("DateTime.parse($expression as String)");
return buffer.toString();
return commonNullPrefix(
nullable, expression, "DateTime.parse($expression as String)");
}
}

Expand Down
31 changes: 31 additions & 0 deletions lib/src/type_helpers/enum_helper.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import 'package:analyzer/dart/element/type.dart';
import '../type_helper.dart';
import 'type_helper_utils.dart';

class EnumHelper extends TypeHelper {
const EnumHelper();

@override
String serialize(DartType targetType, String expression, bool nullable,
TypeHelperGenerator serializeNested) {
if (targetType is InterfaceType && targetType.element.isEnum) {
return commonNullPrefix(
nullable, expression, '$expression.toString().split(".")[1]');
}

return null;
}

@override
String deserialize(DartType targetType, String expression, bool nullable,
TypeHelperGenerator deserializeNested) {
if (targetType is InterfaceType && targetType.element.isEnum) {
return commonNullPrefix(
nullable,
expression,
'$targetType.values.singleWhere('
'(x) => x.toString() == "$targetType.\${$expression}")');
}
return null;
}
}
7 changes: 2 additions & 5 deletions lib/src/type_helpers/json_helper.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/type.dart';
import '../type_helper.dart';
import 'type_helper_utils.dart';

class JsonHelper extends TypeHelper {
const JsonHelper();
Expand Down Expand Up @@ -43,11 +44,7 @@ class JsonHelper extends TypeHelper {
// github.com/dart-lang/json_serializable/issues/19
var result = "new ${targetType.name}.fromJson($expression$asCast)";

if (nullable) {
result = "$expression == null ? null : " + result;
}

return result;
return commonNullPrefix(nullable, expression, result);
}
}

Expand Down
20 changes: 4 additions & 16 deletions lib/src/type_helpers/map_helper.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:analyzer/dart/element/type.dart';
import 'package:source_gen/source_gen.dart' show TypeChecker;
import '../type_helper.dart';
import 'type_helper_utils.dart';

/// Name used for closure argument when generating calls to `map`.
final _closureArg = "e";
Expand Down Expand Up @@ -41,11 +42,7 @@ class MapHelper extends TypeHelper {
"$expression.keys,"
"$expression.values.map(($_closureArg) => $subFieldValue))";

if (nullable) {
result = "$expression == null ? null :" + result;
}

return result;
return commonNullPrefix(nullable, expression, result);
}
throw new UnsupportedTypeError(targetType, expression);
}
Expand Down Expand Up @@ -85,12 +82,7 @@ class MapHelper extends TypeHelper {
// No mapping of the values is required!

var result = "new Map<String, $valueArg>.from($expression as Map)";

if (nullable) {
result = "$expression == null ? null :" + result;
}

return result;
return commonNullPrefix(nullable, expression, result);
}

// In this case, we're going to create a new Map with matching reified
Expand All @@ -102,11 +94,7 @@ class MapHelper extends TypeHelper {
"($expression as Map<String, dynamic>).keys,"
"($expression as Map).values.map(($_closureArg) => $itemSubVal))";

if (nullable) {
result = "$expression == null ? null :" + result;
}

return result;
return commonNullPrefix(nullable, expression, result);
}
}

Expand Down
5 changes: 5 additions & 0 deletions lib/src/type_helpers/type_helper_utils.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
String commonNullPrefix(
Copy link
Member

Choose a reason for hiding this comment

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

[optional]

String commonNullPrefix(
        bool nullable, String expression, String unsafeExpression) =>
    nullable
        ? "$expression == null ? null : $unsafeExpression"
        : unsafeExpression;

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

You're a machine.

bool nullable, String expression, String unsafeExpression) =>
nullable
? "$expression == null ? null : $unsafeExpression"
: unsafeExpression;
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: json_serializable
version: 0.2.1
version: 0.2.2-dev
author: Dart Team <misc@dartlang.org>
description: Generates utilities to aid in serializing to/from JSON.
homepage: https://github.com/dart-lang/json_serializable
Expand Down
27 changes: 18 additions & 9 deletions test/json_serializable_integration_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,22 @@ void main() {
}

test("null", () {
roundTripPerson(new Person(null, null));
roundTripPerson(new Person(null, null, null));
});

test("empty", () {
roundTripPerson(new Person('', '',
roundTripPerson(new Person('', '', null,
middleName: '',
dateOfBirth: new DateTime.fromMillisecondsSinceEpoch(0)));
});

test("now", () {
roundTripPerson(new Person('a', 'b',
roundTripPerson(new Person('a', 'b', House.gryffindor,
middleName: 'c', dateOfBirth: new DateTime.now()));
});

test("now toUtc", () {
roundTripPerson(new Person('a', 'b',
roundTripPerson(new Person('a', 'b', House.hufflepuff,
middleName: 'c', dateOfBirth: new DateTime.now().toUtc()));
});

Expand All @@ -46,17 +46,17 @@ void main() {
}

test("null", () {
roundTripOrder(new Order());
roundTripOrder(new Order(Category.charmed));
});

test("empty", () {
roundTripOrder(new Order(const [])
roundTripOrder(new Order(Category.strange, const [])
..count = 0
..isRushed = false);
});

test("simple", () {
roundTripOrder(new Order(<Item>[
roundTripOrder(new Order(Category.top, <Item>[
new Item(24)
..itemNumber = 42
..saleDates = [new DateTime.now()]
Expand All @@ -65,11 +65,20 @@ void main() {
..isRushed = true);
});

test('empty json', () {
var order = new Order.fromJson({});
test('almost empty json', () {
var order = new Order.fromJson({'category': 'top'});
expect(order.items, isEmpty);
expect(order.category, Category.top);
roundTripOrder(order);
});

test('required, but missing enum value fails', () {
expect(() => new Order.fromJson({}), throwsStateError);
});

test('mismatched enum value fails', () {
expect(() => new Order.fromJson({'category': 'weird'}), throwsStateError);
});
});

group('Item', () {
Expand Down
13 changes: 11 additions & 2 deletions test/test_files/json_test_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,16 @@ import 'package:json_serializable/annotations.dart';

part 'json_test_example.g.dart';

enum House { gryffindor, hufflepuff, ravenclaw, slytherin }

@JsonSerializable()
class Person extends Object with _$PersonSerializerMixin {
final String firstName, middleName, lastName;
final DateTime dateOfBirth;
final House house;

Person(this.firstName, this.lastName, {this.middleName, this.dateOfBirth});
Person(this.firstName, this.lastName, this.house,
{this.middleName, this.dateOfBirth});

factory Person.fromJson(Map<String, dynamic> json) => _$PersonFromJson(json);

Expand All @@ -29,16 +33,21 @@ class Person extends Object with _$PersonSerializerMixin {
dateOfBirth == other.dateOfBirth;
}

enum Category { top, bottom, strange, charmed, up, down }

@JsonSerializable()
class Order extends Object with _$OrderSerializerMixin {
int count;
bool isRushed;

@JsonKey(nullable: false)
final Category category;
final UnmodifiableListView<Item> items;
Platform platform;

int get price => items.fold(0, (total, item) => item.price + total);

Order([Iterable<Item> items])
Order(this.category, [Iterable<Item> items])
: this.items = new UnmodifiableListView<Item>(
new List<Item>.unmodifiable(items ?? const <Item>[]));

Expand Down
39 changes: 25 additions & 14 deletions test/test_files/json_test_example.g.dart

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