Skip to content

Add coalesce free function and Row method #1645

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Oct 6, 2024

Conversation

philmitchell
Copy link

As discussed with @groue, added coalesce methods as a best-effort alternative in cases where nil-coalescing fails. (I did investigate addressing the failure directly, but given the double optionals that represent null values (.some(.none)), it seems unlikely to bear fruit.)

Pull Request Checklist

  • CONTRIBUTING: You have read https://github.com/groue/GRDB.swift/blob/master/CONTRIBUTING.md
  • BRANCH: This pull request is submitted against the development branch.
  • DOCUMENTATION: Inline documentation has been updated.
  • DOCUMENTATION: README.md or another dedicated guide has been updated.
  • TESTS: Changes are tested.
  • TESTS: The make smokeTest terminal command runs without failure.

@groue
Copy link
Owner

groue commented Sep 30, 2024

Hello @philmitchell, I'm glad to hear about you :) Please give me a couple of days before I can review the pull request.

groue added 8 commits October 6, 2024 12:19
This allows the user to call those methods with an Array, or any other collection at hand (including lazy ones)

This change makes `return coalesce(columns.lazy.map { $0.name })` iterate lazily. Before this commit, `map` was returning a non-lazy Array.
Also, remove "essential".
@groue
Copy link
Owner

groue commented Oct 6, 2024

Welcome to the GRDB contributors, @philmitchell! 🙂

I added a few commits, and we're going to be able to merge.

c6781f4 replaces arrays like [String] with generic collections some Collection<String>. It helps users feed the methods with whatever they have at hand: an Array, but also a subsequence, etc. This is also how Row.coalesce can support lazy collections, as expected (there's no need to iterate the whole collection when we can stop on the first non-null value).

761b00f and a17d72e test and fix the original code so that we can coalesce collections with less than 2 elements. Since we provide apis that accept a collection of values, we must wonder if it works with 0, 1, 2, etc.

@groue groue merged commit 97c0c72 into groue:development Oct 6, 2024
8 checks passed
@groue
Copy link
Owner

groue commented Oct 6, 2024

⛵ Shipped in 7.0.0-beta.3

@philmitchell
Copy link
Author

Thanks, @groue, it's really a privilege! Sorry about all the cleanup you had to do, I will never be offended if you reject a PR w/ suggestions :)

Hopefully we will figure out something around GRDB 7 and SQLCipher so that I can actually use this!

@groue
Copy link
Owner

groue commented Oct 7, 2024

You're very welcome, because coalesce addresses a real need! 😄 I was looking forward to merge, so I made a few changes on the go.

Hopefully we will figure out something around GRDB 7 and SQLCipher so that I can actually use this!

Yes, that's a big topic. Last year's development efforts have been absorbed by Swift Concurrency. I hope the path is now cleared for new improvements, such as restoring SQLCipher support in a way or another.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants