1 package ldfiledata 2 3 import ( 4 "github.com/launchdarkly/go-sdk-common/v3/ldlog" 5 "github.com/launchdarkly/go-server-sdk/v6/subsystems" 6 ) 7 8 // ReloaderFactory is a function type used with DataSourceBuilder.Reloader, to specify a mechanism for 9 // detecting when data files should be reloaded. Its standard implementation is in the ldfilewatch package. 10 type ReloaderFactory func(paths []string, loggers ldlog.Loggers, reload func(), closeCh <-chan struct{}) error 11 12 // DuplicateKeysHandling is a parameter type used with DataSourceBuilder.DuplicateKeysHandling. 13 type DuplicateKeysHandling string 14 15 const ( 16 // DuplicateKeysFail is an option for DataSourceBuilder.DuplicateKeysHandling, meaning that data loading 17 // should fail if keys are duplicated across files. This is the default behavior. 18 DuplicateKeysFail DuplicateKeysHandling = "fail" 19 20 // DuplicateKeysIgnoreAllButFirst is an option for DataSourceBuilder.DuplicateKeysHandling, meaning that 21 // if keys are duplicated across files the first occurrence will be used. 22 DuplicateKeysIgnoreAllButFirst DuplicateKeysHandling = "ignore" 23 ) 24 25 // DataSourceBuilder is a builder for configuring the file-based data source. 26 // 27 // Obtain an instance of this type by calling [DataSource]. After calling its methods to specify any 28 // desired custom settings, store it in the DataSource field of [github.com/launchdarkly/go-server-sdk/v6.Config]. 29 // 30 // Builder calls can be chained, for example: 31 // 32 // config.DataStore = ldfiledata.DataSource().FilePaths("file1").FilePaths("file2") 33 // 34 // You do not need to call the builder's Build method yourself; that will be done by the SDK. 35 type DataSourceBuilder struct { 36 filePaths []string 37 duplicateKeysHandling DuplicateKeysHandling 38 reloaderFactory ReloaderFactory 39 } 40 41 // DataSource returns a configurable builder for a file-based data source. 42 func DataSource() *DataSourceBuilder { 43 return &DataSourceBuilder{duplicateKeysHandling: DuplicateKeysFail} 44 } 45 46 // DuplicateKeysHandling specifies how to handle keys that are duplicated across files. 47 // 48 // If this is not specified, or if you set it to an unrecognized value, the default is DuplicateKeysFail. 49 func (b *DataSourceBuilder) DuplicateKeysHandling(duplicateKeysHandling DuplicateKeysHandling) *DataSourceBuilder { 50 b.duplicateKeysHandling = duplicateKeysHandling 51 return b 52 } 53 54 // FilePaths specifies the input data files. The paths may be any number of absolute or relative file paths. 55 func (b *DataSourceBuilder) FilePaths(paths ...string) *DataSourceBuilder { 56 b.filePaths = append(b.filePaths, paths...) 57 return b 58 } 59 60 // Reloader specifies a mechanism for reloading data files. 61 // 62 // It is normally used with the [github.com/launchdarkly/go-server-sdk/v6/ldfilewatch] package, as follows: 63 // 64 // config := ld.Config{ 65 // DataSource: ldfiledata.DataSource(). 66 // FilePaths(filePaths). 67 // Reloader(ldfilewatch.WatchFiles), 68 // } 69 func (b *DataSourceBuilder) Reloader(reloaderFactory ReloaderFactory) *DataSourceBuilder { 70 b.reloaderFactory = reloaderFactory 71 return b 72 } 73 74 // Build is called internally by the SDK. 75 func (b *DataSourceBuilder) Build(context subsystems.ClientContext) (subsystems.DataSource, error) { 76 return newFileDataSourceImpl(context, context.GetDataSourceUpdateSink(), b.filePaths, 77 b.duplicateKeysHandling, b.reloaderFactory) 78 } 79