From 71a73ffbf1d108ce5955f83d6f36b88e99e685a6 Mon Sep 17 00:00:00 2001 From: Casey Waldren Date: Fri, 30 Jun 2023 15:37:06 -0700 Subject: [PATCH 1/3] feat: add Flag to SDKDataSet model --- .../include/launchdarkly/data_model/flag.hpp | 2 ++ .../include/launchdarkly/data_model/sdk_data_set.hpp | 3 ++- .../internal/src/serialization/json_sdk_data_set.cpp | 2 ++ .../internal/tests/data_model_serialization_test.cpp | 12 ++++++++++++ 4 files changed, 18 insertions(+), 1 deletion(-) diff --git a/libs/internal/include/launchdarkly/data_model/flag.hpp b/libs/internal/include/launchdarkly/data_model/flag.hpp index 8c3d0bc4e..69896a9e6 100644 --- a/libs/internal/include/launchdarkly/data_model/flag.hpp +++ b/libs/internal/include/launchdarkly/data_model/flag.hpp @@ -83,5 +83,7 @@ struct Flag { bool trackEvents; bool trackEventsFallthrough; std::optional debugEventsUntilDate; + + [[nodiscard]] inline std::uint64_t Version() const { return version; } }; } // namespace launchdarkly::data_model diff --git a/libs/internal/include/launchdarkly/data_model/sdk_data_set.hpp b/libs/internal/include/launchdarkly/data_model/sdk_data_set.hpp index aa0d6be8e..0cdc773fb 100644 --- a/libs/internal/include/launchdarkly/data_model/sdk_data_set.hpp +++ b/libs/internal/include/launchdarkly/data_model/sdk_data_set.hpp @@ -1,5 +1,6 @@ #pragma once +#include #include #include @@ -14,7 +15,7 @@ namespace launchdarkly::data_model { struct SDKDataSet { using FlagKey = std::string; using SegmentKey = std::string; - // std::unordered_map> flags; + std::optional>> flags; std::optional>> segments; }; diff --git a/libs/internal/src/serialization/json_sdk_data_set.cpp b/libs/internal/src/serialization/json_sdk_data_set.cpp index 2c4f4afe3..12eeab992 100644 --- a/libs/internal/src/serialization/json_sdk_data_set.cpp +++ b/libs/internal/src/serialization/json_sdk_data_set.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -19,6 +20,7 @@ tl::expected, JsonError> tag_invoke( data_model::SDKDataSet data_set; + PARSE_CONDITIONAL_FIELD(data_set.flags, obj, "flags"); PARSE_CONDITIONAL_FIELD(data_set.segments, obj, "segments"); return data_set; diff --git a/libs/internal/tests/data_model_serialization_test.cpp b/libs/internal/tests/data_model_serialization_test.cpp index a718933d1..d599d4c93 100644 --- a/libs/internal/tests/data_model_serialization_test.cpp +++ b/libs/internal/tests/data_model_serialization_test.cpp @@ -14,6 +14,7 @@ TEST(SDKDataSetTests, DeserializesEmptyDataSet) { boost::json::parse("{}")); ASSERT_TRUE(result); ASSERT_FALSE(result->segments); + ASSERT_FALSE(result->flags); } TEST(SDKDataSetTests, ErrorOnInvalidSchema) { @@ -31,6 +32,17 @@ TEST(SDKDataSetTests, DeserializesZeroSegments) { ASSERT_TRUE(result); ASSERT_TRUE(result->segments); ASSERT_TRUE(result->segments->empty()); + ASSERT_FALSE(result->flags); +} + +TEST(SDKDataSetTests, DeserializesZeroFlags) { + auto result = + boost::json::value_to>( + boost::json::parse(R"({"flags":{}})")); + ASSERT_TRUE(result); + ASSERT_TRUE(result->flags); + ASSERT_TRUE(result->flags->empty()); + ASSERT_FALSE(result->segments); } TEST(SegmentTests, DeserializesMinimumValid) { From 8e74485d67bafd27a5854aaf4a34955a807aabe3 Mon Sep 17 00:00:00 2001 From: Casey Waldren Date: Fri, 30 Jun 2023 15:39:43 -0700 Subject: [PATCH 2/3] make flag/segments fields default to empty unordered_maps --- .../include/launchdarkly/data_model/sdk_data_set.hpp | 5 ++--- .../internal/src/serialization/json_sdk_data_set.cpp | 4 ++-- .../internal/tests/data_model_serialization_test.cpp | 12 ++++-------- 3 files changed, 8 insertions(+), 13 deletions(-) diff --git a/libs/internal/include/launchdarkly/data_model/sdk_data_set.hpp b/libs/internal/include/launchdarkly/data_model/sdk_data_set.hpp index 0cdc773fb..50edf0183 100644 --- a/libs/internal/include/launchdarkly/data_model/sdk_data_set.hpp +++ b/libs/internal/include/launchdarkly/data_model/sdk_data_set.hpp @@ -15,9 +15,8 @@ namespace launchdarkly::data_model { struct SDKDataSet { using FlagKey = std::string; using SegmentKey = std::string; - std::optional>> flags; - std::optional>> - segments; + std::unordered_map> flags; + std::unordered_map> segments; }; } // namespace launchdarkly::data_model diff --git a/libs/internal/src/serialization/json_sdk_data_set.cpp b/libs/internal/src/serialization/json_sdk_data_set.cpp index 12eeab992..84043c377 100644 --- a/libs/internal/src/serialization/json_sdk_data_set.cpp +++ b/libs/internal/src/serialization/json_sdk_data_set.cpp @@ -20,8 +20,8 @@ tl::expected, JsonError> tag_invoke( data_model::SDKDataSet data_set; - PARSE_CONDITIONAL_FIELD(data_set.flags, obj, "flags"); - 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; } diff --git a/libs/internal/tests/data_model_serialization_test.cpp b/libs/internal/tests/data_model_serialization_test.cpp index d599d4c93..43bf8eaa4 100644 --- a/libs/internal/tests/data_model_serialization_test.cpp +++ b/libs/internal/tests/data_model_serialization_test.cpp @@ -13,8 +13,8 @@ TEST(SDKDataSetTests, DeserializesEmptyDataSet) { boost::json::value_to>( boost::json::parse("{}")); ASSERT_TRUE(result); - ASSERT_FALSE(result->segments); - ASSERT_FALSE(result->flags); + ASSERT_TRUE(result->segments.empty()); + ASSERT_TRUE(result->flags.empty()); } TEST(SDKDataSetTests, ErrorOnInvalidSchema) { @@ -30,9 +30,7 @@ TEST(SDKDataSetTests, DeserializesZeroSegments) { boost::json::value_to>( boost::json::parse(R"({"segments":{}})")); ASSERT_TRUE(result); - ASSERT_TRUE(result->segments); - ASSERT_TRUE(result->segments->empty()); - ASSERT_FALSE(result->flags); + ASSERT_TRUE(result->segments.empty()); } TEST(SDKDataSetTests, DeserializesZeroFlags) { @@ -40,9 +38,7 @@ TEST(SDKDataSetTests, DeserializesZeroFlags) { boost::json::value_to>( boost::json::parse(R"({"flags":{}})")); ASSERT_TRUE(result); - ASSERT_TRUE(result->flags); - ASSERT_TRUE(result->flags->empty()); - ASSERT_FALSE(result->segments); + ASSERT_TRUE(result->flags.empty()); } TEST(SegmentTests, DeserializesMinimumValid) { From 55dd5432253bb902983c3af0ccab51255ab1826b Mon Sep 17 00:00:00 2001 From: Casey Waldren Date: Thu, 6 Jul 2023 10:47:33 -0700 Subject: [PATCH 3/3] add doc comments to flag/segment.hpp on Version() method --- libs/internal/include/launchdarkly/data_model/flag.hpp | 5 +++++ libs/internal/include/launchdarkly/data_model/segment.hpp | 6 ++++++ 2 files changed, 11 insertions(+) diff --git a/libs/internal/include/launchdarkly/data_model/flag.hpp b/libs/internal/include/launchdarkly/data_model/flag.hpp index 69896a9e6..282dd4bad 100644 --- a/libs/internal/include/launchdarkly/data_model/flag.hpp +++ b/libs/internal/include/launchdarkly/data_model/flag.hpp @@ -84,6 +84,11 @@ struct Flag { bool trackEventsFallthrough; std::optional 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 diff --git a/libs/internal/include/launchdarkly/data_model/segment.hpp b/libs/internal/include/launchdarkly/data_model/segment.hpp index 63003b4a4..cb048796a 100644 --- a/libs/internal/include/launchdarkly/data_model/segment.hpp +++ b/libs/internal/include/launchdarkly/data_model/segment.hpp @@ -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