From ac8b61b788d923ce7079a7c108e672cb4d740537 Mon Sep 17 00:00:00 2001 From: Casey Waldren Date: Thu, 21 Dec 2023 18:45:22 -0800 Subject: [PATCH] feat: add LDAllFlagsState_Map C binding --- .../bindings/c/all_flags_state/all_flags_state.h | 16 ++++++++++++++++ .../c/all_flags_state/all_flags_state.cpp | 14 ++++++++++++++ libs/server-sdk/tests/server_c_bindings_test.cpp | 13 ++++++++++++- 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/libs/server-sdk/include/launchdarkly/server_side/bindings/c/all_flags_state/all_flags_state.h b/libs/server-sdk/include/launchdarkly/server_side/bindings/c/all_flags_state/all_flags_state.h index ab8d42af9..087c40762 100644 --- a/libs/server-sdk/include/launchdarkly/server_side/bindings/c/all_flags_state/all_flags_state.h +++ b/libs/server-sdk/include/launchdarkly/server_side/bindings/c/all_flags_state/all_flags_state.h @@ -71,6 +71,22 @@ LDAllFlagsState_SerializeJSON(LDAllFlagsState state); LD_EXPORT(LDValue) LDAllFlagsState_Value(LDAllFlagsState state, char const* flag_key); +/** + * Returns an object-type LDValue where the keys are flag keys + * and the values are the flag values for the context used to generate this + * LDAllFlagsState. + * + * The LDValue is owned by the caller and must be freed. This + * may cause a large heap allocation. If you're interested in bootstrapping + * a client-side SDK, this is not the right method. See @ref LDAllFlagsState_SerializeJSON. + * + * @param state An LDAllFlagsState. Must not be NULL. + * @return An object-type LDValue of flag-key/flag-value pairs. The caller MUST + * free this value using LDValue_Free. + */ +LD_EXPORT(LDValue) +LDAllFlagsState_Map(LDAllFlagsState state); + /** * Defines options that may be used with LDServerSDK_AllFlagsState. To * obtain default behavior, pass LD_ALLFLAGSSTATE_DEFAULT. diff --git a/libs/server-sdk/src/bindings/c/all_flags_state/all_flags_state.cpp b/libs/server-sdk/src/bindings/c/all_flags_state/all_flags_state.cpp index 112b18666..ab5d725b0 100644 --- a/libs/server-sdk/src/bindings/c/all_flags_state/all_flags_state.cpp +++ b/libs/server-sdk/src/bindings/c/all_flags_state/all_flags_state.cpp @@ -54,3 +54,17 @@ LDAllFlagsState_Value(LDAllFlagsState state, char const* flag_key) { return FROM_VALUE(const_cast(&val_ref)); } + +LD_EXPORT(LDValue) +LDAllFlagsState_Map(LDAllFlagsState state) { + LD_ASSERT_NOT_NULL(state); + + auto const& values = TO_ALLFLAGS(state)->Values(); + + std::map map; + for (auto const& pair : values) { + map.emplace(pair.first, pair.second); + } + + return FROM_VALUE(new Value(std::move(map))); +} diff --git a/libs/server-sdk/tests/server_c_bindings_test.cpp b/libs/server-sdk/tests/server_c_bindings_test.cpp index 636751df6..bd23fcb7e 100644 --- a/libs/server-sdk/tests/server_c_bindings_test.cpp +++ b/libs/server-sdk/tests/server_c_bindings_test.cpp @@ -2,8 +2,8 @@ #include #include -#include #include +#include #include @@ -158,6 +158,17 @@ TEST(ClientBindings, AllFlagsState) { LDValue nonexistent_flag = LDAllFlagsState_Value(state, "nonexistent_flag"); ASSERT_EQ(LDValue_Type(nonexistent_flag), LDValueType_Null); + LDValue value_map = LDAllFlagsState_Map(state); + ASSERT_EQ(LDValue_Type(value_map), LDValueType_Object); + + LDValue_ObjectIter value_iter = LDValue_ObjectIter_New(value_map); + ASSERT_TRUE(value_iter); + + ASSERT_TRUE(LDValue_ObjectIter_End(value_iter)); + LDValue_ObjectIter_Free(value_iter); + + LDValue_Free(value_map); + LDAllFlagsState_Free(state); LDContext_Free(context); LDServerSDK_Free(sdk);