Decode plain DateTime, also better TypeScript serialization

This commit is contained in:
Tobe O 2018-03-30 14:53:38 -04:00
parent c02c95344c
commit 1ab858d658
8 changed files with 37 additions and 16 deletions

View file

@ -1,3 +1,7 @@
# 2.0.5
* Deserialization now supports un-serialized `DateTime`.
* Better support for regular typed Lists and Maps in TypeScript.
# 2.0.4
* Fields in TypeScript definitions are now nullable by default.

View file

@ -139,8 +139,9 @@ class SerializerGenerator extends GeneratorForAnnotation<Serializable> {
// Deserialize dates
if (dateTimeTypeChecker.isAssignableFromType(field.type))
deserializedRepresentation =
"map['$alias'] != null ? DateTime.parse(map['$alias']) : null";
deserializedRepresentation = "map['$alias'] != null ? "
"(map['$alias'] is DateTime ? map['$alias'] : DateTime.parse(map['$alias']))"
" : null";
// Serialize model classes via `XSerializer.toMap`
else if (isModelClass(field.type)) {

View file

@ -32,7 +32,8 @@ class TypeScriptDefinitionBuilder implements Builder {
var arg = await compileToTypeScriptType(
fieldName, type.typeArguments[0], ext, buildStep);
typeScriptType = '$arg[]';
} else if (isMapToModelType(type)) {
} else if (const TypeChecker.fromRuntime(List).isAssignableFromType(type) &&
type.typeArguments.length == 2) {
var key = await compileToTypeScriptType(
fieldName, type.typeArguments[0], ext, buildStep);
var value = await compileToTypeScriptType(
@ -58,7 +59,13 @@ class TypeScriptDefinitionBuilder implements Builder {
..outdent()
..writeln('}'));
} else if (const TypeChecker.fromRuntime(List).isAssignableFromType(type)) {
typeScriptType = 'any[]';
if (type.typeArguments.length == 0)
typeScriptType = 'any[]';
else {
var arg = await compileToTypeScriptType(
fieldName, type.typeArguments[0], ext, buildStep);
typeScriptType = '$arg[]';
}
} else if (isModelClass(type)) {
var ctx = await buildContext(
type.element,

View file

@ -1,5 +1,5 @@
name: angel_serialize_generator
version: 2.0.4
version: 2.0.5
description: Model serialization generators, designed for use with Angel. Combine with angel_serialize for flexible modeling.
author: Tobe O <thosakwe@gmail.com>
homepage: https://github.com/angel-dart/serialize

View file

@ -1,10 +1,7 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
interface Library {
id?: string;
collection?: BookCollection;
collection?: any;
created_at?: any;
updated_at?: any;
}
interface BookCollection {
[key: string]: Book;
}

View file

@ -20,10 +20,14 @@ abstract class AuthorSerializer {
: null,
obscured: map['obscured'],
createdAt: map['created_at'] != null
? DateTime.parse(map['created_at'])
? (map['created_at'] is DateTime
? map['created_at']
: DateTime.parse(map['created_at']))
: null,
updatedAt: map['updated_at'] != null
? DateTime.parse(map['updated_at'])
? (map['updated_at'] is DateTime
? map['updated_at']
: DateTime.parse(map['updated_at']))
: null);
}
@ -71,10 +75,14 @@ abstract class LibrarySerializer {
})
: null,
createdAt: map['created_at'] != null
? DateTime.parse(map['created_at'])
? (map['created_at'] is DateTime
? map['created_at']
: DateTime.parse(map['created_at']))
: null,
updatedAt: map['updated_at'] != null
? DateTime.parse(map['updated_at'])
? (map['updated_at'] is DateTime
? map['updated_at']
: DateTime.parse(map['updated_at']))
: null);
}

View file

@ -5,7 +5,7 @@ interface Book {
title?: string;
description?: string;
page_count?: number;
not_models?: any[];
not_models?: number[];
camelCase?: string;
created_at?: any;
updated_at?: any;

View file

@ -17,10 +17,14 @@ abstract class BookSerializer {
notModels: map['not_models'],
camelCaseString: map['camelCase'],
createdAt: map['created_at'] != null
? DateTime.parse(map['created_at'])
? (map['created_at'] is DateTime
? map['created_at']
: DateTime.parse(map['created_at']))
: null,
updatedAt: map['updated_at'] != null
? DateTime.parse(map['updated_at'])
? (map['updated_at'] is DateTime
? map['updated_at']
: DateTime.parse(map['updated_at']))
: null);
}