-
Notifications
You must be signed in to change notification settings - Fork 2
Client API #53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Client API #53
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
240ac77
feat: add global context manager
NeaguGeorgiana23 8563306
feat: add client api and boolean evaluation
NeaguGeorgiana23 71c09c7
Update openfeature/global_context_manager.cpp
NeaguGeorgiana23 0c9e6ee
Merge branch 'main' into feature/split-2-client
NeaguGeorgiana23 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| 8.5.1 | ||
| 8.5.1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| #include "client_api.h" | ||
|
|
||
| #include <utility> | ||
|
|
||
| #include "openfeature/flag_metadata.h" | ||
| #include "openfeature/global_context_manager.h" | ||
| #include "openfeature/reason.h" | ||
|
|
||
| namespace openfeature { | ||
|
|
||
| ClientAPI::ClientAPI(ProviderRepository& repository, std::string_view domain) | ||
| : provider_repository_(repository), domain_(domain) {} | ||
|
|
||
| Metadata ClientAPI::GetMetadata() { return Metadata{domain_}; } | ||
|
|
||
| EvaluationContext ClientAPI::GetEvaluationContext() { | ||
| std::lock_guard<std::mutex> lock(context_mutex_); | ||
| return evaluation_context_; | ||
| } | ||
|
|
||
| void ClientAPI::SetEvaluationContext(const EvaluationContext& ctx) { | ||
| std::lock_guard<std::mutex> lock(context_mutex_); | ||
| evaluation_context_ = ctx; | ||
| } | ||
|
|
||
| ProviderStatus ClientAPI::GetProviderStatus() { | ||
| return provider_repository_.GetProviderStatus(domain_); | ||
| } | ||
|
|
||
| bool ClientAPI::GetBooleanValue(std::string_view flag_key, bool default_value) { | ||
| return EvaluateBooleanFlag(flag_key, default_value, std::nullopt)->GetValue(); | ||
| } | ||
|
|
||
| bool ClientAPI::GetBooleanValue(std::string_view flag_key, bool default_value, | ||
| const EvaluationContext& ctx) { | ||
| return EvaluateBooleanFlag(flag_key, default_value, ctx)->GetValue(); | ||
| } | ||
|
|
||
| std::unique_ptr<BoolResolutionDetails> ClientAPI::EvaluateBooleanFlag( | ||
| std::string_view flag_key, bool default_value, | ||
| const std::optional<EvaluationContext>& ctx) { | ||
| if (GetProviderStatus() != ProviderStatus::kReady) { | ||
| return std::make_unique<BoolResolutionDetails>( | ||
| default_value, Reason::kError, std::nullopt, FlagMetadata(), | ||
| ErrorCode::kProviderNotReady, "Provider is not ready"); | ||
| } | ||
|
|
||
| std::shared_ptr<FeatureProvider> provider = | ||
| provider_repository_.GetProvider(domain_); | ||
| if (!provider) { | ||
| return std::make_unique<BoolResolutionDetails>( | ||
| default_value, Reason::kError, std::nullopt, FlagMetadata(), | ||
| ErrorCode::kProviderFatal, "Provider not found for domain"); | ||
| } | ||
|
|
||
| EvaluationContext merged_context = MergeContexts(ctx); | ||
| return provider->GetBooleanEvaluation(flag_key, default_value, | ||
| merged_context); | ||
| } | ||
|
|
||
| EvaluationContext ClientAPI::MergeContexts( | ||
| const std::optional<EvaluationContext>& invocation_ctx) { | ||
| // TODO: Add context merging logic after EvaluationContext is implemented. | ||
|
|
||
| if (invocation_ctx) { | ||
| return *invocation_ctx; | ||
| } | ||
| return GetEvaluationContext(); | ||
| } | ||
|
|
||
| } // namespace openfeature | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| #ifndef CPP_SDK_INCLUDE_OPENFEATURE_CLIENT_API_H_ | ||
| #define CPP_SDK_INCLUDE_OPENFEATURE_CLIENT_API_H_ | ||
|
|
||
| #include <memory> | ||
| #include <mutex> | ||
| #include <optional> | ||
| #include <string> | ||
| #include <string_view> | ||
|
|
||
| #include "openfeature/client.h" | ||
| #include "openfeature/evaluation_context.h" | ||
| #include "openfeature/features.h" | ||
| #include "openfeature/global_context_manager.h" | ||
| #include "openfeature/metadata.h" | ||
| #include "openfeature/provider.h" | ||
| #include "openfeature/provider_repository.h" | ||
| #include "openfeature/provider_status.h" | ||
| #include "openfeature/resolution_details.h" | ||
|
|
||
| namespace openfeature { | ||
|
|
||
| // OpenFeature client implementation. | ||
| class ClientAPI : public Client { | ||
| public: | ||
| ClientAPI(ProviderRepository& repository, std::string_view domain); | ||
|
|
||
| ~ClientAPI() override = default; | ||
|
|
||
| ClientAPI(const ClientAPI&) = delete; | ||
| ClientAPI& operator=(const ClientAPI&) = delete; | ||
|
|
||
| Metadata GetMetadata() override; | ||
|
|
||
| // Return an optional client-level evaluation context. | ||
| EvaluationContext GetEvaluationContext() override; | ||
|
|
||
| // Set the client-level evaluation context. | ||
| void SetEvaluationContext(const EvaluationContext& ctx) override; | ||
|
|
||
| // Returns the current status of the associated provider. | ||
| ProviderStatus GetProviderStatus() override; | ||
|
|
||
| bool GetBooleanValue(std::string_view flag_key, bool default_value) override; | ||
| bool GetBooleanValue(std::string_view flag_key, bool default_value, | ||
| const EvaluationContext& ctx) override; | ||
|
|
||
| // TODO: Add methods to get and set Hooks. | ||
| // TODO: Add methods for flag evaluation for other types (e.g. string, int, | ||
| // float, object). | ||
| // TODO: Add methods for detailed flag evaluation. | ||
| // TODO: Overload method "GetBooleanValue" to accept "Evaluation Options". | ||
|
|
||
| private: | ||
| std::unique_ptr<BoolResolutionDetails> EvaluateBooleanFlag( | ||
| std::string_view flag_key, bool default_value, | ||
| const std::optional<EvaluationContext>& ctx); | ||
|
|
||
| EvaluationContext MergeContexts( | ||
| const std::optional<EvaluationContext>& invocation_ctx); | ||
|
|
||
| ProviderRepository& provider_repository_; | ||
| std::string domain_; | ||
| EvaluationContext evaluation_context_; | ||
| mutable std::mutex context_mutex_; | ||
| }; | ||
|
|
||
| } // namespace openfeature | ||
|
|
||
| #endif // CPP_SDK_INCLUDE_OPENFEATURE_CLIENT_API_H_ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| #include "openfeature/client_api.h" | ||
|
|
||
| #include <gmock/gmock.h> | ||
| #include <gtest/gtest.h> | ||
|
|
||
| #include <memory> | ||
| #include <string> | ||
|
|
||
| #include "absl/status/status.h" | ||
| #include "mocks/mock_feature_provider.h" | ||
| #include "openfeature/evaluation_context.h" | ||
| #include "openfeature/global_context_manager.h" | ||
| #include "openfeature/provider_status.h" | ||
|
|
||
| using namespace openfeature; | ||
| using ::testing::_; | ||
| using ::testing::NiceMock; | ||
| using ::testing::Return; | ||
| using ::testing::StrictMock; | ||
|
|
||
| class ClientAPITest : public ::testing::Test { | ||
| protected: | ||
| void SetUp() override { | ||
| // Reset the Global Context to a clean state before each test. | ||
| GlobalContextManager::GetInstance().SetGlobalEvaluationContext( | ||
| EvaluationContext{}); | ||
| } | ||
| ProviderRepository repo_; | ||
| }; | ||
|
|
||
| // Test that the constructor correctly sets the domain in the metadata. | ||
| TEST_F(ClientAPITest, ConstructorSetsDomainMetadata) { | ||
| std::string domain = "test-domain"; | ||
| ClientAPI client(repo_, domain); | ||
|
|
||
| Metadata metadata = client.GetMetadata(); | ||
| EXPECT_EQ(metadata.name, domain); | ||
| } | ||
|
|
||
| // Test that the provider status is Ready by default. | ||
| TEST_F(ClientAPITest, GetProviderStatusDefaultsToReady) { | ||
| ClientAPI client(repo_, "test-domain"); | ||
| EXPECT_EQ(client.GetProviderStatus(), ProviderStatus::kReady); | ||
| } | ||
|
|
||
| // Test setting and getting the EvaluationContext. | ||
| TEST_F(ClientAPITest, SetAndGetEvaluationContext) { | ||
| ClientAPI client(repo_, "test-domain"); | ||
| EvaluationContext ctx; | ||
|
|
||
| // Verify we can set the context without error. | ||
| EXPECT_NO_THROW(client.SetEvaluationContext(ctx)); | ||
| } | ||
|
|
||
| // Test that GetBooleanValue returns the default value when using the default | ||
| // provider. | ||
| TEST_F(ClientAPITest, GetBooleanValueReturnsDefaultWithNoopProvider) { | ||
| ClientAPI client(repo_, "test-domain"); | ||
| std::string flag_key = "my-boolean-flag"; | ||
|
|
||
| EXPECT_TRUE(client.GetBooleanValue(flag_key, true)); | ||
|
|
||
| EXPECT_FALSE(client.GetBooleanValue(flag_key, false)); | ||
| } | ||
|
|
||
| // Test GetBooleanValue with an EvaluationContext passed in. | ||
| TEST_F(ClientAPITest, GetBooleanValueWithContextReturnsDefault) { | ||
| ClientAPI client(repo_, "test-domain"); | ||
| EvaluationContext ctx; | ||
| std::string flag_key = "my-boolean-flag"; | ||
|
|
||
| EXPECT_TRUE(client.GetBooleanValue(flag_key, true, ctx)); | ||
| EXPECT_FALSE(client.GetBooleanValue(flag_key, false, ctx)); | ||
| } | ||
|
|
||
| // Test context merging logic indirectly. | ||
| TEST_F(ClientAPITest, GetBooleanValueSafeWithMergedContexts) { | ||
| EvaluationContext global_ctx; | ||
| GlobalContextManager::GetInstance().SetGlobalEvaluationContext(global_ctx); | ||
|
|
||
| ClientAPI client(repo_, "test-domain"); | ||
| EvaluationContext client_ctx; | ||
| client.SetEvaluationContext(client_ctx); | ||
|
|
||
| EvaluationContext invocation_ctx; | ||
|
|
||
| // This call forces a merge of Global + Client + Invocation contexts. | ||
| // We expect the NoopProvider to handle the result gracefully (return | ||
| // default). | ||
| EXPECT_TRUE(client.GetBooleanValue("flag", true, invocation_ctx)); | ||
| } | ||
|
|
||
| // Test behavior when the domain is empty. | ||
| TEST_F(ClientAPITest, WorksWithEmptyDomain) { | ||
| ClientAPI client(repo_, ""); | ||
| EXPECT_EQ(client.GetMetadata().name, ""); | ||
| EXPECT_TRUE(client.GetBooleanValue("flag", true)); | ||
| } | ||
|
|
||
| // TODO: If ClientAPI is refactored to allow injecting a MockFeatureProvider | ||
| // (e.g. by passing the OpenFeatureAPI's repository or via constructor), | ||
| // add tests here to verify that the MockProvider receives the correct | ||
| // flag key and merged EvaluationContext. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current implementation of
MergeContextsdoesn't follow the OpenFeature specification for context merging. It should merge contexts from the invocation, client, and API (global) levels. This implementation only considers the invocation and client contexts, ignoring the global context available viaGlobalContextManager. WhileEvaluationContextis not fully implemented, the logic here should at least be structured to reflect the specified merging hierarchy (invocation>client>global) as a placeholder.