Skip to content

feat: Add HookContext class#115

Open
NeaguGeorgiana23 wants to merge 14 commits into
mainfrom
hooks_context
Open

feat: Add HookContext class#115
NeaguGeorgiana23 wants to merge 14 commits into
mainfrom
hooks_context

Conversation

@NeaguGeorgiana23

Copy link
Copy Markdown
Contributor

This PR

Implements the HookContext<T> template class and unit tests to support the OpenFeature flag evaluation lifecycle hooks specification.

  • HookContext<T> Implementation:
    • Implements the core HookContext template class encapsulating flag evaluation state (flag_key, type, default_value, EvaluationContext, client/provider Metadata, and HookData).
    • Provides full getter accessors and SetEvaluationContext(ctx) to allow hook mutations.
    • Defines standard type aliases (BoolHookContext, StringHookContext, IntHookContext, DoubleHookContext, and ObjectHookContext).
    • Adds explicit template instantiations for standard flag types (bool, std::string, int64_t, double, Value).

Related Issues

Fixes #111

NeaguGeorgiana23 and others added 12 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>
@NeaguGeorgiana23 NeaguGeorgiana23 requested review from a team as code owners July 13, 2026 08:57
@coderabbitai

coderabbitai Bot commented Jul 13, 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: 44 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: 1a79ba57-9233-4406-8758-3206deee62f6

📥 Commits

Reviewing files that changed from the base of the PR and between e333df0 and 45ca64a.

📒 Files selected for processing (2)
  • openfeature/hook_context.cpp
  • test/hook_context_test.cpp
📝 Walkthrough

Walkthrough

Adds public hook data structures, typed hook contexts, flag value types, hook hints, Bazel targets, and GoogleTest coverage for storage, context updates, metadata, and shared state.

Changes

Hook data structures

Layer / File(s) Summary
Public hook contracts
openfeature/flag_type_value.h, openfeature/hook_hints.h, openfeature/hook_data.h, openfeature/hook_context.h
Defines FlagValueType, HookHints, HookData, and typed HookContext APIs with common aliases.
Hook storage and context implementation
openfeature/hook_data.cpp, openfeature/hook_context.cpp
Implements typed map storage, retrieval, context accessors, evaluation-context replacement, and explicit context instantiations.
Bazel wiring and behavioral validation
openfeature/BUILD, test/BUILD, test/hook_data_test.cpp, test/hook_context_test.cpp
Adds Bazel libraries and tests covering HookData storage and overwrite behavior plus HookContext initialization and mutation.ren

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

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Linked Issues check ⚠️ Warning The PR covers hook hints, hook data, and hook context, but it does not implement the linked evaluation option requirement. Add the evaluation option data structure or update the linked issue scope to exclude it.
Docstring Coverage ⚠️ Warning Docstring coverage is 5.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title matches the main change: adding HookContext, though the PR also adds related hook data structures.
Description check ✅ Passed The description is directly related to the change and accurately summarizes the HookContext work.
Out of Scope Changes check ✅ Passed All added files support hook context/data or their tests; no unrelated changes stand out.

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.

Signed-off-by: NeaguGeorgiana23 <neagugeorgiana@google.com>

@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: 1

🧹 Nitpick comments (2)
openfeature/hook_data.h (1)

22-29: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Add a const overload for GetAs.

GetAs is non-const only, so callers with a const HookData& must fall back to Get + manual std::any_cast. Adding a const overload is straightforward and improves API ergonomics for read-only contexts.

♻️ Proposed const overload
   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 alongside the existing non-const template, accepting the same
key and returning a const T pointer. Implement it using the const data_ lookup
and std::any_cast<const T> semantics, while preserving the current nullptr
behavior for missing keys.
test/hook_context_test.cpp (1)

34-133: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Consider typed/parameterized tests to reduce repetition.

The five ConstructorAndAccessorsFor* tests follow an identical assertion pattern, differing only in type, flag key, and default value. GoogleTest's TYPED_TEST_SUITE / TYPED_TEST would consolidate these into a single test body, reducing duplication and making it easier to add new types.

