Repository navigation
Add cbor_map_get with parameterized equality function - #409
Merged
Merged
Conversation
Adds cbor_map_get(map, key, eq), a linear-scan map lookup that accepts a caller-supplied equality predicate. The equality function is deliberately not fixed: most applications constrain their key types and can use a cheaper type-specific comparator; the parameter also allows any desired data-model semantics (e.g. width-ignoring integer equality) without library changes. Pass cbor_structurally_equal for exact encoding-level matching, or supply a custom predicate for application-specific needs. Includes 11 tests covering: basic lookup, missing key, empty map, wrong-type key, encoding-width sensitivity with structural equality, a custom width-ignoring integer comparator, a type-specific string comparator, first-match-wins on duplicate keys, refcount contract, indefinite maps, and negint keys. Related to #96. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #409 +/- ##
==========================================
+ Coverage 99.53% 99.59% +0.05%
==========================================
Files 20 20
Lines 1731 1737 +6
==========================================
+ Hits 1723 1730 +7
+ Misses 8 7 -1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
cbor_map_get(const cbor_item_t *map, const cbor_item_t *key, bool (*eq)(const cbor_item_t *, const cbor_item_t *))tomaps.h/maps.ctest/map_get_test.cdoc/source/api/type_5_maps.rstDesign rationale
The equality function is parameterized on purpose. The two main reasons:
Type-specific comparators are cheaper. Most protocols constrain map keys to a single type (e.g., WebAuthn/COSE use small integers; JSON-derived formats use text strings). A comparator that only handles one type avoids the full dispatch overhead of a generic comparator and can be inlined by the compiler.
Any data-model semantics can be plugged in. Callers can pass
cbor_structurally_equalfor exact encoding-level matching, a width-ignoring integer comparator, a case-insensitive string comparator, or anything else — without the library needing to anticipate every use case.Usage
The returned item has its reference count incremented; caller must
cbor_decrefit. ReturnsNULLif no key matched.Closes #96 (alongside #408).
Test plan
ctest -R map_get_testpasses (11/11)bash clang-format.shproduces no diff🤖 Generated with Claude Code