{-# LANGUAGE BlockArguments #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE ViewPatterns #-}
module LaunchDarkly.Server.Context
( Context
, makeContext
, makeMultiContext
, withName
, withAnonymous
, withAttribute
, withPrivateAttributes
, isValid
, getError
, getIndividualContext
, getValueForReference
, getValue
)
where
import Data.Aeson (Value (..))
import Data.Maybe (fromMaybe)
import Data.Text (Text)
import LaunchDarkly.AesonCompat (lookupKey)
import LaunchDarkly.Server.Context.Internal (Context (..), MultiContext (..), SingleContext (..), makeContext, makeMultiContext, withAnonymous, withAttribute, withName, withPrivateAttributes)
import LaunchDarkly.Server.Reference (Reference)
import qualified LaunchDarkly.Server.Reference as R
isValid :: Context -> Bool
isValid :: Context -> Bool
isValid (Invalid Text
_) = Bool
False
isValid Context
_ = Bool
True
getError :: Context -> Text
getError :: Context -> Text
getError (Invalid Text
e) = Text
e
getError Context
_ = Text
""
getIndividualContext :: Text -> Context -> Maybe Context
getIndividualContext :: Text -> Context -> Maybe Context
getIndividualContext Text
kind (Multi (MultiContext {KeyMap SingleContext
contexts :: KeyMap SingleContext
$sel:contexts:MultiContext :: MultiContext -> KeyMap SingleContext
contexts})) = SingleContext -> Context
Single (SingleContext -> Context) -> Maybe SingleContext -> Maybe Context
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> KeyMap SingleContext -> Maybe SingleContext
forall v. Text -> KeyMap v -> Maybe v
lookupKey Text
kind KeyMap SingleContext
contexts
getIndividualContext Text
kind c :: Context
c@(Single (SingleContext {kind :: SingleContext -> Text
kind = Text
k}))
| Text
kind Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
k = Context -> Maybe Context
forall a. a -> Maybe a
Just Context
c
| Bool
otherwise = Maybe Context
forall a. Maybe a
Nothing
getIndividualContext Text
_ Context
_ = Maybe Context
forall a. Maybe a
Nothing
getValue :: Text -> Context -> Value
getValue :: Text -> Context -> Value
getValue Text
ref = Reference -> Context -> Value
getValueForReference (Text -> Reference
R.makeLiteral Text
ref)
getValueForReference :: Reference -> Context -> Value
getValueForReference :: Reference -> Context -> Value
getValueForReference (Reference -> Bool
R.isValid -> Bool
False) Context
_ = Value
Null
getValueForReference Reference
reference Context
context = case Reference -> [Text]
R.getComponents Reference
reference of
[] -> Value
Null
(Text
component : [Text]
components) ->
let value :: Value
value = Text -> Context -> Value
getTopLevelValue Text
component Context
context
in (Value -> Text -> Value) -> Value -> [Text] -> Value
forall b a. (b -> a -> b) -> b -> [a] -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl Value -> Text -> Value
getValueFromJsonObject Value
value [Text]
components
getValueFromJsonObject :: Value -> Text -> Value
getValueFromJsonObject :: Value -> Text -> Value
getValueFromJsonObject (Object Object
nm) Text
component = Value -> Maybe Value -> Value
forall a. a -> Maybe a -> a
fromMaybe Value
Null (Text -> Object -> Maybe Value
forall v. Text -> KeyMap v -> Maybe v
lookupKey Text
component Object
nm)
getValueFromJsonObject Value
_ Text
_ = Value
Null
getTopLevelValue :: Text -> Context -> Value
getTopLevelValue :: Text -> Context -> Value
getTopLevelValue Text
_ (Invalid Text
_) = Value
Null
getTopLevelValue Text
"kind" (Multi MultiContext
_) = Value
"multi"
getTopLevelValue Text
_ (Multi MultiContext
_) = Value
Null
getTopLevelValue Text
"key" (Single SingleContext {Text
key :: Text
$sel:key:SingleContext :: SingleContext -> Text
key}) = Text -> Value
String Text
key
getTopLevelValue Text
"kind" (Single SingleContext {Text
kind :: SingleContext -> Text
kind :: Text
kind}) = Text -> Value
String Text
kind
getTopLevelValue Text
"name" (Single SingleContext {$sel:name:SingleContext :: SingleContext -> Maybe Text
name = Maybe Text
Nothing}) = Value
Null
getTopLevelValue Text
"name" (Single SingleContext {$sel:name:SingleContext :: SingleContext -> Maybe Text
name = Just Text
n}) = Text -> Value
String Text
n
getTopLevelValue Text
"anonymous" (Single SingleContext {Bool
anonymous :: Bool
$sel:anonymous:SingleContext :: SingleContext -> Bool
anonymous}) = Bool -> Value
Bool Bool
anonymous
getTopLevelValue Text
_ (Single SingleContext {$sel:attributes:SingleContext :: SingleContext -> Maybe Object
attributes = Maybe Object
Nothing}) = Value
Null
getTopLevelValue Text
key (Single SingleContext {$sel:attributes:SingleContext :: SingleContext -> Maybe Object
attributes = Just Object
attrs}) = Value -> Maybe Value -> Value
forall a. a -> Maybe a -> a
fromMaybe Value
Null (Maybe Value -> Value) -> Maybe Value -> Value
forall a b. (a -> b) -> a -> b
$ Text -> Object -> Maybe Value
forall v. Text -> KeyMap v -> Maybe v
lookupKey Text
key Object
attrs