Skip to content

feat: Add Hooks class - #118

Open
NeaguGeorgiana23 wants to merge 26 commits into
mainfrom
hooks
Open

feat: Add Hooks class#118
NeaguGeorgiana23 wants to merge 26 commits into
mainfrom
hooks

Conversation

@NeaguGeorgiana23

@NeaguGeorgiana23 NeaguGeorgiana23 commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

This PR

Introduces the foundation for Hooks. It defines both general (non-templated) and typed hook interfaces, context propagation, evaluation details, and cross-stage state management.

  • General Hook Interface: Adds GeneralHook , a non-templated base class, allowing different types of hooks to be stored together. Uses abstract base interfaces (GeneralHookContext and GeneralFlagEvaluationDetails) that expose flag values as Value objects (GetDefaultValueAsValue(), GetValueAsValue()), allowing general hooks to observe any flag type without downcasting.
  • Typed Hook Interface: Adds the Hook<T> template class that overrides GeneralHook methods (final) with dynamic_cast downcasting and delegates to virtual Before, After, Error, and Finally methods accepting const HookContext<T>&.
  • Flag Evaluation Details: Adds FlagEvaluationDetails<T>, which extends ResolutionDetails<T> by adding the flag_key to associate the evaluation results with the requested flag.
  • Type Aliases: Provides convenient type aliases for common types (BoolHook, StringHook, IntHook, DoubleHook, ObjectHook and their corresponding FlagEvaluationDetails counterparts).
  • Unit Tests: Adds comprehensive unit tests for both FlagEvaluationDetails and the Hook lifecycle methods.

Fixes #117

NeaguGeorgiana23 and others added 15 commits July 7, 2026 13:19
Signed-off-by: NeaguGeorgiana23 <neagugeorgiana@google.com>
Signed-off-by: NeaguGeorgiana23 <neagugeorgiana@google.com>
Signed-off-by: NeaguGeorgiana23 <neagugeorgiana@google.com>
Signed-off-by: NeaguGeorgiana23 <neagugeorgiana@google.com>
Signed-off-by: NeaguGeorgiana23 <neagugeorgiana@google.com>
Signed-off-by: NeaguGeorgiana23 <neagugeorgiana@google.com>
Signed-off-by: NeaguGeorgiana23 <neagugeorgiana@google.com>
Signed-off-by: NeaguGeorgiana23 <neagugeorgiana@google.com>
Signed-off-by: NeaguGeorgiana23 <neagugeorgiana@google.com>
Signed-off-by: NeaguGeorgiana23 <neagugeorgiana@google.com>
Signed-off-by: NeaguGeorgiana23 <neagugeorgiana@google.com>
Signed-off-by: NeaguGeorgiana23 <neagugeorgiana@google.com>
Signed-off-by: NeaguGeorgiana23 <neagugeorgiana@google.com>
Signed-off-by: NeaguGeorgiana23 <neagugeorgiana@google.com>
@NeaguGeorgiana23
NeaguGeorgiana23 requested review from a team as code owners July 14, 2026 09:50
@coderabbitai

coderabbitai Bot commented Jul 14, 2026

Copy link
Copy Markdown

Review Change Stack

Warning

Review limit reached

@NeaguGeorgiana23, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 4 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 9c24a70b-781c-4ab1-a4bd-3de2f38476e7

📥 Commits

Reviewing files that changed from the base of the PR and between 48ddb25 and a351780.

📒 Files selected for processing (5)
  • openfeature/flag_evaluation_details.h
  • openfeature/general_hook.h
  • openfeature/hook.h
  • test/hook_context_test.cpp
  • test/hook_test.cpp
📝 Walkthrough

Walkthrough

Adds general hook context and evaluation-detail interfaces, typed implementations with value conversion, polymorphic hook lifecycle dispatch, Bazel targets, explicit template instantiations, and comprehensive tests across supported flag types.

Changes

Hook interface foundations

