From 76016f20624621738748daeec38a3d4407184270 Mon Sep 17 00:00:00 2001 From: Tobe O Date: Tue, 30 Apr 2019 11:21:05 -0400 Subject: [PATCH] gen@2.0.4 --- angel_orm_generator/CHANGELOG.md | 3 + angel_orm_generator/build.yaml | 24 +- angel_orm_generator/example/main.dart | 68 +---- angel_orm_generator/example/main.g.dart | 276 +++++++++++++++++- .../example/main.serializer.g.dart | 64 ---- .../lib/src/orm_build_context.dart | 17 +- angel_orm_generator/pubspec.yaml | 2 +- angel_orm_test/lib/src/models/tree.g.dart | 2 +- .../lib/src/models/unorthodox.g.dart | 2 +- 9 files changed, 298 insertions(+), 160 deletions(-) delete mode 100644 angel_orm_generator/example/main.serializer.g.dart diff --git a/angel_orm_generator/CHANGELOG.md b/angel_orm_generator/CHANGELOG.md index e8d8fbf8..ac70d393 100644 --- a/angel_orm_generator/CHANGELOG.md +++ b/angel_orm_generator/CHANGELOG.md @@ -1,3 +1,6 @@ +# 2.0.4 +* Fix `reviveColumn` and element finding to properly detect all annotations now. + # 2.0.3 * Remove `targets` in `build.yaml`. diff --git a/angel_orm_generator/build.yaml b/angel_orm_generator/build.yaml index e0fe4857..67dbe792 100644 --- a/angel_orm_generator/build.yaml +++ b/angel_orm_generator/build.yaml @@ -16,26 +16,4 @@ builders: applies_builders: - angel_serialize_generator|angel_serialize - source_gen|combining_builder - - source_gen|part_cleanup" -# targets: -# _standalone: -# sources: -# - test/models/author.dart -# - test/models/car.dart -# - test/models/customer.dart -# - test/models/foot.dart -# - test/models/fruit.dart -# - test/models/has_map.dart -# - test/models/role.dart -# - test/models/unorthodox.dart -# $default: -# dependencies: -# - angel_serialize_generator -# - :_standalone -# sources: -# - test/models/book.dart -# - test/models/has_car.dart -# - test/models/leg.dart -# - test/models/order.dart -# - test/models/tree.dart -# - test/models/user.dart \ No newline at end of file + - source_gen|part_cleanup" \ No newline at end of file diff --git a/angel_orm_generator/example/main.dart b/angel_orm_generator/example/main.dart index c5c73be4..7651e8f9 100644 --- a/angel_orm_generator/example/main.dart +++ b/angel_orm_generator/example/main.dart @@ -1,11 +1,10 @@ import 'dart:async'; - +import 'package:angel_migration/angel_migration.dart'; import 'package:angel_model/angel_model.dart'; import 'package:angel_orm/angel_orm.dart'; import 'package:angel_orm/src/query.dart'; import 'package:angel_serialize/angel_serialize.dart'; part 'main.g.dart'; -part 'main.serializer.g.dart'; main() async { var query = new EmployeeQuery() @@ -46,67 +45,8 @@ abstract class _Employee extends Model { String get lastName; + @Column(indexType: IndexType.unique) + String uniqueId; + double get salary; } - -class EmployeeQuery extends Query { - @override - final QueryValues values = new MapQueryValues(); - - EmployeeQueryWhere _where; - - EmployeeQuery() { - _where = new EmployeeQueryWhere(this); - } - - @override - EmployeeQueryWhere get where => _where; - - @override - String get tableName => 'employees'; - - @override - List get fields => - ['id', 'first_name', 'last_name', 'salary', 'created_at', 'updated_at']; - - @override - EmployeeQueryWhere newWhereClause() => new EmployeeQueryWhere(this); - - @override - Employee deserialize(List row) { - return new Employee( - id: row[0].toString(), - firstName: row[1] as String, - lastName: row[2] as String, - salary: row[3] as double, - createdAt: row[4] as DateTime, - updatedAt: row[5] as DateTime); - } -} - -class EmployeeQueryWhere extends QueryWhere { - EmployeeQueryWhere(EmployeeQuery query) - : id = new NumericSqlExpressionBuilder(query, 'id'), - firstName = new StringSqlExpressionBuilder(query, 'first_name'), - lastName = new StringSqlExpressionBuilder(query, 'last_name'), - salary = new NumericSqlExpressionBuilder(query, 'salary'), - createdAt = new DateTimeSqlExpressionBuilder(query, 'created_at'), - updatedAt = new DateTimeSqlExpressionBuilder(query, 'updated_at'); - - @override - Iterable get expressionBuilders { - return [id, firstName, lastName, salary, createdAt, updatedAt]; - } - - final NumericSqlExpressionBuilder id; - - final StringSqlExpressionBuilder firstName; - - final StringSqlExpressionBuilder lastName; - - final NumericSqlExpressionBuilder salary; - - final DateTimeSqlExpressionBuilder createdAt; - - final DateTimeSqlExpressionBuilder updatedAt; -} diff --git a/angel_orm_generator/example/main.g.dart b/angel_orm_generator/example/main.g.dart index 950cbd12..d1e834e8 100644 --- a/angel_orm_generator/example/main.g.dart +++ b/angel_orm_generator/example/main.g.dart @@ -2,6 +2,179 @@ part of 'main.dart'; +// ************************************************************************** +// MigrationGenerator +// ************************************************************************** + +class EmployeeMigration extends Migration { + @override + up(Schema schema) { + schema.create('employees', (table) { + table.serial('id')..primaryKey(); + table.varChar('unique_id')..unique(); + table.varChar('first_name'); + table.varChar('last_name'); + table.declare('salary', ColumnType('decimal')); + table.timeStamp('created_at'); + table.timeStamp('updated_at'); + }); + } + + @override + down(Schema schema) { + schema.drop('employees'); + } +} + +// ************************************************************************** +// OrmGenerator +// ************************************************************************** + +class EmployeeQuery extends Query { + EmployeeQuery({Set trampoline}) { + trampoline ??= Set(); + trampoline.add(tableName); + _where = EmployeeQueryWhere(this); + } + + @override + final EmployeeQueryValues values = EmployeeQueryValues(); + + EmployeeQueryWhere _where; + + @override + get casts { + return {'salary': 'text'}; + } + + @override + get tableName { + return 'employees'; + } + + @override + get fields { + return const [ + 'id', + 'unique_id', + 'first_name', + 'last_name', + 'salary', + 'created_at', + 'updated_at' + ]; + } + + @override + EmployeeQueryWhere get where { + return _where; + } + + @override + EmployeeQueryWhere newWhereClause() { + return EmployeeQueryWhere(this); + } + + static Employee parseRow(List row) { + if (row.every((x) => x == null)) return null; + var model = Employee( + id: row[0].toString(), + uniqueId: (row[1] as String), + firstName: (row[2] as String), + lastName: (row[3] as String), + salary: double.tryParse(row[4].toString()), + createdAt: (row[5] as DateTime), + updatedAt: (row[6] as DateTime)); + return model; + } + + @override + deserialize(List row) { + return parseRow(row); + } +} + +class EmployeeQueryWhere extends QueryWhere { + EmployeeQueryWhere(EmployeeQuery query) + : id = NumericSqlExpressionBuilder(query, 'id'), + uniqueId = StringSqlExpressionBuilder(query, 'unique_id'), + firstName = StringSqlExpressionBuilder(query, 'first_name'), + lastName = StringSqlExpressionBuilder(query, 'last_name'), + salary = NumericSqlExpressionBuilder(query, 'salary'), + createdAt = DateTimeSqlExpressionBuilder(query, 'created_at'), + updatedAt = DateTimeSqlExpressionBuilder(query, 'updated_at'); + + final NumericSqlExpressionBuilder id; + + final StringSqlExpressionBuilder uniqueId; + + final StringSqlExpressionBuilder firstName; + + final StringSqlExpressionBuilder lastName; + + final NumericSqlExpressionBuilder salary; + + final DateTimeSqlExpressionBuilder createdAt; + + final DateTimeSqlExpressionBuilder updatedAt; + + @override + get expressionBuilders { + return [id, uniqueId, firstName, lastName, salary, createdAt, updatedAt]; + } +} + +class EmployeeQueryValues extends MapQueryValues { + @override + get casts { + return {'salary': 'decimal'}; + } + + String get id { + return (values['id'] as String); + } + + set id(String value) => values['id'] = value; + String get uniqueId { + return (values['unique_id'] as String); + } + + set uniqueId(String value) => values['unique_id'] = value; + String get firstName { + return (values['first_name'] as String); + } + + set firstName(String value) => values['first_name'] = value; + String get lastName { + return (values['last_name'] as String); + } + + set lastName(String value) => values['last_name'] = value; + double get salary { + return double.tryParse((values['salary'] as String)); + } + + set salary(double value) => values['salary'] = value.toString(); + DateTime get createdAt { + return (values['created_at'] as DateTime); + } + + set createdAt(DateTime value) => values['created_at'] = value; + DateTime get updatedAt { + return (values['updated_at'] as DateTime); + } + + set updatedAt(DateTime value) => values['updated_at'] = value; + void copyFrom(Employee model) { + uniqueId = model.uniqueId; + firstName = model.firstName; + lastName = model.lastName; + salary = model.salary; + createdAt = model.createdAt; + updatedAt = model.updatedAt; + } +} + // ************************************************************************** // JsonModelGenerator // ************************************************************************** @@ -10,6 +183,7 @@ part of 'main.dart'; class Employee extends _Employee { Employee( {this.id, + this.uniqueId, this.firstName, this.lastName, this.salary, @@ -19,6 +193,9 @@ class Employee extends _Employee { @override final String id; + @override + final String uniqueId; + @override final String firstName; @@ -36,6 +213,7 @@ class Employee extends _Employee { Employee copyWith( {String id, + String uniqueId, String firstName, String lastName, double salary, @@ -43,6 +221,7 @@ class Employee extends _Employee { DateTime updatedAt}) { return new Employee( id: id ?? this.id, + uniqueId: uniqueId ?? this.uniqueId, firstName: firstName ?? this.firstName, lastName: lastName ?? this.lastName, salary: salary ?? this.salary, @@ -53,6 +232,7 @@ class Employee extends _Employee { bool operator ==(other) { return other is _Employee && other.id == id && + other.uniqueId == uniqueId && other.firstName == firstName && other.lastName == lastName && other.salary == salary && @@ -62,10 +242,104 @@ class Employee extends _Employee { @override int get hashCode { - return hashObjects([id, firstName, lastName, salary, createdAt, updatedAt]); + return hashObjects( + [id, uniqueId, firstName, lastName, salary, createdAt, updatedAt]); + } + + @override + String toString() { + return "Employee(id=$id, uniqueId=$uniqueId, firstName=$firstName, lastName=$lastName, salary=$salary, createdAt=$createdAt, updatedAt=$updatedAt)"; } Map toJson() { return EmployeeSerializer.toMap(this); } } + +// ************************************************************************** +// SerializerGenerator +// ************************************************************************** + +const EmployeeSerializer employeeSerializer = const EmployeeSerializer(); + +class EmployeeEncoder extends Converter { + const EmployeeEncoder(); + + @override + Map convert(Employee model) => EmployeeSerializer.toMap(model); +} + +class EmployeeDecoder extends Converter { + const EmployeeDecoder(); + + @override + Employee convert(Map map) => EmployeeSerializer.fromMap(map); +} + +class EmployeeSerializer extends Codec { + const EmployeeSerializer(); + + @override + get encoder => const EmployeeEncoder(); + @override + get decoder => const EmployeeDecoder(); + static Employee fromMap(Map map) { + return new Employee( + id: map['id'] as String, + uniqueId: map['unique_id'] as String, + firstName: map['first_name'] as String, + lastName: map['last_name'] as String, + salary: map['salary'] as double, + createdAt: map['created_at'] != null + ? (map['created_at'] is DateTime + ? (map['created_at'] as DateTime) + : DateTime.parse(map['created_at'].toString())) + : null, + updatedAt: map['updated_at'] != null + ? (map['updated_at'] is DateTime + ? (map['updated_at'] as DateTime) + : DateTime.parse(map['updated_at'].toString())) + : null); + } + + static Map toMap(_Employee model) { + if (model == null) { + return null; + } + return { + 'id': model.id, + 'unique_id': model.uniqueId, + 'first_name': model.firstName, + 'last_name': model.lastName, + 'salary': model.salary, + 'created_at': model.createdAt?.toIso8601String(), + 'updated_at': model.updatedAt?.toIso8601String() + }; + } +} + +abstract class EmployeeFields { + static const List allFields = [ + id, + uniqueId, + firstName, + lastName, + salary, + createdAt, + updatedAt + ]; + + static const String id = 'id'; + + static const String uniqueId = 'unique_id'; + + static const String firstName = 'first_name'; + + static const String lastName = 'last_name'; + + static const String salary = 'salary'; + + static const String createdAt = 'created_at'; + + static const String updatedAt = 'updated_at'; +} diff --git a/angel_orm_generator/example/main.serializer.g.dart b/angel_orm_generator/example/main.serializer.g.dart deleted file mode 100644 index 5e160b3b..00000000 --- a/angel_orm_generator/example/main.serializer.g.dart +++ /dev/null @@ -1,64 +0,0 @@ -// GENERATED CODE - DO NOT MODIFY BY HAND - -part of 'main.dart'; - -// ************************************************************************** -// SerializerGenerator -// ************************************************************************** - -abstract class EmployeeSerializer { - static Employee fromMap(Map map) { - return new Employee( - id: map['id'] as String, - firstName: map['first_name'] as String, - lastName: map['last_name'] as String, - salary: map['salary'] as double, - createdAt: map['created_at'] != null - ? (map['created_at'] is DateTime - ? (map['created_at'] as DateTime) - : DateTime.parse(map['created_at'].toString())) - : null, - updatedAt: map['updated_at'] != null - ? (map['updated_at'] is DateTime - ? (map['updated_at'] as DateTime) - : DateTime.parse(map['updated_at'].toString())) - : null); - } - - static Map toMap(Employee model) { - if (model == null) { - return null; - } - return { - 'id': model.id, - 'first_name': model.firstName, - 'last_name': model.lastName, - 'salary': model.salary, - 'created_at': model.createdAt?.toIso8601String(), - 'updated_at': model.updatedAt?.toIso8601String() - }; - } -} - -abstract class EmployeeFields { - static const List allFields = const [ - id, - firstName, - lastName, - salary, - createdAt, - updatedAt - ]; - - static const String id = 'id'; - - static const String firstName = 'first_name'; - - static const String lastName = 'last_name'; - - static const String salary = 'salary'; - - static const String createdAt = 'created_at'; - - static const String updatedAt = 'updated_at'; -} diff --git a/angel_orm_generator/lib/src/orm_build_context.dart b/angel_orm_generator/lib/src/orm_build_context.dart index ad29dc94..5604e6d1 100644 --- a/angel_orm_generator/lib/src/orm_build_context.dart +++ b/angel_orm_generator/lib/src/orm_build_context.dart @@ -27,11 +27,15 @@ bool isSpecialId(OrmBuildContext ctx, FieldElement field) { .isAssignableFromType(ctx.buildContext.clazz.type)); } +Element _findElement(FieldElement field) { + return (field.setter == null ? field.getter : field) ?? field; +} + FieldElement findPrimaryFieldInList( OrmBuildContext ctx, Iterable fields) { for (var field_ in fields) { var field = field_ is RelationFieldImpl ? field_.originalField : field_; - var element = field.getter ?? field; + var element = _findElement(field); // print( // 'Searching in ${ctx.buildContext.originalClassName}=>${field?.name} (${field.runtimeType})'); // Check for column annotation... @@ -95,8 +99,9 @@ Future buildOrmContext( for (var field in buildCtx.fields) { // Check for column annotation... Column column; - var element = field.getter ?? field; + var element = _findElement(field); var columnAnnotation = columnTypeChecker.firstAnnotationOf(element); + // print('${element.name} => $columnAnnotation'); if (columnAnnotation != null) { column = reviveColumn(new ConstantReader(columnAnnotation)); @@ -128,7 +133,7 @@ Future buildOrmContext( } // Try to find a relationship - var el = field.setter == null ? field.getter : field; + var el = _findElement(field); el ??= field; var ann = relationshipTypeChecker.firstAnnotationOf(el); @@ -296,11 +301,13 @@ ColumnType inferColumnType(DartType type) { Column reviveColumn(ConstantReader cr) { ColumnType columnType; + var indexTypeObj = cr.peek('indexType')?.objectValue; + indexTypeObj ??= cr.revive().namedArguments['indexType']; + var columnObj = cr.peek('type')?.objectValue?.getField('name')?.toStringValue(); var indexType = IndexType.values[ - cr.peek('indexType')?.objectValue?.getField('index')?.toIntValue() ?? - IndexType.none.index]; + indexTypeObj?.getField('index')?.toIntValue() ?? IndexType.none.index]; if (const TypeChecker.fromRuntime(PrimaryKey) .isAssignableFromType(cr.objectValue.type)) { diff --git a/angel_orm_generator/pubspec.yaml b/angel_orm_generator/pubspec.yaml index fea7cfa5..db4911be 100644 --- a/angel_orm_generator/pubspec.yaml +++ b/angel_orm_generator/pubspec.yaml @@ -1,5 +1,5 @@ name: angel_orm_generator -version: 2.0.3 +version: 2.0.4 description: Code generators for Angel's ORM. Generates query builder classes. author: Tobe O homepage: https://github.com/angel-dart/orm diff --git a/angel_orm_test/lib/src/models/tree.g.dart b/angel_orm_test/lib/src/models/tree.g.dart index cdfd6b4d..cc24dc35 100644 --- a/angel_orm_test/lib/src/models/tree.g.dart +++ b/angel_orm_test/lib/src/models/tree.g.dart @@ -11,7 +11,7 @@ class TreeMigration extends Migration { up(Schema schema) { schema.create('trees', (table) { table.serial('id')..primaryKey(); - table.integer('rings'); + table.declare('rings', ColumnType('smallint')); table.timeStamp('created_at'); table.timeStamp('updated_at'); }); diff --git a/angel_orm_test/lib/src/models/unorthodox.g.dart b/angel_orm_test/lib/src/models/unorthodox.g.dart index 676f8ac5..67cb266e 100644 --- a/angel_orm_test/lib/src/models/unorthodox.g.dart +++ b/angel_orm_test/lib/src/models/unorthodox.g.dart @@ -56,7 +56,7 @@ class NumbaMigration extends Migration { @override up(Schema schema) { schema.create('numbas', (table) { - table.integer('i'); + table.declare('i', ColumnType('serial'))..primaryKey(); table.integer('parent'); }); }