Skip to content

Commit

Permalink
feat: add client-side C binding for fetching data source state
Browse files Browse the repository at this point in the history
  • Loading branch information
cwaldren-ld committed Sep 30, 2024
1 parent 04e7e0e commit 7e13a0b
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -501,9 +501,23 @@ enum LDDataSourceStatus_State {
* SDK key will never become valid), or because the SDK client was
* explicitly shut down.
*/
LD_DATASOURCESTATUS_STATE_SHUTDOWN = 4
LD_DATASOURCESTATUS_STATE_SHUTDOWN = 4,

LD_DATASOURCESTATUS_STATE_UNUSED_MAXVALUE =
INT32_MAX /* Used to ensure the underlying type is
* at least 32 bits. */
};

/**
* @param state The state to convert to a string.
* @param default_if_unknown The default string to return if the state is not
* recognized.
* @return Returns the name of the given LDDataSourceStatus_State.
*/
LD_EXPORT(char const*)
LDDataSourceStatus_State_Name(enum LDDataSourceStatus_State state,
char const* default_if_unknown);

/**
* Get an enumerated value representing the overall current state of the data
* source.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <chrono>
#include <cstddef>
#include <cstdint>
#include <functional>
#include <memory>
#include <optional>
Expand All @@ -15,7 +16,7 @@ namespace launchdarkly::client_side::data_sources {
/**
* Enumeration of possible data source states.
*/
enum class DataSourceState {
enum class DataSourceState : std::int32_t {
/**
* The initial state of the data source when the SDK is being
* initialized.
Expand Down Expand Up @@ -62,15 +63,17 @@ enum class DataSourceState {
* SDK key will never become valid), or because the SDK client was
* explicitly shut down.
*/
kShutdown = 4,

// BackgroundDisabled,

// TODO: A plugin of sorts would likely be required to implement
// network availability.
// kNetworkUnavailable,
kShutdown = 4
};

/**
*
* @return Returns the name of the given DataSourceState as a string. If
* the enum value is not recognized, the default string value is returned.
*/
char const* GetDataSourceStateName(DataSourceState state,
char const* default_if_unknown);

using DataSourceStatus =
common::data_sources::DataSourceStatusBase<DataSourceState>;

Expand Down
8 changes: 8 additions & 0 deletions libs/client-sdk/src/bindings/c/sdk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,14 @@ LDDataSourceStatus_GetState(LDDataSourceStatus status) {
TO_DATASOURCESTATUS(status)->State());
}

LD_EXPORT(char const*)
LDDataSourceStatus_State_Name(enum LDDataSourceStatus_State state,
char const* default_if_unknown) {
return GetDataSourceStateName(
static_cast<data_sources::DataSourceStatus::DataSourceState>(state),
default_if_unknown);
}

LD_EXPORT(LDDataSourceStatus_ErrorInfo)
LDDataSourceStatus_GetLastError(LDDataSourceStatus status) {
LD_ASSERT_NOT_NULL(status);
Expand Down
25 changes: 13 additions & 12 deletions libs/client-sdk/src/data_sources/data_source_status.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,27 @@

namespace launchdarkly::client_side::data_sources {

std::ostream& operator<<(std::ostream& out,
DataSourceStatus::DataSourceState const& state) {
char const* GetDataSourceStateName(DataSourceState state,
char const* default_if_unknown) {
switch (state) {
case DataSourceStatus::DataSourceState::kInitializing:
out << "INITIALIZING";
break;
return "INITIALIZING";
case DataSourceStatus::DataSourceState::kValid:
out << "VALID";
break;
return "VALID";
case DataSourceStatus::DataSourceState::kInterrupted:
out << "INTERRUPTED";
break;
return "INTERRUPTED";
case DataSourceStatus::DataSourceState::kSetOffline:
out << "OFFLINE";
break;
return "OFFLINE";
case DataSourceStatus::DataSourceState::kShutdown:
out << "SHUTDOWN";
break;
return "SHUTDOWN";
default:
return default_if_unknown;
}
}

std::ostream& operator<<(std::ostream& out,
DataSourceStatus::DataSourceState const& state) {
out << GetDataSourceStateName(state, "UNKNOWN");
return out;
}

Expand Down
26 changes: 24 additions & 2 deletions libs/client-sdk/tests/client_c_bindings_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,9 @@ TEST(ClientBindings, RegisterFlagListener) {
}

void StatusListenerFunction(LDDataSourceStatus status, void* user_data) {
EXPECT_EQ(LD_DATASOURCESTATUS_STATE_OFFLINE,
LDDataSourceStatus_GetState(status));
auto const state = LDDataSourceStatus_GetState(status);
EXPECT_EQ(LD_DATASOURCESTATUS_STATE_OFFLINE, state);
EXPECT_STREQ("OFFLINE", LDDataSourceStatus_State_Name(state, "unknown"));
}

// This test registers a listener. It doesn't use the listener, but it
Expand Down Expand Up @@ -194,3 +195,24 @@ TEST(ClientBindings, ComplexDataSourceStatus) {

LDDataSourceStatus_ErrorInfo_Free(info);
}

TEST(ClientBindings, TestDataSourceStatusStateName) {
ASSERT_STREQ(LDDataSourceStatus_State_Name(LD_DATASOURCESTATUS_STATE_VALID,
"unknown"),
"VALID");
ASSERT_STREQ(LDDataSourceStatus_State_Name(
LD_DATASOURCESTATUS_STATE_OFFLINE, "unknown"),
"OFFLINE");
ASSERT_STREQ(LDDataSourceStatus_State_Name(
LD_DATASOURCESTATUS_STATE_INITIALIZING, "unknown"),
"INITIALIZING");
ASSERT_STREQ(LDDataSourceStatus_State_Name(
LD_DATASOURCESTATUS_STATE_SHUTDOWN, "unknown"),
"SHUTDOWN");
ASSERT_STREQ(LDDataSourceStatus_State_Name(
LD_DATASOURCESTATUS_STATE_INTERRUPTED, "unknown"),
"INTERRUPTED");
ASSERT_STREQ(LDDataSourceStatus_State_Name(
LD_DATASOURCESTATUS_STATE_UNUSED_MAXVALUE, "unknown"),
"unknown");
}

0 comments on commit 7e13a0b

Please sign in to comment.