Skip to content

Commit

Permalink
Allow feature flag state configuration via application settings (#2963)
Browse files Browse the repository at this point in the history
* Allow feature flag state configuration via application settings

* Use string values for flags

* Update src/Core/Services/Implementations/LaunchDarklyFeatureService.cs

Remove useless `ToString()`.

Co-authored-by: Justin Baur <[email protected]>

---------

Co-authored-by: Justin Baur <[email protected]>
  • Loading branch information
withinfocus and justindbaur authored May 26, 2023
1 parent 5079c2b commit beb3479
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
28 changes: 25 additions & 3 deletions src/Core/Services/Implementations/LaunchDarklyFeatureService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,37 @@ public LaunchDarklyFeatureService(
.FilePaths(globalSettings.LaunchDarkly?.FlagDataFilePath)
.AutoUpdate(true)
);
}
// support configuration directly from settings
else if (globalSettings.LaunchDarkly?.FlagValues?.Any() is true)
{
var source = TestData.DataSource();
foreach (var kvp in globalSettings.LaunchDarkly.FlagValues)
{
if (bool.TryParse(kvp.Value, out bool boolValue))
{
source.Update(source.Flag(kvp.Key).ValueForAll(LaunchDarkly.Sdk.LdValue.Of(boolValue)));
}
else if (int.TryParse(kvp.Value, out int intValue))
{
source.Update(source.Flag(kvp.Key).ValueForAll(LaunchDarkly.Sdk.LdValue.Of(intValue)));
}
else
{
source.Update(source.Flag(kvp.Key).ValueForAll(LaunchDarkly.Sdk.LdValue.Of(kvp.Value)));
}
}

// do not provide analytics events
ldConfig.Events(Components.NoEvents);
ldConfig.DataSource(source);
}
else
{
// when a file-based fallback isn't available, work offline
// when fallbacks aren't available, work offline
ldConfig.Offline(true);
}

// do not provide analytics events
ldConfig.Events(Components.NoEvents);
}
else if (globalSettings.SelfHosted)
{
Expand Down
1 change: 1 addition & 0 deletions src/Core/Settings/GlobalSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -545,5 +545,6 @@ public class LaunchDarklySettings : ILaunchDarklySettings
{
public string SdkKey { get; set; }
public string FlagDataFilePath { get; set; } = "flags.json";
public Dictionary<string, string> FlagValues { get; set; } = new Dictionary<string, string>();
}
}
1 change: 1 addition & 0 deletions src/Core/Settings/ILaunchDarklySettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ public interface ILaunchDarklySettings
{
public string SdkKey { get; set; }
public string FlagDataFilePath { get; set; }
public Dictionary<string, string> FlagValues { get; set; }
}

0 comments on commit beb3479

Please sign in to comment.