mirror of https://github.com/AMT-Cheif/drift.git
Backwards compatible attempt
This commit is contained in:
parent
ba28b51125
commit
ff9d419559
|
@ -1,2 +1,3 @@
|
|||
// This field is analyzed by drift_dev to easily obtain common types.
|
||||
export 'runtime/types/converters.dart' show TypeConverter, JsonTypeConverter;
|
||||
export 'runtime/types/converters.dart'
|
||||
show TypeConverter, JsonTypeConverter, JsonTypeConverterWithDifferentTypes;
|
||||
|
|
|
@ -33,13 +33,9 @@ abstract class TypeConverter<D, S> {
|
|||
/// to SQL (and vice-versa).
|
||||
/// When a [BuildGeneralColumn.map] column (or a `MAPPED BY` constraint in
|
||||
/// `.drift` files) refers to a type converter that inherits from
|
||||
/// [JsonTypeConverter], it will also be used for the conversion from and to
|
||||
/// [JsonTypeConverterWithDifferentTypes], it will also be used for the conversion from and to
|
||||
/// JSON.
|
||||
abstract class JsonTypeConverter<D, S, J> extends TypeConverter<D, S> {
|
||||
/// Empty constant constructor so that subclasses can have a constant
|
||||
/// constructor.
|
||||
const JsonTypeConverter();
|
||||
|
||||
mixin JsonTypeConverterWithDifferentTypes<D, S, J> on TypeConverter<D, S> {
|
||||
/// Map a value from the Data class to json.
|
||||
///
|
||||
/// Defaults to doing the same conversion as for Dart -> SQL, [toSql].
|
||||
|
@ -56,9 +52,39 @@ abstract class JsonTypeConverter<D, S, J> extends TypeConverter<D, S> {
|
|||
/// The returned type converter will use the [inner] type converter for non-
|
||||
/// null values. Further, `null` is mapped to `null` in both directions (from
|
||||
/// Dart to SQL and vice-versa).
|
||||
static JsonTypeConverter<D?, S?, J?>
|
||||
static JsonTypeConverterWithDifferentTypes<D?, S?, J?>
|
||||
asNullable<D, S extends Object, J extends Object>(
|
||||
JsonTypeConverter<D, S, J> inner) {
|
||||
JsonTypeConverterWithDifferentTypes<D, S, J> inner) {
|
||||
return _NullWrappingTypeConverterWithJson(inner);
|
||||
}
|
||||
}
|
||||
|
||||
/// A mixin for [TypeConverter]s that should also apply to drift's builtin
|
||||
/// JSON serialization of data classes.
|
||||
///
|
||||
/// By default, a [TypeConverter] only applies to the serialization from Dart
|
||||
/// to SQL (and vice-versa).
|
||||
/// When a [BuildGeneralColumn.map] column (or a `MAPPED BY` constraint in
|
||||
/// `.drift` files) refers to a type converter that inherits from
|
||||
/// [JsonTypeConverter], it will also be used for the conversion from and to
|
||||
/// JSON.
|
||||
mixin JsonTypeConverter<D, S>
|
||||
implements JsonTypeConverterWithDifferentTypes<D, S, S> {
|
||||
@override
|
||||
S toJson(D value) => toSql(value);
|
||||
|
||||
@override
|
||||
D fromJson(S json) => fromSql(json);
|
||||
|
||||
/// Wraps an [inner] type converter that only considers non-nullable values
|
||||
/// as a type converter that handles null values too.
|
||||
///
|
||||
/// The returned type converter will use the [inner] type converter for non-
|
||||
/// null values. Further, `null` is mapped to `null` in both directions (from
|
||||
/// Dart to SQL and vice-versa).
|
||||
static JsonTypeConverterWithDifferentTypes<D?, S?, J?>
|
||||
asNullable<D, S extends Object, J extends Object>(
|
||||
JsonTypeConverterWithDifferentTypes<D, S, J> inner) {
|
||||
return _NullWrappingTypeConverterWithJson(inner);
|
||||
}
|
||||
}
|
||||
|
@ -157,8 +183,8 @@ class _NullWrappingTypeConverter<D, S extends Object>
|
|||
|
||||
class _NullWrappingTypeConverterWithJson<D, S extends Object, J extends Object>
|
||||
extends NullAwareTypeConverter<D, S>
|
||||
implements JsonTypeConverter<D?, S?, J?> {
|
||||
final JsonTypeConverter<D, S, J> _inner;
|
||||
implements JsonTypeConverterWithDifferentTypes<D?, S?, J?> {
|
||||
final JsonTypeConverterWithDifferentTypes<D, S, J> _inner;
|
||||
|
||||
const _NullWrappingTypeConverterWithJson(this._inner);
|
||||
|
||||
|
|
|
@ -135,14 +135,8 @@ class CustomConverter extends TypeConverter<MyCustomObject, String> {
|
|||
}
|
||||
|
||||
class CustomJsonConverter extends CustomConverter
|
||||
implements JsonTypeConverter<MyCustomObject, String, String> {
|
||||
with JsonTypeConverter<MyCustomObject, String> {
|
||||
const CustomJsonConverter();
|
||||
|
||||
@override
|
||||
MyCustomObject fromJson(String json) => fromSql(json);
|
||||
|
||||
@override
|
||||
String toJson(MyCustomObject value) => toSql(value);
|
||||
}
|
||||
|
||||
abstract class CategoryTodoCountView extends View {
|
||||
|
|
|
@ -1373,10 +1373,11 @@ class $PureDefaultsTable extends PureDefaults
|
|||
return $PureDefaultsTable(attachedDatabase, alias);
|
||||
}
|
||||
|
||||
static JsonTypeConverter<MyCustomObject, String, String> $converter0 =
|
||||
const CustomJsonConverter();
|
||||
static JsonTypeConverter<MyCustomObject?, String?, String?> $converter0n =
|
||||
JsonTypeConverter.asNullable($converter0);
|
||||
static JsonTypeConverterWithDifferentTypes<MyCustomObject, String, String>
|
||||
$converter0 = const CustomJsonConverter();
|
||||
static JsonTypeConverterWithDifferentTypes<MyCustomObject?, String?, String?>
|
||||
$converter0n =
|
||||
JsonTypeConverterWithDifferentTypes.asNullable($converter0);
|
||||
}
|
||||
|
||||
class CategoryTodoCountViewData extends DataClass {
|
||||
|
|
|
@ -181,7 +181,7 @@ UsedTypeConverter? readTypeConverter(
|
|||
"potentially map to `null` which can't be stored in the database.");
|
||||
} else if (!canBeSkippedForNulls) {
|
||||
final alternative = appliesToJsonToo
|
||||
? 'JsonTypeConverter.asNullable'
|
||||
? 'JsonTypeConverterWithDifferentTypes.asNullable'
|
||||
: 'NullAwareTypeConverter.wrap';
|
||||
|
||||
reportError('This column is nullable, but the type converter has a non-'
|
||||
|
|
|
@ -27,18 +27,18 @@ class HelperLibrary {
|
|||
}
|
||||
|
||||
/// Converts the given Dart [type] into an instantiation of the
|
||||
/// `TypeConverter` class from drift.
|
||||
/// `JsonTypeConverter` class from drift.
|
||||
///
|
||||
/// Returns `null` if [type] is not a subtype of `TypeConverter`.
|
||||
InterfaceType? asJsonTypeConverter(DartType type) {
|
||||
final converter = helperLibrary.exportNamespace.get('JsonTypeConverter')
|
||||
as InterfaceElement;
|
||||
final converter = helperLibrary.exportNamespace
|
||||
.get('JsonTypeConverterWithDifferentTypes') as InterfaceElement;
|
||||
return type.asInstanceOf(converter);
|
||||
}
|
||||
|
||||
bool isJsonAwareTypeConverter(DartType? type, LibraryElement context) {
|
||||
final jsonMixin = helperLibrary.exportNamespace.get('JsonTypeConverter')
|
||||
as InterfaceElement;
|
||||
final jsonMixin = helperLibrary.exportNamespace
|
||||
.get('JsonTypeConverterWithDifferentTypes') as InterfaceElement;
|
||||
final jsonConverterType = jsonMixin.instantiate(
|
||||
typeArguments: [
|
||||
context.typeProvider.dynamicType,
|
||||
|
|
|
@ -143,7 +143,7 @@ class UsedTypeConverter {
|
|||
if (makeNullable) jsonDartType += '?';
|
||||
|
||||
if (alsoAppliesToJsonConversion) {
|
||||
return 'JsonTypeConverter<${dartTypeCode(makeNullable)}, $sqlDartType, $jsonDartType>';
|
||||
return 'JsonTypeConverterWithDifferentTypes<${dartTypeCode(makeNullable)}, $sqlDartType, $jsonDartType>';
|
||||
}
|
||||
return 'TypeConverter<${dartTypeCode(makeNullable)}, $sqlDartType>';
|
||||
}
|
||||
|
|
|
@ -336,7 +336,7 @@ class TableWriter extends TableOrViewWriter {
|
|||
converter.converterNameInCode(makeNullable: true);
|
||||
|
||||
final wrap = converter.alsoAppliesToJsonConversion
|
||||
? 'JsonTypeConverter.asNullable'
|
||||
? 'JsonTypeConverterWithDifferentTypes.asNullable'
|
||||
: 'NullAwareTypeConverter.wrap';
|
||||
|
||||
final code = '$wrap(${converter.fieldName})';
|
||||
|
|
|
@ -15,7 +15,7 @@ void main() {
|
|||
import 'package:drift/drift.dart';
|
||||
|
||||
TypeConverter<String, String> withoutJson() => throw 'stub';
|
||||
JsonTypeConverter<String, String, String> withJson() => throw 'stub';
|
||||
JsonTypeConverter<String, String> withJson() => throw 'stub';
|
||||
|
||||
class Users extends Table {
|
||||
TextColumn get foo => text().map(withoutJson())();
|
||||
|
|
Loading…
Reference in New Issue