Layer / File(s) Summary
Flag evaluation details
openfeature/general_flag_evaluation_details.h, openfeature/flag_evaluation_details.*, openfeature/BUILD, test/flag_evaluation_details_test.cpp, test/BUILD
Adds general and typed evaluation-detail APIs, forwarding accessors, common type aliases, Bazel targets, and tests for boolean, string, integer, double, and object values.
Hook context and shared state
openfeature/general_hook_context.h, openfeature/hook_context.*, openfeature/BUILD, test/hook_context_test.cpp, test/BUILD
Makes typed hook contexts implement the general context interface, adds GetDefaultValueAsValue(), and tests typed and general accessors including structured object defaults.
Typed hook lifecycle
openfeature/general_hook.h, openfeature/hook.*, openfeature/BUILD, test/hook_test.cpp, test/BUILD
Adds general and typed lifecycle callbacks, conditional typed dispatch through dynamic_cast, explicit instantiations, and tests for callbacks, hints, polymorphism, and mismatched types.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Possibly related PRs

Suggested reviewers: oxddr

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 9.68% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title is concise and accurately summarizes the main change: adding hooks support.
Description check ✅ Passed The description is clearly related to the changeset and describes the added hook interfaces and tests.
Linked Issues check ✅ Passed The PR implements the hook interface foundation requested by issue #117, including typed hooks and evaluation details.
Out of Scope Changes check ✅ Passed The changes stay focused on hook infrastructure, related evaluation types, and their tests without unrelated additions.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai

coderabbitai Bot commented Jul 14, 2026

Copy link
Copy Markdown

Caution

Failed to replace (edit) comment. This is likely due to insufficient permissions or the comment being deleted.

Error details
putComment timed out

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Nitpick comments (2)
test/hook_test.cpp (1)

5-10: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Add missing <vector> include.

The test uses std::vector (Line 222) but relies on transitive inclusion. Including it explicitly prevents unexpected compilation failures if downstream headers are refactored.

♻️ Proposed fix
 `#include` <any>
 `#include` <memory>
 `#include` <optional>
 `#include` <stdexcept>
 `#include` <string>
