Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ git_repository(
remote = "https://github.com/open-feature/flagd-schemas.git",
)

git_repository(
name = "qcbor",
build_file = "//providers/flagd:qcbor.BUILD",
remote = "https://github.com/laurencelundblade/QCBOR.git",
tag = "v1.6.1",
)

git_repository(
name = "nlohmann_json_schema_validator",
build_file = "//providers/flagd:json_schema_validator.BUILD",
Expand Down
16 changes: 16 additions & 0 deletions providers/flagd/qcbor.BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
load("@rules_cc//cc:defs.bzl", "cc_library")

cc_library(
name = "qcbor",
srcs = glob([
"src/*.c",
"src/*.h",
]),
hdrs = glob([
"inc/qcbor/*.h",
"inc/*.h",
]),
includes = ["inc"],
copts = ["-include stdint.h"],
visibility = ["//visibility:public"],
)
1 change: 1 addition & 0 deletions providers/flagd/src/evaluator/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ cc_library(
"@abseil-cpp//absl/log",
"@abseil-cpp//absl/strings",
"@nlohmann_json//:json",
"@qcbor//:qcbor",
],
)

Expand Down
42 changes: 42 additions & 0 deletions providers/flagd/src/evaluator/evaluator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,44 @@ openfeature::Value JsonToValue(const nlohmann::json& json_val) {
return {};
}

nlohmann::json ValueToJson(const openfeature::Value& value) {
if (value.IsBool()) {
return value.AsBool().value();
}
if (value.IsString()) {
return value.AsString().value();
}
if (value.IsNumber()) {
std::optional<int64_t> val_i = value.AsInt();
std::optional<double> val_d = value.AsDouble();
if (val_i && val_d && static_cast<double>(*val_i) == *val_d) {
return *val_i;
}
return val_d.value_or(0.0);
}
if (value.IsStructure()) {
nlohmann::json obj = nlohmann::json::object();
const auto* structure = value.AsStructure();
if (structure != nullptr) {
for (const auto& [key, val] : *structure) {
obj[key] = ValueToJson(val);
}
}
return obj;
}
if (value.IsList()) {
nlohmann::json arr = nlohmann::json::array();
const auto* list = value.AsList();
if (list != nullptr) {
for (const auto& val : *list) {
arr.push_back(ValueToJson(val));
}
}
return arr;
}
return nullptr;
}

std::optional<openfeature::FlagMetadataValue> JsonToMetadataValue(
const nlohmann::json& json) {
if (json.is_boolean()) {
Expand Down Expand Up @@ -113,6 +151,10 @@ nlohmann::json ContextToJson(const openfeature::EvaluationContext& ctx) {
json[key] = *dbl_val;
} else if (const auto* bool_val = std::any_cast<bool>(&value)) {
json[key] = *bool_val;
} else if (const auto* of_val = std::any_cast<openfeature::Value>(&value)) {
json[key] = ValueToJson(*of_val);
} else if (const auto* json_val = std::any_cast<nlohmann::json>(&value)) {
json[key] = *json_val;
} else {
LOG(WARNING) << "Unsupported attribute type for key: " << key;
}
Expand Down
Loading
Loading