🤖 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_context_test.cpp` around lines 34 - 133, Consolidate the five
duplicated ConstructorAndAccessorsForBool, ConstructorAndAccessorsForString,
ConstructorAndAccessorsForInteger, ConstructorAndAccessorsForDouble, and
ConstructorAndAccessorsForObject tests into a GoogleTest typed or parameterized
test. Define per-type test data for the flag type, key, default value, and
expected value access, then reuse one shared assertion body while preserving all
existing coverage.
🤖 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 `@test/hook_context_test.cpp`:
- Around line 1-13: Update the cc_test target in test/BUILD that compiles
hook_context_test.cpp to add direct dependencies on //openfeature:flag_metadata,
//openfeature:flag_type_value, and //openfeature:metadata, matching the directly
included headers while preserving the existing hook_context dependency.

---

Nitpick comments:
In `@openfeature/hook_data.h`:
- Around line 22-29: Add a const overload of HookData::GetAs alongside the
existing non-const template, accepting the same key and returning a const T
pointer. Implement it using the const data_ lookup and std::any_cast<const T>
semantics, while preserving the current nullptr behavior for missing keys.

In `@test/hook_context_test.cpp`:
- Around line 34-133: Consolidate the five duplicated
ConstructorAndAccessorsForBool, ConstructorAndAccessorsForString,
ConstructorAndAccessorsForInteger, ConstructorAndAccessorsForDouble, and
ConstructorAndAccessorsForObject tests into a GoogleTest typed or parameterized
test. Define per-type test data for the flag type, key, default value, and
expected value access, then reuse one shared assertion body while preserving all
existing coverage.
🪄 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: 284b5272-5b48-4c42-9d9a-13e771dfa8d6

📥 Commits

Reviewing files that changed from the base of the PR and between 8254496 and e333df0.

📒 Files selected for processing (10)
  • openfeature/BUILD
  • openfeature/flag_type_value.h
  • openfeature/hook_context.cpp
  • openfeature/hook_context.h
  • openfeature/hook_data.cpp
  • openfeature/hook_data.h
  • openfeature/hook_hints.h
  • test/BUILD
  • test/hook_context_test.cpp
  • test/hook_data_test.cpp

Comment on lines +1 to +13
#include "openfeature/hook_context.h"

#include <gtest/gtest.h>

#include <memory>
#include <string>

#include "openfeature/evaluation_context.h"
#include "openfeature/flag_metadata.h"
#include "openfeature/flag_type_value.h"
#include "openfeature/hook_data.h"
#include "openfeature/metadata.h"
#include "openfeature/value.h"

@coderabbitai coderabbitai Bot Jul 13, 2026

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win

Declare direct Bazel deps for all directly-included headers.

The test file directly includes openfeature/flag_metadata.h, openfeature/flag_type_value.h, and openfeature/metadata.h, but the cc_test target in test/BUILD does not list //openfeature:flag_metadata, //openfeature:flag_type_value, or //openfeature:metadata as direct deps. They are only available transitively through //openfeature:hook_context. This works today but will break under strict dependency checking.

🔧 Add missing deps to test/BUILD
 cc_test(
     name = "hook_context_test",
     srcs = ["hook_context_test.cpp"],
     deps = [
         "//openfeature:evaluation_context",
+        "//openfeature:flag_metadata",
+        "//openfeature:flag_type_value",
         "//openfeature:hook_context",
         "//openfeature:hook_data",
+        "//openfeature:metadata",
         "//openfeature:value",
         "`@googletest//`:gtest_main",
     ],
 )
🤖 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_context_test.cpp` around lines 1 - 13, Update the cc_test target in
test/BUILD that compiles hook_context_test.cpp to add direct dependencies on
//openfeature:flag_metadata, //openfeature:flag_type_value, and
//openfeature:metadata, matching the directly included headers while preserving
the existing hook_context dependency.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please fix.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

@oxddr Sure, fixing this now.

  • Add missing direct Bazel dependencies (//openfeature:flag_metadata, //openfeature:flag_type_value, //openfeature:metadata) to the hook_context_test target in test/BUILD.

The requested changes have been implemented and a pull request has been created: View PR

Signed-off-by: NeaguGeorgiana23 <neagugeorgiana@google.com>
@NeaguGeorgiana23 NeaguGeorgiana23 requested a review from oxddr July 13, 2026 09:16
Comment on lines +1 to +13
#include "openfeature/hook_context.h"

#include <gtest/gtest.h>

#include <memory>
#include <string>

#include "openfeature/evaluation_context.h"
#include "openfeature/flag_metadata.h"
#include "openfeature/flag_type_value.h"
#include "openfeature/hook_data.h"
#include "openfeature/metadata.h"
#include "openfeature/value.h"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please fix.

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.

Hook data structures

2 participants