Skip to content

Commit

Permalink
feat: add Flag model to SDKDataSet (#159)
Browse files Browse the repository at this point in the history
Adds in missing `Flag` model to the top-level `SDKDataSet`.

Additionally, both the flag/segment fields now use use `PARSE_FIELD`, so
we can get an empty `unordered_map` if they are missing.
  • Loading branch information
cwaldren-ld authored Jul 6, 2023
1 parent f38e038 commit 09d2bd6
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 7 deletions.
7 changes: 7 additions & 0 deletions libs/internal/include/launchdarkly/data_model/flag.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,12 @@ struct Flag {
bool trackEvents;
bool trackEventsFallthrough;
std::optional<std::uint64_t> debugEventsUntilDate;

/**
* Returns the flag's version. Satisfies ItemDescriptor template
* constraints.
* @return Version of this flag.
*/
[[nodiscard]] inline std::uint64_t Version() const { return version; }
};
} // namespace launchdarkly::data_model
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <launchdarkly/data_model/flag.hpp>
#include <launchdarkly/data_model/item_descriptor.hpp>
#include <launchdarkly/data_model/segment.hpp>

Expand All @@ -14,9 +15,8 @@ namespace launchdarkly::data_model {
struct SDKDataSet {
using FlagKey = std::string;
using SegmentKey = std::string;
// std::unordered_map<FlagKey, ItemDescriptor<Flag>> flags;
std::optional<std::unordered_map<SegmentKey, ItemDescriptor<Segment>>>
segments;
std::unordered_map<FlagKey, ItemDescriptor<Flag>> flags;
std::unordered_map<SegmentKey, ItemDescriptor<Segment>> segments;
};

} // namespace launchdarkly::data_model
6 changes: 6 additions & 0 deletions libs/internal/include/launchdarkly/data_model/segment.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ struct Segment {

// TODO(cwaldren): make Kind a real type that is deserialized, so we can
// make empty string an error.

/**
* Returns the segment's version. Satisfies ItemDescriptor template
* constraints.
* @return Version of this segment.
*/
[[nodiscard]] inline std::uint64_t Version() const { return version; }
};
} // namespace launchdarkly::data_model
4 changes: 3 additions & 1 deletion libs/internal/src/serialization/json_sdk_data_set.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <boost/core/ignore_unused.hpp>
#include <boost/json.hpp>
#include <launchdarkly/serialization/json_flag.hpp>
#include <launchdarkly/serialization/json_item_descriptor.hpp>
#include <launchdarkly/serialization/json_sdk_data_set.hpp>
#include <launchdarkly/serialization/json_segment.hpp>
Expand All @@ -19,7 +20,8 @@ tl::expected<std::optional<data_model::SDKDataSet>, JsonError> tag_invoke(

data_model::SDKDataSet data_set;

PARSE_CONDITIONAL_FIELD(data_set.segments, obj, "segments");
PARSE_FIELD(data_set.flags, obj, "flags");
PARSE_FIELD(data_set.segments, obj, "segments");

return data_set;
}
Expand Down
14 changes: 11 additions & 3 deletions libs/internal/tests/data_model_serialization_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ TEST(SDKDataSetTests, DeserializesEmptyDataSet) {
boost::json::value_to<tl::expected<data_model::SDKDataSet, JsonError>>(
boost::json::parse("{}"));
ASSERT_TRUE(result);
ASSERT_FALSE(result->segments);
ASSERT_TRUE(result->segments.empty());
ASSERT_TRUE(result->flags.empty());
}

TEST(SDKDataSetTests, ErrorOnInvalidSchema) {
Expand All @@ -29,8 +30,15 @@ TEST(SDKDataSetTests, DeserializesZeroSegments) {
boost::json::value_to<tl::expected<data_model::SDKDataSet, JsonError>>(
boost::json::parse(R"({"segments":{}})"));
ASSERT_TRUE(result);
ASSERT_TRUE(result->segments);
ASSERT_TRUE(result->segments->empty());
ASSERT_TRUE(result->segments.empty());
}

TEST(SDKDataSetTests, DeserializesZeroFlags) {
auto result =
boost::json::value_to<tl::expected<data_model::SDKDataSet, JsonError>>(
boost::json::parse(R"({"flags":{}})"));
ASSERT_TRUE(result);
ASSERT_TRUE(result->flags.empty());
}

TEST(SegmentTests, DeserializesMinimumValid) {
Expand Down

0 comments on commit 09d2bd6

Please sign in to comment.