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 # 2.0.8
* Now supports de/serialization of `enum` types. * Now supports de/serialization of `enum` types.
* Generate `const` constructors when possible. * Generate `const` constructors when possible.

View file

@ -86,7 +86,7 @@ class JsonModelGenerator extends GeneratorForAnnotation<Serializable> {
} }
for (var field in ctx.fields) { for (var field in ctx.fields) {
if (isListOrMapType(field.type)) { if (!shouldBeConstant(ctx) && isListOrMapType(field.type)) {
String typeName = const TypeChecker.fromRuntime(List) String typeName = const TypeChecker.fromRuntime(List)
.isAssignableFromType(field.type) .isAssignableFromType(field.type)
? 'List' ? '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) { for (var field in ctx.fields) {
constructor.optionalParameters.add(new Parameter((b) { constructor.optionalParameters.add(new Parameter((b) {
b b
..toThis = shouldBeConstant(ctx)
..name = field.name ..name = field.name
..named = true; ..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 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. description: Model serialization generators, designed for use with Angel. Combine with angel_serialize for flexible modeling.
author: Tobe O <thosakwe@gmail.com> author: Tobe O <thosakwe@gmail.com>
homepage: https://github.com/angel-dart/serialize 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:angel_serialize/angel_serialize.dart';
import 'package:collection/collection.dart';
part 'with_enum.g.dart'; part 'with_enum.g.dart';
part 'with_enum.serializer.g.dart'; part 'with_enum.serializer.g.dart';
@Serializable(autoIdAndDateFields: false) @Serializable(autoIdAndDateFields: false)
abstract class _WithEnum { abstract class _WithEnum {
WithEnumType get type; WithEnumType get type;
List<int> get finalList;
} }
enum WithEnumType { a, b, c } enum WithEnumType { a, b, c }

View file

@ -7,17 +7,24 @@ part of 'with_enum.dart';
// ************************************************************************** // **************************************************************************
class WithEnum implements _WithEnum { class WithEnum implements _WithEnum {
const WithEnum({this.type}); const WithEnum({this.type, List<int> this.finalList});
@override @override
final WithEnumType type; final WithEnumType type;
WithEnum copyWith({WithEnumType type}) { @override
return new WithEnum(type: type ?? this.type); 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) { 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() { Map<String, dynamic> toJson() {

View file

@ -11,17 +11,21 @@ abstract class WithEnumSerializer {
return new WithEnum( return new WithEnum(
type: map['type'] is WithEnumType type: map['type'] is WithEnumType
? map['type'] ? 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) { static Map<String, dynamic> toMap(WithEnum model) {
return { return {
'type': '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 { abstract class WithEnumFields {
static const String type = 'type'; static const String type = 'type';
static const String finalList = 'final_list';
} }