Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
Context is a collection of attributes that can be referenced in flag evaluations and analytics events.
To create a Context of a single kind, such as a user, you may use
makeContext
.
To create an LDContext with multiple kinds, use makeMultiContext
.
Additional properties can be set on a single-kind context using the set methods found in this module.
Each method will always return a Context. However, that Context may be
invalid. You can check the validity of the resulting context, and the
associated errors by calling isValid
and getError
.
Synopsis
- data Context
- makeContext :: Text -> Text -> Context
- makeMultiContext :: [Context] -> Context
- withName :: Text -> Context -> Context
- withAnonymous :: Bool -> Context -> Context
- withAttribute :: Text -> Value -> Context -> Context
- withPrivateAttributes :: Set Reference -> Context -> Context
- isValid :: Context -> Bool
- getError :: Context -> Text
- getIndividualContext :: Text -> Context -> Maybe Context
- getValueForReference :: Reference -> Context -> Value
- getValue :: Text -> Context -> Value
Documentation
data record for the Context type
makeContext :: Text -> Text -> Context Source #
Create a single kind context from the provided hash.
The provided hash must match the format as outlined in the SDK documentation.
makeMultiContext :: [Context] -> Context Source #
Create a multi-kind context from the list of Contexts provided.
A multi-kind context is comprised of two or more single kind contexts. You cannot include a multi-kind context instead another multi-kind context.
Additionally, the kind of each single-kind context must be unique. For instance, you cannot create a multi-kind context that includes two user kind contexts.
If you attempt to create a multi-kind context from one single-kind context, this method will return the single-kind context instead of a new multi-kind context wrapping that one single-kind.
withName :: Text -> Context -> Context Source #
Sets the name attribute for a single-kind context.
Calling this method on an invalid or multi-kind context is a no-op.
withAnonymous :: Bool -> Context -> Context Source #
Sets the anonymous attribute for a single-kind context.
Calling this method on an invalid or multi-kind context is a no-op.
withAttribute :: Text -> Value -> Context -> Context Source #
Sets the value of any attribute for the context.
This includes only attributes that are addressable in evaluations -- not
metadata such as private attributes. For example, if the attribute name is
"privateAttributes", you will be setting an attribute with that name which
you can use in evaluations or to record data for your own purposes, but it
will be unrelated to withPrivateAttributes
.
If attribute name is "privateAttributeNames", it is ignored and no attribute is set.
This method uses the Value type to represent a value of any JSON type: null, boolean, number, string, array, or object. For all attribute names that do not have special meaning to LaunchDarkly, you may use any of those types. Values of different JSON types are always treated as different values: for instance, null, false, and the empty string "" are not the same, and the number 1 is not the same as the string "1".
The following attribute names have special restrictions on their value types, and any value of an unsupported type will be ignored (leaving the attribute unchanged):
- "name": Must be a string.
- "anonymous": Must be a boolean.
The attribute name "_meta" is not allowed, because it has special meaning in the JSON schema for contexts; any attempt to set an attribute with this name has no effect.
The attribute names "kind" and "key" are not allowed. They must be provided
during the initial context creation. See makeContext
.
Values that are JSON arrays or objects have special behavior when referenced in flag/segment rules.
For attributes that aren't subject to the special restrictions mentioned above, a value of Null is equivalent to removing any current non-default value of the attribute. Null is not a valid attribute value in the LaunchDarkly model; any expressions in feature flags that reference an attribute with a null value will behave as if the attribute did not exist.
Calling this method on an invalid or multi-kind context is a no-op.
withPrivateAttributes :: Set Reference -> Context -> Context Source #
Sets the private attributes for a single-kind context.
Calling this method on an invalid or multi-kind context is a no-op.
getIndividualContext :: Text -> Context -> Maybe Context Source #
Returns the single-kind Context corresponding to one of the kinds in this context.
If this method is called on a single-kind Context and the requested kind matches the context's kind, then that context is returned.
If the method is called on a multi-context, the provided kind must match the context kind of one of the individual contexts.
If there is no context corresponding to $sel:kind:SingleContext
, the method returns Nothing.
getValueForReference :: Reference -> Context -> Value Source #
Looks up the value of any attribute of the Context, or a value contained
within an attribute, based on a Reference
instance. This includes only
attributes that are addressable in evaluations-- not metadata such as
private attributes.
This implements the same behavior that the SDK uses to resolve attribute
references during a flag evaluation. In a single-kind context, the
Reference
can represent a simple attribute name-- either a built-in one
like "name" or "key", or a custom attribute -- or, it can be a
slash-delimited path using a JSON-Pointer-like syntax. See Reference
for
more details.
For a multi-kind context, the only supported attribute name is "kind". Use
getIndividualContext
to inspect a Context for a particular kind and then
get its attributes.
If the value is found, the return value is the attribute value; otherwise, it is Null.
getValue :: Text -> Context -> Value Source #
Looks up the value of any attribute of the Context by name. This includes only attributes that are addressable in evaluations-- not metadata such as private attributes.
For a single-kind context, the attribute name can be any custom attribute. It can also be one of the built-in ones like "kind", "key", or "name".
For a multi-kind context, the only supported attribute name is "kind". Use
getIndividualContext
to inspect a Context for a particular kind and then
get its attributes.
This method does not support complex expressions for getting individual
values out of JSON objects or arrays, such as "addressstreet". Use
getValueForReference
for that purpose.
If the value is found, the return value is the attribute value; otherwise, it is Null.