{-# 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 _) = Bool
False
isValid _ = Bool
True
getError :: Context -> Text
getError :: Context -> Text
getError (Invalid e :: Text
e) = Text
e
getError _ = ""
getIndividualContext :: Text -> Context -> Maybe Context
getIndividualContext :: Text -> Context -> Maybe Context
getIndividualContext kind :: Text
kind (Multi (MultiContext {KeyMap SingleContext
$sel:contexts:MultiContext :: MultiContext -> KeyMap SingleContext
contexts :: 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 -> HashMap Text v -> Maybe v
lookupKey Text
kind KeyMap SingleContext
contexts
getIndividualContext kind :: Text
kind c :: Context
c@(Single (SingleContext {$sel:kind:SingleContext :: 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 _ _ = Maybe Context
forall a. Maybe a
Nothing
getValue :: Text -> Context -> Value
getValue :: Text -> Context -> Value
getValue ref :: 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) _ = Value
Null
getValueForReference reference :: Reference
reference context :: Context
context = case Reference -> [Text]
R.getComponents Reference
reference of
[] -> Value
Null
(component :: Text
component : components :: [Text]
components) ->
let value :: Value
value = Text -> Context -> Value
getTopLevelValue Text
component Context
context
in (Value -> Text -> Value) -> Value -> [Text] -> Value
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 nm :: Object
nm) component :: Text
component = Value -> Maybe Value -> Value
forall a. a -> Maybe a -> a
fromMaybe Value
Null (Text -> Object -> Maybe Value
forall v. Text -> HashMap Text v -> Maybe v
lookupKey Text
component Object
nm)
getValueFromJsonObject _ _ = Value
Null
getTopLevelValue :: Text -> Context -> Value
getTopLevelValue :: Text -> Context -> Value
getTopLevelValue _ (Invalid _) = Value
Null
getTopLevelValue "kind" (Multi _) = "multi"
getTopLevelValue _ (Multi _) = Value
Null
getTopLevelValue "key" (Single SingleContext {Text
$sel:key:SingleContext :: SingleContext -> Text
key :: Text
key}) = Text -> Value
String Text
key
getTopLevelValue "kind" (Single SingleContext {Text
kind :: Text
$sel:kind:SingleContext :: SingleContext -> Text
kind}) = Text -> Value
String Text
kind
getTopLevelValue "name" (Single SingleContext {$sel:name:SingleContext :: SingleContext -> Maybe Text
name = Maybe Text
Nothing}) = Value
Null
getTopLevelValue "name" (Single SingleContext {$sel:name:SingleContext :: SingleContext -> Maybe Text
name = Just n :: Text
n}) = Text -> Value
String Text
n
getTopLevelValue "anonymous" (Single SingleContext {Bool
$sel:anonymous:SingleContext :: SingleContext -> Bool
anonymous :: Bool
anonymous}) = Bool -> Value
Bool Bool
anonymous
getTopLevelValue _ (Single SingleContext {$sel:attributes:SingleContext :: SingleContext -> Maybe Object
attributes = Maybe Object
Nothing}) = Value
Null
getTopLevelValue key :: Text
key (Single SingleContext {$sel:attributes:SingleContext :: SingleContext -> Maybe Object
attributes = Just attrs :: 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 -> HashMap Text v -> Maybe v
lookupKey Text
key Object
attrs