{-# 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 = forall v. Key -> KeyMap v -> KeyMap v
KeyMap.delete

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

objectKeys :: KeyMap.KeyMap v -> [T.Text]
objectKeys :: forall v. KeyMap v -> [Text]
objectKeys = forall a b. (a -> b) -> [a] -> [b]
map Key -> Text
Key.toText forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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 = forall k v. (Eq k, Hashable k) => [(k, v)] -> InsOrdHashMap k v
InsOrdHashMap.fromList forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first Key -> Text
Key.toText) forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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 = forall v. [(Key, v)] -> KeyMap v
KeyMap.fromList forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first Text -> Key
Key.fromText) forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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 = forall v. Key -> KeyMap v -> Maybe v
KeyMap.lookup 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 = forall a. Key -> KeyMap a -> Bool
KeyMap.member 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