Skip to content

Add cbor_map_get with parameterized equality function - #409

Merged
PJK merged 3 commits into
masterfrom
cbor-map-get
Mar 22, 2026
Merged

PJK merged 3 commits into
masterfrom
cbor-map-get

Conversation

@PJK

@PJK PJK commented Mar 22, 2026

Copy link
Copy Markdown
Owner

Summary

  • Adds cbor_map_get(const cbor_item_t *map, const cbor_item_t *key, bool (*eq)(const cbor_item_t *, const cbor_item_t *)) to maps.h/maps.c
  • 11 tests in test/map_get_test.c
  • Documentation in doc/source/api/type_5_maps.rst
  • Changelog entry

Design rationale

The equality function is parameterized on purpose. The two main reasons:

  1. 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.

  2. Any data-model semantics can be plugged in. Callers can pass cbor_structurally_equal for 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

// Structural equality (encoding-level): uint8(1) ≠ uint16(1)
cbor_item_t *v = cbor_map_get(map, key, cbor_structurally_equal);

// Custom comparator that ignores integer encoding width
cbor_item_t *v = cbor_map_get(map, key, my_int_value_eq);

The returned item has its reference count incremented; caller must cbor_decref it. Returns NULL if no key matched.

Closes #96 (alongside #408).

Test plan

  • ctest -R map_get_test passes (11/11)
  • bash clang-format.sh produces no diff
  • CI green

🤖 Generated with Claude Code

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

codecov Bot commented Mar 22, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.59%. Comparing base (dd5676a) to head (31b52ef).
⚠️ Report is 14 commits behind head on master.

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

PJK and others added 2 commits March 22, 2026 18:40
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@PJK
PJK merged commit d5222af into master Mar 22, 2026
18 checks passed
@PJK
PJK deleted the cbor-map-get branch March 22, 2026 18:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

⌂ 换网址