launchdarkly-server-sdk-4.3.0: Server-side SDK for integrating with LaunchDarkly
Safe HaskellNone
LanguageHaskell2010

LaunchDarkly.Server.Integrations.TestData

Description

A mechanism for providing dynamically updatable feature flag state in a simplified form to an SDK client in test scenarios.

Unlike LaunchDarkly.Server.Integrations.FileData, this mechanism does not use any external resources. It provides only the data that the application has put into it using the update function.

td <- TestData.newTestData
update td =<< (flag td "flag-key-1"
                <&> booleanFlag
                <&> variationForAll True)

let config = makeConfig "sdkKey"
                & configSetDataSourceFactory (dataSourceFactory td)
client <- makeClient config

-- flags can be updated at any time:
update td =<<
   (flag td "flag-key-2"
         <&> variationForKey "user" "some-user-key" True
         <&> fallthroughVariation False)

The above example uses a simple boolean flag, but more complex configurations are possible using the methods of the FlagBuilder that is returned by flag. FlagBuilder supports many of the ways a flag can be configured on the LaunchDarkly dashboard, but does not currently support:

  1. Rule operators other than "in" and "not in"
  2. Percentage rollouts.

If the same TestData instance is used to configure multiple Client instances, any changes made to the data will propagate to all of the Clients.

see LaunchDarkly.Server.Integrations.FileData

Since: 2.2.1

Synopsis

Documentation

newTestData Source #

Arguments

:: IO TestData

a new configurable test data source

Creates a new instance of the test data source.

flag Source #

Arguments

:: TestData 
-> Text

the flag key

-> IO FlagBuilder

a flag configuration builder

Creates or copies a FlagBuilder for building a test flag configuration.

If this flag key has already been defined in this TestData instance, then the builder starts with the same configuration that was last provided for this flag.

Otherwise, it starts with a new default configuration in which the flag has True and False variations, is True for all users when targeting is turned on and False otherwise, and currently has targeting turned on. You can change any of those properties, and provide more complex behavior, using the FlagBuilder methods.

Once you have set the desired configuration, pass the builder to update.

see update

update Source #

Arguments

:: TestData 
-> FlagBuilder

a flag configuration builder

-> IO () 

Updates the test data with the specified flag configuration.

This has the same effect as if a flag were added or modified on the LaunchDarkly dashboard. It immediately propagates the flag change to any Client instance(s) that you have already configured to use this TestData. If no Client has been started yet, it simply adds this flag to the test data which will be provided to any Client that you subsequently configure.

Any subsequent changes to this FlagBuilder instance do not affect the test data, unless you call update

see flag

dataSourceFactory :: TestData -> DataSourceFactory Source #

FlagBuilder

data FlagBuilder Source #

A builder for feature flag configurations to be used with LaunchDarkly.Server.Integrations.TestData.

see flag and update

booleanFlag :: FlagBuilder -> FlagBuilder Source #

A shortcut for setting the flag to use the standard boolean configuration.

This is the default for all new flags created with flag. The flag will have two variations, True and False (in that order); it will return False whenever targeting is off, and True when targeting is on if no other settings specify otherwise.

on Source #

Arguments

:: Bool

isOn True if targeting should be on

-> FlagBuilder 
-> FlagBuilder 

Sets targeting to be on or off for this flag.

The effect of this depends on the rest of the flag configuration, just as it does on the real LaunchDarkly dashboard. In the default configuration that you get from calling flag with a new flag key, the flag will return False whenever targeting is off, and True when targeting is on.

fallthroughVariation Source #

Arguments

:: Variation val 
=> val

True or False or the desired fallthrough variation index: 0 for the first, 1 for the second, etc.

-> FlagBuilder 
-> FlagBuilder 

Specifies the fallthrough variation. The fallthrough is the value that is returned if targeting is on and the context was not matched by a more specific target or rule.

If the flag was previously configured with other variations and the variation specified is a boolean, this also changes it to a boolean flag.

offVariation Source #

Arguments

:: Variation val 
=> val

True or False or the desired fallthrough variation index: 0 for the first, 1 for the second, etc.

-> FlagBuilder 
-> FlagBuilder 

Specifies the off variation for a flag. This is the variation that is returned whenever targeting is off.

If the flag was previously configured with other variations and the variation specified is a boolean, this also changes it to a boolean flag.

