{-# language AllowAmbiguousTypes   #-}
{-# language CPP                   #-}
{-# language ConstraintKinds       #-}
{-# language DataKinds             #-}
{-# language FlexibleContexts      #-}
{-# language FlexibleInstances     #-}
{-# language GADTs                 #-}
{-# language MultiParamTypeClasses #-}
{-# language OverloadedStrings     #-}
{-# language PolyKinds             #-}
{-# language ScopedTypeVariables   #-}
{-# language TypeApplications      #-}
{-# language TypeFamilies          #-}
{-# language TypeOperators         #-}
{-# language UndecidableInstances  #-}
{-# language ViewPatterns          #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
{-|
Description : Adapter for Protocol Buffers serialization

Just import the module and you can turn any
value with a 'ToSchema' and 'FromSchema' from
and to Protocol Buffers. Since Protocol Buffers
need information about field identifiers, you
need to annotate your schema using 'ProtoBufAnnotation'.
-}
module Mu.Adapter.ProtoBuf (
  -- * Custom annotations
  ProtoBufAnnotation(..)
  -- * Conversion using schemas
, IsProtoSchema
, toProtoViaSchema
, fromProtoViaSchema
, parseProtoViaSchema
  -- * Conversion using registry
, FromProtoBufRegistry
, fromProtoBufWithRegistry
, parseProtoBufWithRegistry
) where

import           Control.Applicative
import qualified Data.ByteString          as BS
import           Data.Functor.MaybeLike
import           Data.Int
import           Data.SOP                 (All)
import qualified Data.Text                as T
import qualified Data.Text.Lazy           as LT
import           GHC.TypeLits
import           Proto3.Wire
import qualified Proto3.Wire.Decode       as PBDec
import qualified Proto3.Wire.Encode       as PBEnc

import           Mu.Schema.Annotations
import           Mu.Schema.Class
import           Mu.Schema.Definition
import           Mu.Schema.Interpretation
import qualified Mu.Schema.Registry       as R

#if MIN_VERSION_proto3_wire(1,1,0)
instance ProtoEnum Bool
#endif

-- | Annotations for Protocol Buffers fields.
data ProtoBufAnnotation
  = -- | Numeric field identifier for normal fields
    ProtoBufId Nat
    -- | List of identifiers for fields which contain a union
  | ProtoBufOneOfIds [Nat]

type family FindProtoBufId (sch :: Schema tn fn) (t :: tn) (f :: fn) where
  FindProtoBufId sch t f
    = FindProtoBufId' t f (GetFieldAnnotation (AnnotatedSchema ProtoBufAnnotation sch) t f)

type family FindProtoBufId' (t :: tn) (f :: fn) (p :: ProtoBufAnnotation) :: Nat where
  FindProtoBufId' t f ('ProtoBufId n) = n
  FindProtoBufId' t f other
    = TypeError ('Text "protocol buffers id not available for field "
                 ':<>: 'ShowType t ':<>: 'Text "/" ':<>: 'ShowType f)

type family FindProtoBufOneOfIds (sch :: Schema tn fn) (t :: tn) (f :: fn) where
  FindProtoBufOneOfIds sch t f
    = FindProtoBufOneOfIds' t f (GetFieldAnnotation (AnnotatedSchema ProtoBufAnnotation sch) t f)

type family FindProtoBufOneOfIds' (t :: tn) (f :: fn) (p :: ProtoBufAnnotation) :: [Nat] where
  FindProtoBufOneOfIds' t f ('ProtoBufOneOfIds ns) = ns
  FindProtoBufOneOfIds' t f other
    = TypeError ('Text "protocol buffers id not available for oneof field "
                 ':<>: 'ShowType t ':<>: 'Text "/" ':<>: 'ShowType f)

-- CONVERSION USING SCHEMAS

-- | Represents those 'Schema's which are supported by Protocol Buffers.
--   Some values which can be represented as 'Term's cannot be so in
--   Protocol Buffers. For example, you cannot have a list within an option.
class ProtoBridgeTerm w sch (sch :/: sty) => IsProtoSchema w sch sty
instance ProtoBridgeTerm w sch (sch :/: sty) => IsProtoSchema w sch sty

-- type HasProtoSchema w sch sty a = (HasSchema w sch sty a, IsProtoSchema w sch sty)

-- | Conversion to Protocol Buffers mediated by a schema.
toProtoViaSchema :: forall t f (sch :: Schema t f) a sty.
                    (IsProtoSchema Maybe sch sty, ToSchema Maybe sch sty a)
                 => a -> PBEnc.MessageBuilder
toProtoViaSchema :: a -> MessageBuilder
toProtoViaSchema = Term Maybe sch (sch :/: sty) -> MessageBuilder
forall tn fn (w :: * -> *) (sch :: Schema tn fn)
       (t :: TypeDef tn fn).
ProtoBridgeTerm w sch t =>
Term w sch t -> MessageBuilder
termToProto (Term Maybe sch (sch :/: sty) -> MessageBuilder)
-> (a -> Term Maybe sch (sch :/: sty)) -> a -> MessageBuilder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t (sty :: t).
ToSchema Maybe sch sty t =>
t -> Term Maybe sch (sch :/: sty)
forall fn tn (sch :: Schema tn fn) (w :: * -> *) t (sty :: tn).
ToSchema w sch sty t =>
t -> Term w sch (sch :/: sty)
toSchema' @_ @_ @sch @Maybe

-- | Conversion from Protocol Buffers mediated by a schema.
--   This function requires a 'PBDec.RawMessage', which means
--   that we already know that the Protocol Buffers message
--   is well-formed. Use 'parseProtoViaSchema' to parse directly
--   from a 'BS.ByteString'.
fromProtoViaSchema :: forall t f (sch :: Schema t f) a sty.
                      (IsProtoSchema Maybe sch sty, FromSchema Maybe sch sty a)
                   => PBDec.Parser PBDec.RawMessage a
fromProtoViaSchema :: Parser RawMessage a
fromProtoViaSchema = forall t (sty :: t).
FromSchema Maybe sch sty t =>
Term Maybe sch (sch :/: sty) -> t
forall fn tn (sch :: Schema tn fn) (w :: * -> *) t (sty :: tn).
FromSchema w sch sty t =>
Term w sch (sch :/: sty) -> t
fromSchema' @_ @_ @sch @Maybe (Term Maybe sch (sch :/: sty) -> a)
-> Parser RawMessage (Term Maybe sch (sch :/: sty))
-> Parser RawMessage a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser RawMessage (Term Maybe sch (sch :/: sty))
forall tn fn (w :: * -> *) (sch :: Schema tn fn)
       (t :: TypeDef tn fn).
ProtoBridgeTerm w sch t =>
Parser RawMessage (Term w sch t)
protoToTerm

-- | Conversion from Protocol Buffers mediated by a schema.
--   This function receives the 'BS.ByteString' directly,
--   and parses it as part of its duty.
parseProtoViaSchema :: forall sch a sty.
                       (IsProtoSchema Maybe sch sty, FromSchema Maybe sch sty a)
                    => BS.ByteString -> Either PBDec.ParseError a
parseProtoViaSchema :: ByteString -> Either ParseError a
parseProtoViaSchema = Parser RawMessage a -> ByteString -> Either ParseError a
forall a. Parser RawMessage a -> ByteString -> Either ParseError a
PBDec.parse (forall a (sty :: typeName).
(IsProtoSchema Maybe sch sty, FromSchema Maybe sch sty a) =>
Parser RawMessage a
forall t f (sch :: Schema t f) a (sty :: t).
(IsProtoSchema Maybe sch sty, FromSchema Maybe sch sty a) =>
Parser RawMessage a
fromProtoViaSchema @_ @_ @sch)

-- CONVERSION USING REGISTRY

-- | Conversion from Protocol Buffers by checking
--   all the 'Schema's in a 'R.Registry'.
--
--   As 'fromProtoViaSchema', this version requires
--   an already well-formed Protocol Buffers message.
fromProtoBufWithRegistry
  :: forall (r :: R.Registry) t.
     FromProtoBufRegistry r t
  => PBDec.Parser PBDec.RawMessage t
fromProtoBufWithRegistry :: Parser RawMessage t
fromProtoBufWithRegistry = Proxy r -> Parser RawMessage t
forall (ms :: Mappings Nat Schema') t.
FromProtoBufRegistry ms t =>
Proxy ms -> Parser RawMessage t
fromProtoBufRegistry' (Proxy r
forall k (t :: k). Proxy t
Proxy @r)

-- | Conversion from Protocol Buffers by checking
--   all the 'Schema's in a 'R.Registry'.
--
--   As 'parseProtoViaSchema', this version receives
--   a 'BS.ByteString' and parses it as part of its duty.
parseProtoBufWithRegistry
  :: forall (r :: R.Registry) t.
     FromProtoBufRegistry r t
  => BS.ByteString -> Either PBDec.ParseError t
parseProtoBufWithRegistry :: ByteString -> Either ParseError t
parseProtoBufWithRegistry = Parser RawMessage t -> ByteString -> Either ParseError t
forall a. Parser RawMessage a -> ByteString -> Either ParseError a
PBDec.parse (forall (r :: Mappings Nat Schema') t.
FromProtoBufRegistry r t =>
Parser RawMessage t
forall t. FromProtoBufRegistry r t => Parser RawMessage t
fromProtoBufWithRegistry @r)

-- | Represents 'R.Registry's for which every 'Schema'
--   is supported by the Protocol Buffers format.
class FromProtoBufRegistry (ms :: Mappings Nat Schema') t where
  fromProtoBufRegistry' :: Proxy ms -> PBDec.Parser PBDec.RawMessage t

instance FromProtoBufRegistry '[] t where
  fromProtoBufRegistry' :: Proxy '[] -> Parser RawMessage t
fromProtoBufRegistry' _ = (RawMessage -> Either ParseError t) -> Parser RawMessage t
forall input a. (input -> Either ParseError a) -> Parser input a
PBDec.Parser (\_ -> ParseError -> Either ParseError t
forall a b. a -> Either a b
Left (Text -> ParseError
PBDec.WireTypeError "no schema found in registry"))
instance (IsProtoSchema Maybe s sty, FromSchema Maybe s sty t, FromProtoBufRegistry ms t)
         => FromProtoBufRegistry ( (n ':-> s) ': ms) t where
  fromProtoBufRegistry' :: Proxy ((n ':-> s) : ms) -> Parser RawMessage t
fromProtoBufRegistry' _ = forall t f (sch :: Schema t f) a (sty :: t).
(IsProtoSchema Maybe sch sty, FromSchema Maybe sch sty a) =>
Parser RawMessage a
forall a (sty :: Symbol).
(IsProtoSchema Maybe s sty, FromSchema Maybe s sty a) =>
Parser RawMessage a
fromProtoViaSchema @_ @_ @s Parser RawMessage t -> Parser RawMessage t -> Parser RawMessage t
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Proxy ms -> Parser RawMessage t
forall (ms :: Mappings Nat Schema') t.
FromProtoBufRegistry ms t =>
Proxy ms -> Parser RawMessage t
fromProtoBufRegistry' (Proxy ms
forall k (t :: k). Proxy t
Proxy @ms)


-- =======================================
-- IMPLEMENTATION OF GENERIC SERIALIZATION
-- =======================================

instance Alternative (PBDec.Parser i) where
  empty :: Parser i a
empty = (i -> Either ParseError a) -> Parser i a
forall input a. (input -> Either ParseError a) -> Parser input a
PBDec.Parser (\_ -> ParseError -> Either ParseError a
forall a b. a -> Either a b
Left (Text -> ParseError
PBDec.WireTypeError "cannot parse"))
  PBDec.Parser x :: i -> Either ParseError a
x <|> :: Parser i a -> Parser i a -> Parser i a
<|> PBDec.Parser y :: i -> Either ParseError a
y
    = (i -> Either ParseError a) -> Parser i a
forall input a. (input -> Either ParseError a) -> Parser input a
PBDec.Parser ((i -> Either ParseError a) -> Parser i a)
-> (i -> Either ParseError a) -> Parser i a
forall a b. (a -> b) -> a -> b
$ \i :: i
i -> case i -> Either ParseError a
x i
i of
                             Left _      -> i -> Either ParseError a
y i
i
                             r :: Either ParseError a
r@(Right _) -> Either ParseError a
r

-- Top-level terms
class ProtoBridgeTerm (w :: * -> *) (sch :: Schema tn fn) (t :: TypeDef tn fn) where
  termToProto :: Term w sch t -> PBEnc.MessageBuilder
  protoToTerm :: PBDec.Parser PBDec.RawMessage (Term w sch t)

-- Embedded terms
class ProtoBridgeEmbedTerm (w :: * -> *) (sch :: Schema tn fn) (t :: TypeDef tn fn) where
  termToEmbedProto :: FieldNumber -> Term w sch t -> PBEnc.MessageBuilder
  embedProtoToFieldValue :: PBDec.Parser PBDec.RawField (Term w sch t)
  embedProtoToOneFieldValue :: PBDec.Parser PBDec.RawPrimitive (Term w sch t)

class ProtoBridgeField (w :: * -> *) (sch :: Schema tn fn) (ty :: tn) (f :: FieldDef tn fn) where
  fieldToProto :: Field w sch f -> PBEnc.MessageBuilder
  protoToField :: PBDec.Parser PBDec.RawMessage (Field w sch f)

class ProtoBridgeFieldValue (w :: * -> *) (sch :: Schema tn fn) (t :: FieldType tn) where
  fieldValueToProto :: FieldNumber -> FieldValue w sch t -> PBEnc.MessageBuilder
  protoToFieldValue :: PBDec.Parser PBDec.RawField (FieldValue w sch t)

class ProtoBridgeOneFieldValue (w :: * -> *) (sch :: Schema tn fn) (t :: FieldType tn) where
  protoToOneFieldValue :: PBDec.Parser PBDec.RawPrimitive (FieldValue w sch t)

class ProtoBridgeUnionFieldValue (w :: * -> *) (ids :: [Nat]) (sch :: Schema tn fn) (ts :: [FieldType tn]) where
  unionFieldValueToProto :: NS (FieldValue w sch) ts -> PBEnc.MessageBuilder
  protoToUnionFieldValue :: PBDec.Parser PBDec.RawMessage (NS (FieldValue w sch) ts)

-- --------
-- TERMS --
-- --------

-- RECORDS
-- -------

instance (All (ProtoBridgeField w sch name) args, ProtoBridgeFields w sch name args)
         => ProtoBridgeTerm w sch ('DRecord name args) where
  termToProto :: Term w sch ('DRecord name args) -> MessageBuilder
termToProto (TRecord fields :: NP (Field w sch) args
fields) = NP (Field w sch) args -> MessageBuilder
forall (fs :: [FieldDef typeName fieldName]).
All (ProtoBridgeField w sch name) fs =>
NP (Field w sch) fs -> MessageBuilder
go NP (Field w sch) args
fields
    where go :: forall fs. All (ProtoBridgeField w sch name) fs
             => NP (Field w sch) fs -> PBEnc.MessageBuilder
          go :: NP (Field w sch) fs -> MessageBuilder
go Nil       = MessageBuilder
forall a. Monoid a => a
mempty
          go (f :: Field w sch x
f :* fs :: NP (Field w sch) xs
fs) = Field w sch x -> MessageBuilder
forall tn fn (w :: * -> *) (sch :: Schema tn fn) (ty :: tn)
       (f :: FieldDef tn fn).
ProtoBridgeField w sch ty f =>
Field w sch f -> MessageBuilder
fieldToProto @_ @_ @w @sch @name Field w sch x
f MessageBuilder -> MessageBuilder -> MessageBuilder
forall a. Semigroup a => a -> a -> a
<> NP (Field w sch) xs -> MessageBuilder
forall (fs :: [FieldDef typeName fieldName]).
All (ProtoBridgeField w sch name) fs =>
NP (Field w sch) fs -> MessageBuilder
go NP (Field w sch) xs
fs
  protoToTerm :: Parser RawMessage (Term w sch ('DRecord name args))
protoToTerm = NP (Field w sch) args -> Term w sch ('DRecord name args)
forall typeName fieldName (w :: * -> *)
       (sch :: Schema typeName fieldName)
       (args :: [FieldDef typeName fieldName]) (name :: typeName).
NP (Field w sch) args -> Term w sch ('DRecord name args)
TRecord (NP (Field w sch) args -> Term w sch ('DRecord name args))
-> Parser RawMessage (NP (Field w sch) args)
-> Parser RawMessage (Term w sch ('DRecord name args))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (fields :: [FieldDef typeName fieldName]).
ProtoBridgeFields w sch name fields =>
Parser RawMessage (NP (Field w sch) fields)
forall tn fn (w :: * -> *) (sch :: Schema tn fn) (ty :: tn)
       (fields :: [FieldDef tn fn]).
ProtoBridgeFields w sch ty fields =>
Parser RawMessage (NP (Field w sch) fields)
protoToFields @_ @_ @w @sch @name

class ProtoBridgeFields (w :: * -> *) (sch :: Schema tn fn) (ty :: tn) (fields :: [FieldDef tn fn]) where
  protoToFields :: PBDec.Parser PBDec.RawMessage (NP (Field w sch) fields)
instance ProtoBridgeFields w sch ty '[] where
  protoToFields :: Parser RawMessage (NP (Field w sch) '[])
protoToFields = NP (Field w sch) '[] -> Parser RawMessage (NP (Field w sch) '[])
forall (f :: * -> *) a. Applicative f => a -> f a
pure NP (Field w sch) '[]
forall k (a :: k -> *). NP a '[]
Nil
instance (ProtoBridgeField w sch ty f, ProtoBridgeFields w sch ty fs)
         => ProtoBridgeFields w sch ty (f ': fs) where
  protoToFields :: Parser RawMessage (NP (Field w sch) (f : fs))
protoToFields = Field w sch f -> NP (Field w sch) fs -> NP (Field w sch) (f : fs)
forall k (a :: k -> *) (x :: k) (xs :: [k]).
a x -> NP a xs -> NP a (x : xs)
(:*) (Field w sch f -> NP (Field w sch) fs -> NP (Field w sch) (f : fs))
-> Parser RawMessage (Field w sch f)
-> Parser
     RawMessage (NP (Field w sch) fs -> NP (Field w sch) (f : fs))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall tn fn (w :: * -> *) (sch :: Schema tn fn) (ty :: tn)
       (f :: FieldDef tn fn).
ProtoBridgeField w sch ty f =>
Parser RawMessage (Field w sch f)
forall (f :: FieldDef tn fn).
ProtoBridgeField w sch ty f =>
Parser RawMessage (Field w sch f)
protoToField @_ @_ @w @sch @ty Parser
  RawMessage (NP (Field w sch) fs -> NP (Field w sch) (f : fs))
-> Parser RawMessage (NP (Field w sch) fs)
-> Parser RawMessage (NP (Field w sch) (f : fs))
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall (fields :: [FieldDef tn fn]).
ProtoBridgeFields w sch ty fields =>
Parser RawMessage (NP (Field w sch) fields)
forall tn fn (w :: * -> *) (sch :: Schema tn fn) (ty :: tn)
       (fields :: [FieldDef tn fn]).
ProtoBridgeFields w sch ty fields =>
Parser RawMessage (NP (Field w sch) fields)
protoToFields @_ @_ @w @sch @ty

instance ProtoBridgeTerm w sch ('DRecord name args)
         => ProtoBridgeEmbedTerm w sch ('DRecord name args) where
  termToEmbedProto :: FieldNumber -> Term w sch ('DRecord name args) -> MessageBuilder
termToEmbedProto fid :: FieldNumber
fid v :: Term w sch ('DRecord name args)
v = FieldNumber -> MessageBuilder -> MessageBuilder
PBEnc.embedded FieldNumber
fid (Term w sch ('DRecord name args) -> MessageBuilder
forall tn fn (w :: * -> *) (sch :: Schema tn fn)
       (t :: TypeDef tn fn).
ProtoBridgeTerm w sch t =>
Term w sch t -> MessageBuilder
termToProto Term w sch ('DRecord name args)
v)
  embedProtoToFieldValue :: Parser RawField (Term w sch ('DRecord name args))
embedProtoToFieldValue = do
    Maybe (Term w sch ('DRecord name args))
t <- Parser RawMessage (Term w sch ('DRecord name args))
-> Parser RawField (Maybe (Term w sch ('DRecord name args)))
forall a. Parser RawMessage a -> Parser RawField (Maybe a)
PBDec.embedded (ProtoBridgeTerm w sch ('DRecord name args) =>
Parser RawMessage (Term w sch ('DRecord name args))
forall tn fn (w :: * -> *) (sch :: Schema tn fn)
       (t :: TypeDef tn fn).
ProtoBridgeTerm w sch t =>
Parser RawMessage (Term w sch t)
protoToTerm @_ @_ @w @sch @('DRecord name args))
    case Maybe (Term w sch ('DRecord name args))
t of
      Nothing -> (RawField -> Either ParseError (Term w sch ('DRecord name args)))
-> Parser RawField (Term w sch ('DRecord name args))
forall input a. (input -> Either ParseError a) -> Parser input a
PBDec.Parser (\_ -> ParseError -> Either ParseError (Term w sch ('DRecord name args))
forall a b. a -> Either a b
Left (Text -> ParseError
PBDec.WireTypeError "expected message"))
      Just v :: Term w sch ('DRecord name args)
v  -> Term w sch ('DRecord name args)
-> Parser RawField (Term w sch ('DRecord name args))
forall (m :: * -> *) a. Monad m => a -> m a
return Term w sch ('DRecord name args)
v
  embedProtoToOneFieldValue :: Parser RawPrimitive (Term w sch ('DRecord name args))
embedProtoToOneFieldValue = Parser RawMessage (Term w sch ('DRecord name args))
-> Parser RawPrimitive (Term w sch ('DRecord name args))
forall a. Parser RawMessage a -> Parser RawPrimitive a
PBDec.embedded' (ProtoBridgeTerm w sch ('DRecord name args) =>
Parser RawMessage (Term w sch ('DRecord name args))
forall tn fn (w :: * -> *) (sch :: Schema tn fn)
       (t :: TypeDef tn fn).
ProtoBridgeTerm w sch t =>
Parser RawMessage (Term w sch t)
protoToTerm @_ @_ @w @sch @('DRecord name args))

-- ENUMERATIONS
-- ------------

instance TypeError ('Text "protobuf requires wrapping enums in a message")
         => ProtoBridgeTerm w sch ('DEnum name choices) where
  termToProto :: Term w sch ('DEnum name choices) -> MessageBuilder
termToProto = [Char] -> Term w sch ('DEnum name choices) -> MessageBuilder
forall a. HasCallStack => [Char] -> a
error "protobuf requires wrapping enums in a message"
  protoToTerm :: Parser RawMessage (Term w sch ('DEnum name choices))
protoToTerm = [Char] -> Parser RawMessage (Term w sch ('DEnum name choices))
forall a. HasCallStack => [Char] -> a
error "protobuf requires wrapping enums in a message"

instance ProtoBridgeEnum sch name choices
         => ProtoBridgeEmbedTerm w sch ('DEnum name choices) where
  termToEmbedProto :: FieldNumber -> Term w sch ('DEnum name choices) -> MessageBuilder
termToEmbedProto fid :: FieldNumber
fid (TEnum v :: NS Proxy choices
v) = FieldNumber -> NS Proxy choices -> MessageBuilder
forall tn fn (sch :: Schema tn fn) (ty :: tn)
       (choices :: [ChoiceDef fn]).
ProtoBridgeEnum sch ty choices =>
FieldNumber -> NS Proxy choices -> MessageBuilder
enumToProto @_ @_ @sch @name FieldNumber
fid NS Proxy choices
v
  embedProtoToFieldValue :: Parser RawField (Term w sch ('DEnum name choices))
embedProtoToFieldValue    = do Int32
n <- Parser RawPrimitive Int32 -> Int32 -> Parser RawField Int32
forall a. Parser RawPrimitive a -> a -> Parser RawField a
PBDec.one Parser RawPrimitive Int32
PBDec.int32 0
                                 NS Proxy choices -> Term w sch ('DEnum name choices)
forall fieldName typeName (choices :: [ChoiceDef fieldName])
       (w :: * -> *) (sch :: Schema typeName fieldName)
       (name :: typeName).
NS Proxy choices -> Term w sch ('DEnum name choices)
TEnum (NS Proxy choices -> Term w sch ('DEnum name choices))
-> Parser RawField (NS Proxy choices)
-> Parser RawField (Term w sch ('DEnum name choices))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int32 -> Parser RawField (NS Proxy choices)
forall tn fn (sch :: Schema tn fn) (ty :: tn)
       (choices :: [ChoiceDef fn]) a.
ProtoBridgeEnum sch ty choices =>
Int32 -> Parser a (NS Proxy choices)
protoToEnum @_ @_ @sch @name Int32
n
  embedProtoToOneFieldValue :: Parser RawPrimitive (Term w sch ('DEnum name choices))
embedProtoToOneFieldValue = do Int32
n <- Parser RawPrimitive Int32
PBDec.int32
                                 NS Proxy choices -> Term w sch ('DEnum name choices)
forall fieldName typeName (choices :: [ChoiceDef fieldName])
       (w :: * -> *) (sch :: Schema typeName fieldName)
       (name :: typeName).
NS Proxy choices -> Term w sch ('DEnum name choices)
TEnum (NS Proxy choices -> Term w sch ('DEnum name choices))
-> Parser RawPrimitive (NS Proxy choices)
-> Parser RawPrimitive (Term w sch ('DEnum name choices))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int32 -> Parser RawPrimitive (NS Proxy choices)
forall tn fn (sch :: Schema tn fn) (ty :: tn)
       (choices :: [ChoiceDef fn]) a.
ProtoBridgeEnum sch ty choices =>
Int32 -> Parser a (NS Proxy choices)
protoToEnum @_ @_ @sch @name Int32
n

class ProtoBridgeEnum (sch :: Schema tn fn) (ty :: tn) (choices :: [ChoiceDef fn]) where
  enumToProto :: FieldNumber -> NS Proxy choices -> PBEnc.MessageBuilder
  protoToEnum :: Int32 -> PBDec.Parser a (NS Proxy choices)
instance ProtoBridgeEnum sch ty '[] where
  enumToProto :: FieldNumber -> NS Proxy '[] -> MessageBuilder
enumToProto = [Char] -> FieldNumber -> NS Proxy '[] -> MessageBuilder
forall a. HasCallStack => [Char] -> a
error "empty enum"
  protoToEnum :: Int32 -> Parser a (NS Proxy '[])
protoToEnum _ = (a -> Either ParseError (NS Proxy '[])) -> Parser a (NS Proxy '[])
forall input a. (input -> Either ParseError a) -> Parser input a
PBDec.Parser (\_ -> ParseError -> Either ParseError (NS Proxy '[])
forall a b. a -> Either a b
Left (Text -> ParseError
PBDec.WireTypeError "unknown enum type"))
instance (KnownNat (FindProtoBufId sch ty c), ProtoBridgeEnum sch ty cs)
         => ProtoBridgeEnum sch ty ('ChoiceDef c ': cs) where
  enumToProto :: FieldNumber -> NS Proxy ('ChoiceDef c : cs) -> MessageBuilder
enumToProto fid :: FieldNumber
fid (Z _) = FieldNumber -> Int32 -> MessageBuilder
PBEnc.int32 FieldNumber
fid Int32
enumValue
    where enumValue :: Int32
enumValue = Integer -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Proxy
  (FindProtoBufId'
     ty
     c
     (GetFieldAnnotation (AnnotatedSchema ProtoBufAnnotation sch) ty c))
-> Integer
forall (n :: Nat) (proxy :: Nat -> *).
KnownNat n =>
proxy n -> Integer
natVal (Proxy (FindProtoBufId sch ty c)
forall k (t :: k). Proxy t
Proxy @(FindProtoBufId sch ty c)))
  enumToProto fid :: FieldNumber
fid (S v :: NS Proxy xs
v) = FieldNumber -> NS Proxy xs -> MessageBuilder
forall tn fn (sch :: Schema tn fn) (ty :: tn)
       (choices :: [ChoiceDef fn]).
ProtoBridgeEnum sch ty choices =>
FieldNumber -> NS Proxy choices -> MessageBuilder
enumToProto @_ @_ @sch @ty FieldNumber
fid NS Proxy xs
v
  protoToEnum :: Int32 -> Parser a (NS Proxy ('ChoiceDef c : cs))
protoToEnum n :: Int32
n
    | Int32
n Int32 -> Int32 -> Bool
forall a. Eq a => a -> a -> Bool
== Int32
enumValue = NS Proxy ('ChoiceDef c : cs)
-> Parser a (NS Proxy ('ChoiceDef c : cs))
forall (m :: * -> *) a. Monad m => a -> m a
return (Proxy ('ChoiceDef c) -> NS Proxy ('ChoiceDef c : cs)
forall k (a :: k -> *) (x :: k) (xs :: [k]). a x -> NS a (x : xs)
Z Proxy ('ChoiceDef c)
forall k (t :: k). Proxy t
Proxy)
    | Bool
otherwise      = NS Proxy cs -> NS Proxy ('ChoiceDef c : cs)
forall k (a :: k -> *) (xs :: [k]) (x :: k).
NS a xs -> NS a (x : xs)
S (NS Proxy cs -> NS Proxy ('ChoiceDef c : cs))
-> Parser a (NS Proxy cs)
-> Parser a (NS Proxy ('ChoiceDef c : cs))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int32 -> Parser a (NS Proxy cs)
forall tn fn (sch :: Schema tn fn) (ty :: tn)
       (choices :: [ChoiceDef fn]) a.
ProtoBridgeEnum sch ty choices =>
Int32 -> Parser a (NS Proxy choices)
protoToEnum @_ @_ @sch @ty Int32
n
    where enumValue :: Int32
enumValue = Integer -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Proxy
  (FindProtoBufId'
     ty
     c
     (GetFieldAnnotation (AnnotatedSchema ProtoBufAnnotation sch) ty c))
-> Integer
forall (n :: Nat) (proxy :: Nat -> *).
KnownNat n =>
proxy n -> Integer
natVal (Proxy (FindProtoBufId sch ty c)
forall k (t :: k). Proxy t
Proxy @(FindProtoBufId sch ty c)))

-- SIMPLE
-- ------

instance TypeError ('Text "protobuf requires wrapping primitives in a message")
         => ProtoBridgeTerm w sch ('DSimple t) where
  termToProto :: Term w sch ('DSimple t) -> MessageBuilder
termToProto = [Char] -> Term w sch ('DSimple t) -> MessageBuilder
forall a. HasCallStack => [Char] -> a
error "protobuf requires wrapping primitives in a message"
  protoToTerm :: Parser RawMessage (Term w sch ('DSimple t))
protoToTerm = [Char] -> Parser RawMessage (Term w sch ('DSimple t))
forall a. HasCallStack => [Char] -> a
error "protobuf requires wrapping primitives in a message"

-- ---------
-- FIELDS --
-- ---------

instance {-# OVERLAPPABLE #-}
         (MaybeLike w, Alternative w, ProtoBridgeFieldValue w sch t, KnownNat (FindProtoBufId sch ty name))
         => ProtoBridgeField w sch ty ('FieldDef name t) where
  fieldToProto :: Field w sch ('FieldDef name t) -> MessageBuilder
fieldToProto (Field (w (FieldValue w sch t) -> Maybe (FieldValue w sch t)
forall (f :: * -> *) a. MaybeLike f => f a -> Maybe a
likeMaybe -> Just v :: FieldValue w sch t
v)) = FieldNumber -> FieldValue w sch t -> MessageBuilder
forall tn fn (w :: * -> *) (sch :: Schema tn fn)
       (t :: FieldType tn).
ProtoBridgeFieldValue w sch t =>
FieldNumber -> FieldValue w sch t -> MessageBuilder
fieldValueToProto FieldNumber
fieldId FieldValue w sch t
v
    where fieldId :: FieldNumber
fieldId = Integer -> FieldNumber
forall a. Num a => Integer -> a
fromInteger (Integer -> FieldNumber) -> Integer -> FieldNumber
forall a b. (a -> b) -> a -> b
$ Proxy
  (FindProtoBufId'
     ty
     name
     (GetFieldAnnotation
        (AnnotatedSchema ProtoBufAnnotation sch) ty name))
-> Integer
forall (n :: Nat) (proxy :: Nat -> *).
KnownNat n =>
proxy n -> Integer
natVal (Proxy (FindProtoBufId sch ty name)
forall k (t :: k). Proxy t
Proxy @(FindProtoBufId sch ty name))
  fieldToProto (Field _) = MessageBuilder
forall a. Monoid a => a
mempty
  protoToField :: Parser RawMessage (Field w sch ('FieldDef name t))
protoToField = w (FieldValue w sch t) -> Field w sch ('FieldDef name t)
forall typeName fieldName (w :: * -> *)
       (sch :: Schema typeName fieldName) (t :: FieldType typeName)
       (name :: fieldName).
w (FieldValue w sch t) -> Field w sch ('FieldDef name t)
Field (w (FieldValue w sch t) -> Field w sch ('FieldDef name t))
-> Parser RawMessage (w (FieldValue w sch t))
-> Parser RawMessage (Field w sch ('FieldDef name t))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ((FieldValue w sch t -> w (FieldValue w sch t)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (FieldValue w sch t -> w (FieldValue w sch t))
-> Parser RawMessage (FieldValue w sch t)
-> Parser RawMessage (w (FieldValue w sch t))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser RawField (FieldValue w sch t)
forall tn fn (w :: * -> *) (sch :: Schema tn fn)
       (t :: FieldType tn).
ProtoBridgeFieldValue w sch t =>
Parser RawField (FieldValue w sch t)
protoToFieldValue Parser RawField (FieldValue w sch t)
-> FieldNumber -> Parser RawMessage (FieldValue w sch t)
forall a. Parser RawField a -> FieldNumber -> Parser RawMessage a
`at` FieldNumber
fieldId) Parser RawMessage (w (FieldValue w sch t))
-> Parser RawMessage (w (FieldValue w sch t))
-> Parser RawMessage (w (FieldValue w sch t))
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> w (FieldValue w sch t)
-> Parser RawMessage (w (FieldValue w sch t))
forall (f :: * -> *) a. Applicative f => a -> f a
pure w (FieldValue w sch t)
forall (f :: * -> *) a. Alternative f => f a
empty)
    where fieldId :: FieldNumber
fieldId = Integer -> FieldNumber
forall a. Num a => Integer -> a
fromInteger (Integer -> FieldNumber) -> Integer -> FieldNumber
forall a b. (a -> b) -> a -> b
$ Proxy
  (FindProtoBufId'
     ty
     name
     (GetFieldAnnotation
        (AnnotatedSchema ProtoBufAnnotation sch) ty name))
-> Integer
forall (n :: Nat) (proxy :: Nat -> *).
KnownNat n =>
proxy n -> Integer
natVal (Proxy (FindProtoBufId sch ty name)
forall k (t :: k). Proxy t
Proxy @(FindProtoBufId sch ty name))

instance {-# OVERLAPS #-}
         (MaybeLike w, Alternative w, ProtoBridgeUnionFieldValue w (FindProtoBufOneOfIds sch ty name) sch ts)
         => ProtoBridgeField w sch ty ('FieldDef name ('TUnion ts)) where
  fieldToProto :: Field w sch ('FieldDef name ('TUnion ts)) -> MessageBuilder
fieldToProto (Field (w (FieldValue w sch t) -> Maybe (FieldValue w sch t)
forall (f :: * -> *) a. MaybeLike f => f a -> Maybe a
likeMaybe -> Just (FUnion v :: NS (FieldValue w sch) choices
v)))
    = NS (FieldValue w sch) choices -> MessageBuilder
forall tn fn (w :: * -> *) (ids :: [Nat]) (sch :: Schema tn fn)
       (ts :: [FieldType tn]).
ProtoBridgeUnionFieldValue w ids sch ts =>
NS (FieldValue w sch) ts -> MessageBuilder
unionFieldValueToProto @_ @_ @w @(FindProtoBufOneOfIds sch ty name) NS (FieldValue w sch) choices
v
  fieldToProto (Field _) = MessageBuilder
forall a. Monoid a => a
mempty
  protoToField :: Parser RawMessage (Field w sch ('FieldDef name ('TUnion ts)))
protoToField
    = w (FieldValue w sch ('TUnion ts))
-> Field w sch ('FieldDef name ('TUnion ts))
forall typeName fieldName (w :: * -> *)
       (sch :: Schema typeName fieldName) (t :: FieldType typeName)
       (name :: fieldName).
w (FieldValue w sch t) -> Field w sch ('FieldDef name t)
Field (w (FieldValue w sch ('TUnion ts))
 -> Field w sch ('FieldDef name ('TUnion ts)))
-> (NS (FieldValue w sch) ts -> w (FieldValue w sch ('TUnion ts)))
-> NS (FieldValue w sch) ts
-> Field w sch ('FieldDef name ('TUnion ts))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FieldValue w sch ('TUnion ts) -> w (FieldValue w sch ('TUnion ts))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (FieldValue w sch ('TUnion ts)
 -> w (FieldValue w sch ('TUnion ts)))
-> (NS (FieldValue w sch) ts -> FieldValue w sch ('TUnion ts))
-> NS (FieldValue w sch) ts
-> w (FieldValue w sch ('TUnion ts))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NS (FieldValue w sch) ts -> FieldValue w sch ('TUnion ts)
forall typeName fieldName (w :: * -> *)
       (sch :: Schema typeName fieldName)
       (choices :: [FieldType typeName]).
NS (FieldValue w sch) choices -> FieldValue w sch ('TUnion choices)
FUnion (NS (FieldValue w sch) ts
 -> Field w sch ('FieldDef name ('TUnion ts)))
-> Parser RawMessage (NS (FieldValue w sch) ts)
-> Parser RawMessage (Field w sch ('FieldDef name ('TUnion ts)))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (sch :: Schema typeName fieldName)
       (ts :: [FieldType typeName]).
ProtoBridgeUnionFieldValue
  w (FindProtoBufOneOfIds sch ty name) sch ts =>
Parser RawMessage (NS (FieldValue w sch) ts)
forall tn fn (w :: * -> *) (ids :: [Nat]) (sch :: Schema tn fn)
       (ts :: [FieldType tn]).
ProtoBridgeUnionFieldValue w ids sch ts =>
Parser RawMessage (NS (FieldValue w sch) ts)
protoToUnionFieldValue @_ @_ @w @(FindProtoBufOneOfIds sch ty name)
    Parser RawMessage (Field w sch ('FieldDef name ('TUnion ts)))
-> Parser RawMessage (Field w sch ('FieldDef name ('TUnion ts)))
-> Parser RawMessage (Field w sch ('FieldDef name ('TUnion ts)))
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Field w sch ('FieldDef name ('TUnion ts))
-> Parser RawMessage (Field w sch ('FieldDef name ('TUnion ts)))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (w (FieldValue w sch ('TUnion ts))
-> Field w sch ('FieldDef name ('TUnion ts))
forall typeName fieldName (w :: * -> *)
       (sch :: Schema typeName fieldName) (t :: FieldType typeName)
       (name :: fieldName).
w (FieldValue w sch t) -> Field w sch ('FieldDef name t)
Field w (FieldValue w sch ('TUnion ts))
forall (f :: * -> *) a. Alternative f => f a
empty)

-- ------------------
-- TYPES OF FIELDS --
-- ------------------

-- SCHEMATIC
-- ---------

instance ProtoBridgeEmbedTerm w sch (sch :/: t)
         => ProtoBridgeFieldValue w sch ('TSchematic t) where
  fieldValueToProto :: FieldNumber -> FieldValue w sch ('TSchematic t) -> MessageBuilder
fieldValueToProto fid :: FieldNumber
fid (FSchematic v :: Term w sch (sch :/: t1)
v) = FieldNumber -> Term w sch (sch :/: t) -> MessageBuilder
forall tn fn (w :: * -> *) (sch :: Schema tn fn)
       (t :: TypeDef tn fn).
ProtoBridgeEmbedTerm w sch t =>
FieldNumber -> Term w sch t -> MessageBuilder
termToEmbedProto FieldNumber
fid Term w sch (sch :/: t)
Term w sch (sch :/: t1)
v
  protoToFieldValue :: Parser RawField (FieldValue w sch ('TSchematic t))
protoToFieldValue = Term w sch (sch :/: t) -> FieldValue w sch ('TSchematic t)
forall typeName fieldName (w :: * -> *)
       (sch :: Schema typeName fieldName) (t1 :: typeName).
Term w sch (sch :/: t1) -> FieldValue w sch ('TSchematic t1)
FSchematic (Term w sch (sch :/: t) -> FieldValue w sch ('TSchematic t))
-> Parser RawField (Term w sch (sch :/: t))
-> Parser RawField (FieldValue w sch ('TSchematic t))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser RawField (Term w sch (sch :/: t))
forall tn fn (w :: * -> *) (sch :: Schema tn fn)
       (t :: TypeDef tn fn).
ProtoBridgeEmbedTerm w sch t =>
Parser RawField (Term w sch t)
embedProtoToFieldValue
instance ProtoBridgeEmbedTerm w sch (sch :/: t)
         => ProtoBridgeOneFieldValue w sch ('TSchematic t) where
  protoToOneFieldValue :: Parser RawPrimitive (FieldValue w sch ('TSchematic t))
protoToOneFieldValue = Term w sch (sch :/: t) -> FieldValue w sch ('TSchematic t)
forall typeName fieldName (w :: * -> *)
       (sch :: Schema typeName fieldName) (t1 :: typeName).
Term w sch (sch :/: t1) -> FieldValue w sch ('TSchematic t1)
FSchematic (Term w sch (sch :/: t) -> FieldValue w sch ('TSchematic t))
-> Parser RawPrimitive (Term w sch (sch :/: t))
-> Parser RawPrimitive (FieldValue w sch ('TSchematic t))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser RawPrimitive (Term w sch (sch :/: t))
forall tn fn (w :: * -> *) (sch :: Schema tn fn)
       (t :: TypeDef tn fn).
ProtoBridgeEmbedTerm w sch t =>
Parser RawPrimitive (Term w sch t)
embedProtoToOneFieldValue

-- PRIMITIVE TYPES
-- ---------------

instance TypeError ('Text "null cannot be converted to protobuf")
         => ProtoBridgeFieldValue w sch 'TNull where
  fieldValueToProto :: FieldNumber -> FieldValue w sch 'TNull -> MessageBuilder
fieldValueToProto = [Char] -> FieldNumber -> FieldValue w sch 'TNull -> MessageBuilder
forall a. HasCallStack => [Char] -> a
error "null cannot be converted to protobuf"
  protoToFieldValue :: Parser RawField (FieldValue w sch 'TNull)
protoToFieldValue = [Char] -> Parser RawField (FieldValue w sch 'TNull)
forall a. HasCallStack => [Char] -> a
error "null cannot be converted to protobuf"
instance TypeError ('Text "null cannot be converted to protobuf")
         => ProtoBridgeOneFieldValue w sch 'TNull where
  protoToOneFieldValue :: Parser RawPrimitive (FieldValue w sch 'TNull)
protoToOneFieldValue = [Char] -> Parser RawPrimitive (FieldValue w sch 'TNull)
forall a. HasCallStack => [Char] -> a
error "null cannot be converted to protobuf"

instance ProtoBridgeFieldValue w sch ('TPrimitive Int) where
  fieldValueToProto :: FieldNumber -> FieldValue w sch ('TPrimitive Int) -> MessageBuilder
fieldValueToProto fid :: FieldNumber
fid (FPrimitive n :: t1
n) = FieldNumber -> Int32 -> MessageBuilder
PBEnc.int32 FieldNumber
fid (t1 -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral t1
n)
  protoToFieldValue :: Parser RawField (FieldValue w sch ('TPrimitive Int))
protoToFieldValue = Int -> FieldValue w sch ('TPrimitive Int)
forall typeName fieldName t1 (w :: * -> *)
       (sch :: Schema typeName fieldName).
t1 -> FieldValue w sch ('TPrimitive t1)
FPrimitive (Int -> FieldValue w sch ('TPrimitive Int))
-> (Int32 -> Int) -> Int32 -> FieldValue w sch ('TPrimitive Int)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int32 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int32 -> FieldValue w sch ('TPrimitive Int))
-> Parser RawField Int32
-> Parser RawField (FieldValue w sch ('TPrimitive Int))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser RawPrimitive Int32 -> Int32 -> Parser RawField Int32
forall a. Parser RawPrimitive a -> a -> Parser RawField a
PBDec.one Parser RawPrimitive Int32
PBDec.int32 0
instance ProtoBridgeOneFieldValue w sch ('TPrimitive Int) where
  protoToOneFieldValue :: Parser RawPrimitive (FieldValue w sch ('TPrimitive Int))
protoToOneFieldValue = Int -> FieldValue w sch ('TPrimitive Int)
forall typeName fieldName t1 (w :: * -> *)
       (sch :: Schema typeName fieldName).
t1 -> FieldValue w sch ('TPrimitive t1)
FPrimitive (Int -> FieldValue w sch ('TPrimitive Int))
-> (Int32 -> Int) -> Int32 -> FieldValue w sch ('TPrimitive Int)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int32 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int32 -> FieldValue w sch ('TPrimitive Int))
-> Parser RawPrimitive Int32
-> Parser RawPrimitive (FieldValue w sch ('TPrimitive Int))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser RawPrimitive Int32
PBDec.int32

instance ProtoBridgeFieldValue w sch ('TPrimitive Int32) where
  fieldValueToProto :: FieldNumber
-> FieldValue w sch ('TPrimitive Int32) -> MessageBuilder
fieldValueToProto fid :: FieldNumber
fid (FPrimitive n :: t1
n) = FieldNumber -> Int32 -> MessageBuilder
PBEnc.int32 FieldNumber
fid t1
Int32
n
  protoToFieldValue :: Parser RawField (FieldValue w sch ('TPrimitive Int32))
protoToFieldValue = Int32 -> FieldValue w sch ('TPrimitive Int32)
forall typeName fieldName t1 (w :: * -> *)
       (sch :: Schema typeName fieldName).
t1 -> FieldValue w sch ('TPrimitive t1)
FPrimitive (Int32 -> FieldValue w sch ('TPrimitive Int32))
-> Parser RawField Int32
-> Parser RawField (FieldValue w sch ('TPrimitive Int32))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser RawPrimitive Int32 -> Int32 -> Parser RawField Int32
forall a. Parser RawPrimitive a -> a -> Parser RawField a
PBDec.one Parser RawPrimitive Int32
PBDec.int32 0
instance ProtoBridgeOneFieldValue w sch ('TPrimitive Int32) where
  protoToOneFieldValue :: Parser RawPrimitive (FieldValue w sch ('TPrimitive Int32))
protoToOneFieldValue = Int32 -> FieldValue w sch ('TPrimitive Int32)
forall typeName fieldName t1 (w :: * -> *)
       (sch :: Schema typeName fieldName).
t1 -> FieldValue w sch ('TPrimitive t1)
FPrimitive (Int32 -> FieldValue w sch ('TPrimitive Int32))
-> Parser RawPrimitive Int32
-> Parser RawPrimitive (FieldValue w sch ('TPrimitive Int32))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser RawPrimitive Int32
PBDec.int32

instance ProtoBridgeFieldValue w sch ('TPrimitive Int64) where
  fieldValueToProto :: FieldNumber
-> FieldValue w sch ('TPrimitive Int64) -> MessageBuilder
fieldValueToProto fid :: FieldNumber
fid (FPrimitive n :: t1
n) = FieldNumber -> Int64 -> MessageBuilder
PBEnc.int64 FieldNumber
fid t1
Int64
n
  protoToFieldValue :: Parser RawField (FieldValue w sch ('TPrimitive Int64))
protoToFieldValue = Int64 -> FieldValue w sch ('TPrimitive Int64)
forall typeName fieldName t1 (w :: * -> *)
       (sch :: Schema typeName fieldName).
t1 -> FieldValue w sch ('TPrimitive t1)
FPrimitive (Int64 -> FieldValue w sch ('TPrimitive Int64))
-> Parser RawField Int64
-> Parser RawField (FieldValue w sch ('TPrimitive Int64))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser RawPrimitive Int64 -> Int64 -> Parser RawField Int64
forall a. Parser RawPrimitive a -> a -> Parser RawField a
PBDec.one Parser RawPrimitive Int64
PBDec.int64 0
instance ProtoBridgeOneFieldValue w sch ('TPrimitive Int64) where
  protoToOneFieldValue :: Parser RawPrimitive (FieldValue w sch ('TPrimitive Int64))
protoToOneFieldValue = Int64 -> FieldValue w sch ('TPrimitive Int64)
forall typeName fieldName t1 (w :: * -> *)
       (sch :: Schema typeName fieldName).
t1 -> FieldValue w sch ('TPrimitive t1)
FPrimitive (Int64 -> FieldValue w sch ('TPrimitive Int64))
-> Parser RawPrimitive Int64
-> Parser RawPrimitive (FieldValue w sch ('TPrimitive Int64))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser RawPrimitive Int64
PBDec.int64

-- WARNING! These instances may go out of bounds
instance ProtoBridgeFieldValue w sch ('TPrimitive Integer) where
  fieldValueToProto :: FieldNumber
-> FieldValue w sch ('TPrimitive Integer) -> MessageBuilder
fieldValueToProto fid :: FieldNumber
fid (FPrimitive n :: t1
n) = FieldNumber -> Int64 -> MessageBuilder
PBEnc.int64 FieldNumber
fid (Integer -> Int64
forall a. Num a => Integer -> a
fromInteger t1
Integer
n)
  protoToFieldValue :: Parser RawField (FieldValue w sch ('TPrimitive Integer))
protoToFieldValue = Integer -> FieldValue w sch ('TPrimitive Integer)
forall typeName fieldName t1 (w :: * -> *)
       (sch :: Schema typeName fieldName).
t1 -> FieldValue w sch ('TPrimitive t1)
FPrimitive (Integer -> FieldValue w sch ('TPrimitive Integer))
-> (Int64 -> Integer)
-> Int64
-> FieldValue w sch ('TPrimitive Integer)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int64 -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int64 -> FieldValue w sch ('TPrimitive Integer))
-> Parser RawField Int64
-> Parser RawField (FieldValue w sch ('TPrimitive Integer))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser RawPrimitive Int64 -> Int64 -> Parser RawField Int64
forall a. Parser RawPrimitive a -> a -> Parser RawField a
PBDec.one Parser RawPrimitive Int64
PBDec.int64 0
instance ProtoBridgeOneFieldValue w sch ('TPrimitive Integer) where
  protoToOneFieldValue :: Parser RawPrimitive (FieldValue w sch ('TPrimitive Integer))
protoToOneFieldValue = Integer -> FieldValue w sch ('TPrimitive Integer)
forall typeName fieldName t1 (w :: * -> *)
       (sch :: Schema typeName fieldName).
t1 -> FieldValue w sch ('TPrimitive t1)
FPrimitive (Integer -> FieldValue w sch ('TPrimitive Integer))
-> (Int64 -> Integer)
-> Int64
-> FieldValue w sch ('TPrimitive Integer)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int64 -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int64 -> FieldValue w sch ('TPrimitive Integer))
-> Parser RawPrimitive Int64
-> Parser RawPrimitive (FieldValue w sch ('TPrimitive Integer))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser RawPrimitive Int64
PBDec.int64

instance ProtoBridgeFieldValue w sch ('TPrimitive Float) where
  fieldValueToProto :: FieldNumber
-> FieldValue w sch ('TPrimitive Float) -> MessageBuilder
fieldValueToProto fid :: FieldNumber
fid (FPrimitive n :: t1
n) = FieldNumber -> Float -> MessageBuilder
PBEnc.float FieldNumber
fid t1
Float
n
  protoToFieldValue :: Parser RawField (FieldValue w sch ('TPrimitive Float))
protoToFieldValue = Float -> FieldValue w sch ('TPrimitive Float)
forall typeName fieldName t1 (w :: * -> *)
       (sch :: Schema typeName fieldName).
t1 -> FieldValue w sch ('TPrimitive t1)
FPrimitive (Float -> FieldValue w sch ('TPrimitive Float))
-> Parser RawField Float
-> Parser RawField (FieldValue w sch ('TPrimitive Float))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser RawPrimitive Float -> Float -> Parser RawField Float
forall a. Parser RawPrimitive a -> a -> Parser RawField a
PBDec.one Parser RawPrimitive Float
PBDec.float 0
instance ProtoBridgeOneFieldValue w sch ('TPrimitive Float) where
  protoToOneFieldValue :: Parser RawPrimitive (FieldValue w sch ('TPrimitive Float))
protoToOneFieldValue = Float -> FieldValue w sch ('TPrimitive Float)
forall typeName fieldName t1 (w :: * -> *)
       (sch :: Schema typeName fieldName).
t1 -> FieldValue w sch ('TPrimitive t1)
FPrimitive (Float -> FieldValue w sch ('TPrimitive Float))
-> Parser RawPrimitive Float
-> Parser RawPrimitive (FieldValue w sch ('TPrimitive Float))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser RawPrimitive Float
PBDec.float

instance ProtoBridgeFieldValue w sch ('TPrimitive Double) where
  fieldValueToProto :: FieldNumber
-> FieldValue w sch ('TPrimitive Double) -> MessageBuilder
fieldValueToProto fid :: FieldNumber
fid (FPrimitive n :: t1
n) = FieldNumber -> Double -> MessageBuilder
PBEnc.double FieldNumber
fid t1
Double
n
  protoToFieldValue :: Parser RawField (FieldValue w sch ('TPrimitive Double))
protoToFieldValue = Double -> FieldValue w sch ('TPrimitive Double)
forall typeName fieldName t1 (w :: * -> *)
       (sch :: Schema typeName fieldName).
t1 -> FieldValue w sch ('TPrimitive t1)
FPrimitive (Double -> FieldValue w sch ('TPrimitive Double))
-> Parser RawField Double
-> Parser RawField (FieldValue w sch ('TPrimitive Double))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser RawPrimitive Double -> Double -> Parser RawField Double
forall a. Parser RawPrimitive a -> a -> Parser RawField a
PBDec.one Parser RawPrimitive Double
PBDec.double 0
instance ProtoBridgeOneFieldValue w sch ('TPrimitive Double) where
  protoToOneFieldValue :: Parser RawPrimitive (FieldValue w sch ('TPrimitive Double))
protoToOneFieldValue = Double -> FieldValue w sch ('TPrimitive Double)
forall typeName fieldName t1 (w :: * -> *)
       (sch :: Schema typeName fieldName).
t1 -> FieldValue w sch ('TPrimitive t1)
FPrimitive (Double -> FieldValue w sch ('TPrimitive Double))
-> Parser RawPrimitive Double
-> Parser RawPrimitive (FieldValue w sch ('TPrimitive Double))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser RawPrimitive Double
PBDec.double

instance ProtoBridgeFieldValue w sch ('TPrimitive Bool) where
  fieldValueToProto :: FieldNumber
-> FieldValue w sch ('TPrimitive Bool) -> MessageBuilder
fieldValueToProto fid :: FieldNumber
fid (FPrimitive n :: t1
n) = FieldNumber -> t1 -> MessageBuilder
forall e. ProtoEnum e => FieldNumber -> e -> MessageBuilder
PBEnc.enum FieldNumber
fid t1
n
  protoToFieldValue :: Parser RawField (FieldValue w sch ('TPrimitive Bool))
protoToFieldValue = Bool -> FieldValue w sch ('TPrimitive Bool)
forall typeName fieldName t1 (w :: * -> *)
       (sch :: Schema typeName fieldName).
t1 -> FieldValue w sch ('TPrimitive t1)
FPrimitive (Bool -> FieldValue w sch ('TPrimitive Bool))
-> Parser RawField Bool
-> Parser RawField (FieldValue w sch ('TPrimitive Bool))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser RawPrimitive Bool -> Bool -> Parser RawField Bool
forall a. Parser RawPrimitive a -> a -> Parser RawField a
PBDec.one Parser RawPrimitive Bool
PBDec.bool Bool
False
instance ProtoBridgeOneFieldValue w sch ('TPrimitive Bool) where
  protoToOneFieldValue :: Parser RawPrimitive (FieldValue w sch ('TPrimitive Bool))
protoToOneFieldValue = Bool -> FieldValue w sch ('TPrimitive Bool)
forall typeName fieldName t1 (w :: * -> *)
       (sch :: Schema typeName fieldName).
t1 -> FieldValue w sch ('TPrimitive t1)
FPrimitive (Bool -> FieldValue w sch ('TPrimitive Bool))
-> Parser RawPrimitive Bool
-> Parser RawPrimitive (FieldValue w sch ('TPrimitive Bool))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser RawPrimitive Bool
PBDec.bool

instance ProtoBridgeFieldValue w sch ('TPrimitive T.Text) where
  fieldValueToProto :: FieldNumber
-> FieldValue w sch ('TPrimitive Text) -> MessageBuilder
fieldValueToProto fid :: FieldNumber
fid (FPrimitive n :: t1
n) = FieldNumber -> Text -> MessageBuilder
PBEnc.text FieldNumber
fid (Text -> Text
LT.fromStrict t1
Text
n)
  protoToFieldValue :: Parser RawField (FieldValue w sch ('TPrimitive Text))
protoToFieldValue = Text -> FieldValue w sch ('TPrimitive Text)
forall typeName fieldName t1 (w :: * -> *)
       (sch :: Schema typeName fieldName).
t1 -> FieldValue w sch ('TPrimitive t1)
FPrimitive (Text -> FieldValue w sch ('TPrimitive Text))
-> (Text -> Text) -> Text -> FieldValue w sch ('TPrimitive Text)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Text
LT.toStrict (Text -> FieldValue w sch ('TPrimitive Text))
-> Parser RawField Text
-> Parser RawField (FieldValue w sch ('TPrimitive Text))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser RawPrimitive Text -> Text -> Parser RawField Text
forall a. Parser RawPrimitive a -> a -> Parser RawField a
PBDec.one Parser RawPrimitive Text
PBDec.text ""
instance ProtoBridgeOneFieldValue w sch ('TPrimitive T.Text) where
  protoToOneFieldValue :: Parser RawPrimitive (FieldValue w sch ('TPrimitive Text))
protoToOneFieldValue = Text -> FieldValue w sch ('TPrimitive Text)
forall typeName fieldName t1 (w :: * -> *)
       (sch :: Schema typeName fieldName).
t1 -> FieldValue w sch ('TPrimitive t1)
FPrimitive (Text -> FieldValue w sch ('TPrimitive Text))
-> (Text -> Text) -> Text -> FieldValue w sch ('TPrimitive Text)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Text
LT.toStrict (Text -> FieldValue w sch ('TPrimitive Text))
-> Parser RawPrimitive Text
-> Parser RawPrimitive (FieldValue w sch ('TPrimitive Text))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser RawPrimitive Text
PBDec.text

instance ProtoBridgeFieldValue w sch ('TPrimitive LT.Text) where
  fieldValueToProto :: FieldNumber
-> FieldValue w sch ('TPrimitive Text) -> MessageBuilder
fieldValueToProto fid :: FieldNumber
fid (FPrimitive n :: t1
n) = FieldNumber -> Text -> MessageBuilder
PBEnc.text FieldNumber
fid t1
Text
n
  protoToFieldValue :: Parser RawField (FieldValue w sch ('TPrimitive Text))
protoToFieldValue = Text -> FieldValue w sch ('TPrimitive Text)
forall typeName fieldName t1 (w :: * -> *)
       (sch :: Schema typeName fieldName).
t1 -> FieldValue w sch ('TPrimitive t1)
FPrimitive (Text -> FieldValue w sch ('TPrimitive Text))
-> Parser RawField Text
-> Parser RawField (FieldValue w sch ('TPrimitive Text))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser RawPrimitive Text -> Text -> Parser RawField Text
forall a. Parser RawPrimitive a -> a -> Parser RawField a
PBDec.one Parser RawPrimitive Text
PBDec.text ""
instance ProtoBridgeOneFieldValue w sch ('TPrimitive LT.Text) where
  protoToOneFieldValue :: Parser RawPrimitive (FieldValue w sch ('TPrimitive Text))
protoToOneFieldValue = Text -> FieldValue w sch ('TPrimitive Text)
forall typeName fieldName t1 (w :: * -> *)
       (sch :: Schema typeName fieldName).
t1 -> FieldValue w sch ('TPrimitive t1)
FPrimitive (Text -> FieldValue w sch ('TPrimitive Text))
-> Parser RawPrimitive Text
-> Parser RawPrimitive (FieldValue w sch ('TPrimitive Text))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser RawPrimitive Text
PBDec.text

instance ProtoBridgeFieldValue w sch ('TPrimitive BS.ByteString) where
  fieldValueToProto :: FieldNumber
-> FieldValue w sch ('TPrimitive ByteString) -> MessageBuilder
fieldValueToProto fid :: FieldNumber
fid (FPrimitive n :: t1
n) = FieldNumber -> ByteString -> MessageBuilder
PBEnc.byteString FieldNumber
fid t1
ByteString
n
  protoToFieldValue :: Parser RawField (FieldValue w sch ('TPrimitive ByteString))
protoToFieldValue = ByteString -> FieldValue w sch ('TPrimitive ByteString)
forall typeName fieldName t1 (w :: * -> *)
       (sch :: Schema typeName fieldName).
t1 -> FieldValue w sch ('TPrimitive t1)
FPrimitive (ByteString -> FieldValue w sch ('TPrimitive ByteString))
-> Parser RawField ByteString
-> Parser RawField (FieldValue w sch ('TPrimitive ByteString))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser RawPrimitive ByteString
-> ByteString -> Parser RawField ByteString
forall a. Parser RawPrimitive a -> a -> Parser RawField a
PBDec.one Parser RawPrimitive ByteString
PBDec.byteString ""
instance ProtoBridgeOneFieldValue w sch ('TPrimitive BS.ByteString) where
  protoToOneFieldValue :: Parser RawPrimitive (FieldValue w sch ('TPrimitive ByteString))
protoToOneFieldValue = ByteString -> FieldValue w sch ('TPrimitive ByteString)
forall typeName fieldName t1 (w :: * -> *)
       (sch :: Schema typeName fieldName).
t1 -> FieldValue w sch ('TPrimitive t1)
FPrimitive (ByteString -> FieldValue w sch ('TPrimitive ByteString))
-> Parser RawPrimitive ByteString
-> Parser RawPrimitive (FieldValue w sch ('TPrimitive ByteString))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser RawPrimitive ByteString
PBDec.byteString

-- Note that Maybes and Lists require that we recur on the OneFieldValue class

instance (ProtoBridgeFieldValue w sch t, ProtoBridgeOneFieldValue w sch t)
         => ProtoBridgeFieldValue w sch ('TOption t) where
  fieldValueToProto :: FieldNumber -> FieldValue w sch ('TOption t) -> MessageBuilder
fieldValueToProto _   (FOption Nothing)  = MessageBuilder
forall a. Monoid a => a
mempty
  fieldValueToProto fid :: FieldNumber
fid (FOption (Just v :: FieldValue w sch t1
v)) = FieldNumber -> FieldValue w sch t1 -> MessageBuilder
forall tn fn (w :: * -> *) (sch :: Schema tn fn)
       (t :: FieldType tn).
ProtoBridgeFieldValue w sch t =>
FieldNumber -> FieldValue w sch t -> MessageBuilder
fieldValueToProto FieldNumber
fid FieldValue w sch t1
v
  protoToFieldValue :: Parser RawField (FieldValue w sch ('TOption t))
protoToFieldValue = Maybe (FieldValue w sch t) -> FieldValue w sch ('TOption t)
forall typeName fieldName (w :: * -> *)
       (sch :: Schema typeName fieldName) (t1 :: FieldType typeName).
Maybe (FieldValue w sch t1) -> FieldValue w sch ('TOption t1)
FOption (Maybe (FieldValue w sch t) -> FieldValue w sch ('TOption t))
-> Parser RawField (Maybe (FieldValue w sch t))
-> Parser RawField (FieldValue w sch ('TOption t))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser RawPrimitive (Maybe (FieldValue w sch t))
-> Maybe (FieldValue w sch t)
-> Parser RawField (Maybe (FieldValue w sch t))
forall a. Parser RawPrimitive a -> a -> Parser RawField a
PBDec.one (FieldValue w sch t -> Maybe (FieldValue w sch t)
forall a. a -> Maybe a
Just (FieldValue w sch t -> Maybe (FieldValue w sch t))
-> Parser RawPrimitive (FieldValue w sch t)
-> Parser RawPrimitive (Maybe (FieldValue w sch t))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser RawPrimitive (FieldValue w sch t)
forall tn fn (w :: * -> *) (sch :: Schema tn fn)
       (t :: FieldType tn).
ProtoBridgeOneFieldValue w sch t =>
Parser RawPrimitive (FieldValue w sch t)
protoToOneFieldValue) Maybe (FieldValue w sch t)
forall a. Maybe a
Nothing

instance TypeError ('Text "optionals cannot be nested in protobuf")
         => ProtoBridgeOneFieldValue w sch ('TOption t) where
  protoToOneFieldValue :: Parser RawPrimitive (FieldValue w sch ('TOption t))
protoToOneFieldValue = [Char] -> Parser RawPrimitive (FieldValue w sch ('TOption t))
forall a. HasCallStack => [Char] -> a
error "optionals cannot be nested in protobuf"

instance (ProtoBridgeFieldValue w sch t, ProtoBridgeOneFieldValue w sch t)
         => ProtoBridgeFieldValue w sch ('TList t) where
  fieldValueToProto :: FieldNumber -> FieldValue w sch ('TList t) -> MessageBuilder
fieldValueToProto fid :: FieldNumber
fid (FList xs :: [FieldValue w sch t1]
xs) = (FieldValue w sch t1 -> MessageBuilder)
-> [FieldValue w sch t1] -> MessageBuilder
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap (FieldNumber -> FieldValue w sch t1 -> MessageBuilder
forall tn fn (w :: * -> *) (sch :: Schema tn fn)
       (t :: FieldType tn).
ProtoBridgeFieldValue w sch t =>
FieldNumber -> FieldValue w sch t -> MessageBuilder
fieldValueToProto FieldNumber
fid) [FieldValue w sch t1]
xs
  protoToFieldValue :: Parser RawField (FieldValue w sch ('TList t))
protoToFieldValue = [FieldValue w sch t] -> FieldValue w sch ('TList t)
forall typeName fieldName (w :: * -> *)
       (sch :: Schema typeName fieldName) (t1 :: FieldType typeName).
[FieldValue w sch t1] -> FieldValue w sch ('TList t1)
FList ([FieldValue w sch t] -> FieldValue w sch ('TList t))
-> Parser RawField [FieldValue w sch t]
-> Parser RawField (FieldValue w sch ('TList t))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser RawPrimitive (FieldValue w sch t)
-> Parser RawField [FieldValue w sch t]
forall a. Parser RawPrimitive a -> Parser RawField [a]
PBDec.repeated Parser RawPrimitive (FieldValue w sch t)
forall tn fn (w :: * -> *) (sch :: Schema tn fn)
       (t :: FieldType tn).
ProtoBridgeOneFieldValue w sch t =>
Parser RawPrimitive (FieldValue w sch t)
protoToOneFieldValue

instance TypeError ('Text "lists cannot be nested in protobuf")
         => ProtoBridgeOneFieldValue w sch ('TList t) where
  protoToOneFieldValue :: Parser RawPrimitive (FieldValue w sch ('TList t))
protoToOneFieldValue = [Char] -> Parser RawPrimitive (FieldValue w sch ('TList t))
forall a. HasCallStack => [Char] -> a
error "lists cannot be nested in protobuf"

instance TypeError ('Text "maps are not currently supported")
         => ProtoBridgeFieldValue w sch ('TMap k v) where
  fieldValueToProto :: FieldNumber -> FieldValue w sch ('TMap k v) -> MessageBuilder
fieldValueToProto = [Char]
-> FieldNumber -> FieldValue w sch ('TMap k v) -> MessageBuilder
forall a. HasCallStack => [Char] -> a
error "maps are not currently supported"
  protoToFieldValue :: Parser RawField (FieldValue w sch ('TMap k v))
protoToFieldValue = [Char] -> Parser RawField (FieldValue w sch ('TMap k v))
forall a. HasCallStack => [Char] -> a
error "maps are not currently supported"

instance TypeError ('Text "nested unions are not currently supported")
         => ProtoBridgeFieldValue w sch ('TUnion choices) where
  fieldValueToProto :: FieldNumber -> FieldValue w sch ('TUnion choices) -> MessageBuilder
fieldValueToProto = [Char]
-> FieldNumber
-> FieldValue w sch ('TUnion choices)
-> MessageBuilder
forall a. HasCallStack => [Char] -> a
error "nested unions are not currently supported"
  protoToFieldValue :: Parser RawField (FieldValue w sch ('TUnion choices))
protoToFieldValue = [Char] -> Parser RawField (FieldValue w sch ('TUnion choices))
forall a. HasCallStack => [Char] -> a
error "nested unions are not currently supported"

-- UNIONS
-- ------

instance ProtoBridgeUnionFieldValue w ids sch '[] where
  unionFieldValueToProto :: NS (FieldValue w sch) '[] -> MessageBuilder
unionFieldValueToProto = [Char] -> NS (FieldValue w sch) '[] -> MessageBuilder
forall a. HasCallStack => [Char] -> a
error "empty list of unions"
  protoToUnionFieldValue :: Parser RawMessage (NS (FieldValue w sch) '[])
protoToUnionFieldValue = (RawMessage -> Either ParseError (NS (FieldValue w sch) '[]))
-> Parser RawMessage (NS (FieldValue w sch) '[])
forall input a. (input -> Either ParseError a) -> Parser input a
PBDec.Parser (\_ -> ParseError -> Either ParseError (NS (FieldValue w sch) '[])
forall a b. a -> Either a b
Left (Text -> ParseError
PBDec.WireTypeError "unknown type in an union"))

instance ( ProtoBridgeFieldValue w sch t, KnownNat thisId
         , ProtoBridgeUnionFieldValue w restIds sch ts )
         => ProtoBridgeUnionFieldValue w (thisId ': restIds) sch (t ': ts) where
  unionFieldValueToProto :: NS (FieldValue w sch) (t : ts) -> MessageBuilder
unionFieldValueToProto (Z v :: FieldValue w sch x
v) = FieldNumber -> FieldValue w sch x -> MessageBuilder
forall tn fn (w :: * -> *) (sch :: Schema tn fn)
       (t :: FieldType tn).
ProtoBridgeFieldValue w sch t =>
FieldNumber -> FieldValue w sch t -> MessageBuilder
fieldValueToProto FieldNumber
fieldId FieldValue w sch x
v
    where fieldId :: FieldNumber
fieldId = Integer -> FieldNumber
forall a. Num a => Integer -> a
fromInteger (Integer -> FieldNumber) -> Integer -> FieldNumber
forall a b. (a -> b) -> a -> b
$ Proxy thisId -> Integer
forall (n :: Nat) (proxy :: Nat -> *).
KnownNat n =>
proxy n -> Integer
natVal (Proxy thisId
forall k (t :: k). Proxy t
Proxy @thisId)
  unionFieldValueToProto (S v :: NS (FieldValue w sch) xs
v) = NS (FieldValue w sch) xs -> MessageBuilder
forall tn fn (w :: * -> *) (ids :: [Nat]) (sch :: Schema tn fn)
       (ts :: [FieldType tn]).
ProtoBridgeUnionFieldValue w ids sch ts =>
NS (FieldValue w sch) ts -> MessageBuilder
unionFieldValueToProto @_ @_ @w @restIds NS (FieldValue w sch) xs
v
  protoToUnionFieldValue :: Parser RawMessage (NS (FieldValue w sch) (t : ts))
protoToUnionFieldValue
    = FieldValue w sch t -> NS (FieldValue w sch) (t : ts)
forall k (a :: k -> *) (x :: k) (xs :: [k]). a x -> NS a (x : xs)
Z (FieldValue w sch t -> NS (FieldValue w sch) (t : ts))
-> Parser RawMessage (FieldValue w sch t)
-> Parser RawMessage (NS (FieldValue w sch) (t : ts))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser RawField (FieldValue w sch t)
forall tn fn (w :: * -> *) (sch :: Schema tn fn)
       (t :: FieldType tn).
ProtoBridgeFieldValue w sch t =>
Parser RawField (FieldValue w sch t)
protoToFieldValue Parser RawField (FieldValue w sch t)
-> FieldNumber -> Parser RawMessage (FieldValue w sch t)
forall a. Parser RawField a -> FieldNumber -> Parser RawMessage a
`at` FieldNumber
fieldId Parser RawMessage (NS (FieldValue w sch) (t : ts))
-> Parser RawMessage (NS (FieldValue w sch) (t : ts))
-> Parser RawMessage (NS (FieldValue w sch) (t : ts))
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> NS (FieldValue w sch) ts -> NS (FieldValue w sch) (t : ts)
forall k (a :: k -> *) (xs :: [k]) (x :: k).
NS a xs -> NS a (x : xs)
S (NS (FieldValue w sch) ts -> NS (FieldValue w sch) (t : ts))
-> Parser RawMessage (NS (FieldValue w sch) ts)
-> Parser RawMessage (NS (FieldValue w sch) (t : ts))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (sch :: Schema tn fn) (ts :: [FieldType tn]).
ProtoBridgeUnionFieldValue w restIds sch ts =>
Parser RawMessage (NS (FieldValue w sch) ts)
forall tn fn (w :: * -> *) (ids :: [Nat]) (sch :: Schema tn fn)
       (ts :: [FieldType tn]).
ProtoBridgeUnionFieldValue w ids sch ts =>
Parser RawMessage (NS (FieldValue w sch) ts)
protoToUnionFieldValue @_ @_ @w @restIds
    where fieldId :: FieldNumber
fieldId = Integer -> FieldNumber
forall a. Num a => Integer -> a
fromInteger (Integer -> FieldNumber) -> Integer -> FieldNumber
forall a b. (a -> b) -> a -> b
$ Proxy thisId -> Integer
forall (n :: Nat) (proxy :: Nat -> *).
KnownNat n =>
proxy n -> Integer
natVal (Proxy thisId
forall k (t :: k). Proxy t
Proxy @thisId)