Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add Flag model to SDKDataSet #159

Merged
merged 3 commits into from
Jul 6, 2023
Merged
Show file tree
Hide file tree
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
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; }
cwaldren-ld marked this conversation as resolved.
Show resolved Hide resolved
};
} // 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