Skip to content

Commit c71bed4

Browse files
authored
Attached evaluator to provider (#50)
* Attached evaluator to provider Signed-off-by: Marcin Olko <molko@google.com> * Updated test Signed-off-by: Marcin Olko <molko@google.com> --------- Signed-off-by: Marcin Olko <molko@google.com>
1 parent d8e13bd commit c71bed4

4 files changed

Lines changed: 21 additions & 8 deletions

File tree

providers/flagd/src/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ cc_library(
77
include_prefix = "flagd",
88
visibility = ["//visibility:public"],
99
deps = [
10+
":evaluator",
1011
":flagd_configuration",
1112
":sync",
1213
"@abseil-cpp//absl/log",

providers/flagd/src/provider.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ FlagdProvider::FlagdProvider(FlagdProviderConfig config)
2323
LOG(FATAL) << "File sync is not implemented yet!";
2424
}
2525

26-
sync_ = std::make_unique<GrpcSync>(configuration_);
26+
sync_ = std::make_shared<GrpcSync>(configuration_);
27+
28+
evaluator_ = std::make_unique<JsonLogicEvaluator>(sync_);
2729
}
2830

2931
FlagdProvider::~FlagdProvider() {
@@ -57,9 +59,13 @@ std::unique_ptr<openfeature::BoolResolutionDetails>
5759
FlagdProvider::GetBooleanEvaluation(const std::string_view flag,
5860
bool default_value,
5961
const openfeature::EvaluationContext& ctx) {
60-
return std::make_unique<openfeature::BoolResolutionDetails>(
61-
default_value, openfeature::Reason::kDefault, "default-variant",
62-
openfeature::FlagMetadata(), std::nullopt, std::nullopt);
62+
if (!is_ready_) {
63+
return std::make_unique<openfeature::BoolResolutionDetails>(
64+
default_value, openfeature::Reason::kError, "",
65+
openfeature::FlagMetadata(), openfeature::ErrorCode::kProviderNotReady,
66+
"Provider not ready");
67+
}
68+
return evaluator_->ResolveBoolean(flag, default_value, ctx);
6369
}
6470

6571
} // namespace flagd

providers/flagd/src/provider.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include "absl/status/status.h"
1010
#include "flagd/configuration.h"
11+
#include "flagd/evaluator.h"
1112
#include "flagd/sync.h"
1213

1314
namespace flagd {
@@ -31,7 +32,8 @@ class FlagdProvider : public openfeature::FeatureProvider {
3132

3233
private:
3334
FlagdProviderConfig configuration_;
34-
std::unique_ptr<FlagSync> sync_;
35+
std::shared_ptr<FlagSync> sync_;
36+
std::unique_ptr<Evaluator> evaluator_;
3537
std::atomic<bool> is_ready_;
3638
};
3739

providers/flagd/tests/smoke/openfeature.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,17 @@ TEST(FlagdProviderTest, ProviderCreation) {
1313
std::shared_ptr<openfeature::FeatureProvider> provider =
1414
std::make_shared<flagd::FlagdProvider>(config);
1515

16-
provider->Init({}).IgnoreError();
17-
1816
std::unique_ptr<openfeature::BoolResolutionDetails> details =
1917
provider->GetBooleanEvaluation("test_flag", false, {});
2018

19+
// We didn't call Init, so the provider is not ready.
2120
auto expected_details = openfeature::BoolResolutionDetails{
22-
false, openfeature::Reason::kDefault, "default-variant", {}};
21+
false,
22+
openfeature::Reason::kError,
23+
"",
24+
{},
25+
openfeature::ErrorCode::kProviderNotReady,
26+
"Provider not ready"};
2327

2428
EXPECT_EQ(details->GetValue(), expected_details.GetValue());
2529
EXPECT_EQ(details->GetReason(), expected_details.GetReason());

0 commit comments

Comments
 (0)