Skip to content
Merged
Changes from all 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
35 changes: 20 additions & 15 deletions libs/internal/src/serialization/json_evaluation_result.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include <boost/json.hpp>

namespace launchdarkly {

tl::expected<std::optional<EvaluationResult>, JsonError> tag_invoke(
boost::json::value_to_tag<
tl::expected<std::optional<EvaluationResult>, JsonError>> const& unused,
Expand Down Expand Up @@ -56,15 +55,22 @@ tl::expected<std::optional<EvaluationResult>, JsonError> tag_invoke(
// when deserializing FlagMeta. Primarily `variation` not
// `variationIndex`.

auto* value_iter = json_obj.find("value");
if (value_iter == json_obj.end()) {
return tl::unexpected(JsonError::kSchemaFailure);
}

auto maybe_value = boost::json::value_to<tl::expected<Value, JsonError>>(
value_iter->value());
if (!maybe_value) {
return tl::unexpected(maybe_value.error());
// todo(cwaldren): SC-203949 + SC-236165
// We're looking for the evaluated value. If it's not there, we should treat
// it as null and *not* a schema error. This is because the server-side SDK
// that produced the EvaluationResult may have omitted the 'null' value.
// Otherwise, if it is there, it must deserialize to a valid Value (which
// might itself be null.)
Value value = Value::Null();
if (auto* value_iter = json_obj.find("value");
value_iter != json_obj.end()) {
auto maybe_value =
boost::json::value_to<tl::expected<Value, JsonError>>(
value_iter->value());
if (!maybe_value) {
return tl::unexpected(maybe_value.error());
}
value = *maybe_value;
}

auto* variation_iter = json_obj.find("variation");
Expand All @@ -85,7 +91,7 @@ tl::expected<std::optional<EvaluationResult>, JsonError> tag_invoke(
track_events,
track_reason,
debug_events_until_date,
EvaluationDetailInternal(*maybe_value, variation,
EvaluationDetailInternal(std::move(value), variation,
std::make_optional(reason.value()))};
}
// We could not parse the reason.
Expand All @@ -99,7 +105,7 @@ tl::expected<std::optional<EvaluationResult>, JsonError> tag_invoke(
track_events,
track_reason,
debug_events_until_date,
EvaluationDetailInternal(*maybe_value, variation, std::nullopt)};
EvaluationDetailInternal(std::move(value), variation, std::nullopt)};
}

void tag_invoke(boost::json::value_from_tag const& unused,
Expand Down Expand Up @@ -127,7 +133,7 @@ void tag_invoke(boost::json::value_from_tag const& unused,
"debugEventsUntilDate",
std::chrono::duration_cast<std::chrono::milliseconds>(
evaluation_result.DebugEventsUntilDate()->time_since_epoch())
.count());
.count());
}

auto& detail = evaluation_result.Detail();
Expand All @@ -143,5 +149,4 @@ void tag_invoke(boost::json::value_from_tag const& unused,
obj.emplace("reason", reason_json);
}
}

} // namespace launchdarkly
} // namespace launchdarkly