feat: add generic key-path convenience API for structured collections#8
feat: add generic key-path convenience API for structured collections#8kielgillard wants to merge 1 commit intoordo-one:mainfrom
Conversation
Add ItemMatchResult<Item> and key-path overloads of topMatches/matches so users can search collections of structured items (e.g., [Instrument]) by a string property (e.g., \.name) without manually extracting and reassociating strings. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Summary of ChangesHello @kielgillard, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request enhances the fuzzy matching library by introducing a generic key-path based API. This allows users to perform fuzzy searches directly on specific string properties of custom data structures, eliminating the need for manual string extraction and reassociation. The change significantly improves the usability and type safety when working with structured collections, making the fuzzy matching process more streamlined and intuitive. Highlights
Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces a valuable convenience API for fuzzy matching on collections of structured items using key paths. The implementation is well-structured and accompanied by a comprehensive set of tests. My review includes feedback on a potential crash for an edge case (limit: 0) and a suggestion to improve the performance of the topMatches function. Overall, this is a great addition to the library.
| by keyPath: KeyPath<Item, String>, | ||
| against query: FuzzyQuery, | ||
| limit: Int = 10 | ||
| ) -> [ItemMatchResult<Item>] { |
There was a problem hiding this comment.
This function will crash if called with limit: 0. When limit is 0, the results array remains empty. The condition else if match.score > results[results.count - 1].match.score on line 71 will then attempt to access an index out of bounds, causing a fatal error.
Please add a guard at the beginning of the function to handle this edge case, for example:
guard limit > 0 else { return [] }| } | ||
| } else if match.score > results[results.count - 1].match.score { | ||
| results[results.count - 1] = result | ||
| results.sort { $0.match.score > $1.match.score } |
There was a problem hiding this comment.
Re-sorting the results array on every insertion after the limit is reached is inefficient. For a large number of candidates, this can become a performance bottleneck.
A more efficient approach is to replace the last element and then "bubble up" the new element to its correct sorted position. This changes the complexity of an insertion from O(L log L) to O(L), where L is the limit.
You could replace the sort with something like this:
var i = results.count - 1
while i > 0 && results[i].match.score > results[i - 1].match.score {
results.swapAt(i, i - 1)
i -= 1
}|
Would be nice with benchmarks for these too, also perhaps support for providing the matching buffer (just skimming on my phone). But overall interesting direction - will have a more proper look a bit later - away on a trip for a week now. |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #8 +/- ##
==========================================
- Coverage 98.87% 98.84% -0.02%
==========================================
Files 39 41 +2
Lines 8315 8482 +167
==========================================
+ Hits 8221 8384 +163
- Misses 94 98 +4
Continue to review full report in Codecov by Sentry.
🚀 New features to boost your workflow:
|
Description
Add ItemMatchResult and key-path overloads of topMatches/matches so users can search collections of structured items (e.g., [Instrument]) by a string property (e.g., .name) without manually extracting and reassociating strings.
I am opening this for feedback, I do not intend to submit for merging just yet.
How Has This Been Tested?
Claude generated a handful of tests that are passing.
Checklist
///comments) for any new public APIsswift test)