Fix const bug

This commit is contained in:
Tobe O 2018-06-27 01:45:46 -04:00
parent 5b2d765b3c
commit dfb4957bf7
6 changed files with 34 additions and 15 deletions

View file

@ -1,3 +1,6 @@
# 2.0.8+1
* Fix generation of `const` constructors with iterables.
# 2.0.8
* Now supports de/serialization of `enum` types.
* Generate `const` constructors when possible.

View file

@ -86,7 +86,7 @@ class JsonModelGenerator extends GeneratorForAnnotation<Serializable> {
}
for (var field in ctx.fields) {
if (isListOrMapType(field.type)) {
if (!shouldBeConstant(ctx) && isListOrMapType(field.type)) {
String typeName = const TypeChecker.fromRuntime(List)
.isAssignableFromType(field.type)
? 'List'
@ -98,15 +98,10 @@ class JsonModelGenerator extends GeneratorForAnnotation<Serializable> {
}
}
if (ctx.constructorParameters.isNotEmpty) {
constructor.initializers.add(
new Code('super(${ctx.constructorParameters.map((p) => p.name).join(
',')})'));
}
for (var field in ctx.fields) {
constructor.optionalParameters.add(new Parameter((b) {
b
..toThis = shouldBeConstant(ctx)
..name = field.name
..named = true;
@ -121,6 +116,14 @@ class JsonModelGenerator extends GeneratorForAnnotation<Serializable> {
}
}));
}
if (ctx.constructorParameters.isNotEmpty) {
if (!shouldBeConstant(ctx) ||
ctx.clazz.unnamedConstructor?.isConst == true)
constructor.initializers.add(new Code(
'super(${ctx.constructorParameters.map((p) => p.name).join(
',')})'));
}
}));
}

View file

@ -1,5 +1,5 @@
name: angel_serialize_generator
version: 2.0.8
version: 2.0.8+1
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,11 +1,13 @@
import 'package:angel_model/angel_model.dart';
import 'package:angel_serialize/angel_serialize.dart';
import 'package:collection/collection.dart';
part 'with_enum.g.dart';
part 'with_enum.serializer.g.dart';
@Serializable(autoIdAndDateFields: false)
abstract class _WithEnum {
WithEnumType get type;
List<int> get finalList;
}
enum WithEnumType { a, b, c }

View file

@ -7,17 +7,24 @@ part of 'with_enum.dart';
// **************************************************************************
class WithEnum implements _WithEnum {
const WithEnum({this.type});
const WithEnum({this.type, List<int> this.finalList});
@override
final WithEnumType type;
WithEnum copyWith({WithEnumType type}) {
return new WithEnum(type: type ?? this.type);
@override
final List<int> finalList;
WithEnum copyWith({WithEnumType type, List<int> finalList}) {
return new WithEnum(
type: type ?? this.type, finalList: finalList ?? this.finalList);
}
bool operator ==(other) {
return other is _WithEnum && other.type == type;
return other is _WithEnum &&
other.type == type &&
const ListEquality<int>(const DefaultEquality<int>())
.equals(other.finalList, finalList);
}
Map<String, dynamic> toJson() {

View file

@ -11,17 +11,21 @@ abstract class WithEnumSerializer {
return new WithEnum(
type: map['type'] is WithEnumType
? map['type']
: (map['type'] is int ? WithEnumType.values[map['type']] : null));
: (map['type'] is int ? WithEnumType.values[map['type']] : null),
finalList: map['final_list'] as List<int>);
}
static Map<String, dynamic> toMap(WithEnum model) {
return {
'type':
model.type == null ? null : WithEnumType.values.indexOf(model.type)
model.type == null ? null : WithEnumType.values.indexOf(model.type),
'final_list': model.finalList
};
}
}
abstract class WithEnumFields {
static const String type = 'type';
static const String finalList = 'final_list';
}