Skip to content

Commit

Permalink
feat: add LDAllFlagsState_Map C binding
Browse files Browse the repository at this point in the history
  • Loading branch information
cwaldren-ld committed Dec 22, 2023
1 parent 34cafc6 commit ac8b61b
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
14 changes: 14 additions & 0 deletions libs/server-sdk/src/bindings/c/all_flags_state/all_flags_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,17 @@ LDAllFlagsState_Value(LDAllFlagsState state, char const* flag_key) {

return FROM_VALUE(const_cast<Value*>(&val_ref));
}

LD_EXPORT(LDValue)
LDAllFlagsState_Map(LDAllFlagsState state) {
LD_ASSERT_NOT_NULL(state);

auto const& values = TO_ALLFLAGS(state)->Values();

std::map<std::string, Value> map;
for (auto const& pair : values) {
map.emplace(pair.first, pair.second);
}

return FROM_VALUE(new Value(std::move(map)));
}
13 changes: 12 additions & 1 deletion libs/server-sdk/tests/server_c_bindings_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

#include <launchdarkly/server_side/bindings/c/config/builder.h>
#include <launchdarkly/server_side/bindings/c/sdk.h>
#include <launchdarkly/server_side/data_source_status.hpp>
#include <launchdarkly/server_side/config/config.hpp>
#include <launchdarkly/server_side/data_source_status.hpp>

#include <launchdarkly/bindings/c/context_builder.h>

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit ac8b61b

Please sign in to comment.