Skip to content

Commit e7c0dfe

Browse files
authored
feat: allow nil to be passed for clientInit config (#94)
This tiny feature allows users to pass `nil` as the third argument to `clientInit` to request default configuration: ```lua ld.clientInit("sdk-key", 1000, nil) ``` Before, you had to explicitly pass an empty `{}` table.
1 parent efe82f7 commit e7c0dfe

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

launchdarkly-server-sdk.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1039,9 +1039,14 @@ makeConfig(lua_State *const l, const char *const sdk_key)
10391039
{
10401040
LDServerConfigBuilder builder = LDServerConfigBuilder_New(sdk_key);
10411041

1042-
// Recursively visit the hierarchical configs, modifying the builder
1043-
// as we go along.
1044-
traverse_config(l, builder, &top_level_config);
1042+
1043+
// Allow users to pass in a nil config, which is equivalent to passing an
1044+
// empty table (i.e. default configuration.)
1045+
if (lua_type(l, -1) != LUA_TNIL) {
1046+
// Recursively visit the hierarchical configs, modifying the builder
1047+
// as we go along.
1048+
traverse_config(l, builder, &top_level_config);
1049+
}
10451050

10461051
LDServerConfigBuilder_HttpProperties_WrapperName(builder, "lua-server-sdk");
10471052
LDServerConfigBuilder_HttpProperties_WrapperVersion(builder, SDKVersion);
@@ -1061,12 +1066,14 @@ makeConfig(lua_State *const l, const char *const sdk_key)
10611066
Initialize a new client, and connect to LaunchDarkly.
10621067
Applications should instantiate a single instance for the lifetime of their application.
10631068
1069+
To initialize with default configuration, you may pass nil or an empty table as the third argument.
1070+
10641071
@function clientInit
10651072
@param string Environment SDK key
10661073
@param int Initialization timeout in milliseconds. clientInit will
10671074
block for this long before returning a non-fully initialized client. Pass 0 to return
10681075
immediately without waiting (note that the client will continue initializing in the background.)
1069-
@tparam table config list of configuration options
1076+
@tparam table config optional configuration options, or nil/empty table for default configuration.
10701077
@tparam[opt] boolean config.offline Sets whether this client is offline.
10711078
An offline client will not make any network connections to LaunchDarkly or
10721079
a data source like Redis, nor send any events, and will return application-defined

test.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ function TestAll:testSetNoConfigFields()
3838
u.assertNotIsNil(l.clientInit("sdk-test", 0, {}))
3939
end
4040

41+
function TestAll:testNilConfig()
42+
u.assertNotIsNil(l.clientInit("sdk-test", 0, nil))
43+
end
4144

4245
function TestAll:testSetAllConfigFields()
4346
local c = l.clientInit("sdk-test", 0, {

0 commit comments

Comments
 (0)