variationForAll Source #

Arguments

:: Variation val 
=> val

True or False or the desired fallthrough variation index: 0 for the first, 1 for the second, etc.

-> FlagBuilder 
-> FlagBuilder 

Sets the flag to always return the specified variation for all contexts.

The variation is specified, Targeting is switched on, and any existing targets or rules are removed. The fallthrough variation is set to the specified value. The off variation is left unchanged.

If the flag was previously configured with other variations and the variation specified is a boolean, this also changes it to a boolean flag.

variationForAllUsers Source #

Arguments

:: Variation val 
=> val

True or False or the desired fallthrough variation index: 0 for the first, 1 for the second, etc.

-> FlagBuilder 
-> FlagBuilder 

Deprecated: Use variationForAll instead

Sets the flag to always return the specified variation for all users.

The variation is specified, Targeting is switched on, and any existing targets or rules are removed. The fallthrough variation is set to the specified value. The off variation is left unchanged.

If the flag was previously configured with other variations and the variation specified is a boolean, this also changes it to a boolean flag.

valueForAll :: ToJSON value => value -> FlagBuilder -> FlagBuilder Source #

Sets the flag to always return the specified variation value for all contexts.

The value may be of any type that implements ToJSON. This method changes the flag to have only a single variation, which is this value, and to return the same variation regardless of whether targeting is on or off. Any existing targets or rules are removed.

valueForAllUsers :: ToJSON value => value -> FlagBuilder -> FlagBuilder Source #

Deprecated: Use valueForAll instead

Sets the flag to always return the specified variation value for all users.

This function is an alias to valueForAll.

The value may be of any type that implements ToJSON. This method changes the flag to have only a single variation, which is this value, and to return the same variation regardless of whether targeting is on or off. Any existing targets or rules are removed.

variationForKey Source #

Arguments

:: Variation val 
=> Text

The context kind to match against

-> Text

a key to target

-> val

True or False or the desired fallthrough variation index: 0 for the first, 1 for the second, etc.

-> FlagBuilder 
-> FlagBuilder 

Sets the flag to return the specified variation for a specific context kind and key when targeting is on.

This has no effect when targeting is turned off for the flag.

If the flag was previously configured with other variations and the variation specified is a boolean, this also changes it to a boolean flag.

variationForUser Source #

Arguments

:: Variation val 
=> Text

a user key to target

-> val

True or False or the desired fallthrough variation index: 0 for the first, 1 for the second, etc.

-> FlagBuilder 
-> FlagBuilder 

Deprecated: Use variationForKey instead

Sets the flag to return the specified variation for a specific user key when targeting is on.

This has no effect when targeting is turned off for the flag.

If the flag was previously configured with other variations and the variation specified is a boolean, this also changes it to a boolean flag.

variations Source #

Arguments

:: [Value]

the desired variations

-> FlagBuilder 
-> FlagBuilder 

Changes the allowable variation values for the flag.

The value may be of any JSON type, as defined by Value. For instance, a boolean flag normally has [toJSON True, toJSON False]; a string-valued flag might have [toJSON "red", toJSON "green"]; etc.

ifMatch Source #

Arguments

:: Text

the context attribute to match against

-> [Value]

values to compare to

-> FlagBuilder 
-> FlagRuleBuilder

call thenReturn to finish the rule, or add more tests with andMatch or andNotMatch

Deprecated: Use ifMatchContext instead

Starts defining a flag rule, using the "is one of" operator.

This is a shortcut for calling ifMatch with a context kind of "user".

For example, this creates a rule that returns True if the name is "Patsy" or "Edina":

testData
    & flag "flag"
    & ifMatch "name" [toJSON "Patsy", toJSON "Edina"]
    & thenReturn True

ifMatchContext Source #

Arguments

:: Text

the context kind to match again

-> Text

the context attribute to match against

-> [Value]

values to compare to

-> FlagBuilder 
-> FlagRuleBuilder

call thenReturn to finish the rule, or add more tests with andMatch or andNotMatch

Starts defining a flag rule, using the "is one of" operator.

For example, this creates a rule that returns True if the name is "Patsy" or "Edina":

testData
    & flag "flag"
    & ifMatchContext "user" "name" [toJSON "Patsy", toJSON "Edina"]
    & thenReturn True

ifNotMatch Source #

Arguments

