diff --git a/angel_serialize_generator/lib/angel_serialize_generator.dart b/angel_serialize_generator/lib/angel_serialize_generator.dart index 664c9dd7..c9643f58 100644 --- a/angel_serialize_generator/lib/angel_serialize_generator.dart +++ b/angel_serialize_generator/lib/angel_serialize_generator.dart @@ -53,12 +53,14 @@ Expression convertObject(DartObject o) { if (o.toBoolValue() != null) return literalBool(o.toBoolValue()); if (o.toIntValue() != null) return literalNum(o.toIntValue()); if (o.toDoubleValue() != null) return literalNum(o.toDoubleValue()); - if (o.toSymbolValue() != null) + if (o.toSymbolValue() != null) { return CodeExpression(Code('#' + o.toSymbolValue())); + } if (o.toStringValue() != null) return literalString(o.toStringValue()); if (o.toTypeValue() != null) return convertTypeReference(o.toTypeValue()); - if (o.toListValue() != null) + if (o.toListValue() != null) { return literalList(o.toListValue().map(convertObject)); + } if (o.toMapValue() != null) { return literalMap(o .toMapValue() @@ -80,8 +82,9 @@ String dartObjectToString(DartObject v) { if (v.toDoubleValue() != null) return v.toDoubleValue().toString(); if (v.toSymbolValue() != null) return '#' + v.toSymbolValue(); if (v.toTypeValue() != null) return v.toTypeValue().name; - if (v.toListValue() != null) + if (v.toListValue() != null) { return 'const [' + v.toListValue().map(dartObjectToString).join(', ') + ']'; + } if (v.toMapValue() != null) { return 'const {' + v.toMapValue().entries.map((entry) { diff --git a/angel_serialize_generator/lib/build_context.dart b/angel_serialize_generator/lib/build_context.dart index 31d92216..c5b4d471 100644 --- a/angel_serialize_generator/lib/build_context.dart +++ b/angel_serialize_generator/lib/build_context.dart @@ -3,7 +3,6 @@ import 'package:analyzer/dart/constant/value.dart'; import 'package:analyzer/dart/element/element.dart'; import 'package:analyzer/dart/element/type.dart'; import 'package:analyzer/src/dart/element/element.dart'; -import 'package:angel_model/angel_model.dart'; import 'package:angel_serialize/angel_serialize.dart'; import 'package:build/build.dart'; import 'package:meta/meta.dart'; diff --git a/angel_serialize_generator/lib/model.dart b/angel_serialize_generator/lib/model.dart index be91a446..01d5ec16 100644 --- a/angel_serialize_generator/lib/model.dart +++ b/angel_serialize_generator/lib/model.dart @@ -6,8 +6,9 @@ class JsonModelGenerator extends GeneratorForAnnotation { @override Future generateForAnnotatedElement( Element element, ConstantReader annotation, BuildStep buildStep) async { - if (element.kind != ElementKind.CLASS) + if (element.kind != ElementKind.CLASS) { throw 'Only classes can be annotated with a @Serializable() annotation.'; + } var ctx = await buildContext(element as ClassElement, annotation, buildStep, await buildStep.resolver, true); @@ -138,9 +139,9 @@ class JsonModelGenerator extends GeneratorForAnnotation { b.defaultTo = Code(dartObjectToString(existingDefault)); } - if (!isListOrMapType(field.type)) + if (!isListOrMapType(field.type)) { b.toThis = true; - else { + } else { b.type = convertTypeReference(field.type); } @@ -153,9 +154,10 @@ class JsonModelGenerator extends GeneratorForAnnotation { if (ctx.constructorParameters.isNotEmpty) { if (!shouldBeConstant(ctx) || - ctx.clazz.unnamedConstructor?.isConst == true) + ctx.clazz.unnamedConstructor?.isConst == true) { constructor.initializers.add(Code( 'super(${ctx.constructorParameters.map((p) => p.name).join(',')})')); + } } })); } @@ -209,16 +211,18 @@ class JsonModelGenerator extends GeneratorForAnnotation { if (type.typeParameters.length == 1) { var eq = generateEquality(type.typeArguments[0]); return 'ListEquality<${type.typeArguments[0].name}>($eq)'; - } else + } else { return 'ListEquality()'; + } } else if (const TypeChecker.fromRuntime(Map) .isAssignableFromType(type)) { if (type.typeParameters.length == 2) { var keq = generateEquality(type.typeArguments[0]), veq = generateEquality(type.typeArguments[1]); return 'MapEquality<${type.typeArguments[0].name}, ${type.typeArguments[1].name}>(keys: $keq, values: $veq)'; - } else + } else { return 'MapEquality()'; + } } return nullable ? null : 'DefaultEquality<${type.name}>()'; @@ -228,8 +232,9 @@ class JsonModelGenerator extends GeneratorForAnnotation { } static String Function(String, String) generateComparator(DartType type) { - if (type is! InterfaceType || type.name == 'dynamic') + if (type is! InterfaceType || type.name == 'dynamic') { return (a, b) => '$a == $b'; + } var eq = generateEquality(type, true); if (eq == null) return (a, b) => '$a == $b'; return (a, b) => '$eq.equals($a, $b)'; diff --git a/angel_serialize_generator/lib/serialize.dart b/angel_serialize_generator/lib/serialize.dart index 09061ae3..7947efc0 100644 --- a/angel_serialize_generator/lib/serialize.dart +++ b/angel_serialize_generator/lib/serialize.dart @@ -8,8 +8,9 @@ class SerializerGenerator extends GeneratorForAnnotation { @override Future generateForAnnotatedElement( Element element, ConstantReader annotation, BuildStep buildStep) async { - if (element.kind != ElementKind.CLASS) + if (element.kind != ElementKind.CLASS) { throw 'Only classes can be annotated with a @Serializable() annotation.'; + } var ctx = await buildContext(element as ClassElement, annotation, buildStep, await buildStep.resolver, autoSnakeCaseNames != false); @@ -147,8 +148,9 @@ class ${pascal}Decoder extends Converter { } // Serialize dates - else if (dateTimeTypeChecker.isAssignableFromType(type)) + else if (dateTimeTypeChecker.isAssignableFromType(type)) { serializedRepresentation = 'model.${field.name}?.toIso8601String()'; + } // Serialize model classes via `XSerializer.toMap` else if (isModelClass(type)) { @@ -266,10 +268,11 @@ class ${pascal}Decoder extends Converter { var name = MirrorSystem.getName(ctx.fieldInfo[field.name].deserializer); deserializedRepresentation = "$name(map['$alias'])"; - } else if (dateTimeTypeChecker.isAssignableFromType(type)) + } else if (dateTimeTypeChecker.isAssignableFromType(type)) { deserializedRepresentation = "map['$alias'] != null ? " "(map['$alias'] is DateTime ? (map['$alias'] as DateTime) : DateTime.parse(map['$alias'].toString()))" " : $defaultValue"; + } // Serialize model classes via `XSerializer.toMap` else if (isModelClass(type)) { diff --git a/angel_serialize_generator/lib/typescript.dart b/angel_serialize_generator/lib/typescript.dart index 08f61c3f..cb071b61 100644 --- a/angel_serialize_generator/lib/typescript.dart +++ b/angel_serialize_generator/lib/typescript.dart @@ -29,8 +29,9 @@ class TypeScriptDefinitionBuilder implements Builder { }; types.forEach((t, tsType) { - if (TypeChecker.fromRuntime(t).isAssignableFromType(type)) + if (TypeChecker.fromRuntime(t).isAssignableFromType(type)) { typeScriptType = tsType; + } }); if (type is InterfaceType) { @@ -67,9 +68,9 @@ class TypeScriptDefinitionBuilder implements Builder { ..writeln('}')); } else if (const TypeChecker.fromRuntime(List) .isAssignableFromType(type)) { - if (type.typeArguments.isEmpty) + if (type.typeArguments.isEmpty) { typeScriptType = 'any[]'; - else { + } else { var arg = await compileToTypeScriptType( ctx, fieldName, type.typeArguments[0], refs, ext, buildStep); typeScriptType = '$arg[]'; @@ -139,8 +140,9 @@ class TypeScriptDefinitionBuilder implements Builder { } for (var element in elements) { - if (element.element.kind != ElementKind.CLASS) + if (element.element.kind != ElementKind.CLASS) { throw 'Only classes can be annotated with a @Serializable() annotation.'; + } var annotation = element.annotation;