Skip to content

Commit

Permalink
feat: allow nil to be passed for clientInit config (#94)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
cwaldren-ld authored Mar 11, 2024
1 parent efe82f7 commit e7c0dfe
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
15 changes: 11 additions & 4 deletions launchdarkly-server-sdk.c
Original file line number Diff line number Diff line change
Expand Up @@ -1039,9 +1039,14 @@ makeConfig(lua_State *const l, const char *const sdk_key)
{
LDServerConfigBuilder builder = LDServerConfigBuilder_New(sdk_key);

// Recursively visit the hierarchical configs, modifying the builder
// as we go along.
traverse_config(l, builder, &top_level_config);

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

LDServerConfigBuilder_HttpProperties_WrapperName(builder, "lua-server-sdk");
LDServerConfigBuilder_HttpProperties_WrapperVersion(builder, SDKVersion);
Expand All @@ -1061,12 +1066,14 @@ makeConfig(lua_State *const l, const char *const sdk_key)
Initialize a new client, and connect to LaunchDarkly.
Applications should instantiate a single instance for the lifetime of their application.
To initialize with default configuration, you may pass nil or an empty table as the third argument.
@function clientInit
@param string Environment SDK key
@param int Initialization timeout in milliseconds. clientInit will
block for this long before returning a non-fully initialized client. Pass 0 to return
immediately without waiting (note that the client will continue initializing in the background.)
@tparam table config list of configuration options
@tparam table config optional configuration options, or nil/empty table for default configuration.
@tparam[opt] boolean config.offline Sets whether this client is offline.
An offline client will not make any network connections to LaunchDarkly or
a data source like Redis, nor send any events, and will return application-defined
Expand Down
3 changes: 3 additions & 0 deletions test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ function TestAll:testSetNoConfigFields()
u.assertNotIsNil(l.clientInit("sdk-test", 0, {}))
end

function TestAll:testNilConfig()
u.assertNotIsNil(l.clientInit("sdk-test", 0, nil))
end

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

0 comments on commit e7c0dfe

Please sign in to comment.