:: Text

attribute to match against

-> [Value]

values to compare to

-> FlagBuilder 
-> FlagRuleBuilder

call thenReturn to finish the rule, or add more tests with andMatch or andNotMatch

Deprecated: Use ifNotMatchContext instead

Starts defining a flag rule, using the "is not one of" operator.

This is a shortcut for calling ifNotMatchContext with a context kind of "user".

For example, this creates a rule that returns True if the name is neither "Saffron" nor "Bubble"

testData
    & flag "flag"
    & ifNotMatch "name" [toJSON "Saffron", toJSON "Bubble"]
    & thenReturn True

ifNotMatchContext Source #

Arguments

:: Text

context kind to match again

-> Text

attribute to match against

-> [Value]

values to compare to

-> FlagBuilder 
-> FlagRuleBuilder

call thenReturn to finish the rule, or add more tests with andMatch or andNotMatch

Starts defining a flag rule, using the "is not one of" operator.

For example, this creates a rule that returns True if the name is neither "Saffron" nor "Bubble"

testData
    & flag "flag"
    & ifNotMatchContext "user" "name" [toJSON "Saffron", toJSON "Bubble"]
    & thenReturn True

FlagRuleBuilder

data FlagRuleBuilder Source #

A builder for feature flag rules to be used with FlagBuilder.

In the LaunchDarkly model, a flag can have any number of rules, and a rule can have any number of clauses. A clause is an individual test such as "name is 'X'". A rule matches a context if all of the rule's clauses match the context.

To start defining a rule, use one of the matching functions such as ifMatch or ifNotMatch. This defines the first clause for the rule.

Optionally, you may add more clauses with the rule builder functions such as andMatch and andNotMatch.

Finally, call thenReturn to finish defining the rule.

andMatch Source #

Arguments

:: Text

the context attribute to match against

-> [Value]

values to compare to

-> FlagRuleBuilder 
-> FlagRuleBuilder 

Deprecated: Use andMatchContext instead

Adds another clause, using the "is one of" operator.

This is a shortcut for calling andMatchContext with a context kind of "user".

For example, this creates a rule that returns True if the name is "Patsy" and the country is "gb":

testData
    & flag "flag"
    & ifMatch "name" [toJSON "Patsy"]
    & andMatch "country" [toJSON "gb"]
    & thenReturn True

andMatchContext Source #

Arguments

:: Text

the context kind to match again

-> Text

the context attribute to match against

-> [Value]

values to compare to

-> FlagRuleBuilder 
-> FlagRuleBuilder 

Adds another clause, using the "is one of" operator.

For example, this creates a rule that returns True if the name is "Patsy" and the country is "gb":

testData
    & flag "flag"
    & ifMatch "name" [toJSON "Patsy"]
    & andMatch "country" [toJSON "gb"]
    & thenReturn True

andNotMatch Source #

Arguments

:: Text

the context attribute to match against

-> [Value]

values to compare to

-> FlagRuleBuilder 
-> FlagRuleBuilder 

Deprecated: Use andNotMatchContext instead

Adds another clause, using the "is not one of" operator.

This is a shortcut for calling andNotMatchContext with a context kind of "user".

For example, this creates a rule that returns True if the name is "Patsy" and the country is not "gb":

testData
    & flag "flag"
    & ifMatch "name" [toJSON "Patsy"]
    & andNotMatch "country" [toJSON "gb"]
    & thenReturn True

andNotMatchContext Source #

Arguments

:: Text

the context kind to match against

-> Text

the context attribute to match against

-> [Value]

values to compare to

-> FlagRuleBuilder 
-> FlagRuleBuilder 

Adds another clause, using the "is not one of" operator.

For example, this creates a rule that returns True if the name is "Patsy" and the country is not "gb":

testData
    & flag "flag"
    & ifMatchContext "user" "name" [toJSON "Patsy"]
    & andNotMatchContext "user" "country" [toJSON "gb"]
    & thenReturn True

thenReturn Source #

Arguments

:: Variation val 
=> val

True or False or the desired fallthrough variation index: 0 for the first, 1 for the second, etc.

-> FlagRuleBuilder 
-> FlagBuilder 

Finishes defining the rule, specifying the result as either a boolean or a variation index.

If the flag was previously configured with other variations and the variation specified is a boolean, this also changes it to a boolean flag.