Skip to content

Generalize an isEnum helper, added to utils.dart #131

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
Apr 6, 2018
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
29 changes: 15 additions & 14 deletions json_serializable/lib/src/type_helpers/enum_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,28 @@ class EnumHelper extends TypeHelper {
@override
String serialize(
DartType targetType, String expression, SerializeContext context) {
if (targetType is InterfaceType && targetType.element.isEnum) {
return commonNullPrefix(
context.nullable, expression, "$expression.toString().split('.')[1]");
if (!isEnum(targetType)) {
return null;
}

return null;
return commonNullPrefix(
context.nullable, expression, "$expression.toString().split('.')[1]");
}

@override
String deserialize(
DartType targetType, String expression, DeserializeContext context) {
if (targetType is InterfaceType && targetType.element.isEnum) {
var wrappedExpression =
simpleExpression.hasMatch(expression) ? expression : '{$expression}';

return commonNullPrefix(
context.nullable,
expression,
'$targetType.values.singleWhere('
"(x) => x.toString() == '$targetType.\$$wrappedExpression')");
if (!isEnum(targetType)) {
return null;
}
return null;

var wrappedExpression =
simpleExpression.hasMatch(expression) ? expression : '{$expression}';

return commonNullPrefix(
context.nullable,
expression,
'$targetType.values.singleWhere('
"(x) => x.toString() == '$targetType.\$$wrappedExpression')");
}
}
4 changes: 4 additions & 0 deletions json_serializable/lib/src/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@

import 'package:analyzer/analyzer.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/type.dart';

// ignore: implementation_imports
import 'package:analyzer/src/dart/resolver/inheritance_manager.dart'
show InheritanceManager;

import 'package:source_gen/source_gen.dart';

bool isEnum(DartType targetType) =>
targetType is InterfaceType && targetType.element.isEnum;

/// Returns a quoted String literal for [value] that can be used in generated
/// Dart code.
String escapeDartString(String value) {
Expand Down
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: 0.5.0
version: 0.5.1-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
5 changes: 3 additions & 2 deletions json_serializable/tool/build.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// BSD-style license that can be found in the LICENSE file.

import 'dart:async';
import 'dart:io' show exitCode;

import 'package:build/build.dart';
import 'package:build_config/build_config.dart';
Expand Down Expand Up @@ -130,6 +131,6 @@ class _Replacement {
}
}

main(List<String> args) {
run(args, builders);
main(List<String> args) async {
exitCode = await run(args, builders);
}