module Ribosome.Host.Class.Msgpack.Map where
import Data.MessagePack (Object)
import Ribosome.Host.Class.Msgpack.Encode (MsgpackEncode (toMsgpack))
class MsgpackMapElem a where
msgpackMapElem :: a -> Map Text Object
instance {-# overlappable #-} (
MsgpackEncode a,
t ~ Text
) => MsgpackMapElem (t, a) where
msgpackMapElem :: (t, a) -> Map Text Object
msgpackMapElem (t
k, a
v) =
[(t
k, a -> Object
forall a. MsgpackEncode a => a -> Object
toMsgpack a
v)]
instance (
MsgpackEncode a,
t ~ Text
) => MsgpackMapElem (t, Maybe a) where
msgpackMapElem :: (t, Maybe a) -> Map Text Object
msgpackMapElem = \case
(t
k, Just a
v) ->
[(t
k, a -> Object
forall a. MsgpackEncode a => a -> Object
toMsgpack a
v)]
(t
_, Maybe a
Nothing) ->
Map Text Object
forall a. Monoid a => a
mempty
class MsgpackMap a where
msgpackMap :: a
instance MsgpackMap (Map Text Object -> Object) where
msgpackMap :: Map Text Object -> Object
msgpackMap =
Map Text Object -> Object
forall a. MsgpackEncode a => a -> Object
toMsgpack
instance MsgpackMap (a -> a) where
msgpackMap :: a -> a
msgpackMap =
a -> a
forall a. a -> a
id
instance (
MsgpackMapElem (t, a),
MsgpackMap (Map Text Object -> b)
) => MsgpackMap (Map Text Object -> (t, a) -> b) where
msgpackMap :: Map Text Object -> (t, a) -> b
msgpackMap Map Text Object
m (t, a)
a =
Map Text Object -> b
forall a. MsgpackMap a => a
msgpackMap (Map Text Object
m Map Text Object -> Map Text Object -> Map Text Object
forall a. Semigroup a => a -> a -> a
<> (t, a) -> Map Text Object
forall a. MsgpackMapElem a => a -> Map Text Object
msgpackMapElem (t, a)
a)
instance (
MsgpackMapElem (t, a),
MsgpackMap (Map Text Object -> b)
) => MsgpackMap ((t, a) -> b) where
msgpackMap :: (t, a) -> b
msgpackMap (t, a)
a =
Map Text Object -> b
forall a. MsgpackMap a => a
msgpackMap ((t, a) -> Map Text Object
forall a. MsgpackMapElem a => a -> Map Text Object
msgpackMapElem (t, a)
a)