fox-it / flow.record

Recordization library
GNU Affero General Public License v3.0
7 stars 9 forks source link

Make records hashable #107

Closed JSCU-CNI closed 6 months ago

JSCU-CNI commented 6 months ago

This PR implements the __hash__ functions for the Record class, enabling grouping records in sets. This enables set operations on collections of records, e.g:

records_a = set(target_one.users())
records_b = set(target_two.users())

diff = records_a.symmetric_difference(records_b)

For some record types though, there will be fields that you might want to ignore, such as _generated and possibly _source when comparing record output from two different targets. Preferably you want the fields to ignore to be configurable at runtime. Therefore, we opted to add an environment variable FLOW_RECORD_IGNORE which can contain a list of comma-separated field names to ignore.

We implemented a check where hashed records are considered 'immutable' and a ValueError is raised if a property is changed after __hash__ has been called on a Record instance. This may have some performance impact though. Feel free to suggest alternative approaches.

codecov[bot] commented 6 months ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 80.46%. Comparing base (523b96c) to head (71e7a16).

Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #107 +/- ## ========================================== + Coverage 80.41% 80.46% +0.05% ========================================== Files 34 34 Lines 3165 3174 +9 ========================================== + Hits 2545 2554 +9 Misses 620 620 ``` | [Flag](https://app.codecov.io/gh/fox-it/flow.record/pull/107/flags?src=pr&el=flags&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=fox-it) | Coverage Δ | | |---|---|---| | [unittests](https://app.codecov.io/gh/fox-it/flow.record/pull/107/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=fox-it) | `80.46% <100.00%> (+0.05%)` | :arrow_up: | Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=fox-it#carryforward-flags-in-the-pull-request-comment) to find out more.

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.