mirror of https://github.com/AMT-Cheif/drift.git
Add JsonTypeConverter support to enums
This commit is contained in:
parent
6503624ff1
commit
c1ee1f7274
|
@ -83,7 +83,8 @@ mixin JsonTypeConverter<D, S> implements JsonTypeConverter2<D, S, S> {
|
|||
|
||||
/// Implementation for an enum to int converter that uses the index of the enum
|
||||
/// as the value stored in the database.
|
||||
class EnumIndexConverter<T extends Enum> extends TypeConverter<T, int> {
|
||||
class EnumIndexConverter<T extends Enum> extends TypeConverter<T, int>
|
||||
with JsonTypeConverter<T, int> {
|
||||
/// All values of the enum.
|
||||
final List<T> values;
|
||||
|
||||
|
@ -103,7 +104,8 @@ class EnumIndexConverter<T extends Enum> extends TypeConverter<T, int> {
|
|||
|
||||
/// Implementation for an enum to string converter that uses the name of the
|
||||
/// enum as the value stored in the database.
|
||||
class EnumNameConverter<T extends Enum> extends TypeConverter<T, String> {
|
||||
class EnumNameConverter<T extends Enum> extends TypeConverter<T, String>
|
||||
with JsonTypeConverter<T, String> {
|
||||
/// All values of the enum.
|
||||
final List<T> values;
|
||||
|
||||
|
|
|
@ -337,6 +337,7 @@ class ColumnParser {
|
|||
|
||||
final sqlName = foundExplicitName ?? ReCase(getter.name.lexeme).snakeCase;
|
||||
final sqlType = _startMethodToColumnType(foundStartMethod);
|
||||
final helper = await _resolver.resolver.driver.loadKnownTypes();
|
||||
|
||||
AppliedTypeConverter? converter;
|
||||
if (mappedAs != null) {
|
||||
|
@ -347,7 +348,7 @@ class ColumnParser {
|
|||
nullable,
|
||||
(message) => _resolver.reportError(
|
||||
DriftAnalysisError.inDartAst(element, mappedAs!, message)),
|
||||
await _resolver.resolver.driver.loadKnownTypes(),
|
||||
helper,
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -366,6 +367,7 @@ class ColumnParser {
|
|||
remainingExpr.typeArguments ?? remainingExpr.methodName, msg)),
|
||||
enumType,
|
||||
EnumType.intEnum,
|
||||
helper,
|
||||
);
|
||||
} else if (foundStartMethod == _startTextEnum) {
|
||||
if (converter != null) {
|
||||
|
@ -382,6 +384,7 @@ class ColumnParser {
|
|||
remainingExpr.typeArguments ?? remainingExpr.methodName, msg)),
|
||||
enumType,
|
||||
EnumType.textEnum,
|
||||
helper,
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
@ -28,6 +28,7 @@ class DriftTableResolver extends LocalElementResolver<DiscoveredDriftTable> {
|
|||
Table table;
|
||||
final references = <DriftElement>{};
|
||||
final stmt = discovered.sqlNode;
|
||||
final helper = await resolver.driver.loadKnownTypes();
|
||||
|
||||
try {
|
||||
final reader = SchemaFromCreateTable(
|
||||
|
@ -74,6 +75,7 @@ class DriftTableResolver extends LocalElementResolver<DiscoveredDriftTable> {
|
|||
DriftAnalysisError.inDriftFile(column.definition ?? stmt, msg)),
|
||||
dartClass.classElement.thisType,
|
||||
type == DriftSqlType.int ? EnumType.intEnum : EnumType.textEnum,
|
||||
helper,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -230,7 +230,9 @@ AppliedTypeConverter readEnumConverter(
|
|||
void Function(String) reportError,
|
||||
DartType dartEnumType,
|
||||
EnumType columnEnumType,
|
||||
KnownDriftTypes helper,
|
||||
) {
|
||||
final typeProvider = helper.helperLibrary.typeProvider;
|
||||
if (dartEnumType is! InterfaceType) {
|
||||
reportError('Not a class: `$dartEnumType`');
|
||||
}
|
||||
|
@ -261,7 +263,9 @@ AppliedTypeConverter readEnumConverter(
|
|||
return AppliedTypeConverter(
|
||||
expression: expression,
|
||||
dartType: dartEnumType,
|
||||
jsonType: null,
|
||||
jsonType: columnEnumType == EnumType.intEnum
|
||||
? typeProvider.intType
|
||||
: typeProvider.stringType,
|
||||
sqlType: columnEnumType == EnumType.intEnum
|
||||
? DriftSqlType.int
|
||||
: DriftSqlType.string,
|
||||
|
|
|
@ -133,8 +133,11 @@ class DataClassWriter {
|
|||
|
||||
final typeConverter = column.typeConverter;
|
||||
if (typeConverter != null && typeConverter.alsoAppliesToJsonConversion) {
|
||||
final type =
|
||||
var type =
|
||||
_emitter.dartCode(AnnotatedDartCode.type(typeConverter.jsonType!));
|
||||
if (column.nullable) {
|
||||
type = '$type?';
|
||||
}
|
||||
|
||||
final fromConverter = "serializer.fromJson<$type>(json['$jsonKey'])";
|
||||
final converterField = _converter(column);
|
||||
|
|
Loading…
Reference in New Issue