module Ribosome.Host.Class.Msgpack.Util where

import Data.Map.Strict ((!?))
import qualified Data.Map.Strict as Map (fromList)
import Data.MessagePack (Object (..))
import Exon (exon)

string :: ConvertUtf8 a ByteString => a -> Object
string :: forall a. ConvertUtf8 a ByteString => a -> Object
string =
  ByteString -> Object
ObjectString (ByteString -> Object) -> (a -> ByteString) -> a -> Object
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> ByteString
forall a b. ConvertUtf8 a b => a -> b
encodeUtf8

binary :: ConvertUtf8 a ByteString => a -> Object
binary :: forall a. ConvertUtf8 a ByteString => a -> Object
binary =
  ByteString -> Object
ObjectBinary (ByteString -> Object) -> (a -> ByteString) -> a -> Object
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> ByteString
forall a b. ConvertUtf8 a b => a -> b
encodeUtf8

text :: Text -> Object
text :: Text -> Object
text =
  ByteString -> Object
ObjectString (ByteString -> Object) -> (Text -> ByteString) -> Text -> Object
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> ByteString
forall a b. ConvertUtf8 a b => a -> b
encodeUtf8

assembleMap :: [(String, Object)] -> Object
assembleMap :: [(String, Object)] -> Object
assembleMap =
  Map Object Object -> Object
ObjectMap (Map Object Object -> Object)
-> ([(String, Object)] -> Map Object Object)
-> [(String, Object)]
-> Object
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [(Object, Object)] -> Map Object Object
forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList ([(Object, Object)] -> Map Object Object)
-> ([(String, Object)] -> [(Object, Object)])
-> [(String, Object)]
-> Map Object Object
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (((String, Object) -> (Object, Object))
-> [(String, Object)] -> [(Object, Object)]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (((String, Object) -> (Object, Object))
 -> [(String, Object)] -> [(Object, Object)])
-> ((String -> Object) -> (String, Object) -> (Object, Object))
-> (String -> Object)
-> [(String, Object)]
-> [(Object, Object)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String -> Object) -> (String, Object) -> (Object, Object)
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first) String -> Object
forall a. ConvertUtf8 a ByteString => a -> Object
string

invalid :: Text -> Object -> Either Text a
invalid :: forall a. Text -> Object -> Either Text a
invalid Text
msg Object
obj =
  Text -> Either Text a
forall a b. a -> Either a b
Left [exon|#{msg}: #{show obj}|]

missingRecordKey :: String -> Object -> Either Text a
missingRecordKey :: forall a. String -> Object -> Either Text a
missingRecordKey String
key =
  Text -> Object -> Either Text a
forall a. Text -> Object -> Either Text a
invalid [exon|missing record key #{toText key} in ObjectMap|]

illegalType :: Text -> Object -> Either Text a
illegalType :: forall a. Text -> Object -> Either Text a
illegalType Text
tpe =
  Text -> Object -> Either Text a
forall a. Text -> Object -> Either Text a
invalid [exon|illegal type for #{tpe}|]

lookupObjectMap ::
  ConvertUtf8 a ByteString =>
  a ->
  Map Object Object ->
  Maybe Object
lookupObjectMap :: forall a.
ConvertUtf8 a ByteString =>
a -> Map Object Object -> Maybe Object
lookupObjectMap a
key Map Object Object
o =
  (Map Object Object
o Map Object Object -> Object -> Maybe Object
forall k a. Ord k => Map k a -> k -> Maybe a
!? a -> Object
forall a. ConvertUtf8 a ByteString => a -> Object
string a
key) Maybe Object -> Maybe Object -> Maybe Object
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (Map Object Object
o Map Object Object -> Object -> Maybe Object
forall k a. Ord k => Map k a -> k -> Maybe a
!? a -> Object
forall a. ConvertUtf8 a ByteString => a -> Object
binary a
key)