|
| 1 | +#include "openfeature/memory_provider/in_memory_provider.h" |
| 2 | + |
| 3 | +#include <mutex> |
| 4 | +#include <optional> |
| 5 | +#include <utility> |
| 6 | + |
| 7 | +#include "absl/status/statusor.h" |
| 8 | +#include "openfeature/error_code.h" |
| 9 | +#include "openfeature/memory_provider/flag.h" |
| 10 | +#include "openfeature/reason.h" |
| 11 | + |
| 12 | +namespace openfeature { |
| 13 | + |
| 14 | +static constexpr std::string_view kName = "InMemoryProvider"; |
| 15 | + |
| 16 | +InMemoryProvider::InMemoryProvider( |
| 17 | + std::unordered_map<std::string, std::any> flags) |
| 18 | + : flags_(std::move(flags)), status_(ProviderStatus::kNotReady) {} |
| 19 | + |
| 20 | +Metadata InMemoryProvider::GetMetadata() const { |
| 21 | + return Metadata{std::string(kName)}; |
| 22 | +} |
| 23 | + |
| 24 | +absl::Status InMemoryProvider::Init(const EvaluationContext& ctx) { |
| 25 | + { |
| 26 | + std::unique_lock lock(mutex_); |
| 27 | + status_ = ProviderStatus::kReady; |
| 28 | + } |
| 29 | + return absl::OkStatus(); |
| 30 | +} |
| 31 | + |
| 32 | +absl::Status InMemoryProvider::Shutdown() { |
| 33 | + { |
| 34 | + std::unique_lock lock(mutex_); |
| 35 | + status_ = ProviderStatus::kNotReady; |
| 36 | + } |
| 37 | + return absl::OkStatus(); |
| 38 | +} |
| 39 | + |
| 40 | +void InMemoryProvider::UpdateFlags( |
| 41 | + std::unordered_map<std::string, std::any> new_flags) { |
| 42 | + std::unique_lock lock(mutex_); |
| 43 | + for (auto& [key, value] : new_flags) { |
| 44 | + flags_[key] = value; |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +void InMemoryProvider::UpdateFlag(std::string key, std::any new_flag) { |
| 49 | + std::unique_lock lock(mutex_); |
| 50 | + flags_.insert_or_assign(std::move(key), std::move(new_flag)); |
| 51 | +} |
| 52 | + |
| 53 | +std::unique_ptr<BoolResolutionDetails> InMemoryProvider::GetBooleanEvaluation( |
| 54 | + std::string_view key, bool default_value, const EvaluationContext& ctx) { |
| 55 | + return Evaluate<bool>(key, default_value, ctx); |
| 56 | +} |
| 57 | + |
| 58 | +template <typename T> |
| 59 | +std::unique_ptr<ResolutionDetails<T>> InMemoryProvider::Evaluate( |
| 60 | + std::string_view key, T default_value, const EvaluationContext& ctx) { |
| 61 | + std::shared_lock lock(mutex_); |
| 62 | + |
| 63 | + if (status_ != ProviderStatus::kReady) { |
| 64 | + if (status_ == ProviderStatus::kNotReady) { |
| 65 | + return std::make_unique<ResolutionDetails<T>>( |
| 66 | + default_value, Reason::kError, std::nullopt, FlagMetadata{}, |
| 67 | + ErrorCode::kProviderNotReady, "Provider is not ready"); |
| 68 | + } |
| 69 | + if (status_ == ProviderStatus::kFatal) { |
| 70 | + return std::make_unique<ResolutionDetails<T>>( |
| 71 | + default_value, Reason::kError, std::nullopt, FlagMetadata{}, |
| 72 | + ErrorCode::kProviderFatal, "Provider is in fatal error state"); |
| 73 | + } |
| 74 | + return std::make_unique<ResolutionDetails<T>>( |
| 75 | + default_value, Reason::kError, std::nullopt, FlagMetadata{}, |
| 76 | + ErrorCode::kGeneral, "Unknown error"); |
| 77 | + } |
| 78 | + |
| 79 | + std::string key_str{key}; |
| 80 | + auto it = flags_.find(key_str); |
| 81 | + if (it == flags_.end()) { |
| 82 | + return std::make_unique<ResolutionDetails<T>>( |
| 83 | + default_value, Reason::kError, std::nullopt, FlagMetadata{}, |
| 84 | + ErrorCode::kFlagNotFound, "Flag " + key_str + " not found"); |
| 85 | + } |
| 86 | + |
| 87 | + const Flag<T>* flag = std::any_cast<Flag<T>>(&it->second); |
| 88 | + |
| 89 | + if (!flag) { |
| 90 | + return std::make_unique<ResolutionDetails<T>>( |
| 91 | + default_value, Reason::kError, std::nullopt, FlagMetadata{}, |
| 92 | + ErrorCode::kTypeMismatch, "Flag type mismatch"); |
| 93 | + } |
| 94 | + |
| 95 | + if (flag->IsDisabled()) { |
| 96 | + return std::make_unique<ResolutionDetails<T>>( |
| 97 | + default_value, Reason::kDisabled, std::nullopt, |
| 98 | + flag->GetFlagMetadata()); |
| 99 | + } |
| 100 | + |
| 101 | + T value; |
| 102 | + Reason reason = Reason::kStatic; |
| 103 | + const std::optional<std::string>& variant_key = flag->GetDefaultVariant(); |
| 104 | + bool context_eval_success = false; |
| 105 | + const auto& evaluator = flag->GetContextEvaluator(); |
| 106 | + |
| 107 | + if (evaluator != nullptr) { |
| 108 | + absl::StatusOr<T> result = evaluator(*flag, ctx); |
| 109 | + |
| 110 | + if (result.ok()) { |
| 111 | + value = *result; |
| 112 | + reason = Reason::kTargetingMatch; |
| 113 | + context_eval_success = true; |
| 114 | + } else { |
| 115 | + reason = Reason::kDefault; |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + // Fallback to default variant if context evaluation failed. |
| 120 | + if (!context_eval_success) { |
| 121 | + if (variant_key.has_value()) { |
| 122 | + const std::unordered_map<std::string, T>& variants = flag->GetVariants(); |
| 123 | + auto variant_it = variants.find(*variant_key); |
| 124 | + |
| 125 | + if (variant_it != variants.end()) { |
| 126 | + value = variant_it->second; |
| 127 | + } else { |
| 128 | + return std::make_unique<ResolutionDetails<T>>( |
| 129 | + default_value, Reason::kError, variant_key, flag->GetFlagMetadata(), |
| 130 | + ErrorCode::kParseError, |
| 131 | + "Default variant " + *variant_key + " not found in variants map"); |
| 132 | + } |
| 133 | + } else { |
| 134 | + return std::make_unique<ResolutionDetails<T>>( |
| 135 | + default_value, Reason::kDefault, std::nullopt, |
| 136 | + flag->GetFlagMetadata()); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + return std::make_unique<ResolutionDetails<T>>(value, reason, variant_key, |
| 141 | + flag->GetFlagMetadata()); |
| 142 | +} |
| 143 | + |
| 144 | +} // namespace openfeature |
0 commit comments