{-# LANGUAGE CPP #-}
module Composite.Aeson.Record
( ToJsonField(..), FromJsonField(..), JsonField(..)
, field, field', fromField, fromField', toField, toField'
, optionalField, optionalField', fromOptionalField, fromOptionalField', toOptionalField, toOptionalField'
, JsonFormatRecord, ToJsonFormatRecord, FromJsonFormatRecord, zipJsonFormatRecord, toJsonFormatRecord, fromJsonFormatRecord
, DefaultJsonFormatRecord, defaultJsonFormatRecord
, RecordToJsonObject, recordToJsonObject, recordToJson
, RecordFromJson, recordFromJson
, recordJsonFormat
) where
import Composite.Aeson.Base
( JsonProfunctor(JsonProfunctor)
, JsonFormat(JsonFormat)
, wrappedJsonFormat
)
import Composite.Aeson.Formats.Default (DefaultJsonFormat(defaultJsonFormat))
import Composite.Record ((:->))
import Control.Lens (Wrapped(type Unwrapped, _Wrapped'), from, review, view)
import Control.Monad (join)
import qualified Data.Aeson as Aeson
import qualified Data.Aeson.BetterErrors as ABE
#if MIN_VERSION_aeson(2,0,0)
import qualified Data.Aeson.Key as Aeson.Key
import qualified Data.Aeson.KeyMap as Aeson.KeyMap
#else
import qualified Data.HashMap.Strict as HM
#endif
import Data.Functor.Contravariant (Contravariant, contramap)
import Data.Functor.Identity (Identity(Identity))
import Data.Proxy (Proxy(Proxy))
import Data.Text (Text, pack)
import Data.Vinyl (RApply, RMap, Rec((:&), RNil), rmap, rzipWith)
import GHC.TypeLits (KnownSymbol, symbolVal)
newtype ToJsonField a = ToJsonField { ToJsonField a -> a -> Maybe Value
unToJsonField :: a -> Maybe Aeson.Value }
instance Contravariant ToJsonField where
contramap :: (a -> b) -> ToJsonField b -> ToJsonField a
contramap a -> b
f (ToJsonField b -> Maybe Value
g) = (a -> Maybe Value) -> ToJsonField a
forall a. (a -> Maybe Value) -> ToJsonField a
ToJsonField (b -> Maybe Value
g (b -> Maybe Value) -> (a -> b) -> a -> Maybe Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b
f)
newtype FromJsonField e a = FromJsonField { FromJsonField e a -> Text -> Parse e a
unFromJsonField :: Text -> ABE.Parse e a }
instance Functor (FromJsonField e) where
fmap :: (a -> b) -> FromJsonField e a -> FromJsonField e b
fmap a -> b
f (FromJsonField Text -> Parse e a
g) = (Text -> Parse e b) -> FromJsonField e b
forall e a. (Text -> Parse e a) -> FromJsonField e a
FromJsonField ((a -> b) -> Parse e a -> Parse e b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f (Parse e a -> Parse e b)
-> (Text -> Parse e a) -> Text -> Parse e b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Parse e a
g)
data JsonField e a = JsonField (a -> Maybe Aeson.Value) (Text -> ABE.Parse e a)
field :: (Wrapped a', Unwrapped a' ~ a) => JsonFormat e a -> JsonField e a'
field :: JsonFormat e a -> JsonField e a'
field JsonFormat e a
fmt = JsonFormat e a' -> JsonField e a'
forall e a. JsonFormat e a -> JsonField e a
field' (JsonFormat e (Unwrapped a') -> JsonFormat e a'
forall a e.
Wrapped a =>
JsonFormat e (Unwrapped a) -> JsonFormat e a
wrappedJsonFormat JsonFormat e a
JsonFormat e (Unwrapped a')
fmt)
field' :: JsonFormat e a -> JsonField e a
field' :: JsonFormat e a -> JsonField e a
field' (JsonFormat (JsonProfunctor a -> Value
o Parse e a
i)) = (a -> Maybe Value) -> (Text -> Parse e a) -> JsonField e a
forall e a.
(a -> Maybe Value) -> (Text -> Parse e a) -> JsonField e a
JsonField (Value -> Maybe Value
forall a. a -> Maybe a
Just (Value -> Maybe Value) -> (a -> Value) -> a -> Maybe Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Value
o) (Text -> Parse e a -> Parse e a
forall (m :: * -> *) err a.
(Functor m, Monad m) =>
Text -> ParseT err m a -> ParseT err m a
`ABE.key` Parse e a
i)
fromField :: Wrapped a => ABE.Parse e (Unwrapped a) -> FromJsonField e a
fromField :: Parse e (Unwrapped a) -> FromJsonField e a
fromField = (Text -> Parse e a) -> FromJsonField e a
forall e a. (Text -> Parse e a) -> FromJsonField e a
FromJsonField ((Text -> Parse e a) -> FromJsonField e a)
-> (Parse e (Unwrapped a) -> Text -> Parse e a)
-> Parse e (Unwrapped a)
-> FromJsonField e a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Text -> Parse e a -> Parse e a) -> Parse e a -> Text -> Parse e a
forall a b c. (a -> b -> c) -> b -> a -> c
flip Text -> Parse e a -> Parse e a
forall (m :: * -> *) err a.
(Functor m, Monad m) =>
Text -> ParseT err m a -> ParseT err m a
ABE.key (Parse e a -> Text -> Parse e a)
-> (Parse e (Unwrapped a) -> Parse e a)
-> Parse e (Unwrapped a)
-> Text
-> Parse e a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Unwrapped a -> a) -> Parse e (Unwrapped a) -> Parse e a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (AReview a (Unwrapped a) -> Unwrapped a -> a
forall b (m :: * -> *) t. MonadReader b m => AReview t b -> m t
review AReview a (Unwrapped a)
forall s. Wrapped s => Iso' s (Unwrapped s)
_Wrapped')
fromField' :: ABE.Parse e a -> FromJsonField e a
fromField' :: Parse e a -> FromJsonField e a
fromField' = (Text -> Parse e a) -> FromJsonField e a
forall e a. (Text -> Parse e a) -> FromJsonField e a
FromJsonField ((Text -> Parse e a) -> FromJsonField e a)
-> (Parse e a -> Text -> Parse e a)
-> Parse e a
-> FromJsonField e a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Text -> Parse e a -> Parse e a) -> Parse e a -> Text -> Parse e a
forall a b c. (a -> b -> c) -> b -> a -> c
flip Text -> Parse e a -> Parse e a
forall (m :: * -> *) err a.
(Functor m, Monad m) =>
Text -> ParseT err m a -> ParseT err m a
ABE.key
toField :: (Wrapped a', Unwrapped a' ~ a) => (a -> Aeson.Value) -> ToJsonField a'
toField :: (a -> Value) -> ToJsonField a'
toField a -> Value
o = (a' -> Maybe Value) -> ToJsonField a'
forall a. (a -> Maybe Value) -> ToJsonField a
ToJsonField ((a' -> Maybe Value) -> ToJsonField a')
-> (a' -> Maybe Value) -> ToJsonField a'
forall a b. (a -> b) -> a -> b
$ Value -> Maybe Value
forall a. a -> Maybe a
Just (Value -> Maybe Value) -> (a' -> Value) -> a' -> Maybe Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Value
o (a -> Value) -> (a' -> a) -> a' -> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Getting a a' a -> a' -> a
forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view Getting a a' a
forall s. Wrapped s => Iso' s (Unwrapped s)
_Wrapped'
toField' :: (a -> Aeson.Value) -> ToJsonField a
toField' :: (a -> Value) -> ToJsonField a
toField' = (a -> Maybe Value) -> ToJsonField a
forall a. (a -> Maybe Value) -> ToJsonField a
ToJsonField ((a -> Maybe Value) -> ToJsonField a)
-> ((a -> Value) -> a -> Maybe Value)
-> (a -> Value)
-> ToJsonField a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Value -> Maybe Value) -> (a -> Value) -> a -> Maybe Value
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Value -> Maybe Value
forall a. a -> Maybe a
Just
optionalField :: (Wrapped a', Unwrapped a' ~ Maybe a) => JsonFormat e a -> JsonField e a'
optionalField :: JsonFormat e a -> JsonField e a'
optionalField (JsonFormat (JsonProfunctor a -> Value
o Parse e a
i)) =
(a' -> Maybe Value) -> (Text -> Parse e a') -> JsonField e a'
forall e a.
(a -> Maybe Value) -> (Text -> Parse e a) -> JsonField e a
JsonField
((a -> Value) -> Maybe a -> Maybe Value
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> Value
o (Maybe a -> Maybe Value) -> (a' -> Maybe a) -> a' -> Maybe Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Getting (Maybe a) a' (Maybe a) -> a' -> Maybe a
forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view Getting (Maybe a) a' (Maybe a)
forall s. Wrapped s => Iso' s (Unwrapped s)
_Wrapped')
(\ Text
k -> Getting a' (Maybe a) a' -> Maybe a -> a'
forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view (AnIso a' a' (Maybe a) (Maybe a) -> Iso (Maybe a) (Maybe a) a' a'
forall s t a b. AnIso s t a b -> Iso b a t s
from AnIso a' a' (Maybe a) (Maybe a)
forall s. Wrapped s => Iso' s (Unwrapped s)
_Wrapped') (Maybe a -> a')
-> (Maybe (Maybe a) -> Maybe a) -> Maybe (Maybe a) -> a'
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Maybe (Maybe a) -> Maybe a
forall (m :: * -> *) a. Monad m => m (m a) -> m a
join (Maybe (Maybe a) -> a')
-> ParseT e Identity (Maybe (Maybe a)) -> Parse e a'
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text
-> ParseT e Identity (Maybe a)
-> ParseT e Identity (Maybe (Maybe a))
forall (m :: * -> *) err a.
(Functor m, Monad m) =>
Text -> ParseT err m a -> ParseT err m (Maybe a)
ABE.keyMay Text
k (Parse e a -> ParseT e Identity (Maybe a)
forall (m :: * -> *) err a.
(Functor m, Monad m) =>
ParseT err m a -> ParseT err m (Maybe a)
ABE.perhaps Parse e a
i))
optionalField' :: JsonFormat e a -> JsonField e (Maybe a)
optionalField' :: JsonFormat e a -> JsonField e (Maybe a)
optionalField' (JsonFormat (JsonProfunctor a -> Value
o Parse e a
i)) =
(Maybe a -> Maybe Value)
-> (Text -> Parse e (Maybe a)) -> JsonField e (Maybe a)
forall e a.
(a -> Maybe Value) -> (Text -> Parse e a) -> JsonField e a
JsonField
((a -> Value) -> Maybe a -> Maybe Value
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> Value
o)
(\ Text
k -> Maybe (Maybe a) -> Maybe a
forall (m :: * -> *) a. Monad m => m (m a) -> m a
join (Maybe (Maybe a) -> Maybe a)
-> ParseT e Identity (Maybe (Maybe a)) -> Parse e (Maybe a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> Parse e (Maybe a) -> ParseT e Identity (Maybe (Maybe a))
forall (m :: * -> *) err a.
(Functor m, Monad m) =>
Text -> ParseT err m a -> ParseT err m (Maybe a)
ABE.keyMay Text
k (Parse e a -> Parse e (Maybe a)
forall (m :: * -> *) err a.
(Functor m, Monad m) =>
ParseT err m a -> ParseT err m (Maybe a)
ABE.perhaps Parse e a
i))
fromOptionalField :: (Wrapped a', Unwrapped a' ~ Maybe a) => ABE.Parse e a -> FromJsonField e a'
fromOptionalField :: Parse e a -> FromJsonField e a'
fromOptionalField Parse e a
i = (Text -> Parse e a') -> FromJsonField e a'
forall e a. (Text -> Parse e a) -> FromJsonField e a
FromJsonField Text -> Parse e a'
f
where
f :: Text -> Parse e a'
f Text
k = Getting a' (Maybe a) a' -> Maybe a -> a'
forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view (AnIso a' a' (Maybe a) (Maybe a) -> Iso (Maybe a) (Maybe a) a' a'
forall s t a b. AnIso s t a b -> Iso b a t s
from AnIso a' a' (Maybe a) (Maybe a)
forall s. Wrapped s => Iso' s (Unwrapped s)
_Wrapped') (Maybe a -> a')
-> (Maybe (Maybe a) -> Maybe a) -> Maybe (Maybe a) -> a'
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Maybe (Maybe a) -> Maybe a
forall (m :: * -> *) a. Monad m => m (m a) -> m a
join (Maybe (Maybe a) -> a')
-> ParseT e Identity (Maybe (Maybe a)) -> Parse e a'
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text
-> ParseT e Identity (Maybe a)
-> ParseT e Identity (Maybe (Maybe a))
forall (m :: * -> *) err a.
(Functor m, Monad m) =>
Text -> ParseT err m a -> ParseT err m (Maybe a)
ABE.keyMay Text
k (Parse e a -> ParseT e Identity (Maybe a)
forall (m :: * -> *) err a.
(Functor m, Monad m) =>
ParseT err m a -> ParseT err m (Maybe a)
ABE.perhaps Parse e a
i)
fromOptionalField' :: ABE.Parse e a -> FromJsonField e (Maybe a)
fromOptionalField' :: Parse e a -> FromJsonField e (Maybe a)
fromOptionalField' Parse e a
i = (Text -> Parse e (Maybe a)) -> FromJsonField e (Maybe a)
forall e a. (Text -> Parse e a) -> FromJsonField e a
FromJsonField Text -> Parse e (Maybe a)
f
where
f :: Text -> Parse e (Maybe a)
f Text
k = Maybe (Maybe a) -> Maybe a
forall (m :: * -> *) a. Monad m => m (m a) -> m a
join (Maybe (Maybe a) -> Maybe a)
-> ParseT e Identity (Maybe (Maybe a)) -> Parse e (Maybe a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> Parse e (Maybe a) -> ParseT e Identity (Maybe (Maybe a))
forall (m :: * -> *) err a.
(Functor m, Monad m) =>
Text -> ParseT err m a -> ParseT err m (Maybe a)
ABE.keyMay Text
k (Parse e a -> Parse e (Maybe a)
forall (m :: * -> *) err a.
(Functor m, Monad m) =>
ParseT err m a -> ParseT err m (Maybe a)
ABE.perhaps Parse e a
i)
toOptionalField :: (Wrapped a', Unwrapped a' ~ Maybe a) => (a -> Aeson.Value) -> ToJsonField a'
toOptionalField :: (a -> Value) -> ToJsonField a'
toOptionalField a -> Value
o = (a' -> Maybe Value) -> ToJsonField a'
forall a. (a -> Maybe Value) -> ToJsonField a
ToJsonField ((a -> Value) -> Maybe a -> Maybe Value
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> Value
o (Maybe a -> Maybe Value) -> (a' -> Maybe a) -> a' -> Maybe Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Getting (Maybe a) a' (Maybe a) -> a' -> Maybe a
forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view Getting (Maybe a) a' (Maybe a)
forall s. Wrapped s => Iso' s (Unwrapped s)
_Wrapped')
toOptionalField' :: (a -> Aeson.Value) -> ToJsonField (Maybe a)
toOptionalField' :: (a -> Value) -> ToJsonField (Maybe a)
toOptionalField' a -> Value
o = (Maybe a -> Maybe Value) -> ToJsonField (Maybe a)
forall a. (a -> Maybe Value) -> ToJsonField a
ToJsonField ((a -> Value) -> Maybe a -> Maybe Value
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> Value
o)
type JsonFormatRecord e rs = Rec (JsonField e) rs
zipJsonFormatRecord :: (RMap rs, RApply rs) => ToJsonFormatRecord rs -> FromJsonFormatRecord e rs -> JsonFormatRecord e rs
zipJsonFormatRecord :: ToJsonFormatRecord rs
-> FromJsonFormatRecord e rs -> JsonFormatRecord e rs
zipJsonFormatRecord = (forall x. ToJsonField x -> FromJsonField e x -> JsonField e x)
-> ToJsonFormatRecord rs
-> FromJsonFormatRecord e rs
-> JsonFormatRecord e rs
forall u (xs :: [u]) (f :: u -> *) (g :: u -> *) (h :: u -> *).
(RMap xs, RApply xs) =>
(forall (x :: u). f x -> g x -> h x)
-> Rec f xs -> Rec g xs -> Rec h xs
rzipWith (\ (ToJsonField o) (FromJsonField i) -> (x -> Maybe Value) -> (Text -> Parse e x) -> JsonField e x
forall e a.
(a -> Maybe Value) -> (Text -> Parse e a) -> JsonField e a
JsonField x -> Maybe Value
o Text -> Parse e x
i)
type FromJsonFormatRecord e rs = Rec (FromJsonField e) rs
fromJsonFormatRecord :: RMap rs => JsonFormatRecord e rs -> FromJsonFormatRecord e rs
fromJsonFormatRecord :: JsonFormatRecord e rs -> FromJsonFormatRecord e rs
fromJsonFormatRecord = (forall x. JsonField e x -> FromJsonField e x)
-> JsonFormatRecord e rs -> FromJsonFormatRecord e rs
forall u (rs :: [u]) (f :: u -> *) (g :: u -> *).
RMap rs =>
(forall (x :: u). f x -> g x) -> Rec f rs -> Rec g rs
rmap (\ (JsonField _ i) -> (Text -> Parse e x) -> FromJsonField e x
forall e a. (Text -> Parse e a) -> FromJsonField e a
FromJsonField Text -> Parse e x
i)
type ToJsonFormatRecord rs = Rec ToJsonField rs
toJsonFormatRecord :: RMap rs => JsonFormatRecord e rs -> ToJsonFormatRecord rs
toJsonFormatRecord :: JsonFormatRecord e rs -> ToJsonFormatRecord rs
toJsonFormatRecord = (forall x. JsonField e x -> ToJsonField x)
-> JsonFormatRecord e rs -> ToJsonFormatRecord rs
forall u (rs :: [u]) (f :: u -> *) (g :: u -> *).
RMap rs =>
(forall (x :: u). f x -> g x) -> Rec f rs -> Rec g rs
rmap (\ (JsonField o _) -> (x -> Maybe Value) -> ToJsonField x
forall a. (a -> Maybe Value) -> ToJsonField a
ToJsonField x -> Maybe Value
o)
class RecordToJsonObject rs where
recordToJsonObject :: Rec ToJsonField rs -> Rec Identity rs -> Aeson.Object
instance RecordToJsonObject '[] where
recordToJsonObject :: Rec ToJsonField '[] -> Rec Identity '[] -> Object
recordToJsonObject Rec ToJsonField '[]
_ = Object -> Rec Identity '[] -> Object
forall a b. a -> b -> a
const Object
forall a. Monoid a => a
mempty
instance forall s a rs. (KnownSymbol s, RecordToJsonObject rs) => RecordToJsonObject (s :-> a ': rs) where
recordToJsonObject :: Rec ToJsonField ((s :-> a) : rs)
-> Rec Identity ((s :-> a) : rs) -> Object
recordToJsonObject (ToJsonField r -> Maybe Value
aToField :& Rec ToJsonField rs
fs) (Identity r
a :& Rec Identity rs
as) =
#if MIN_VERSION_aeson(2,0,0)
(Object -> Object)
-> (Value -> Object -> Object) -> Maybe Value -> Object -> Object
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Object -> Object
forall a. a -> a
id (Key -> Value -> Object -> Object
forall v. Key -> v -> KeyMap v -> KeyMap v
Aeson.KeyMap.insert (String -> Key
Aeson.Key.fromString (String -> Key) -> (Proxy s -> String) -> Proxy s -> Key
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Proxy s -> String
forall (n :: Symbol) (proxy :: Symbol -> *).
KnownSymbol n =>
proxy n -> String
symbolVal (Proxy s -> Key) -> Proxy s -> Key
forall a b. (a -> b) -> a -> b
$ (Proxy s
forall k (t :: k). Proxy t
Proxy :: Proxy s))) (r -> Maybe Value
aToField r
r
a) (Object -> Object) -> Object -> Object
forall a b. (a -> b) -> a -> b
$
#else
maybe id (HM.insert (pack . symbolVal $ (Proxy :: Proxy s))) (aToField a) $
#endif
Rec ToJsonField rs -> Rec Identity rs -> Object
forall (rs :: [*]).
RecordToJsonObject rs =>
Rec ToJsonField rs -> Rec Identity rs -> Object
recordToJsonObject Rec ToJsonField rs
fs Rec Identity rs
Rec Identity rs
as
recordToJson :: RecordToJsonObject rs => Rec ToJsonField rs -> Rec Identity rs -> Aeson.Value
recordToJson :: Rec ToJsonField rs -> Rec Identity rs -> Value
recordToJson = (Object -> Value)
-> (Rec Identity rs -> Object) -> Rec Identity rs -> Value
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Object -> Value
Aeson.Object ((Rec Identity rs -> Object) -> Rec Identity rs -> Value)
-> (Rec ToJsonField rs -> Rec Identity rs -> Object)
-> Rec ToJsonField rs
-> Rec Identity rs
-> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Rec ToJsonField rs -> Rec Identity rs -> Object
forall (rs :: [*]).
RecordToJsonObject rs =>
Rec ToJsonField rs -> Rec Identity rs -> Object
recordToJsonObject
class RecordFromJson rs where
recordFromJson :: Rec (FromJsonField e) rs -> ABE.Parse e (Rec Identity rs)
instance RecordFromJson '[] where
recordFromJson :: Rec (FromJsonField e) '[] -> Parse e (Rec Identity '[])
recordFromJson Rec (FromJsonField e) '[]
_ = Rec Identity '[] -> Parse e (Rec Identity '[])
forall (f :: * -> *) a. Applicative f => a -> f a
pure Rec Identity '[]
forall u (a :: u -> *). Rec a '[]
RNil
instance forall s a rs. (KnownSymbol s, RecordFromJson rs) => RecordFromJson (s :-> a ': rs) where
recordFromJson :: Rec (FromJsonField e) ((s :-> a) : rs)
-> Parse e (Rec Identity ((s :-> a) : rs))
recordFromJson (FromJsonField Text -> Parse e r
aFromField :& Rec (FromJsonField e) rs
fs) =
Identity r -> Rec Identity rs -> Rec Identity (r : rs)
forall u (a :: u -> *) (r :: u) (rs :: [u]).
a r -> Rec a rs -> Rec a (r : rs)
(:&)
(Identity r -> Rec Identity rs -> Rec Identity (r : rs))
-> ParseT e Identity (Identity r)
-> ParseT e Identity (Rec Identity rs -> Rec Identity (r : rs))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (r -> Identity r
forall a. a -> Identity a
Identity (r -> Identity r) -> Parse e r -> ParseT e Identity (Identity r)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> Parse e r
aFromField (String -> Text
pack (String -> Text) -> (Proxy s -> String) -> Proxy s -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Proxy s -> String
forall (n :: Symbol) (proxy :: Symbol -> *).
KnownSymbol n =>
proxy n -> String
symbolVal (Proxy s -> Text) -> Proxy s -> Text
forall a b. (a -> b) -> a -> b
$ (Proxy s
forall k (t :: k). Proxy t
Proxy :: Proxy s)))
ParseT e Identity (Rec Identity rs -> Rec Identity (r : rs))
-> ParseT e Identity (Rec Identity rs)
-> ParseT e Identity (Rec Identity (r : rs))
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Rec (FromJsonField e) rs -> ParseT e Identity (Rec Identity rs)
forall (rs :: [*]) e.
RecordFromJson rs =>
Rec (FromJsonField e) rs -> Parse e (Rec Identity rs)
recordFromJson Rec (FromJsonField e) rs
fs
recordJsonFormat :: (RMap rs, RecordToJsonObject rs, RecordFromJson rs) => JsonFormatRecord e rs -> JsonFormat e (Rec Identity rs)
recordJsonFormat :: JsonFormatRecord e rs -> JsonFormat e (Rec Identity rs)
recordJsonFormat JsonFormatRecord e rs
formatRec =
JsonProfunctor e (Rec Identity rs) (Rec Identity rs)
-> JsonFormat e (Rec Identity rs)
forall e a. JsonProfunctor e a a -> JsonFormat e a
JsonFormat (JsonProfunctor e (Rec Identity rs) (Rec Identity rs)
-> JsonFormat e (Rec Identity rs))
-> JsonProfunctor e (Rec Identity rs) (Rec Identity rs)
-> JsonFormat e (Rec Identity rs)
forall a b. (a -> b) -> a -> b
$ (Rec Identity rs -> Value)
-> Parse e (Rec Identity rs)
-> JsonProfunctor e (Rec Identity rs) (Rec Identity rs)
forall e a b. (a -> Value) -> Parse e b -> JsonProfunctor e a b
JsonProfunctor
(Rec ToJsonField rs -> Rec Identity rs -> Value
forall (rs :: [*]).
RecordToJsonObject rs =>
Rec ToJsonField rs -> Rec Identity rs -> Value
recordToJson (Rec ToJsonField rs -> Rec Identity rs -> Value)
-> (JsonFormatRecord e rs -> Rec ToJsonField rs)
-> JsonFormatRecord e rs
-> Rec Identity rs
-> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. JsonFormatRecord e rs -> Rec ToJsonField rs
forall (rs :: [*]) e.
RMap rs =>
JsonFormatRecord e rs -> ToJsonFormatRecord rs
toJsonFormatRecord (JsonFormatRecord e rs -> Rec Identity rs -> Value)
-> JsonFormatRecord e rs -> Rec Identity rs -> Value
forall a b. (a -> b) -> a -> b
$ JsonFormatRecord e rs
formatRec)
(Rec (FromJsonField e) rs -> Parse e (Rec Identity rs)
forall (rs :: [*]) e.
RecordFromJson rs =>
Rec (FromJsonField e) rs -> Parse e (Rec Identity rs)
recordFromJson (Rec (FromJsonField e) rs -> Parse e (Rec Identity rs))
-> (JsonFormatRecord e rs -> Rec (FromJsonField e) rs)
-> JsonFormatRecord e rs
-> Parse e (Rec Identity rs)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. JsonFormatRecord e rs -> Rec (FromJsonField e) rs
forall (rs :: [*]) e.
RMap rs =>
JsonFormatRecord e rs -> FromJsonFormatRecord e rs
fromJsonFormatRecord (JsonFormatRecord e rs -> Parse e (Rec Identity rs))
-> JsonFormatRecord e rs -> Parse e (Rec Identity rs)
forall a b. (a -> b) -> a -> b
$ JsonFormatRecord e rs
formatRec)
class DefaultJsonFormatRecord rs where
defaultJsonFormatRecord :: JsonFormatRecord e rs
instance (KnownSymbol s, DefaultJsonFormat a, DefaultJsonFormatRecord rs) => DefaultJsonFormatRecord (s :-> a ': rs) where
defaultJsonFormatRecord :: JsonFormatRecord e ((s :-> a) : rs)
defaultJsonFormatRecord = JsonFormat e a -> JsonField e (s :-> a)
forall a' a e.
(Wrapped a', Unwrapped a' ~ a) =>
JsonFormat e a -> JsonField e a'
field JsonFormat e a
forall a e. DefaultJsonFormat a => JsonFormat e a
defaultJsonFormat JsonField e (s :-> a)
-> Rec (JsonField e) rs -> JsonFormatRecord e ((s :-> a) : rs)
forall u (a :: u -> *) (r :: u) (rs :: [u]).
a r -> Rec a rs -> Rec a (r : rs)
:& Rec (JsonField e) rs
forall (rs :: [*]) e.
DefaultJsonFormatRecord rs =>
JsonFormatRecord e rs
defaultJsonFormatRecord
instance DefaultJsonFormatRecord '[] where
defaultJsonFormatRecord :: JsonFormatRecord e '[]
defaultJsonFormatRecord = JsonFormatRecord e '[]
forall u (a :: u -> *). Rec a '[]
RNil