{-# LANGUAGE CPP #-}

module Data.OpenApi.Aeson.Compat where

#if MIN_VERSION_aeson(2,0,0)
import           Data.Aeson        (Key)
import qualified Data.Aeson.Key    as Key
import qualified Data.Aeson.KeyMap as KeyMap
#else
import qualified Data.HashMap.Strict as HM
#endif
import           Data.Bifunctor             (first)
import qualified Data.HashMap.Strict.InsOrd as InsOrdHashMap
import qualified Data.Text                  as T

#if MIN_VERSION_aeson(2,0,0)
deleteKey :: Key -> KeyMap.KeyMap v -> KeyMap.KeyMap v
deleteKey :: forall v. Key -> KeyMap v -> KeyMap v
deleteKey = Key -> KeyMap v -> KeyMap v
forall v. Key -> KeyMap v -> KeyMap v
KeyMap.delete

objectToList :: KeyMap.KeyMap v -> [(Key, v)]
objectToList :: forall v. KeyMap v -> [(Key, v)]
objectToList = KeyMap v -> [(Key, v)]
forall v. KeyMap v -> [(Key, v)]
KeyMap.toList

objectKeys :: KeyMap.KeyMap v -> [T.Text]
objectKeys :: forall v. KeyMap v -> [Text]
objectKeys = (Key -> Text) -> [Key] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map Key -> Text
Key.toText ([Key] -> [Text]) -> (KeyMap v -> [Key]) -> KeyMap v -> [Text]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. KeyMap v -> [Key]
forall v. KeyMap v -> [Key]
KeyMap.keys

stringToKey :: String -> Key
stringToKey :: String -> Key
stringToKey = String -> Key
Key.fromString

keyToString :: Key -> String
keyToString :: Key -> String
keyToString = Key -> String
Key.toString

keyToText :: Key -> T.Text
keyToText :: Key -> Text
keyToText = Key -> Text
Key.toText

toInsOrdHashMap :: KeyMap.KeyMap v -> InsOrdHashMap.InsOrdHashMap T.Text v
toInsOrdHashMap :: forall v. KeyMap v -> InsOrdHashMap Text v
toInsOrdHashMap = [(Text, v)] -> InsOrdHashMap Text v
forall k v. (Eq k, Hashable k) => [(k, v)] -> InsOrdHashMap k v
InsOrdHashMap.fromList ([(Text, v)] -> InsOrdHashMap Text v)
-> (KeyMap v -> [(Text, v)]) -> KeyMap v -> InsOrdHashMap Text v
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Key, v) -> (Text, v)) -> [(Key, v)] -> [(Text, v)]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((Key -> Text) -> (Key, v) -> (Text, v)
forall a b c. (a -> b) -> (a, c) -> (b, c)
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first Key -> Text
Key.toText) ([(Key, v)] -> [(Text, v)])
-> (KeyMap v -> [(Key, v)]) -> KeyMap v -> [(Text, v)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. KeyMap v -> [(Key, v)]
forall v. KeyMap v -> [(Key, v)]
KeyMap.toList

fromInsOrdHashMap :: InsOrdHashMap.InsOrdHashMap T.Text v -> KeyMap.KeyMap v
fromInsOrdHashMap :: forall v. InsOrdHashMap Text v -> KeyMap v
fromInsOrdHashMap = [(Key, v)] -> KeyMap v
forall v. [(Key, v)] -> KeyMap v
KeyMap.fromList ([(Key, v)] -> KeyMap v)
-> (InsOrdHashMap Text v -> [(Key, v)])
-> InsOrdHashMap Text v
-> KeyMap v
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Text, v) -> (Key, v)) -> [(Text, v)] -> [(Key, v)]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((Text -> Key) -> (Text, v) -> (Key, v)
forall a b c. (a -> b) -> (a, c) -> (b, c)
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first Text -> Key
Key.fromText) ([(Text, v)] -> [(Key, v)])
-> (InsOrdHashMap Text v -> [(Text, v)])
-> InsOrdHashMap Text v
-> [(Key, v)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. InsOrdHashMap Text v -> [(Text, v)]
forall k v. InsOrdHashMap k v -> [(k, v)]
InsOrdHashMap.toList

lookupKey :: T.Text -> KeyMap.KeyMap v -> Maybe v
lookupKey :: forall v. Text -> KeyMap v -> Maybe v
lookupKey = Key -> KeyMap v -> Maybe v
forall v. Key -> KeyMap v -> Maybe v
KeyMap.lookup (Key -> KeyMap v -> Maybe v)
-> (Text -> Key) -> Text -> KeyMap v -> Maybe v
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Key
Key.fromText

hasKey :: T.Text -> KeyMap.KeyMap a -> Bool
hasKey :: forall a. Text -> KeyMap a -> Bool
hasKey = Key -> KeyMap a -> Bool
forall a. Key -> KeyMap a -> Bool
KeyMap.member (Key -> KeyMap a -> Bool)
-> (Text -> Key) -> Text -> KeyMap a -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Key
Key.fromText
#else
deleteKey :: T.Text -> HM.HashMap T.Text v -> HM.HashMap T.Text v
deleteKey = HM.delete

objectToList :: HM.HashMap T.Text v -> [(T.Text, v)]
objectToList = HM.toList

objectKeys :: HM.HashMap T.Text v -> [T.Text]
objectKeys = HM.keys

stringToKey :: String -> T.Text
stringToKey = T.pack

keyToString :: T.Text -> String
keyToString = T.unpack

keyToText :: T.Text -> T.Text
keyToText = id

toInsOrdHashMap :: HM.HashMap T.Text v -> InsOrdHashMap.InsOrdHashMap T.Text v
toInsOrdHashMap = InsOrdHashMap.fromHashMap

fromInsOrdHashMap :: InsOrdHashMap.InsOrdHashMap T.Text v -> HM.HashMap T.Text v
fromInsOrdHashMap = InsOrdHashMap.toHashMap

lookupKey :: T.Text -> HM.HashMap T.Text v -> Maybe v
lookupKey = HM.lookup

hasKey :: T.Text -> HM.HashMap T.Text a -> Bool
hasKey = HM.member
#endif