Class FileDataSourceBuilder
A builder for configuring the file data source.
Implements
Inherited Members
Namespace: LaunchDarkly.Sdk.Server.Integrations
Assembly: LaunchDarkly.ServerSdk.dll
Syntax
public sealed class FileDataSourceBuilder : IComponentConfigurer<IDataSource>
Remarks
To use the file data source, obtain a new instance of this class with DataSource(); call the builder method {@link #filePaths(String...)} to specify file path(s), and/or {@link #classpathResources(String...)} to specify classpath data resources; then pass the resulting object to DataSource(IComponentConfigurer<IDataSource>).
Constructors
View SourceFileDataSourceBuilder()
Declaration
public FileDataSourceBuilder()
Methods
| Edit this page View SourceAutoUpdate(bool)
Specifies whether the data source should watch for changes to the source file(s) and reload flags whenever there is a change.
Declaration
public FileDataSourceBuilder AutoUpdate(bool autoUpdate)
Parameters
Type | Name | Description |
---|---|---|
bool | autoUpdate | true if flags should be reloaded whenever a source file changes |
Returns
Type | Description |
---|---|
FileDataSourceBuilder | the same factory object |
Remarks
This option is off by default: unless you set this option to true
, files will only be loaded once.
Whenever possible, you should update a file's entire contents in one atomic operation; in Unix-like OSes, that can be done by creating a temporary file, writing to it, and then renaming it to replace the original file. In Windows, that is not always possible, so FileDataSource might detect an update before the file has been fully written; in that case it will retry until it succeeds.
Note that auto-updating may not work if any of the files you specified has an invalid directory path.
Build(LdClientContext)
Called internally by the SDK to create an implementation instance. Applications should not need to call this method.
Declaration
public IDataSource Build(LdClientContext context)
Parameters
Type | Name | Description |
---|---|---|
LdClientContext | context | provides configuration properties and other components from the current SDK client instance |
Returns
Type | Description |
---|---|
IDataSource | a instance of the component type |
DuplicateKeysHandling(DuplicateKeysHandling)
Specifies how to handle keys that are duplicated across files.
Declaration
public FileDataSourceBuilder DuplicateKeysHandling(FileDataTypes.DuplicateKeysHandling duplicateKeysHandling)
Parameters
Type | Name | Description |
---|---|---|
FileDataTypes.DuplicateKeysHandling | duplicateKeysHandling | how duplicate keys should be handled |
Returns
Type | Description |
---|---|
FileDataSourceBuilder | the same factory object |
Remarks
By default, the file data source will throw if keys are duplicated across files.
FilePaths(params string[])
Adds any number of source files for loading flag data, specifying each file path as a string.
Declaration
public FileDataSourceBuilder FilePaths(params string[] paths)
Parameters
Type | Name | Description |
---|---|---|
string[] | paths | path(s) to the source file(s); may be absolute or relative to the current working directory |
Returns
Type | Description |
---|---|
FileDataSourceBuilder | the same factory object |
Remarks
The files will not actually be loaded until the LaunchDarkly client starts up.
Files are normally expected to contain JSON; see Parser(Func<string, object>) for alternatives.
FileReader(IFileReader)
Specifies an alternate file reader which can support custom OS error handling and retry logic.
Declaration
public FileDataSourceBuilder FileReader(FileDataTypes.IFileReader fileReader)
Parameters
Type | Name | Description |
---|---|---|
FileDataTypes.IFileReader | fileReader | The flag file reader. |
Returns
Type | Description |
---|---|
FileDataSourceBuilder | the same factory object |
Parser(Func<string, object>)
Specifies an alternate parsing function to use for non-JSON source files.
Declaration
public FileDataSourceBuilder Parser(Func<string, object> parseFn)
Parameters
Type | Name | Description |
---|---|---|
Func<string, object> | parseFn | the parsing function |
Returns
Type | Description |
---|---|
FileDataSourceBuilder | the same factory object |
Remarks
By default, the file data source attempts to parse files as JSON objects. You may wish to use another format,
such as YAML. To avoid bringing in additional dependencies that might conflict with application dependencies,
the LaunchDarkly SDK does not import a YAML parser, but you can use Parser
to specify a parser of
your choice.
The function you provide should take a string and return an object
which should contain only the basic
types that can be represented in JSON: so, for instance, string values should be string
, arrays should
be List
, and objects with key-value pairs should be Dictionary<string, object>
. It should
throw an exception if it can't parse the data.
The file data source will still try to parse files as JSON if their first non-whitespace character is '{', but if that fails, it will use the custom parser.
Here is an example of how you would do this with the YamlDotNet
package. Note the use of a
DeserializerBuilder
configuration method to tell the parser that it should interpret a property like
on: true
or variation: 3
as a boolean or integer rather than a string; YAML syntax is very
flexible, so how you configure the parser will depend on how you are formatting the file, but data types do
matter in flag configurations so you do not want it to simply read every property as a string.
var yaml = new DeserializerBuilder()
.WithAttemptingUnquotedStringTypeDeserialization()
.Build();
var source = FileData.DataSource()
.FilePaths(myYamlFilePath)
.Parser(s => yaml.Deserialize<object>(s));
SkipMissingPaths(bool)
Specifies to ignore missing file paths instead of treating them as an error.
Declaration
public FileDataSourceBuilder SkipMissingPaths(bool skipMissingPaths)
Parameters
Type | Name | Description |
---|---|---|
bool | skipMissingPaths | If |
Returns
Type | Description |
---|---|
FileDataSourceBuilder | the same factory object |