Skip to content

Commit

Permalink
feat: add tpe convert to annotation and basic tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dickermoshe committed Sep 22, 2024
1 parent 4bf6d83 commit aa65dbd
Show file tree
Hide file tree
Showing 3 changed files with 613 additions and 122 deletions.
62 changes: 50 additions & 12 deletions drift/lib/src/runtime/manager/annotate.dart
Original file line number Diff line number Diff line change
@@ -1,33 +1,71 @@
// ignore_for_file: public_member_api_docs, sort_constructors_first
part of 'manager.dart';

class Annotation<T extends Object> {
final List<JoinBuilder> _joinBuilders;
sealed class _BaseAnnotation<SqlType extends Object, $Table extends Table> {
Expression<SqlType> get _expression;
final Set<JoinBuilder> _joinBuilders;
_BaseAnnotation(this._joinBuilders);
}

final Expression<T> _expression;
class Annotation<SqlType extends Object, $Table extends Table>
extends _BaseAnnotation<SqlType, $Table> {
@override
final Expression<SqlType> _expression;

ColumnFilters<T> get filter {
ColumnFilters<SqlType> get filter {
return ColumnFilters(_expression);
}

ColumnOrderings<T> get order {
return ColumnOrderings(_expression);
SqlType? read(BaseReferences refs) {
return refs.$_typedResult.read(_expression);
}

const Annotation(this._expression, this._joinBuilders);
Annotation(this._expression, super._joinBuilders);

@override
bool operator ==(covariant Annotation<T> other) {
bool operator ==(Object other) {
if (identical(this, other)) return true;
return other is Annotation<SqlType, $Table> &&
other._expression == _expression &&
SetEquality<JoinBuilder>().equals(other._joinBuilders, _joinBuilders);
}

@override
int get hashCode => _expression.hashCode ^ _joinBuilders.hashCode;
}

class AnnotationWithConverter<DartType, SqlType extends Object,
$Table extends Table> extends _BaseAnnotation<SqlType, $Table> {
@override
final GeneratedColumnWithTypeConverter<DartType, SqlType> _expression;

return other._expression == _expression;
ColumnWithTypeConverterFilters<DartType, DartType, SqlType> get filter {
return ColumnWithTypeConverterFilters(_expression);
}

final DartType Function(SqlType) converter;

AnnotationWithConverter(this._expression, super._joinBuilders,
{required this.converter});

@override
int get hashCode => _expression.hashCode;
bool operator ==(Object other) {
if (identical(this, other)) return true;

T? read(BaseReferences refs) {
return refs.$_typedResult.read(_expression);
return other is AnnotationWithConverter<DartType, SqlType, $Table> &&
other._expression == _expression &&
SetEquality<JoinBuilder>().equals(other._joinBuilders, _joinBuilders);
}

@override
int get hashCode => _expression.hashCode ^ _joinBuilders.hashCode;

DartType? read(BaseReferences refs) {
final dartType = refs.$_typedResult.read(_expression);
if (dartType == null) {
return null;
}
return converter(dartType);
}
}

Expand Down
18 changes: 15 additions & 3 deletions drift/lib/src/runtime/manager/manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ abstract class BaseTableManager<
$DataclassWithReferences,
$DataclassWithReferences,
$CreatePrefetchHooksCallback>
withAnnotations(Iterable<Annotation> annotations) {
withAnnotations(Iterable<_BaseAnnotation<Object, $Table>> annotations) {
final joinBuilders =
annotations.map((e) => e._joinBuilders).expand((e) => e).toSet();
final addedColumns = annotations.map((e) => e._expression).toSet();
Expand Down Expand Up @@ -993,11 +993,23 @@ abstract class RootTableManager<
.batch((b) => b.replaceAll($state._tableAsTableInfo, entities));
}

Annotation<T> annotation<T extends Object>(
Annotation<T, $Table> annotation<T extends Object>(
Expression<T> Function($AnnotationComposer a) a,
) {
final composer = $state.createAnnotationComposer();
final expression = a(composer);
return Annotation(expression, composer.$joinBuilders);
return Annotation(expression, composer.$joinBuilders.toSet());
}

AnnotationWithConverter<DartType, SqlType, $Table>
annotationWithConverter<DartType, SqlType extends Object>(
GeneratedColumnWithTypeConverter<DartType, SqlType> Function(
$AnnotationComposer a)
a,
) {
final composer = $state.createAnnotationComposer();
final expression = a(composer);
return AnnotationWithConverter(expression, composer.$joinBuilders.toSet(),
converter: (p0) => expression.converter.fromSql(p0));
}
}
Loading

0 comments on commit aa65dbd

Please sign in to comment.