Add JsonTypeConverter support to enums

This commit is contained in:
Alexander Wilde 2022-12-01 02:04:16 +00:00
parent 6503624ff1
commit c1ee1f7274
5 changed files with 19 additions and 5 deletions

View File

@ -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;

View File

@ -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,
);
}

View File

@ -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,
);
}
}

View File

@ -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,

View File

@ -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);