Skip to content

refactor: deduplicate OCSF builder setters and persistence helpers#1800

Merged
TaylorMutch merged 1 commit into
NVIDIA:mainfrom
ericcurtin:refactor/deduplicate-builder-setters-and-persistence
Jun 8, 2026
Merged

refactor: deduplicate OCSF builder setters and persistence helpers#1800
TaylorMutch merged 1 commit into
NVIDIA:mainfrom
ericcurtin:refactor/deduplicate-builder-setters-and-persistence

Conversation

@ericcurtin

Copy link
Copy Markdown
Contributor

Summary

Eliminate three categories of repeated code identified across the codebase: OCSF event builder setter methods, a protobuf decode-and-hydrate block in the persistence layer, and mechanical enum-dispatch arms on the `Store` type.

Related Issue

N/A — proactive deduplication refactor.

Changes

`crates/openshell-ocsf/src/builders/` — six new `macro_rules!` macros in `mod.rs`:

  • `impl_activity_setter!` — generates the `activity()` setter (applied to all 6 builders)
  • `impl_action_disposition_setters!` — generates `action()` + `disposition()` (5 builders)
  • `impl_actor_process_setter!` — generates `actor_process()` (4 builders)
  • `impl_dst_endpoint_setter!` — generates `dst_endpoint()` (3 builders)
  • `impl_src_endpoint_addr_setter!` — generates `src_endpoint_addr()` (network, ssh)
  • `impl_firewall_rule_setter!` — generates `firewall_rule()` (network, http)

~55 hand-written, identical method bodies removed from `network.rs`, `http.rs`, `ssh.rs`, `process.rs`, `finding.rs`, and `lifecycle.rs`.

`crates/openshell-server/src/persistence/mod.rs`:

  • Extract private `decode_record()` helper — the three-line decode + `set_resource_version` block was duplicated in `get_message`, `get_message_by_name`, `list_messages`, and `list_messages_with_selector`. Each method now uses `.map(decode_record).transpose()` or `.into_iter().map(decode_record).collect()`.
  • Add `store_dispatch!` macro — eliminates the 13 identical `match self { Postgres(s) => s.method(...).await, Sqlite(s) => ... }` arms that covered every public method on `Store`.

Net: -93 lines (180 added, 273 removed across 8 files).

Testing

  • `mise run pre-commit` passes
  • `cargo test -p openshell-ocsf` — 120 tests pass
  • `cargo test -p openshell-server` — all tests pass
  • E2E tests added/updated (not applicable — no behavioral change)

Checklist

  • Follows Conventional Commits
  • Commits are signed off (DCO)

Eliminate three categories of repeated code:

1. OCSF builder setter methods
   Add six macros to builders/mod.rs (impl_activity_setter!,
   impl_action_disposition_setters!, impl_actor_process_setter!,
   impl_dst_endpoint_setter!, impl_src_endpoint_addr_setter!,
   impl_firewall_rule_setter!) that generate the identical one-liner
   setters duplicated across network, http, ssh, process, finding,
   and lifecycle builders. Removes ~55 hand-written method bodies.

2. Persistence protobuf decode-and-hydrate block
   Extract a private decode_record<T>() helper that decodes an
   ObjectRecord into a protobuf message and hydrates resource_version.
   The identical three-line block was repeated in get_message,
   get_message_by_name, list_messages, and list_messages_with_selector.
   Each method now delegates to decode_record via .map().transpose()
   or .into_iter().map().collect().

3. Store enum dispatch arms
   Add a store_dispatch! macro that expands
   match self { Postgres(s) => s.method(...).await, Sqlite(s) => ... }.
   Applied to all 13 dispatch-only methods on Store, removing ~80
   lines of mechanical match arms with zero logic.
@copy-pr-bot

copy-pr-bot Bot commented Jun 7, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@TaylorMutch

Copy link
Copy Markdown
Collaborator

/ok to test 1abffe6

@TaylorMutch TaylorMutch self-assigned this Jun 8, 2026
@TaylorMutch TaylorMutch merged commit 1399f37 into NVIDIA:main Jun 8, 2026
28 checks passed
@ericcurtin ericcurtin deleted the refactor/deduplicate-builder-setters-and-persistence branch June 8, 2026 20:53
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.

2 participants