Skip to content

How to combine, merge, or otherwise edit Companion entries? #3154

Answered by simolus3
wanglophile asked this question in Q&A
Discussion options

You must be logged in to vote

I didn't try it, but I think this should work:

extension Merge<T> on Insertable<T> {
  Insertable<T> merge(Insertable<T> other) {
    return _MergeInsertable(this, other);
  }
}

final class _MergeInsertable<T> implements Insertable<T> {
  final Insertable<T> _base;
  final Insertable<T> _mergedIn;

  _MergeInsertable(this._base, this._mergedIn);

  @override
  Map<String, Expression<Object>> toColumns(bool nullToAbsent) {
    return {
      ..._base.toColumns(nullToAbsent),
      ..._mergedIn.toColumns(nullToAbsent),
    };
  }
}

I'm not entirely convinced that this should be a part of drift though. We have copyWith, but I don't think merges are used that often.

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@wanglophile
Comment options

@wanglophile
Comment options

@simolus3
Comment options

Answer selected by wanglophile
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants