Skip to content

Commit

Permalink
c bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
cwaldren-ld committed Sep 3, 2024
1 parent f9c6d41 commit 0292ed4
Show file tree
Hide file tree
Showing 8 changed files with 240 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@
#include <launchdarkly/config/shared/defaults.hpp>
#include <launchdarkly/config/shared/sdks.hpp>

#include <launchdarkly/error.hpp>

#include <tl/expected.hpp>

#include <chrono>
#include <variant>
#include <type_traits>

namespace launchdarkly::config::shared::builders {
/**
Expand All @@ -19,6 +16,14 @@ namespace launchdarkly::config::shared::builders {
template <typename SDK>
class DataSourceBuilder;

template <typename T>
struct is_server_sdk : std::false_type {
};

template <>
struct is_server_sdk<ServerSDK> : std::true_type {
};

/**
* Builds a configuration for a streaming data source.
*/
Expand All @@ -43,7 +48,7 @@ class StreamingBuilder {
std::chrono::milliseconds initial_reconnect_delay);

/**
* Filter sets the filter key for the streaming connection.
* Sets the filter key for the streaming connection.
*
* By default, the SDK is able to evaluate all flags in an environment.
*
Expand All @@ -59,7 +64,12 @@ class StreamingBuilder {
* malformed key, the SDK will additionally log a runtime error.
* @return Reference to this builder.
*/
StreamingBuilder& Filter(std::string filter_key);
template <typename T = SDK>
std::enable_if_t<is_server_sdk<T>::value, StreamingBuilder&> Filter(
std::string filter_key) {

Check warning on line 69 in libs/common/include/launchdarkly/config/shared/builders/data_source_builder.hpp

View workflow job for this annotation

GitHub Actions / cpp-linter

/libs/common/include/launchdarkly/config/shared/builders/data_source_builder.hpp:69:21 [performance-unnecessary-value-param]

the parameter 'filter_key' is copied for each invocation but only used as a const reference; consider making it a const reference
config_.filter_key = std::move(filter_key);
return *this;
}

/**
* Build the streaming config. Used internal to the SDK.
Expand Down Expand Up @@ -87,7 +97,7 @@ class PollingBuilder {
PollingBuilder& PollInterval(std::chrono::seconds poll_interval);

/**
* Filter sets the filter key for the polling connection.
* Sets the filter key for the polling connection.
*
* By default, the SDK is able to evaluate all flags in an environment.
*
Expand All @@ -104,7 +114,12 @@ class PollingBuilder {
* malformed key, the SDK will additionally log a runtime error.
* @return Reference to this builder.
*/
PollingBuilder& Filter(std::string filter_key);
template <typename T = SDK>
std::enable_if_t<is_server_sdk<T>::value, PollingBuilder&> Filter(
std::string filter_key) {

Check warning on line 119 in libs/common/include/launchdarkly/config/shared/builders/data_source_builder.hpp

View workflow job for this annotation

GitHub Actions / cpp-linter

/libs/common/include/launchdarkly/config/shared/builders/data_source_builder.hpp:119:21 [performance-unnecessary-value-param]

the parameter 'filter_key' is copied for each invocation but only used as a const reference; consider making it a const reference
config_.filter_key = std::move(filter_key);
return *this;
}

/**
* Build the polling config. Used internal to the SDK.
Expand All @@ -116,6 +131,7 @@ class PollingBuilder {
built::PollingConfig<SDK> config_;
};


template <>
class DataSourceBuilder<ClientSDK> {
public:
Expand All @@ -138,7 +154,7 @@ class DataSourceBuilder<ClientSDK> {
DataSourceBuilder& WithReasons(bool value);

/**
* Whether or not to use the REPORT verb to fetch flag settings.
* Whether to use the REPORT verb to fetch flag settings.
*
* If this is true, flag settings will be fetched with a REPORT request
* including a JSON entity body with the context object.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include <variant>

namespace launchdarkly::config::shared::built {

template <typename SDK>
struct StreamingConfig;

Expand All @@ -22,12 +21,14 @@ template <>
struct StreamingConfig<ServerSDK> {
std::chrono::milliseconds initial_reconnect_delay;
std::string streaming_path;
std::optional<std::string> filter_key;
};

inline bool operator==(StreamingConfig<ServerSDK> const& lhs,
StreamingConfig<ServerSDK> const& rhs) {
return lhs.initial_reconnect_delay == rhs.initial_reconnect_delay &&
lhs.streaming_path == rhs.streaming_path;
lhs.streaming_path == rhs.streaming_path && lhs.filter_key == rhs.
filter_key;
}

template <typename SDK>
Expand All @@ -46,6 +47,7 @@ struct PollingConfig<ServerSDK> {
std::chrono::seconds poll_interval;
std::string polling_get_path;
std::chrono::seconds min_polling_interval;
std::optional<std::string> filter_key;
};

template <typename SDK>
Expand All @@ -60,5 +62,6 @@ struct DataSourceConfig<ClientSDK> {
};

template <>
struct DataSourceConfig<ServerSDK> {};
} // namespace launchdarkly::config::shared::built
struct DataSourceConfig<ServerSDK> {
};
} // namespace launchdarkly::config::shared::built
12 changes: 0 additions & 12 deletions libs/common/src/config/data_source_builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,6 @@ StreamingBuilder<SDK>& StreamingBuilder<SDK>::InitialReconnectDelay(
return *this;
}

template <typename SDK>
StreamingBuilder<SDK>& StreamingBuilder<SDK>::Filter(std::string filter_key) {
config_.filter_key = std::move(filter_key);
return *this;
}

template <typename SDK>
built::StreamingConfig<SDK> StreamingBuilder<SDK>::Build() const {
return config_;
Expand All @@ -55,12 +49,6 @@ PollingBuilder<SDK>& PollingBuilder<SDK>::PollInterval(
return *this;
}

template <typename SDK>
PollingBuilder<SDK>& PollingBuilder<SDK>::Filter(std::string filter_key) {
config_.filter_key = std::move(filter_key);
return *this;
}

template <typename SDK>
built::PollingConfig<SDK> PollingBuilder<SDK>::Build() const {
return config_;
Expand Down
28 changes: 14 additions & 14 deletions libs/common/tests/data_source_builder_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,37 +11,37 @@ using namespace launchdarkly;
TEST(DataSourceBuilderTests, CanCreateStreamingClientConfig) {
auto client_config =
client_side::DataSourceBuilder()
.WithReasons(true)
.UseReport(true)
.Method(client_side::DataSourceBuilder::Streaming()
.InitialReconnectDelay(std::chrono::milliseconds{1500}))
.Build();
.WithReasons(true)
.UseReport(true)
.Method(client_side::DataSourceBuilder::Streaming()
.InitialReconnectDelay(std::chrono::milliseconds{1500}))

Check warning on line 17 in libs/common/tests/data_source_builder_test.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

/libs/common/tests/data_source_builder_test.cpp:17:62 [cppcoreguidelines-avoid-magic-numbers

1500 is a magic number; consider replacing it with a named constant
.Build();

EXPECT_TRUE(client_config.use_report);
EXPECT_TRUE(client_config.with_reasons);
EXPECT_EQ(
std::chrono::milliseconds{1500},
std::get<
config::shared::built::StreamingConfig<config::shared::ClientSDK>>(
config::shared::built::StreamingConfig<config::shared::ClientSDK>>(
client_config.method)
.initial_reconnect_delay);
.initial_reconnect_delay);
}

TEST(DataSourceBuilderTests, CanCreatePollingClientConfig) {
auto client_config =
client_side::DataSourceBuilder()
.WithReasons(false)
.UseReport(false)
.Method(client_side::DataSourceBuilder::Polling().PollInterval(
std::chrono::seconds{88000}))
.Build();
.WithReasons(false)
.UseReport(false)
.Method(client_side::DataSourceBuilder::Polling().PollInterval(
std::chrono::seconds{88000}))

Check warning on line 36 in libs/common/tests/data_source_builder_test.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

/libs/common/tests/data_source_builder_test.cpp:36:34 [cppcoreguidelines-avoid-magic-numbers

88000 is a magic number; consider replacing it with a named constant
.Build();

EXPECT_FALSE(client_config.use_report);
EXPECT_FALSE(client_config.with_reasons);
EXPECT_EQ(
std::chrono::seconds{88000},
std::get<
config::shared::built::PollingConfig<config::shared::ClientSDK>>(
config::shared::built::PollingConfig<config::shared::ClientSDK>>(
client_config.method)
.poll_interval);
.poll_interval);
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,17 @@
#include <stddef.h>

#ifdef __cplusplus
extern "C" { // only need to export C interface if
extern "C" {
// only need to export C interface if
// used by C++ source code
#endif

typedef struct _LDServerConfigBuilder* LDServerConfigBuilder;
typedef struct _LDServerDataSourceStreamBuilder*
LDServerDataSourceStreamBuilder;
LDServerDataSourceStreamBuilder;
typedef struct _LDServerDataSourcePollBuilder* LDServerDataSourcePollBuilder;
typedef struct _LDServerHttpPropertiesTlsBuilder*
LDServerHttpPropertiesTlsBuilder;
LDServerHttpPropertiesTlsBuilder;

/**
* Constructs a client-side config builder.
Expand Down Expand Up @@ -63,8 +64,8 @@ LDServerConfigBuilder_ServiceEndpoints_EventsBaseURL(LDServerConfigBuilder b,
*/
LD_EXPORT(void)
LDServerConfigBuilder_ServiceEndpoints_RelayProxyBaseURL(
LDServerConfigBuilder b,
char const* url);
LDServerConfigBuilder b,
char const* url);

/**
* Sets an identifier for the application.
Expand Down Expand Up @@ -201,8 +202,8 @@ LDServerConfigBuilder_Events_PrivateAttribute(LDServerConfigBuilder b,
*/
LD_EXPORT(void)
LDServerConfigBuilder_DataSystem_BackgroundSync_Streaming(
LDServerConfigBuilder b,
LDServerDataSourceStreamBuilder stream_builder);
LDServerConfigBuilder b,
LDServerDataSourceStreamBuilder stream_builder);

/**
* Configures the Background Sync data system with a Polling synchronizer.
Expand All @@ -219,8 +220,8 @@ LDServerConfigBuilder_DataSystem_BackgroundSync_Streaming(
*/
LD_EXPORT(void)
LDServerConfigBuilder_DataSystem_BackgroundSync_Polling(
LDServerConfigBuilder b,
LDServerDataSourcePollBuilder poll_builder);
LDServerConfigBuilder b,
LDServerDataSourcePollBuilder poll_builder);

/**
* Configures the Lazy Load data system. This method is mutually exclusive with
Expand All @@ -235,8 +236,8 @@ LDServerConfigBuilder_DataSystem_BackgroundSync_Polling(
*/
LD_EXPORT(void)
LDServerConfigBuilder_DataSystem_LazyLoad(
LDServerConfigBuilder b,
LDServerLazyLoadBuilder lazy_load_builder);
LDServerConfigBuilder b,
LDServerLazyLoadBuilder lazy_load_builder);

/**
* Specify if the SDK's data system should be enabled or not.
Expand Down Expand Up @@ -274,8 +275,31 @@ LDServerDataSourceStreamBuilder_New();
*/
LD_EXPORT(void)
LDServerDataSourceStreamBuilder_InitialReconnectDelayMs(
LDServerDataSourceStreamBuilder b,
unsigned int milliseconds);
LDServerDataSourceStreamBuilder b,
unsigned int milliseconds);

/**
* Sets the filter key for the streaming connection.
*
* By default, the SDK is able to evaluate all flags in an environment.
*
* If this is undesirable - for example, because the environment contains
* thousands of flags, but this application only needs to evaluate
* a smaller, known subset - then a filter may be setup in LaunchDarkly,
* and the filter's key specified here.
*
* Evaluations for flags that aren't part of the filtered environment will
* return default values.
*
* @param b Streaming method builder. Must not be NULL.
* @param filter_key The filter key. Must not be NULL. If the key is malformed or
* nonexistent, then a full LaunchDarkly environment will be fetched. In the case
* of a malformed key, the SDK will additionally log a runtime error.
*/
LD_EXPORT(void)
LDServerDataSourceStreamBuilder_Filter(
LDServerDataSourceStreamBuilder b,
char const* filter_key);

/**
* Frees a Streaming method builder. Do not call if the builder was consumed by
Expand Down Expand Up @@ -307,6 +331,29 @@ LD_EXPORT(void)
LDServerDataSourcePollBuilder_IntervalS(LDServerDataSourcePollBuilder b,
unsigned int seconds);

/**
* Sets the filter key for the polling connection.
*
* By default, the SDK is able to evaluate all flags in an environment.
*
* If this is undesirable - for example, because the environment contains
* thousands of flags, but this application only needs to evaluate
* a smaller, known subset - then a filter may be setup in LaunchDarkly,
* and the filter's key specified here.
*
* Evaluations for flags that aren't part of the filtered environment will
* return default values.
*
* @param b Polling method builder. Must not be NULL.
* @param filter_key The filter key. Must not be NULL. If the key is malformed or
* nonexistent, then a full LaunchDarkly environment will be fetched. In the case
* of a malformed key, the SDK will additionally log a runtime error.
*/
LD_EXPORT(void)
LDServerDataSourcePollBuilder_Filter(
LDServerDataSourcePollBuilder b,
char const* filter_key);

/**
* Frees a Polling method builder. Do not call if the builder was consumed by
* the config builder.
Expand Down Expand Up @@ -336,8 +383,8 @@ LDServerConfigBuilder_HttpProperties_WrapperName(LDServerConfigBuilder b,
*/
LD_EXPORT(void)
LDServerConfigBuilder_HttpProperties_WrapperVersion(
LDServerConfigBuilder b,
char const* wrapper_version);
LDServerConfigBuilder b,
char const* wrapper_version);

/**
* Set a custom header value. May be called more than once with additional
Expand All @@ -359,8 +406,8 @@ LDServerConfigBuilder_HttpProperties_Header(LDServerConfigBuilder b,
*/
LD_EXPORT(void)
LDServerConfigBuilder_HttpProperties_Tls(
LDServerConfigBuilder b,
LDServerHttpPropertiesTlsBuilder tls_builder);
LDServerConfigBuilder b,
LDServerHttpPropertiesTlsBuilder tls_builder);

/**
* Creates a new TLS options builder for the HttpProperties builder.
Expand Down Expand Up @@ -394,8 +441,8 @@ LDServerHttpPropertiesTlsBuilder_Free(LDServerHttpPropertiesTlsBuilder b);
*/
LD_EXPORT(void)
LDServerHttpPropertiesTlsBuilder_SkipVerifyPeer(
LDServerHttpPropertiesTlsBuilder b,
bool skip_verify_peer);
LDServerHttpPropertiesTlsBuilder b,
bool skip_verify_peer);

/**
* Configures TLS peer certificate verification to use a custom
Expand All @@ -414,8 +461,8 @@ LDServerHttpPropertiesTlsBuilder_SkipVerifyPeer(
*/
LD_EXPORT(void)
LDServerHttpPropertiesTlsBuilder_CustomCAFile(
LDServerHttpPropertiesTlsBuilder b,
char const* custom_ca_file);
LDServerHttpPropertiesTlsBuilder b,
char const* custom_ca_file);

/**
* Disables the default SDK logging.
Expand Down
Loading

0 comments on commit 0292ed4

Please sign in to comment.