Skip to content

Commit

Permalink
Take @SerializableField properties into account when generating `Quer…
Browse files Browse the repository at this point in the history
…y.parseRow` (dart-backend#98)
  • Loading branch information
BenVercammen committed Mar 21, 2023
1 parent 5634447 commit 503d3f4
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 4 deletions.
1 change: 1 addition & 0 deletions packages/orm/angel_orm_mysql/test/migrations/has_car.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
CREATE TABLE IF NOT EXISTS has_cars (
id serial PRIMARY KEY,
type int not null,
color varchar(1),
created_at datetime,
updated_at datetime
);
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
CREATE TEMPORARY TABLE "has_cars" (
id serial PRIMARY KEY,
type int not null,
color varchar(1),
created_at timestamp,
updated_at timestamp
);
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@ void enumAndNestedTests(FutureOr<QueryExecutor> Function() createExecutor,
});

test('insert', () async {
var query = HasCarQuery()..values.type = CarType.sedan;
var query = HasCarQuery()
..values.type = CarType.sedan
..values.color = Color.red;
var resultOpt = await (query.insert(executor));
expect(resultOpt.isPresent, true);
resultOpt.ifPresent((result) {
expect(result.type, CarType.sedan);
expect(result.color, Color.red);
});
});

Expand Down
21 changes: 21 additions & 0 deletions packages/orm/angel_orm_test/lib/src/models/has_car.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,20 @@ part 'has_car.g.dart';

enum CarType { sedan, suv, atv }

Color? codeToColor(String? code) => code == null
? null
: Color.values.firstWhere((color) => color.code == code);

String? colorToCode(Color? color) => color?.code;

enum Color {
red('R'), green('G'), blue('B');

const Color(this.code);

final String code;
}

@orm
@serializable
abstract class _HasCar extends Model {
Expand All @@ -21,4 +35,11 @@ abstract class _HasCar extends Model {

@SerializableField(isNullable: false, defaultValue: CarType.sedan)
CarType? get type;

@SerializableField(
serializesTo: String,
serializer: #colorToCode,
deserializer: #codeToColor,
)
Color? color;
}
39 changes: 36 additions & 3 deletions packages/orm/angel_orm_test/lib/src/models/has_car.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 503d3f4

Please sign in to comment.