+#include <vector>
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@test/hook_test.cpp` around lines 5 - 10, Add an explicit <vector>
standard-library include alongside the existing headers in hook_test.cpp, so the
std::vector usage in the test is directly supported rather than relying on
transitive includes.
openfeature/base_hook.h (1)

12-15: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Delete move operations for polymorphic base class.

BaseHook deletes copy semantics but explicitly defaults move semantics. Since derived classes like Hook explicitly delete move operations, it is safer and more consistent to delete move semantics at the base class level to entirely prevent unintended slicing or partial moves.

♻️ Proposed refactor
-  BaseHook(const BaseHook&) = delete;
-  BaseHook(BaseHook&&) = default;
-  BaseHook& operator=(const BaseHook&) = delete;
-  BaseHook& operator=(BaseHook&&) = default;
+  BaseHook(const BaseHook&) = delete;
+  BaseHook(BaseHook&&) = delete;
+  BaseHook& operator=(const BaseHook&) = delete;
+  BaseHook& operator=(BaseHook&&) = delete;
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@openfeature/base_hook.h` around lines 12 - 15, Delete the move constructor
and move assignment operator declarations in BaseHook, replacing the defaulted
move operations while preserving its existing deleted copy operations. This
ensures the polymorphic base class cannot be moved or partially transferred,
consistent with derived classes such as Hook.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@openfeature/hook_data.h`:
- Around line 22-29: Add a const overload of HookData::GetAs matching the
existing non-const lookup and typed any_cast behavior, returning a const T
pointer from const HookData instances. Preserve the current missing-key behavior
by returning nullptr.

In `@openfeature/hook.h`:
- Around line 34-38: Make the Before hook accept HookContext<T> as a const
reference, matching the immutability contract and the existing After, Error, and
Finally signatures. Update the corresponding mocked override in
test/hook_test.cpp to use the same const-reference signature; both affected
sites require this change.

---

Nitpick comments:
In `@openfeature/base_hook.h`:
- Around line 12-15: Delete the move constructor and move assignment operator
declarations in BaseHook, replacing the defaulted move operations while
preserving its existing deleted copy operations. This ensures the polymorphic
base class cannot be moved or partially transferred, consistent with derived
classes such as Hook.

In `@test/hook_test.cpp`:
- Around line 5-10: Add an explicit <vector> standard-library include alongside
the existing headers in hook_test.cpp, so the std::vector usage in the test is
directly supported rather than relying on transitive includes.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 381c79ae-6a33-413a-ad1c-0990a4889791

📥 Commits

Reviewing files that changed from the base of the PR and between ce590f4 and 3b93d77.

📒 Files selected for processing (17)
  • openfeature/BUILD
  • openfeature/base_hook.h
  • openfeature/flag_evaluation_details.cpp
  • openfeature/flag_evaluation_details.h
  • openfeature/flag_type_value.h
  • openfeature/hook.cpp
  • openfeature/hook.h
  • openfeature/hook_context.cpp
  • openfeature/hook_context.h
  • openfeature/hook_data.cpp
  • openfeature/hook_data.h
  • openfeature/hook_hints.h
  • test/BUILD
  • test/flag_evaluation_details_test.cpp
  • test/hook_context_test.cpp
  • test/hook_data_test.cpp
  • test/hook_test.cpp

Comment thread openfeature/hook_data.h
Comment on lines +22 to +29
template <typename T>
T* GetAs(std::string_view key) {
auto it_key = data_.find(std::string(key));
if (it_key != data_.end()) {
return std::any_cast<T>(&it_key->second);
}
return nullptr;
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

Add a const overload for GetAs.

The standard OpenFeature Hook lifecycle methods (After, Error, and Finally) receive a const HookContext<T>& ctx. This likely limits state retrieval to a const HookData&. Without a const overload for GetAs, hook authors will not be able to use typed retrieval during these critical stages to read the state they initialized in the Before hook.

🛠️ Proposed fix
   template <typename T>
   T* GetAs(std::string_view key) {
     auto it_key = data_.find(std::string(key));
     if (it_key != data_.end()) {
       return std::any_cast<T>(&it_key->second);
     }
     return nullptr;
   }
+
+  template <typename T>
+  const T* GetAs(std::string_view key) const {
+    auto it_key = data_.find(std::string(key));
+    if (it_key != data_.end()) {
+      return std::any_cast<T>(&it_key->second);
+    }
+    return nullptr;
+  }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
template <typename T>
T* GetAs(std::string_view key) {
auto it_key = data_.find(std::string(key));
if (it_key != data_.end()) {
return std::any_cast<T>(&it_key->second);
}
return nullptr;
}
template <typename T>
T* GetAs(std::string_view key) {
auto it_key = data_.find(std::string(key));
if (it_key != data_.end()) {
return std::any_cast<T>(&it_key->second);
}
return nullptr;
}
template <typename T>
const T* GetAs(std::string_view key) const {
auto it_key = data_.find(std::string(key));
if (it_key != data_.end()) {
return std::any_cast<T>(&it_key->second);
}
return nullptr;
}
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@openfeature/hook_data.h` around lines 22 - 29, Add a const overload of
HookData::GetAs matching the existing non-const lookup and typed any_cast
behavior, returning a const T pointer from const HookData instances. Preserve
the current missing-key behavior by returning nullptr.

Comment thread openfeature/hook.h
Signed-off-by: NeaguGeorgiana23 <neagugeorgiana@google.com>
Signed-off-by: NeaguGeorgiana23 <neagugeorgiana@google.com>
Signed-off-by: NeaguGeorgiana23 <neagugeorgiana@google.com>
Signed-off-by: NeaguGeorgiana23 <neagugeorgiana@google.com>
Signed-off-by: NeaguGeorgiana23 <neagugeorgiana@google.com>
Signed-off-by: NeaguGeorgiana23 <115723925+NeaguGeorgiana23@users.noreply.github.com>
This was referenced Jul 15, 2026
NeaguGeorgiana23 and others added 4 commits July 28, 2026 11:12
Signed-off-by: NeaguGeorgiana23 <neagugeorgiana@google.com>
Signed-off-by: NeaguGeorgiana23 <neagugeorgiana@google.com>
Signed-off-by: NeaguGeorgiana23 <neagugeorgiana@google.com>
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.

Implement Hook interface

1 participant