mirror of https://github.com/AMT-Cheif/drift.git
Actually use the real type, add parent for data classes
This commit is contained in:
parent
f83781d250
commit
5db9a5f87d
|
@ -7,7 +7,7 @@ part of 'example.dart';
|
|||
// **************************************************************************
|
||||
|
||||
// ignore_for_file: unnecessary_brace_in_string_interps
|
||||
class Category {
|
||||
class Category extends DataClass {
|
||||
final int id;
|
||||
final String description;
|
||||
Category({this.id, this.description});
|
||||
|
@ -28,6 +28,7 @@ class Category {
|
|||
description: json['description'] as String,
|
||||
);
|
||||
}
|
||||
@override
|
||||
Map<String, dynamic> toJson() {
|
||||
return {
|
||||
'id': id,
|
||||
|
@ -118,7 +119,7 @@ class $CategoriesTable extends Categories
|
|||
}
|
||||
}
|
||||
|
||||
class Recipe {
|
||||
class Recipe extends DataClass {
|
||||
final int id;
|
||||
final String title;
|
||||
final String instructions;
|
||||
|
@ -147,6 +148,7 @@ class Recipe {
|
|||
category: json['category'] as int,
|
||||
);
|
||||
}
|
||||
@override
|
||||
Map<String, dynamic> toJson() {
|
||||
return {
|
||||
'id': id,
|
||||
|
@ -276,7 +278,7 @@ class $RecipesTable extends Recipes with TableInfo<$RecipesTable, Recipe> {
|
|||
}
|
||||
}
|
||||
|
||||
class Ingredient {
|
||||
class Ingredient extends DataClass {
|
||||
final int id;
|
||||
final String name;
|
||||
final int caloriesPer100g;
|
||||
|
@ -300,6 +302,7 @@ class Ingredient {
|
|||
caloriesPer100g: json['caloriesPer100g'] as int,
|
||||
);
|
||||
}
|
||||
@override
|
||||
Map<String, dynamic> toJson() {
|
||||
return {
|
||||
'id': id,
|
||||
|
@ -412,7 +415,7 @@ class $IngredientsTable extends Ingredients
|
|||
}
|
||||
}
|
||||
|
||||
class IngredientInRecipe {
|
||||
class IngredientInRecipe extends DataClass {
|
||||
final int recipe;
|
||||
final int ingredient;
|
||||
final int amountInGrams;
|
||||
|
@ -437,6 +440,7 @@ class IngredientInRecipe {
|
|||
amountInGrams: json['amountInGrams'] as int,
|
||||
);
|
||||
}
|
||||
@override
|
||||
Map<String, dynamic> toJson() {
|
||||
return {
|
||||
'recipe': recipe,
|
||||
|
|
|
@ -22,6 +22,7 @@ export 'package:moor/src/runtime/statements/insert.dart';
|
|||
export 'package:moor/src/runtime/statements/delete.dart';
|
||||
export 'package:moor/src/runtime/structure/columns.dart';
|
||||
export 'package:moor/src/runtime/structure/table_info.dart';
|
||||
export 'package:moor/src/runtime/data_class.dart';
|
||||
export 'package:moor/src/runtime/database.dart';
|
||||
export 'package:moor/src/types/sql_types.dart';
|
||||
export 'package:moor/src/runtime/migration.dart';
|
||||
|
|
|
@ -0,0 +1,50 @@
|
|||
import 'dart:convert';
|
||||
|
||||
/// A common supertype for all data classes generated by moor. Data classes are
|
||||
/// immutable structures that represent a single row.
|
||||
abstract class DataClass {
|
||||
const DataClass();
|
||||
|
||||
// todo better docs, explain ValueSerializer
|
||||
|
||||
/// Converts this object into a representation that can be encoded with
|
||||
/// [json].
|
||||
Map<String, dynamic> toJson(
|
||||
{ValueSerializer serializer = const ValueSerializer.defaults()});
|
||||
|
||||
String toJsonString(
|
||||
{ValueSerializer serializer = const ValueSerializer.defaults()}) {
|
||||
return json.encode(toJson(serializer: serializer));
|
||||
}
|
||||
}
|
||||
|
||||
/// Serializer responsible for mapping atomic types from and to json.
|
||||
abstract class ValueSerializer {
|
||||
const ValueSerializer();
|
||||
const factory ValueSerializer.defaults() = _DefaultValueSerializer;
|
||||
|
||||
dynamic toJson<T>(T value);
|
||||
T fromJson<T>(dynamic json);
|
||||
}
|
||||
|
||||
class _DefaultValueSerializer extends ValueSerializer {
|
||||
const _DefaultValueSerializer();
|
||||
|
||||
@override
|
||||
T fromJson<T>(json) {
|
||||
if (T == DateTime) {
|
||||
return DateTime.fromMillisecondsSinceEpoch(json as int) as T;
|
||||
}
|
||||
|
||||
return json as T;
|
||||
}
|
||||
|
||||
@override
|
||||
dynamic toJson<T>(T value) {
|
||||
if (value is DateTime) {
|
||||
return value.millisecondsSinceEpoch;
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
}
|
|
@ -113,6 +113,8 @@ class BlobType extends SqlType<Uint8List> {
|
|||
}
|
||||
|
||||
class RealType extends SqlType<num> {
|
||||
const RealType();
|
||||
|
||||
@override
|
||||
num mapFromDatabaseResponse(response) => response as num;
|
||||
|
||||
|
|
|
@ -13,7 +13,8 @@ class SqlTypeSystem {
|
|||
StringType(),
|
||||
IntType(),
|
||||
DateTimeType(),
|
||||
BlobType()
|
||||
BlobType(),
|
||||
RealType(),
|
||||
]);
|
||||
|
||||
/// Returns the appropriate sql type for the dart type provided as the
|
||||
|
|
|
@ -7,7 +7,7 @@ part of 'todos.dart';
|
|||
// **************************************************************************
|
||||
|
||||
// ignore_for_file: unnecessary_brace_in_string_interps
|
||||
class TodoEntry {
|
||||
class TodoEntry extends DataClass {
|
||||
final int id;
|
||||
final String title;
|
||||
final String content;
|
||||
|
@ -42,6 +42,7 @@ class TodoEntry {
|
|||
category: json['category'] as int,
|
||||
);
|
||||
}
|
||||
@override
|
||||
Map<String, dynamic> toJson() {
|
||||
return {
|
||||
'id': id,
|
||||
|
@ -199,7 +200,7 @@ class $TodosTableTable extends TodosTable
|
|||
}
|
||||
}
|
||||
|
||||
class Category {
|
||||
class Category extends DataClass {
|
||||
final int id;
|
||||
final String description;
|
||||
Category({this.id, this.description});
|
||||
|
@ -220,6 +221,7 @@ class Category {
|
|||
description: json['description'] as String,
|
||||
);
|
||||
}
|
||||
@override
|
||||
Map<String, dynamic> toJson() {
|
||||
return {
|
||||
'id': id,
|
||||
|
@ -307,7 +309,7 @@ class $CategoriesTable extends Categories
|
|||
}
|
||||
}
|
||||
|
||||
class User {
|
||||
class User extends DataClass {
|
||||
final int id;
|
||||
final String name;
|
||||
final bool isAwesome;
|
||||
|
@ -347,6 +349,7 @@ class User {
|
|||
creationTime: json['creationTime'] as DateTime,
|
||||
);
|
||||
}
|
||||
@override
|
||||
Map<String, dynamic> toJson() {
|
||||
return {
|
||||
'id': id,
|
||||
|
@ -499,7 +502,7 @@ class $UsersTable extends Users with TableInfo<$UsersTable, User> {
|
|||
}
|
||||
}
|
||||
|
||||
class SharedTodo {
|
||||
class SharedTodo extends DataClass {
|
||||
final int todo;
|
||||
final int user;
|
||||
SharedTodo({this.todo, this.user});
|
||||
|
@ -518,6 +521,7 @@ class SharedTodo {
|
|||
user: json['user'] as int,
|
||||
);
|
||||
}
|
||||
@override
|
||||
Map<String, dynamic> toJson() {
|
||||
return {
|
||||
'todo': todo,
|
||||
|
@ -611,7 +615,7 @@ class $SharedTodosTable extends SharedTodos
|
|||
}
|
||||
}
|
||||
|
||||
class TableWithoutPKData {
|
||||
class TableWithoutPKData extends DataClass {
|
||||
final int notReallyAnId;
|
||||
final num someFloat;
|
||||
TableWithoutPKData({this.notReallyAnId, this.someFloat});
|
||||
|
@ -634,6 +638,7 @@ class TableWithoutPKData {
|
|||
someFloat: json['someFloat'] as num,
|
||||
);
|
||||
}
|
||||
@override
|
||||
Map<String, dynamic> toJson() {
|
||||
return {
|
||||
'notReallyAnId': notReallyAnId,
|
||||
|
|
|
@ -10,7 +10,7 @@ class DataClassWriter {
|
|||
DataClassWriter(this.table);
|
||||
|
||||
void writeInto(StringBuffer buffer) {
|
||||
buffer.write('class ${table.dartTypeName} {\n');
|
||||
buffer.write('class ${table.dartTypeName} extends DataClass {\n');
|
||||
|
||||
// write individual fields
|
||||
for (var column in table.columns) {
|
||||
|
@ -115,7 +115,7 @@ class DataClassWriter {
|
|||
}
|
||||
|
||||
void _writeToJson(StringBuffer buffer) {
|
||||
buffer.write('Map<String, dynamic> toJson() {\n return {');
|
||||
buffer.write('@override Map<String, dynamic> toJson() {\n return {');
|
||||
|
||||
for (var column in table.columns) {
|
||||
final getter = column.dartGetterName;
|
||||
|
|
Loading…
Reference in New Issue