    {-# LANGUAGE DeriveFoldable #-}
    {-# LANGUAGE DeriveFunctor #-}
    {-# LANGUAGE DeriveTraversable #-}
    {-# LANGUAGE FlexibleContexts #-}
    {-# LANGUAGE FlexibleInstances #-}
    {-# LANGUAGE GeneralizedNewtypeDeriving #-}
    {-# LANGUAGE TypeFamilies #-}
    {-# LANGUAGE TypeOperators #-}
    {-# LANGUAGE TypeSynonymInstances #-}
    {-# LANGUAGE UndecidableInstances #-}

    module Data.ProtocolBuffers.Message
      ( Message(..)
      , GMessageMonoid
      ) where

    import Control.Applicative
    import Control.DeepSeq (NFData(..))
    import Data.Foldable
    import Data.Monoid hiding ((<>))
    import Data.Serialize.Get
    import Data.Serialize.Put
    import Data.Traversable
    import Data.Semigroup (Semigroup(..))

    import GHC.Generics
    import GHC.TypeLits

    import Data.ProtocolBuffers.Decode
    import Data.ProtocolBuffers.Encode
    import Data.ProtocolBuffers.Types
    import Data.ProtocolBuffers.Wire

    -- |
    -- The way to embed a message within another message.
    -- These embedded messages are stored as length-delimited fields.
    --
    -- For example:
    --
    -- @
    --data Inner = Inner
    --   { innerField :: 'Data.ProtocolBuffers.Required' '1' ('Data.ProtocolBuffers.Value' 'Data.Int.Int64')
    --   } deriving ('GHC.Generics.Generic', 'Prelude.Show')
    --
    -- instance 'Encode' Inner
    --instance 'Decode' Inner
    --
    -- data Outer = Outer
    --   { outerField :: 'Data.ProtocolBuffers.Required' '1' ('Data.ProtocolBuffers.Message' Inner)
    --   } deriving ('GHC.Generics.Generic', 'Prelude.Show')
    --
    -- instance 'Encode' Outer
    --instance 'Decode' Outer
    -- @
    --
    -- It's worth noting that @ 'Message' a @ is a 'Monoid' and 'NFData' instance. The 'Monoid' behavior models
    -- that of the Protocol Buffers documentation, effectively 'Data.Monoid.Last'. It's done with a fairly big hammer
    -- and it isn't possible to override this behavior. This can cause some less-obvious compile errors for
    -- paramterized 'Message' types:
    --
    -- @
    --data Inner = Inner{inner :: 'Required' '2' ('Value' 'Float')} deriving ('Generic', 'Show')
    --instance 'Encode' Inner
    --instance 'Decode' Inner
    --
    --data Outer a = Outer{outer :: 'Required' '3' ('Message' a)} deriving ('Generic', 'Show')
    --instance 'Encode' a => 'Encode' (Outer a)
    --instance 'Decode' a => 'Decode' (Outer a)
    -- @
    --
    -- This fails because 'Decode' needs to know that the message can be merged. The resulting error
    -- implies that you may want to add a constraint to the internal 'GMessageMonoid' class:
    --
    -- @
    -- \/tmp\/tst.hs:18:10:
    --   Could not deduce (protobuf-0.1:'Data.ProtocolBuffers.Message.GMessageMonoid' ('Rep' a))
    --     arising from a use of `protobuf-0.1: 'Data.ProtocolBuffers.Decode' .$gdmdecode'
    --   from the context ('Decode' a)
    --     bound by the instance declaration at \/tmp\/tst.hs:18:10-39
    --   Possible fix:
    --     add an instance declaration for
    --     (protobuf-0.1:'Data.ProtocolBuffers.Message.GMessageMonoid' ('Rep' a))
    --   In the expression:
    --     (protobuf-0.1:'Data.ProtocolBuffers.Decode'.$gdmdecode)
    --   In an equation for `decode':
    --       decode = (protobuf-0.1:'Data.ProtocolBuffers.Decode' .$gdmdecode)
    --   In the instance declaration for `'Decode' (Outer a)'
    -- @
    --
    -- The correct fix is to add the 'Monoid' constraint for the message:
    --
    -- @
    -- - instance ('Encode' a) => 'Decode' (Outer a)
    -- + instance ('Monoid' ('Message' a), 'Decode' a) => 'Decode' (Outer a)
    -- @
    --
    newtype Message m = Message {forall m. Message m -> m
    runMessage :: m}
      deriving (Message m -> Message m -> Bool
    (Message m -> Message m -> Bool)
    -> (Message m -> Message m -> Bool) -> Eq (Message m)
    forall m. Eq m => Message m -> Message m -> Bool
    forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
    $c== :: forall m. Eq m => Message m -> Message m -> Bool
    == :: Message m -> Message m -> Bool
    $c/= :: forall m. Eq m => Message m -> Message m -> Bool
    /= :: Message m -> Message m -> Bool
    Eq, (forall m. Monoid m => Message m -> m)
    -> (forall m a. Monoid m => (a -> m) -> Message a -> m)
    -> (forall m a. Monoid m => (a -> m) -> Message a -> m)
    -> (forall a b. (a -> b -> b) -> b -> Message a -> b)
    -> (forall a b. (a -> b -> b) -> b -> Message a -> b)
    -> (forall b a. (b -> a -> b) -> b -> Message a -> b)
    -> (forall b a. (b -> a -> b) -> b -> Message a -> b)
    -> (forall a. (a -> a -> a) -> Message a -> a)
    -> (forall a. (a -> a -> a) -> Message a -> a)
    -> (forall a. Message a -> [a])
    -> (forall a. Message a -> Bool)
    -> (forall a. Message a -> Int)
    -> (forall a. Eq a => a -> Message a -> Bool)
    -> (forall a. Ord a => Message a -> a)
    -> (forall a. Ord a => Message a -> a)
    -> (forall a. Num a => Message a -> a)
    -> (forall a. Num a => Message a -> a)
    -> Foldable Message
    forall a. Eq a => a -> Message a -> Bool
    forall a. Num a => Message a -> a
    forall a. Ord a => Message a -> a
    forall m. Monoid m => Message m -> m
    forall a. Message a -> Bool
    forall a. Message a -> Int
    forall a. Message a -> [a]
    forall a. (a -> a -> a) -> Message a -> a
    forall m a. Monoid m => (a -> m) -> Message a -> m
    forall b a. (b -> a -> b) -> b -> Message a -> b
    forall a b. (a -> b -> b) -> b -> Message a -> b
    forall (t :: * -> *).
    (forall m. Monoid m => t m -> m)
    -> (forall m a. Monoid m => (a -> m) -> t a -> m)
    -> (forall m a. Monoid m => (a -> m) -> t a -> m)
    -> (forall a b. (a -> b -> b) -> b -> t a -> b)
    -> (forall a b. (a -> b -> b) -> b -> t a -> b)
    -> (forall b a. (b -> a -> b) -> b -> t a -> b)
    -> (forall b a. (b -> a -> b) -> b -> t a -> b)
    -> (forall a. (a -> a -> a) -> t a -> a)
    -> (forall a. (a -> a -> a) -> t a -> a)
    -> (forall a. t a -> [a])
    -> (forall a. t a -> Bool)
    -> (forall a. t a -> Int)
    -> (forall a. Eq a => a -> t a -> Bool)
    -> (forall a. Ord a => t a -> a)
    -> (forall a. Ord a => t a -> a)
    -> (forall a. Num a => t a -> a)
    -> (forall a. Num a => t a -> a)
    -> Foldable t
    $cfold :: forall m. Monoid m => Message m -> m
    fold :: forall m. Monoid m => Message m -> m
    $cfoldMap :: forall m a. Monoid m => (a -> m) -> Message a -> m
    foldMap :: forall m a. Monoid m => (a -> m) -> Message a -> m
    $cfoldMap' :: forall m a. Monoid m => (a -> m) -> Message a -> m
    foldMap' :: forall m a. Monoid m => (a -> m) -> Message a -> m
    $cfoldr :: forall a b. (a -> b -> b) -> b -> Message a -> b
    foldr :: forall a b. (a -> b -> b) -> b -> Message a -> b
    $cfoldr' :: forall a b. (a -> b -> b) -> b -> Message a -> b
    foldr' :: forall a b. (a -> b -> b) -> b -> Message a -> b
    $cfoldl :: forall b a. (b -> a -> b) -> b -> Message a -> b
    foldl :: forall b a. (b -> a -> b) -> b -> Message a -> b
    $cfoldl' :: forall b a. (b -> a -> b) -> b -> Message a -> b
    foldl' :: forall b a. (b -> a -> b) -> b -> Message a -> b
    $cfoldr1 :: forall a. (a -> a -> a) -> Message a -> a
    foldr1 :: forall a. (a -> a -> a) -> Message a -> a
    $cfoldl1 :: forall a. (a -> a -> a) -> Message a -> a
    foldl1 :: forall a. (a -> a -> a) -> Message a -> a
    $ctoList :: forall a. Message a -> [a]
    toList :: forall a. Message a -> [a]
    $cnull :: forall a. Message a -> Bool
    null :: forall a. Message a -> Bool
    $clength :: forall a. Message a -> Int
    length :: forall a. Message a -> Int
    $celem :: forall a. Eq a => a -> Message a -> Bool
    elem :: forall a. Eq a => a -> Message a -> Bool
    $cmaximum :: forall a. Ord a => Message a -> a
    maximum :: forall a. Ord a => Message a -> a
    $cminimum :: forall a. Ord a => Message a -> a
    minimum :: forall a. Ord a => Message a -> a
    $csum :: forall a. Num a => Message a -> a
    sum :: forall a. Num a => Message a -> a
    $cproduct :: forall a. Num a => Message a -> a
    product :: forall a. Num a => Message a -> a
    Foldable, (forall a b. (a -> b) -> Message a -> Message b)
    -> (forall a b. a -> Message b -> Message a) -> Functor Message
    forall a b. a -> Message b -> Message a
    forall a b. (a -> b) -> Message a -> Message b
    forall (f :: * -> *).
    (forall a b. (a -> b) -> f a -> f b)
    -> (forall a b. a -> f b -> f a) -> Functor f
    $cfmap :: forall a b. (a -> b) -> Message a -> Message b
    fmap :: forall a b. (a -> b) -> Message a -> Message b
    $c<$ :: forall a b. a -> Message b -> Message a
    <$ :: forall a b. a -> Message b -> Message a
    Functor, Eq (Message m)
    Eq (Message m) =>
    (Message m -> Message m -> Ordering)
    -> (Message m -> Message m -> Bool)
    -> (Message m -> Message m -> Bool)
    -> (Message m -> Message m -> Bool)
    -> (Message m -> Message m -> Bool)
    -> (Message m -> Message m -> Message m)
    -> (Message m -> Message m -> Message m)
    -> Ord (Message m)
    Message m -> Message m -> Bool
    Message m -> Message m -> Ordering
    Message m -> Message m -> Message m
    forall a.
    Eq a =>
    (a -> a -> Ordering)
    -> (a -> a -> Bool)
    -> (a -> a -> Bool)
    -> (a -> a -> Bool)
    -> (a -> a -> Bool)
    -> (a -> a -> a)
    -> (a -> a -> a)
    -> Ord a
    forall m. Ord m => Eq (Message m)
    forall m. Ord m => Message m -> Message m -> Bool
    forall m. Ord m => Message m -> Message m -> Ordering
    forall m. Ord m => Message m -> Message m -> Message m
    $ccompare :: forall m. Ord m => Message m -> Message m -> Ordering
    compare :: Message m -> Message m -> Ordering
    $c< :: forall m. Ord m => Message m -> Message m -> Bool
    < :: Message m -> Message m -> Bool
    $c<= :: forall m. Ord m => Message m -> Message m -> Bool
    <= :: Message m -> Message m -> Bool
    $c> :: forall m. Ord m => Message m -> Message m -> Bool
    > :: Message m -> Message m -> Bool
    $c>= :: forall m. Ord m => Message m -> Message m -> Bool
    >= :: Message m -> Message m -> Bool
    $cmax :: forall m. Ord m => Message m -> Message m -> Message m
    max :: Message m -> Message m -> Message m
    $cmin :: forall m. Ord m => Message m -> Message m -> Message m
    min :: Message m -> Message m -> Message m
    Ord, Int -> Message m -> ShowS
    [Message m] -> ShowS
    Message m -> String
    (Int -> Message m -> ShowS)
    -> (Message m -> String)
    -> ([Message m] -> ShowS)
    -> Show (Message m)
    forall m. Show m => Int -> Message m -> ShowS
    forall m. Show m => [Message m] -> ShowS
    forall m. Show m => Message m -> String
    forall a.
    (Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
    $cshowsPrec :: forall m. Show m => Int -> Message m -> ShowS
    showsPrec :: Int -> Message m -> ShowS
    $cshow :: forall m. Show m => Message m -> String
    show :: Message m -> String
    $cshowList :: forall m. Show m => [Message m] -> ShowS
    showList :: [Message m] -> ShowS
    Show, Functor Message
    Foldable Message
    (Functor Message, Foldable Message) =>
    (forall (f :: * -> *) a b.
     Applicative f =>
     (a -> f b) -> Message a -> f (Message b))
    -> (forall (f :: * -> *) a.
        Applicative f =>
        Message (f a) -> f (Message a))
    -> (forall (m :: * -> *) a b.
        Monad m =>
        (a -> m b) -> Message a -> m (Message b))
    -> (forall (m :: * -> *) a.
        Monad m =>
        Message (m a) -> m (Message a))
    -> Traversable Message
    forall (t :: * -> *).
    (Functor t, Foldable t) =>
    (forall (f :: * -> *) a b.
     Applicative f =>
     (a -> f b) -> t a -> f (t b))
    -> (forall (f :: * -> *) a. Applicative f => t (f a) -> f (t a))
    -> (forall (m :: * -> *) a b.
        Monad m =>
        (a -> m b) -> t a -> m (t b))
    -> (forall (m :: * -> *) a. Monad m => t (m a) -> m (t a))
    -> Traversable t
    forall (m :: * -> *) a. Monad m => Message (m a) -> m (Message a)
    forall (f :: * -> *) a.
    Applicative f =>
    Message (f a) -> f (Message a)
    forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> Message a -> m (Message b)
    forall (f :: * -> *) a b.
    Applicative f =>
    (a -> f b) -> Message a -> f (Message b)
    $ctraverse :: forall (f :: * -> *) a b.
    Applicative f =>
    (a -> f b) -> Message a -> f (Message b)
    traverse :: forall (f :: * -> *) a b.
    Applicative f =>
    (a -> f b) -> Message a -> f (Message b)
    $csequenceA :: forall (f :: * -> *) a.
    Applicative f =>
    Message (f a) -> f (Message a)
    sequenceA :: forall (f :: * -> *) a.
    Applicative f =>
    Message (f a) -> f (Message a)
    $cmapM :: forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> Message a -> m (Message b)
    mapM :: forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> Message a -> m (Message b)
    $csequence :: forall (m :: * -> *) a. Monad m => Message (m a) -> m (Message a)
    sequence :: forall (m :: * -> *) a. Monad m => Message (m a) -> m (Message a)
    Traversable)

    instance (Generic m, GMessageMonoid (Rep m)) => Semigroup (Message m) where
      Message m
    x <> :: Message m -> Message m -> Message m
    <> Message m
    y = m -> Message m
    forall m. m -> Message m
    Message (m -> Message m)
    -> (Rep m (ZonkAny 2) -> m) -> Rep m (ZonkAny 2) -> Message m
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Rep m (ZonkAny 2) -> m
    forall a x. Generic a => Rep a x -> a
    forall x. Rep m x -> m
    to (Rep m (ZonkAny 2) -> Message m) -> Rep m (ZonkAny 2) -> Message m
    forall a b. (a -> b) -> a -> b
    $ Rep m (ZonkAny 2) -> Rep m (ZonkAny 2) -> Rep m (ZonkAny 2)
    forall a. Rep m a -> Rep m a -> Rep m a
    forall (f :: * -> *) a. GMessageMonoid f => f a -> f a -> f a
    gmappend (m -> Rep m (ZonkAny 2)
    forall x. m -> Rep m x
    forall a x. Generic a => a -> Rep a x
    from m
    x) (m -> Rep m (ZonkAny 2)
    forall x. m -> Rep m x
    forall a x. Generic a => a -> Rep a x
    from m
    y)

    instance (Generic m, GMessageMonoid (Rep m)) => Monoid (Message m) where
      mempty :: Message m
    mempty = m -> Message m
    forall m. m -> Message m
    Message (m -> Message m)
    -> (Rep m (ZonkAny 1) -> m) -> Rep m (ZonkAny 1) -> Message m
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Rep m (ZonkAny 1) -> m
    forall a x. Generic a => Rep a x -> a
    forall x. Rep m x -> m
    to (Rep m (ZonkAny 1) -> Message m) -> Rep m (ZonkAny 1) -> Message m
    forall a b. (a -> b) -> a -> b
    $ Rep m (ZonkAny 1)
    forall a. Rep m a
    forall (f :: * -> *) a. GMessageMonoid f => f a
    gmempty
      mappend :: Message m -> Message m -> Message m
    mappend = Message m -> Message m -> Message m
    forall a. Semigroup a => a -> a -> a
    (<>)

    instance (Decode a, Monoid (Message a), KnownNat n) => GDecode (K1 i (Field n (RequiredField (Always (Message a))))) where
      gdecode :: forall a.
    HashMap Tag [WireField]
    -> Get (K1 i (Field n (RequiredField (Always (Message a)))) a)
    gdecode = (Message a -> RequiredField (Always (Message a)))
    -> HashMap Tag [WireField]
    -> Get (K1 i (Field n (RequiredField (Always (Message a)))) a)
    forall a b i (n :: Nat) p.
    (DecodeWire a, Monoid a, KnownNat n) =>
    (a -> b) -> HashMap Tag [WireField] -> Get (K1 i (Field n b) p)
    fieldDecode (Always (Message a) -> RequiredField (Always (Message a))
    forall a. a -> RequiredField a
    Required (Always (Message a) -> RequiredField (Always (Message a)))
    -> (Message a -> Always (Message a))
    -> Message a
    -> RequiredField (Always (Message a))
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Message a -> Always (Message a)
    forall a. a -> Always a
    Always)

    instance (Decode a, Monoid (Message a), KnownNat n) => GDecode (K1 i (Field n (OptionalField (Maybe (Message a))))) where
      gdecode :: forall a.
    HashMap Tag [WireField]
    -> Get (K1 i (Field n (OptionalField (Maybe (Message a)))) a)
    gdecode HashMap Tag [WireField]
    msg = (Message a -> OptionalField (Maybe (Message a)))
    -> HashMap Tag [WireField]
    -> Get (K1 i (Field n (OptionalField (Maybe (Message a)))) a)
    forall a b i (n :: Nat) p.
    (DecodeWire a, Monoid a, KnownNat n) =>
    (a -> b) -> HashMap Tag [WireField] -> Get (K1 i (Field n b) p)
    fieldDecode (Maybe (Message a) -> OptionalField (Maybe (Message a))
    forall a. a -> OptionalField a
    Optional (Maybe (Message a) -> OptionalField (Maybe (Message a)))
    -> (Message a -> Maybe (Message a))
    -> Message a
    -> OptionalField (Maybe (Message a))
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Message a -> Maybe (Message a)
    forall a. a -> Maybe a
    Just) HashMap Tag [WireField]
    msg Get (K1 i (Field n (OptionalField (Maybe (Message a)))) a)
    -> Get (K1 i (Field n (OptionalField (Maybe (Message a)))) a)
    -> Get (K1 i (Field n (OptionalField (Maybe (Message a)))) a)
    forall a. Get a -> Get a -> Get a
    forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
    <|> K1 i (Field n (OptionalField (Maybe (Message a)))) a
    -> Get (K1 i (Field n (OptionalField (Maybe (Message a)))) a)
    forall a. a -> Get a
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure (Field n (OptionalField (Maybe (Message a)))
    -> K1 i (Field n (OptionalField (Maybe (Message a)))) a
    forall k i c (p :: k). c -> K1 i c p
    K1 Field n (OptionalField (Maybe (Message a)))
    forall a. Monoid a => a
    mempty)

    class GMessageMonoid (f :: * -> *) where
      gmempty :: f a
      gmappend :: f a -> f a -> f a

    instance GMessageMonoid f => GMessageMonoid (M1 i c f) where
      gmempty :: forall a. M1 i c f a
    gmempty = f a -> M1 i c f a
    forall k i (c :: Meta) (f :: k -> *) (p :: k). f p -> M1 i c f p
    M1 f a
    forall a. f a
    forall (f :: * -> *) a. GMessageMonoid f => f a
    gmempty
      gmappend :: forall a. M1 i c f a -> M1 i c f a -> M1 i c f a
    gmappend (M1 f a
    x) (M1 f a
    y) = f a -> M1 i c f a
    forall k i (c :: Meta) (f :: k -> *) (p :: k). f p -> M1 i c f p
    M1 (f a -> f a -> f a
    forall a. f a -> f a -> f a
    forall (f :: * -> *) a. GMessageMonoid f => f a -> f a -> f a
    gmappend f a
    x f a
    y)

    instance (GMessageMonoid x, GMessageMonoid y) => GMessageMonoid (x :*: y) where
      gmempty :: forall a. (:*:) x y a
    gmempty = x a
    forall a. x a
    forall (f :: * -> *) a. GMessageMonoid f => f a
    gmempty x a -> y a -> (:*:) x y a
    forall k (f :: k -> *) (g :: k -> *) (p :: k).
    f p -> g p -> (:*:) f g p
    :*: y a
    forall a. y a
    forall (f :: * -> *) a. GMessageMonoid f => f a
    gmempty
      gmappend :: forall a. (:*:) x y a -> (:*:) x y a -> (:*:) x y a
    gmappend (x a
    x1 :*: y a
    x2) (x a
    y1 :*: y a
    y2) = x a -> x a -> x a
    forall a. x a -> x a -> x a
    forall (f :: * -> *) a. GMessageMonoid f => f a -> f a -> f a
    gmappend x a
    x1 x a
    y1 x a -> y a -> (:*:) x y a
    forall k (f :: k -> *) (g :: k -> *) (p :: k).
    f p -> g p -> (:*:) f g p
    :*: y a -> y a -> y a
    forall a. y a -> y a -> y a
    forall (f :: * -> *) a. GMessageMonoid f => f a -> f a -> f a
    gmappend y a
    x2 y a
    y2

    instance (GMessageMonoid x, GMessageMonoid y) => GMessageMonoid (x :+: y) where
      gmempty :: forall a. (:+:) x y a
    gmempty = x a -> (:+:) x y a
    forall k (f :: k -> *) (g :: k -> *) (p :: k). f p -> (:+:) f g p
    L1 x a
    forall a. x a
    forall (f :: * -> *) a. GMessageMonoid f => f a
    gmempty
      gmappend :: forall a. (:+:) x y a -> (:+:) x y a -> (:+:) x y a
    gmappend (:+:) x y a
    _ = (:+:) x y a -> (:+:) x y a
    forall a. a -> a
    id

    instance (Monoid c) => GMessageMonoid (K1 i c) where
      gmempty :: forall a. K1 i c a
    gmempty = c -> K1 i c a
    forall k i c (p :: k). c -> K1 i c p
    K1 c
    forall a. Monoid a => a
    mempty
      gmappend :: forall a. K1 i c a -> K1 i c a -> K1 i c a
    gmappend (K1 c
    x) (K1 c
    y) = c -> K1 i c a
    forall k i c (p :: k). c -> K1 i c p
    K1 (c -> K1 i c a) -> c -> K1 i c a
    forall a b. (a -> b) -> a -> b
    $ c -> c -> c
    forall a. Monoid a => a -> a -> a
    mappend c
    x c
    y

    instance GMessageMonoid U1 where
      gmempty :: forall a. U1 a
    gmempty = U1 a
    forall k (p :: k). U1 p
    U1
      gmappend :: forall a. U1 a -> U1 a -> U1 a
    gmappend U1 a
    _ = U1 a -> U1 a
    forall a. a -> a
    id

    instance (Generic m, GMessageNFData (Rep m)) => NFData (Message m) where
      rnf :: Message m -> ()
    rnf = Rep m (ZonkAny 0) -> ()
    forall a. Rep m a -> ()
    forall (f :: * -> *) a. GMessageNFData f => f a -> ()
    grnf (Rep m (ZonkAny 0) -> ())
    -> (Message m -> Rep m (ZonkAny 0)) -> Message m -> ()
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . m -> Rep m (ZonkAny 0)
    forall x. m -> Rep m x
    forall a x. Generic a => a -> Rep a x
    from (m -> Rep m (ZonkAny 0))
    -> (Message m -> m) -> Message m -> Rep m (ZonkAny 0)
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Message m -> m
    forall m. Message m -> m
    runMessage

    class GMessageNFData f where
      grnf :: f a -> ()

    instance GMessageNFData f => GMessageNFData (M1 i c f) where
      grnf :: forall a. M1 i c f a -> ()
    grnf = f a -> ()
    forall a. f a -> ()
    forall (f :: * -> *) a. GMessageNFData f => f a -> ()
    grnf (f a -> ()) -> (M1 i c f a -> f a) -> M1 i c f a -> ()
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . M1 i c f a -> f a
    forall k i (c :: Meta) (f :: k -> *) (p :: k). M1 i c f p -> f p
    unM1

    instance (GMessageNFData x, GMessageNFData y) => GMessageNFData (x :*: y) where
      grnf :: forall a. (:*:) x y a -> ()
    grnf (x a
    x :*: y a
    y) = x a -> ()
    forall a. x a -> ()
    forall (f :: * -> *) a. GMessageNFData f => f a -> ()
    grnf x a
    x () -> () -> ()
    forall a b. a -> b -> b
    `seq` y a -> ()
    forall a. y a -> ()
    forall (f :: * -> *) a. GMessageNFData f => f a -> ()
    grnf y a
    y

    instance (GMessageNFData x, GMessageNFData y) => GMessageNFData (x :+: y) where
      grnf :: forall a. (:+:) x y a -> ()
    grnf (L1 x a
    x) = x a -> ()
    forall a. x a -> ()
    forall (f :: * -> *) a. GMessageNFData f => f a -> ()
    grnf x a
    x
      grnf (R1 y a
    y) = y a -> ()
    forall a. y a -> ()
    forall (f :: * -> *) a. GMessageNFData f => f a -> ()
    grnf y a
    y

    instance NFData c => GMessageNFData (K1 i c) where
      grnf :: forall a. K1 i c a -> ()
    grnf = c -> ()
    forall a. NFData a => a -> ()
    rnf (c -> ()) -> (K1 i c a -> c) -> K1 i c a -> ()
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . K1 i c a -> c
    forall k i c (p :: k). K1 i c p -> c
    unK1

    instance GMessageNFData U1 where
      grnf :: forall a. U1 a -> ()
    grnf U1 a
    U1 = ()

    type instance Optional n (Message a) = Field n (OptionalField (Maybe (Message a)))
    type instance Required n (Message a) = Field n (RequiredField (Always (Message a)))

    instance (Foldable f, Encode m) => EncodeWire (f (Message m)) where
      encodeWire :: Tag -> f (Message m) -> Put
    encodeWire Tag
    t =
        (Message m -> Put) -> f (Message m) -> Put
    forall (t :: * -> *) (f :: * -> *) a b.
    (Foldable t, Applicative f) =>
    (a -> f b) -> t a -> f ()
    traverse_ (Tag -> ByteString -> Put
    forall a. EncodeWire a => Tag -> a -> Put
    encodeWire Tag
    t (ByteString -> Put)
    -> (Message m -> ByteString) -> Message m -> Put
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Put -> ByteString
    runPut (Put -> ByteString)
    -> (Message m -> Put) -> Message m -> ByteString
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . m -> Put
    forall a. Encode a => a -> Put
    encode (m -> Put) -> (Message m -> m) -> Message m -> Put
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Message m -> m
    forall m. Message m -> m
    runMessage)

    instance Decode m => DecodeWire (Message m) where
      decodeWire :: WireField -> Get (Message m)
    decodeWire (DelimitedField Tag
    _ ByteString
    bs) =
        case Get m -> ByteString -> Either String m
    forall a. Get a -> ByteString -> Either String a
    runGet Get m
    forall a. Decode a => Get a
    decodeMessage ByteString
    bs of
          Right m
    val -> Message m -> Get (Message m)
    forall a. a -> Get a
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure (Message m -> Get (Message m)) -> Message m -> Get (Message m)
    forall a b. (a -> b) -> a -> b
    $ m -> Message m
    forall m. m -> Message m
    Message m
    val
          Left String
    err  -> String -> Get (Message m)
    forall a. String -> Get a
    forall (m :: * -> *) a. MonadFail m => String -> m a
    fail (String -> Get (Message m)) -> String -> Get (Message m)
    forall a b. (a -> b) -> a -> b
    $ String
    "Embedded message decoding failed: " String -> ShowS
    forall a. [a] -> [a] -> [a]
    ++ ShowS
    forall a. Show a => a -> String
    show String
    err
      decodeWire WireField
    _ = Get (Message m)
    forall a. Get a
    forall (f :: * -> *) a. Alternative f => f a
    empty

    -- | Iso: @ 'FieldType' ('Required' n ('Message' a)) = a @
    instance HasField (Field n (RequiredField (Always (Message a)))) where
      type FieldType (Field n (RequiredField (Always (Message a)))) = a
      getField :: Field n (RequiredField (Always (Message a)))
    -> FieldType (Field n (RequiredField (Always (Message a))))
    getField = Message a -> a
    forall m. Message m -> m
    runMessage (Message a -> a)
    -> (Field n (RequiredField (Always (Message a))) -> Message a)
    -> Field n (RequiredField (Always (Message a)))
    -> a
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Always (Message a) -> Message a
    forall a. Always a -> a
    runAlways(Always (Message a) -> Message a)
    -> (Field n (RequiredField (Always (Message a)))
        -> Always (Message a))
    -> Field n (RequiredField (Always (Message a)))
    -> Message a
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . RequiredField (Always (Message a)) -> Always (Message a)
    forall a. RequiredField a -> a
    runRequired (RequiredField (Always (Message a)) -> Always (Message a))
    -> (Field n (RequiredField (Always (Message a)))
        -> RequiredField (Always (Message a)))
    -> Field n (RequiredField (Always (Message a)))
    -> Always (Message a)
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Field n (RequiredField (Always (Message a)))
    -> RequiredField (Always (Message a))
    forall (n :: Nat) a. Field n a -> a
    runField
      putField :: FieldType (Field n (RequiredField (Always (Message a))))
    -> Field n (RequiredField (Always (Message a)))
    putField = RequiredField (Always (Message a))
    -> Field n (RequiredField (Always (Message a)))
    forall (n :: Nat) a. a -> Field n a
    Field (RequiredField (Always (Message a))
     -> Field n (RequiredField (Always (Message a))))
    -> (a -> RequiredField (Always (Message a)))
    -> a
    -> Field n (RequiredField (Always (Message a)))
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Always (Message a) -> RequiredField (Always (Message a))
    forall a. a -> RequiredField a
    Required (Always (Message a) -> RequiredField (Always (Message a)))
    -> (a -> Always (Message a))
    -> a
    -> RequiredField (Always (Message a))
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Message a -> Always (Message a)
    forall a. a -> Always a
    Always (Message a -> Always (Message a))
    -> (a -> Message a) -> a -> Always (Message a)
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . a -> Message a
    forall m. m -> Message m
    Message

    -- | Iso: @ 'FieldType' ('Optional' n ('Message' a)) = 'Maybe' a @
    instance HasField (Field n (OptionalField (Maybe (Message a)))) where
      type FieldType (Field n (OptionalField (Maybe (Message a)))) = Maybe a
      getField :: Field n (OptionalField (Maybe (Message a)))
    -> FieldType (Field n (OptionalField (Maybe (Message a))))
    getField = (Message a -> a) -> Maybe (Message a) -> Maybe a
    forall a b. (a -> b) -> Maybe a -> Maybe b
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    fmap Message a -> a
    forall m. Message m -> m
    runMessage (Maybe (Message a) -> Maybe a)
    -> (Field n (OptionalField (Maybe (Message a)))
        -> Maybe (Message a))
    -> Field n (OptionalField (Maybe (Message a)))
    -> Maybe a
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . OptionalField (Maybe (Message a)) -> Maybe (Message a)
    forall a. OptionalField a -> a
    runOptional (OptionalField (Maybe (Message a)) -> Maybe (Message a))
    -> (Field n (OptionalField (Maybe (Message a)))
        -> OptionalField (Maybe (Message a)))
    -> Field n (OptionalField (Maybe (Message a)))
    -> Maybe (Message a)
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Field n (OptionalField (Maybe (Message a)))
    -> OptionalField (Maybe (Message a))
    forall (n :: Nat) a. Field n a -> a
    runField
      putField :: FieldType (Field n (OptionalField (Maybe (Message a))))
    -> Field n (OptionalField (Maybe (Message a)))
    putField = OptionalField (Maybe (Message a))
    -> Field n (OptionalField (Maybe (Message a)))
    forall (n :: Nat) a. a -> Field n a
    Field (OptionalField (Maybe (Message a))
     -> Field n (OptionalField (Maybe (Message a))))
    -> (Maybe a -> OptionalField (Maybe (Message a)))
    -> Maybe a
    -> Field n (OptionalField (Maybe (Message a)))
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Maybe (Message a) -> OptionalField (Maybe (Message a))
    forall a. a -> OptionalField a
    Optional (Maybe (Message a) -> OptionalField (Maybe (Message a)))
    -> (Maybe a -> Maybe (Message a))
    -> Maybe a
    -> OptionalField (Maybe (Message a))
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . (a -> Message a) -> Maybe a -> Maybe (Message a)
    forall a b. (a -> b) -> Maybe a -> Maybe b
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    fmap a -> Message a
    forall m. m -> Message m
    Message

    -- | Iso: @ 'FieldType' ('Repeated' n ('Message' a)) = [a] @
    instance HasField (Field n (RepeatedField [Message a])) where
      type FieldType (Field n (RepeatedField [Message a])) = [a]
      getField :: Field n (RepeatedField [Message a])
    -> FieldType (Field n (RepeatedField [Message a]))
    getField = (Message a -> a) -> [Message a] -> [a]
    forall a b. (a -> b) -> [a] -> [b]
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    fmap Message a -> a
    forall m. Message m -> m
    runMessage ([Message a] -> [a])
    -> (Field n (RepeatedField [Message a]) -> [Message a])
    -> Field n (RepeatedField [Message a])
    -> [a]
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . RepeatedField [Message a] -> [Message a]
    forall a. RepeatedField a -> a
    runRepeated (RepeatedField [Message a] -> [Message a])
    -> (Field n (RepeatedField [Message a])
        -> RepeatedField [Message a])
    -> Field n (RepeatedField [Message a])
    -> [Message a]
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Field n (RepeatedField [Message a]) -> RepeatedField [Message a]
    forall (n :: Nat) a. Field n a -> a
    runField
      putField :: FieldType (Field n (RepeatedField [Message a]))
    -> Field n (RepeatedField [Message a])
    putField = RepeatedField [Message a] -> Field n (RepeatedField [Message a])
    forall (n :: Nat) a. a -> Field n a
    Field (RepeatedField [Message a] -> Field n (RepeatedField [Message a]))
    -> ([a] -> RepeatedField [Message a])
    -> [a]
    -> Field n (RepeatedField [Message a])
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . [Message a] -> RepeatedField [Message a]
    forall a. a -> RepeatedField a
    Repeated ([Message a] -> RepeatedField [Message a])
    -> ([a] -> [Message a]) -> [a] -> RepeatedField [Message a]
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . (a -> Message a) -> [a] -> [Message a]
    forall a b. (a -> b) -> [a] -> [b]
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    fmap a -> Message a
    forall m. m -> Message m
    Message