{-# 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  #-}
{-# 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(..)
, ProtoBufOptionConstant(..)
  -- * 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.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
    --   and whether it should be packed (only used for lists of number-like values)
    ProtoBufId Nat [(Symbol, ProtoBufOptionConstant)]
    -- | List of identifiers for fields which contain a union
  | ProtoBufOneOfIds [Nat]

-- Values for constants
data ProtoBufOptionConstant
  = ProtoBufOptionConstantInt    Nat
  | ProtoBufOptionConstantBool   Bool
  | ProtoBufOptionConstantObject [(Symbol, ProtoBufOptionConstant)]
  | ProtoBufOptionConstantOther  Symbol

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 opts) = n
  FindProtoBufId' t f other
    = TypeError ('Text "protocol buffers id not available for field "
                 ':<>: 'ShowType t ':<>: 'Text "/" ':<>: 'ShowType f)

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

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

type family FindProtoBufPacked'' (t :: tn) (f :: fn) (opts :: [(Symbol, ProtoBufOptionConstant)]) :: Bool where
  FindProtoBufPacked'' t f '[] = 'True  -- by default we are packed
  FindProtoBufPacked'' t f ( '("packed", 'ProtoBufOptionConstantBool b) ': rest )
    = b  -- found!
  FindProtoBufPacked'' t f ( '("packed", other) ': rest )
    = TypeError ('Text "non-boolean value for 'packed' for field "
                 ':<>: 'ShowType t ':<>: 'Text "/" ':<>: 'ShowType f)
  FindProtoBufPacked'' t f ( other ': rest)
    = FindProtoBufPacked'' t f rest

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 sch (sch :/: sty) => IsProtoSchema sch sty
instance ProtoBridgeTerm sch (sch :/: sty) => IsProtoSchema 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 sch sty, ToSchema sch sty a)
                 => a -> PBEnc.MessageBuilder
toProtoViaSchema :: a -> MessageBuilder
toProtoViaSchema = Term sch (sch :/: sty) -> MessageBuilder
forall tn fn (sch :: Schema tn fn) (t :: TypeDef tn fn).
ProtoBridgeTerm sch t =>
Term sch t -> MessageBuilder
termToProto (Term sch (sch :/: sty) -> MessageBuilder)
-> (a -> Term sch (sch :/: sty)) -> a -> MessageBuilder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t (sty :: t).
ToSchema sch sty t =>
t -> Term sch (sch :/: sty)
forall fn tn (sch :: Schema tn fn) t (sty :: tn).
ToSchema sch sty t =>
t -> Term sch (sch :/: sty)
toSchema' @_ @_ @sch

-- | 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 sch sty, FromSchema sch sty a)
                   => PBDec.Parser PBDec.RawMessage a
fromProtoViaSchema :: Parser RawMessage a
fromProtoViaSchema = forall t (sty :: t).
FromSchema sch sty t =>
Term sch (sch :/: sty) -> t
forall fn tn (sch :: Schema tn fn) t (sty :: tn).
FromSchema sch sty t =>
Term sch (sch :/: sty) -> t
fromSchema' @_ @_ @sch (Term sch (sch :/: sty) -> a)
-> Parser RawMessage (Term sch (sch :/: sty))
-> Parser RawMessage a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser RawMessage (Term sch (sch :/: sty))
forall tn fn (sch :: Schema tn fn) (t :: TypeDef tn fn).
ProtoBridgeTerm sch t =>
Parser RawMessage (Term 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 sch sty, FromSchema 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 sch sty, FromSchema sch sty a) =>
Parser RawMessage a
forall t f (sch :: Schema t f) a (sty :: t).
(IsProtoSchema sch sty, FromSchema 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' Proxy '[]
_ = (RawMessage -> Either ParseError t) -> Parser RawMessage t
forall input a. (input -> Either ParseError a) -> Parser input a
PBDec.Parser (\RawMessage
_ -> ParseError -> Either ParseError t
forall a b. a -> Either a b
Left (Text -> ParseError
PBDec.WireTypeError Text
"no schema found in registry"))
instance (IsProtoSchema s sty, FromSchema s sty t, FromProtoBufRegistry ms t)
         => FromProtoBufRegistry ( (n ':-> s) ': ms) t where
  fromProtoBufRegistry' :: Proxy ((n ':-> s) : ms) -> Parser RawMessage t
fromProtoBufRegistry' Proxy ((n ':-> s) : ms)
_ = forall t f (sch :: Schema t f) a (sty :: t).
(IsProtoSchema sch sty, FromSchema sch sty a) =>
Parser RawMessage a
forall a (sty :: Symbol).
(IsProtoSchema s sty, FromSchema 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 (\i
_ -> ParseError -> Either ParseError a
forall a b. a -> Either a b
Left (Text -> ParseError
PBDec.WireTypeError Text
"cannot parse"))
  PBDec.Parser i -> Either ParseError a
x <|> :: Parser i a -> Parser i a -> Parser i a
<|> PBDec.Parser 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 -> case i -> Either ParseError a
x i
i of
                             Left ParseError
_      -> i -> Either ParseError a
y i
i
                             r :: Either ParseError a
r@(Right a
_) -> Either ParseError a
r

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

-- Embedded terms
class ProtoBridgeEmbedTerm (sch :: Schema tn fn) (t :: TypeDef tn fn) where
  termToEmbedProto :: FieldNumber -> Term sch t -> PBEnc.MessageBuilder
  embedProtoToOneFieldValue :: PBDec.Parser PBDec.RawPrimitive (Term sch t)
  -- support for packed encodings
  -- https://developers.google.com/protocol-buffers/docs/encoding#packed
  supportsPackingTerm :: Proxy (Term sch t) -> Bool
  termToPackedEmbedProto :: FieldNumber -> [Term sch t] -> PBEnc.MessageBuilder
  embedProtoToPackedFieldValue :: PBDec.Parser PBDec.RawPrimitive [Term sch t]

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

class ProtoBridgeOneFieldValue (sch :: Schema tn fn) (t :: FieldType tn) where
  defaultOneFieldValue :: Maybe (FieldValue sch t)
  oneFieldValueToProto :: FieldNumber -> FieldValue sch t -> PBEnc.MessageBuilder
  protoToOneFieldValue :: PBDec.Parser PBDec.RawPrimitive (FieldValue sch t)
  -- support for packed encodings
  -- https://developers.google.com/protocol-buffers/docs/encoding#packed
  supportsPacking         :: Proxy (FieldValue sch t) -> Bool
  packedFieldValueToProto :: FieldNumber -> [FieldValue sch t] -> PBEnc.MessageBuilder
  protoToPackedFieldValue :: PBDec.Parser PBDec.RawPrimitive [FieldValue sch t]

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

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

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

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

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

instance ProtoBridgeTerm sch ('DRecord name args)
         => ProtoBridgeEmbedTerm sch ('DRecord name args) where
  termToEmbedProto :: FieldNumber -> Term sch ('DRecord name args) -> MessageBuilder
termToEmbedProto FieldNumber
fid Term sch ('DRecord name args)
v = FieldNumber -> MessageBuilder -> MessageBuilder
PBEnc.embedded FieldNumber
fid (Term sch ('DRecord name args) -> MessageBuilder
forall tn fn (sch :: Schema tn fn) (t :: TypeDef tn fn).
ProtoBridgeTerm sch t =>
Term sch t -> MessageBuilder
termToProto Term sch ('DRecord name args)
v)
  embedProtoToOneFieldValue :: Parser RawPrimitive (Term sch ('DRecord name args))
embedProtoToOneFieldValue = Parser RawMessage (Term sch ('DRecord name args))
-> Parser RawPrimitive (Term sch ('DRecord name args))
forall a. Parser RawMessage a -> Parser RawPrimitive a
PBDec.embedded' (ProtoBridgeTerm sch ('DRecord name args) =>
Parser RawMessage (Term sch ('DRecord name args))
forall tn fn (sch :: Schema tn fn) (t :: TypeDef tn fn).
ProtoBridgeTerm sch t =>
Parser RawMessage (Term sch t)
protoToTerm @_ @_ @sch @('DRecord name args))
  supportsPackingTerm :: Proxy (Term sch ('DRecord name args)) -> Bool
supportsPackingTerm Proxy (Term sch ('DRecord name args))
_ = Bool
False
  termToPackedEmbedProto :: FieldNumber -> [Term sch ('DRecord name args)] -> MessageBuilder
termToPackedEmbedProto = [Char]
-> FieldNumber -> [Term sch ('DRecord name args)] -> MessageBuilder
forall a. HasCallStack => [Char] -> a
error [Char]
"this is a bug, since we declare we do not support packed encoding"
  embedProtoToPackedFieldValue :: Parser RawPrimitive [Term sch ('DRecord name args)]
embedProtoToPackedFieldValue = [Char] -> Parser RawPrimitive [Term sch ('DRecord name args)]
forall a. HasCallStack => [Char] -> a
error [Char]
"this is a bug, since we declare we do not support packed encoding"

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

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

instance ProtoBridgeEnum sch name choices
         => ProtoBridgeEmbedTerm sch ('DEnum name choices) where
  termToEmbedProto :: FieldNumber -> Term sch ('DEnum name choices) -> MessageBuilder
termToEmbedProto FieldNumber
fid (TEnum NS Proxy choices
v) = FieldNumber -> Int32 -> MessageBuilder
PBEnc.int32 FieldNumber
fid (NS Proxy choices -> Int32
forall tn fn (sch :: Schema tn fn) (ty :: tn)
       (choices :: [ChoiceDef fn]) a.
(ProtoBridgeEnum sch ty choices, Integral a) =>
NS Proxy choices -> a
enumToProto @_ @_ @sch @name NS Proxy choices
v)
  embedProtoToOneFieldValue :: Parser RawPrimitive (Term sch ('DEnum name choices))
embedProtoToOneFieldValue = Parser RawPrimitive Int32
PBDec.int32 Parser RawPrimitive Int32
-> (Int32 -> Parser RawPrimitive (Term sch ('DEnum name choices)))
-> Parser RawPrimitive (Term sch ('DEnum name choices))
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (NS Proxy choices -> Term sch ('DEnum name choices))
-> Parser RawPrimitive (NS Proxy choices)
-> Parser RawPrimitive (Term sch ('DEnum name choices))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap NS Proxy choices -> Term sch ('DEnum name choices)
forall fieldName typeName (choices :: [ChoiceDef fieldName])
       (sch :: Schema typeName fieldName) (name :: typeName).
NS Proxy choices -> Term sch ('DEnum name choices)
TEnum (Parser RawPrimitive (NS Proxy choices)
 -> Parser RawPrimitive (Term sch ('DEnum name choices)))
-> (Int32 -> Parser RawPrimitive (NS Proxy choices))
-> Int32
-> Parser RawPrimitive (Term sch ('DEnum name choices))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (choices :: [ChoiceDef fieldName]) a.
ProtoBridgeEnum sch name choices =>
Int32 -> Parser a (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
  supportsPackingTerm :: Proxy (Term sch ('DEnum name choices)) -> Bool
supportsPackingTerm Proxy (Term sch ('DEnum name choices))
_ = Bool
True
  termToPackedEmbedProto :: FieldNumber -> [Term sch ('DEnum name choices)] -> MessageBuilder
termToPackedEmbedProto FieldNumber
fid [Term sch ('DEnum name choices)]
ts
    = FieldNumber -> [Word64] -> MessageBuilder
forall (f :: * -> *).
Foldable f =>
FieldNumber -> f Word64 -> MessageBuilder
PBEnc.packedVarints FieldNumber
fid ([Word64] -> MessageBuilder) -> [Word64] -> MessageBuilder
forall a b. (a -> b) -> a -> b
$ (Term sch ('DEnum name choices) -> Word64)
-> [Term sch ('DEnum name choices)] -> [Word64]
forall a b. (a -> b) -> [a] -> [b]
map (\(TEnum NS Proxy choices
v) -> NS Proxy choices -> Word64
forall tn fn (sch :: Schema tn fn) (ty :: tn)
       (choices :: [ChoiceDef fn]) a.
(ProtoBridgeEnum sch ty choices, Integral a) =>
NS Proxy choices -> a
enumToProto @_ @_ @sch @name NS Proxy choices
NS Proxy choices
v) [Term sch ('DEnum name choices)]
ts
  embedProtoToPackedFieldValue :: Parser RawPrimitive [Term sch ('DEnum name choices)]
embedProtoToPackedFieldValue =
    Parser RawPrimitive [Int32]
forall a. Integral a => Parser RawPrimitive [a]
PBDec.packedVarints Parser RawPrimitive [Int32]
-> ([Int32]
    -> Parser RawPrimitive [Term sch ('DEnum name choices)])
-> Parser RawPrimitive [Term sch ('DEnum name choices)]
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Int32 -> Parser RawPrimitive (Term sch ('DEnum name choices)))
-> [Int32] -> Parser RawPrimitive [Term sch ('DEnum name choices)]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse ((NS Proxy choices -> Term sch ('DEnum name choices))
-> Parser RawPrimitive (NS Proxy choices)
-> Parser RawPrimitive (Term sch ('DEnum name choices))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap NS Proxy choices -> Term sch ('DEnum name choices)
forall fieldName typeName (choices :: [ChoiceDef fieldName])
       (sch :: Schema typeName fieldName) (name :: typeName).
NS Proxy choices -> Term sch ('DEnum name choices)
TEnum (Parser RawPrimitive (NS Proxy choices)
 -> Parser RawPrimitive (Term sch ('DEnum name choices)))
-> (Int32 -> Parser RawPrimitive (NS Proxy choices))
-> Int32
-> Parser RawPrimitive (Term sch ('DEnum name choices))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (choices :: [ChoiceDef fieldName]) a.
ProtoBridgeEnum sch name choices =>
Int32 -> Parser a (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)

class ProtoBridgeEnum (sch :: Schema tn fn) (ty :: tn) (choices :: [ChoiceDef fn]) where
  enumToProto :: Integral a => NS Proxy choices -> a
  protoToEnum :: Int32 -> PBDec.Parser a (NS Proxy choices)
instance ProtoBridgeEnum sch ty '[] where
  enumToProto :: NS Proxy '[] -> a
enumToProto = [Char] -> NS Proxy '[] -> a
forall a. HasCallStack => [Char] -> a
error [Char]
"empty enum"
  protoToEnum :: Int32 -> Parser a (NS Proxy '[])
protoToEnum Int32
_ = (a -> Either ParseError (NS Proxy '[])) -> Parser a (NS Proxy '[])
forall input a. (input -> Either ParseError a) -> Parser input a
PBDec.Parser (\a
_ -> ParseError -> Either ParseError (NS Proxy '[])
forall a b. a -> Either a b
Left (Text -> ParseError
PBDec.WireTypeError Text
"unknown enum type"))
instance (KnownNat (FindProtoBufId sch ty c), ProtoBridgeEnum sch ty cs)
         => ProtoBridgeEnum sch ty ('ChoiceDef c ': cs) where
  enumToProto :: NS Proxy ('ChoiceDef c : cs) -> a
enumToProto (Z Proxy x
_) = Integer -> a
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 (S NS Proxy xs
v) = NS Proxy xs -> a
forall tn fn (sch :: Schema tn fn) (ty :: tn)
       (choices :: [ChoiceDef fn]) a.
(ProtoBridgeEnum sch ty choices, Integral a) =>
NS Proxy choices -> a
enumToProto @_ @_ @sch @ty NS Proxy xs
v
  protoToEnum :: Int32 -> Parser a (NS Proxy ('ChoiceDef c : cs))
protoToEnum 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 (f :: * -> *) a. Applicative f => a -> f a
pure (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 sch ('DSimple t) where
  termToProto :: Term sch ('DSimple t) -> MessageBuilder
termToProto = [Char] -> Term sch ('DSimple t) -> MessageBuilder
forall a. HasCallStack => [Char] -> a
error [Char]
"protobuf requires wrapping primitives in a message"
  protoToTerm :: Parser RawMessage (Term sch ('DSimple t))
protoToTerm = [Char] -> Parser RawMessage (Term sch ('DSimple t))
forall a. HasCallStack => [Char] -> a
error [Char]
"protobuf requires wrapping primitives in a message"

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

instance {-# OVERLAPPABLE #-}
         (ProtoBridgeOneFieldValue sch t, KnownNat (FindProtoBufId sch ty name))
         => ProtoBridgeField sch ty ('FieldDef name t) where
  fieldToProto :: Field sch ('FieldDef name t) -> MessageBuilder
fieldToProto (Field FieldValue sch t
v) = FieldNumber -> FieldValue sch t -> MessageBuilder
forall tn fn (sch :: Schema tn fn) (t :: FieldType tn).
ProtoBridgeOneFieldValue sch t =>
FieldNumber -> FieldValue sch t -> MessageBuilder
oneFieldValueToProto FieldNumber
fieldId FieldValue 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))
  protoToField :: Parser RawMessage (Field sch ('FieldDef name t))
protoToField
    = FieldValue sch t -> Field sch ('FieldDef name t)
forall typeName fieldName (sch :: Schema typeName fieldName)
       (t :: FieldType typeName) (name :: fieldName).
FieldValue sch t -> Field sch ('FieldDef name t)
Field (FieldValue sch t -> Field sch ('FieldDef name t))
-> Parser RawMessage (FieldValue sch t)
-> Parser RawMessage (Field sch ('FieldDef name t))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> case Maybe (FieldValue sch t)
forall tn fn (sch :: Schema tn fn) (t :: FieldType tn).
ProtoBridgeOneFieldValue sch t =>
Maybe (FieldValue sch t)
defaultOneFieldValue of
        Maybe (FieldValue sch t)
Nothing -> do Maybe (FieldValue sch t)
r <- Parser RawPrimitive (Maybe (FieldValue sch t))
-> Maybe (FieldValue sch t)
-> Parser RawField (Maybe (FieldValue sch t))
forall a. Parser RawPrimitive a -> a -> Parser RawField a
one (FieldValue sch t -> Maybe (FieldValue sch t)
forall a. a -> Maybe a
Just (FieldValue sch t -> Maybe (FieldValue sch t))
-> Parser RawPrimitive (FieldValue sch t)
-> Parser RawPrimitive (Maybe (FieldValue sch t))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser RawPrimitive (FieldValue sch t)
forall tn fn (sch :: Schema tn fn) (t :: FieldType tn).
ProtoBridgeOneFieldValue sch t =>
Parser RawPrimitive (FieldValue sch t)
protoToOneFieldValue) Maybe (FieldValue sch t)
forall a. Maybe a
Nothing Parser RawField (Maybe (FieldValue sch t))
-> FieldNumber -> Parser RawMessage (Maybe (FieldValue sch t))
forall a. Parser RawField a -> FieldNumber -> Parser RawMessage a
`at` FieldNumber
fieldId
                      Parser RawMessage (FieldValue sch t)
-> (FieldValue sch t -> Parser RawMessage (FieldValue sch t))
-> Maybe (FieldValue sch t)
-> Parser RawMessage (FieldValue sch t)
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Parser RawMessage (FieldValue sch t)
forall (f :: * -> *) a. Alternative f => f a
empty FieldValue sch t -> Parser RawMessage (FieldValue sch t)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe (FieldValue sch t)
r
        Just FieldValue sch t
d  -> Parser RawPrimitive (FieldValue sch t)
-> FieldValue sch t -> Parser RawField (FieldValue sch t)
forall a. Parser RawPrimitive a -> a -> Parser RawField a
one Parser RawPrimitive (FieldValue sch t)
forall tn fn (sch :: Schema tn fn) (t :: FieldType tn).
ProtoBridgeOneFieldValue sch t =>
Parser RawPrimitive (FieldValue sch t)
protoToOneFieldValue FieldValue sch t
d Parser RawField (FieldValue sch t)
-> FieldNumber -> Parser RawMessage (FieldValue sch t)
forall a. Parser RawField a -> FieldNumber -> Parser RawMessage a
`at` FieldNumber
fieldId Parser RawMessage (FieldValue sch t)
-> Parser RawMessage (FieldValue sch t)
-> Parser RawMessage (FieldValue sch t)
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> FieldValue sch t -> Parser RawMessage (FieldValue sch t)
forall (f :: * -> *) a. Applicative f => a -> f a
pure FieldValue sch t
d
    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 #-}
         (ProtoBridgeOneFieldValue sch t, KnownNat (FindProtoBufId sch ty name))
         => ProtoBridgeField sch ty ('FieldDef name ('TOption t)) where
  fieldToProto :: Field sch ('FieldDef name ('TOption t)) -> MessageBuilder
fieldToProto (Field (FOption Maybe (FieldValue sch t1)
Nothing))  = MessageBuilder
forall a. Monoid a => a
mempty
  fieldToProto (Field (FOption (Just FieldValue sch t1
v))) = FieldNumber -> FieldValue sch t1 -> MessageBuilder
forall tn fn (sch :: Schema tn fn) (t :: FieldType tn).
ProtoBridgeOneFieldValue sch t =>
FieldNumber -> FieldValue sch t -> MessageBuilder
oneFieldValueToProto FieldNumber
fieldId FieldValue sch t1
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))
  protoToField :: Parser RawMessage (Field sch ('FieldDef name ('TOption t)))
protoToField = FieldValue sch ('TOption t)
-> Field sch ('FieldDef name ('TOption t))
forall typeName fieldName (sch :: Schema typeName fieldName)
       (t :: FieldType typeName) (name :: fieldName).
FieldValue sch t -> Field sch ('FieldDef name t)
Field (FieldValue sch ('TOption t)
 -> Field sch ('FieldDef name ('TOption t)))
-> (Maybe (FieldValue sch t) -> FieldValue sch ('TOption t))
-> Maybe (FieldValue sch t)
-> Field sch ('FieldDef name ('TOption t))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Maybe (FieldValue sch t) -> FieldValue sch ('TOption t)
forall typeName fieldName (sch :: Schema typeName fieldName)
       (t1 :: FieldType typeName).
Maybe (FieldValue sch t1) -> FieldValue sch ('TOption t1)
FOption (Maybe (FieldValue sch t)
 -> Field sch ('FieldDef name ('TOption t)))
-> Parser RawMessage (Maybe (FieldValue sch t))
-> Parser RawMessage (Field sch ('FieldDef name ('TOption t)))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
                   (Parser RawPrimitive (Maybe (FieldValue sch t))
-> Maybe (FieldValue sch t)
-> Parser RawField (Maybe (FieldValue sch t))
forall a. Parser RawPrimitive a -> a -> Parser RawField a
PBDec.one (FieldValue sch t -> Maybe (FieldValue sch t)
forall a. a -> Maybe a
Just (FieldValue sch t -> Maybe (FieldValue sch t))
-> Parser RawPrimitive (FieldValue sch t)
-> Parser RawPrimitive (Maybe (FieldValue sch t))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser RawPrimitive (FieldValue sch t)
forall tn fn (sch :: Schema tn fn) (t :: FieldType tn).
ProtoBridgeOneFieldValue sch t =>
Parser RawPrimitive (FieldValue sch t)
protoToOneFieldValue) Maybe (FieldValue sch t)
forall a. Maybe a
Nothing Parser RawField (Maybe (FieldValue sch t))
-> FieldNumber -> Parser RawMessage (Maybe (FieldValue sch t))
forall a. Parser RawField a -> FieldNumber -> Parser RawMessage a
`at` FieldNumber
fieldId Parser RawMessage (Maybe (FieldValue sch t))
-> Parser RawMessage (Maybe (FieldValue sch t))
-> Parser RawMessage (Maybe (FieldValue sch t))
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Maybe (FieldValue sch t)
-> Parser RawMessage (Maybe (FieldValue sch t))
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe (FieldValue sch t)
forall a. Maybe a
Nothing)
    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))

class KnownBool (b :: Bool) where
  boolVal :: proxy b -> Bool
instance KnownBool 'True where
  boolVal :: proxy 'True -> Bool
boolVal proxy 'True
_ = Bool
True
instance KnownBool 'False where
  boolVal :: proxy 'False -> Bool
boolVal proxy 'False
_ = Bool
False

instance {-# OVERLAPS #-}
         (ProtoBridgeOneFieldValue sch t, KnownNat (FindProtoBufId sch ty name), KnownBool (FindProtoBufPacked sch ty name))
         => ProtoBridgeField sch ty ('FieldDef name ('TList t)) where
  fieldToProto :: Field sch ('FieldDef name ('TList t)) -> MessageBuilder
fieldToProto (Field (FList [FieldValue sch t1]
xs))
    | Proxy
  (FindProtoBufPacked'
     ty
     name
     (GetFieldAnnotation
        (AnnotatedSchema ProtoBufAnnotation sch) ty name))
-> Bool
forall (b :: Bool) (proxy :: Bool -> *).
KnownBool b =>
proxy b -> Bool
boolVal (Proxy (FindProtoBufPacked sch ty name)
forall k (t :: k). Proxy t
Proxy @(FindProtoBufPacked sch ty name)), Proxy (FieldValue sch t) -> Bool
forall tn fn (sch :: Schema tn fn) (t :: FieldType tn).
ProtoBridgeOneFieldValue sch t =>
Proxy (FieldValue sch t) -> Bool
supportsPacking (Proxy (FieldValue sch t)
forall k (t :: k). Proxy t
Proxy @(FieldValue sch t))
    = FieldNumber -> [FieldValue sch t1] -> MessageBuilder
forall tn fn (sch :: Schema tn fn) (t :: FieldType tn).
ProtoBridgeOneFieldValue sch t =>
FieldNumber -> [FieldValue sch t] -> MessageBuilder
packedFieldValueToProto FieldNumber
fieldId [FieldValue sch t1]
xs
    | Bool
otherwise
    = (FieldValue sch t1 -> MessageBuilder)
-> [FieldValue sch t1] -> MessageBuilder
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap (FieldNumber -> FieldValue sch t1 -> MessageBuilder
forall tn fn (sch :: Schema tn fn) (t :: FieldType tn).
ProtoBridgeOneFieldValue sch t =>
FieldNumber -> FieldValue sch t -> MessageBuilder
oneFieldValueToProto FieldNumber
fieldId) [FieldValue sch t1]
xs
    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))
  protoToField :: Parser RawMessage (Field sch ('FieldDef name ('TList t)))
protoToField = FieldValue sch ('TList t) -> Field sch ('FieldDef name ('TList t))
forall typeName fieldName (sch :: Schema typeName fieldName)
       (t :: FieldType typeName) (name :: fieldName).
FieldValue sch t -> Field sch ('FieldDef name t)
Field (FieldValue sch ('TList t)
 -> Field sch ('FieldDef name ('TList t)))
-> ([FieldValue sch t] -> FieldValue sch ('TList t))
-> [FieldValue sch t]
-> Field sch ('FieldDef name ('TList t))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [FieldValue sch t] -> FieldValue sch ('TList t)
forall typeName fieldName (sch :: Schema typeName fieldName)
       (t1 :: FieldType typeName).
[FieldValue sch t1] -> FieldValue sch ('TList t1)
FList ([FieldValue sch t] -> Field sch ('FieldDef name ('TList t)))
-> Parser RawMessage [FieldValue sch t]
-> Parser RawMessage (Field sch ('FieldDef name ('TList t)))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser RawMessage [FieldValue sch t]
go
    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))
          base :: Parser RawMessage [FieldValue sch t]
base = Parser RawPrimitive (FieldValue sch t)
-> Parser RawField [FieldValue sch t]
forall a. Parser RawPrimitive a -> Parser RawField [a]
PBDec.repeated Parser RawPrimitive (FieldValue sch t)
forall tn fn (sch :: Schema tn fn) (t :: FieldType tn).
ProtoBridgeOneFieldValue sch t =>
Parser RawPrimitive (FieldValue sch t)
protoToOneFieldValue Parser RawField [FieldValue sch t]
-> FieldNumber -> Parser RawMessage [FieldValue sch t]
forall a. Parser RawField a -> FieldNumber -> Parser RawMessage a
`at` FieldNumber
fieldId Parser RawMessage [FieldValue sch t]
-> Parser RawMessage [FieldValue sch t]
-> Parser RawMessage [FieldValue sch t]
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> [FieldValue sch t] -> Parser RawMessage [FieldValue sch t]
forall (f :: * -> *) a. Applicative f => a -> f a
pure []
          go :: Parser RawMessage [FieldValue sch t]
go | Proxy (FieldValue sch t) -> Bool
forall tn fn (sch :: Schema tn fn) (t :: FieldType tn).
ProtoBridgeOneFieldValue sch t =>
Proxy (FieldValue sch t) -> Bool
supportsPacking (Proxy (FieldValue sch t)
forall k (t :: k). Proxy t
Proxy @(FieldValue sch t))
             = Parser RawPrimitive [FieldValue sch t]
-> [FieldValue sch t] -> Parser RawField [FieldValue sch t]
forall a. Parser RawPrimitive a -> a -> Parser RawField a
PBDec.one Parser RawPrimitive [FieldValue sch t]
forall tn fn (sch :: Schema tn fn) (t :: FieldType tn).
ProtoBridgeOneFieldValue sch t =>
Parser RawPrimitive [FieldValue sch t]
protoToPackedFieldValue [] Parser RawField [FieldValue sch t]
-> FieldNumber -> Parser RawMessage [FieldValue sch t]
forall a. Parser RawField a -> FieldNumber -> Parser RawMessage a
`at` FieldNumber
fieldId Parser RawMessage [FieldValue sch t]
-> Parser RawMessage [FieldValue sch t]
-> Parser RawMessage [FieldValue sch t]
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Parser RawMessage [FieldValue sch t]
base
             | Bool
otherwise
             = Parser RawMessage [FieldValue sch t]
base

instance TypeError ('Text "maps are not currently supported")
         => ProtoBridgeField sch ty ('FieldDef name ('TMap k v)) where
  fieldToProto :: Field sch ('FieldDef name ('TMap k v)) -> MessageBuilder
fieldToProto = [Char] -> Field sch ('FieldDef name ('TMap k v)) -> MessageBuilder
forall a. HasCallStack => [Char] -> a
error [Char]
"maps are not currently supported"
  protoToField :: Parser RawMessage (Field sch ('FieldDef name ('TMap k v)))
protoToField = [Char]
-> Parser RawMessage (Field sch ('FieldDef name ('TMap k v)))
forall a. HasCallStack => [Char] -> a
error [Char]
"maps are not currently supported"

instance {-# OVERLAPS #-}
         (ProtoBridgeUnionFieldValue (FindProtoBufOneOfIds sch ty name) sch ts)
         => ProtoBridgeField sch ty ('FieldDef name ('TUnion ts)) where
  fieldToProto :: Field sch ('FieldDef name ('TUnion ts)) -> MessageBuilder
fieldToProto (Field (FUnion NS (FieldValue sch) choices
v))
    = NS (FieldValue sch) choices -> MessageBuilder
forall tn fn (ids :: [Nat]) (sch :: Schema tn fn)
       (ts :: [FieldType tn]).
ProtoBridgeUnionFieldValue ids sch ts =>
NS (FieldValue sch) ts -> MessageBuilder
unionFieldValueToProto @_ @_ @(FindProtoBufOneOfIds sch ty name) NS (FieldValue sch) choices
v
  protoToField :: Parser RawMessage (Field sch ('FieldDef name ('TUnion ts)))
protoToField
    = FieldValue sch ('TUnion ts)
-> Field sch ('FieldDef name ('TUnion ts))
forall typeName fieldName (sch :: Schema typeName fieldName)
       (t :: FieldType typeName) (name :: fieldName).
FieldValue sch t -> Field sch ('FieldDef name t)
Field (FieldValue sch ('TUnion ts)
 -> Field sch ('FieldDef name ('TUnion ts)))
-> (NS (FieldValue sch) ts -> FieldValue sch ('TUnion ts))
-> NS (FieldValue sch) ts
-> Field sch ('FieldDef name ('TUnion ts))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NS (FieldValue sch) ts -> FieldValue sch ('TUnion ts)
forall typeName fieldName (sch :: Schema typeName fieldName)
       (choices :: [FieldType typeName]).
NS (FieldValue sch) choices -> FieldValue sch ('TUnion choices)
FUnion (NS (FieldValue sch) ts -> Field sch ('FieldDef name ('TUnion ts)))
-> Parser RawMessage (NS (FieldValue sch) ts)
-> Parser RawMessage (Field 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
  (FindProtoBufOneOfIds sch ty name) sch ts =>
Parser RawMessage (NS (FieldValue sch) ts)
forall tn fn (ids :: [Nat]) (sch :: Schema tn fn)
       (ts :: [FieldType tn]).
ProtoBridgeUnionFieldValue ids sch ts =>
Parser RawMessage (NS (FieldValue sch) ts)
protoToUnionFieldValue @_ @_ @(FindProtoBufOneOfIds sch ty name)

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

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

instance ProtoBridgeEmbedTerm sch (sch :/: t)
         => ProtoBridgeOneFieldValue sch ('TSchematic t) where
  defaultOneFieldValue :: Maybe (FieldValue sch ('TSchematic t))
defaultOneFieldValue = Maybe (FieldValue sch ('TSchematic t))
forall a. Maybe a
Nothing
  oneFieldValueToProto :: FieldNumber -> FieldValue sch ('TSchematic t) -> MessageBuilder
oneFieldValueToProto FieldNumber
fid (FSchematic Term sch (sch :/: t1)
v) = FieldNumber -> Term sch (sch :/: t) -> MessageBuilder
forall tn fn (sch :: Schema tn fn) (t :: TypeDef tn fn).
ProtoBridgeEmbedTerm sch t =>
FieldNumber -> Term sch t -> MessageBuilder
termToEmbedProto FieldNumber
fid Term sch (sch :/: t)
Term sch (sch :/: t1)
v
  protoToOneFieldValue :: Parser RawPrimitive (FieldValue sch ('TSchematic t))
protoToOneFieldValue = Term sch (sch :/: t) -> FieldValue sch ('TSchematic t)
forall typeName fieldName (sch :: Schema typeName fieldName)
       (t1 :: typeName).
Term sch (sch :/: t1) -> FieldValue sch ('TSchematic t1)
FSchematic (Term sch (sch :/: t) -> FieldValue sch ('TSchematic t))
-> Parser RawPrimitive (Term sch (sch :/: t))
-> Parser RawPrimitive (FieldValue sch ('TSchematic t))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser RawPrimitive (Term sch (sch :/: t))
forall tn fn (sch :: Schema tn fn) (t :: TypeDef tn fn).
ProtoBridgeEmbedTerm sch t =>
Parser RawPrimitive (Term sch t)
embedProtoToOneFieldValue
  supportsPacking :: Proxy (FieldValue sch ('TSchematic t)) -> Bool
supportsPacking Proxy (FieldValue sch ('TSchematic t))
_ = Proxy (Term sch (sch :/: t)) -> Bool
forall tn fn (sch :: Schema tn fn) (t :: TypeDef tn fn).
ProtoBridgeEmbedTerm sch t =>
Proxy (Term sch t) -> Bool
supportsPackingTerm (Proxy (Term sch (sch :/: t))
forall k (t :: k). Proxy t
Proxy @(Term sch (sch :/: t)))
  packedFieldValueToProto :: FieldNumber -> [FieldValue sch ('TSchematic t)] -> MessageBuilder
packedFieldValueToProto FieldNumber
fid [FieldValue sch ('TSchematic t)]
vs = FieldNumber -> [Term sch (sch :/: t)] -> MessageBuilder
forall tn fn (sch :: Schema tn fn) (t :: TypeDef tn fn).
ProtoBridgeEmbedTerm sch t =>
FieldNumber -> [Term sch t] -> MessageBuilder
termToPackedEmbedProto FieldNumber
fid ([Term sch (sch :/: t)] -> MessageBuilder)
-> [Term sch (sch :/: t)] -> MessageBuilder
forall a b. (a -> b) -> a -> b
$ (FieldValue sch ('TSchematic t) -> Term sch (sch :/: t))
-> [FieldValue sch ('TSchematic t)] -> [Term sch (sch :/: t)]
forall a b. (a -> b) -> [a] -> [b]
map (\(FSchematic Term sch (sch :/: t1)
t) -> Term sch (sch :/: t)
Term sch (sch :/: t1)
t) [FieldValue sch ('TSchematic t)]
vs
  protoToPackedFieldValue :: Parser RawPrimitive [FieldValue sch ('TSchematic t)]
protoToPackedFieldValue = (Term sch (sch :/: t) -> FieldValue sch ('TSchematic t))
-> [Term sch (sch :/: t)] -> [FieldValue sch ('TSchematic t)]
forall a b. (a -> b) -> [a] -> [b]
map Term sch (sch :/: t) -> FieldValue sch ('TSchematic t)
forall typeName fieldName (sch :: Schema typeName fieldName)
       (t1 :: typeName).
Term sch (sch :/: t1) -> FieldValue sch ('TSchematic t1)
FSchematic ([Term sch (sch :/: t)] -> [FieldValue sch ('TSchematic t)])
-> Parser RawPrimitive [Term sch (sch :/: t)]
-> Parser RawPrimitive [FieldValue sch ('TSchematic t)]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser RawPrimitive [Term sch (sch :/: t)]
forall tn fn (sch :: Schema tn fn) (t :: TypeDef tn fn).
ProtoBridgeEmbedTerm sch t =>
Parser RawPrimitive [Term sch t]
embedProtoToPackedFieldValue

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

instance TypeError ('Text "null cannot be converted to protobuf")
         => ProtoBridgeOneFieldValue sch 'TNull where
  defaultOneFieldValue :: Maybe (FieldValue sch 'TNull)
defaultOneFieldValue = [Char] -> Maybe (FieldValue sch 'TNull)
forall a. HasCallStack => [Char] -> a
error [Char]
"null cannot be converted to protobuf"
  oneFieldValueToProto :: FieldNumber -> FieldValue sch 'TNull -> MessageBuilder
oneFieldValueToProto = [Char] -> FieldNumber -> FieldValue sch 'TNull -> MessageBuilder
forall a. HasCallStack => [Char] -> a
error [Char]
"null cannot be converted to protobuf"
  protoToOneFieldValue :: Parser RawPrimitive (FieldValue sch 'TNull)
protoToOneFieldValue = [Char] -> Parser RawPrimitive (FieldValue sch 'TNull)
forall a. HasCallStack => [Char] -> a
error [Char]
"null cannot be converted to protobuf"
  supportsPacking :: Proxy (FieldValue sch 'TNull) -> Bool
supportsPacking Proxy (FieldValue sch 'TNull)
_ = Bool
False
  packedFieldValueToProto :: FieldNumber -> [FieldValue sch 'TNull] -> MessageBuilder
packedFieldValueToProto = [Char] -> FieldNumber -> [FieldValue sch 'TNull] -> MessageBuilder
forall a. HasCallStack => [Char] -> a
error [Char]
"null cannot be converted to protobuf"
  protoToPackedFieldValue :: Parser RawPrimitive [FieldValue sch 'TNull]
protoToPackedFieldValue = [Char] -> Parser RawPrimitive [FieldValue sch 'TNull]
forall a. HasCallStack => [Char] -> a
error [Char]
"null cannot be converted to protobuf"

instance ProtoBridgeOneFieldValue sch ('TPrimitive Int) where
  defaultOneFieldValue :: Maybe (FieldValue sch ('TPrimitive Int))
defaultOneFieldValue = FieldValue sch ('TPrimitive Int)
-> Maybe (FieldValue sch ('TPrimitive Int))
forall a. a -> Maybe a
Just (FieldValue sch ('TPrimitive Int)
 -> Maybe (FieldValue sch ('TPrimitive Int)))
-> FieldValue sch ('TPrimitive Int)
-> Maybe (FieldValue sch ('TPrimitive Int))
forall a b. (a -> b) -> a -> b
$ Int -> FieldValue sch ('TPrimitive Int)
forall typeName fieldName t1 (sch :: Schema typeName fieldName).
t1 -> FieldValue sch ('TPrimitive t1)
FPrimitive Int
0
  oneFieldValueToProto :: FieldNumber -> FieldValue sch ('TPrimitive Int) -> MessageBuilder
oneFieldValueToProto FieldNumber
fid (FPrimitive t1
n) = FieldNumber -> Int32 -> MessageBuilder
PBEnc.int32 FieldNumber
fid (t1 -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral t1
n)
  protoToOneFieldValue :: Parser RawPrimitive (FieldValue sch ('TPrimitive Int))
protoToOneFieldValue = Int -> FieldValue sch ('TPrimitive Int)
forall typeName fieldName t1 (sch :: Schema typeName fieldName).
t1 -> FieldValue sch ('TPrimitive t1)
FPrimitive (Int -> FieldValue sch ('TPrimitive Int))
-> (Int32 -> Int) -> Int32 -> FieldValue 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 sch ('TPrimitive Int))
-> Parser RawPrimitive Int32
-> Parser RawPrimitive (FieldValue sch ('TPrimitive Int))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser RawPrimitive Int32
PBDec.int32
  supportsPacking :: Proxy (FieldValue sch ('TPrimitive Int)) -> Bool
supportsPacking Proxy (FieldValue sch ('TPrimitive Int))
_ = Bool
True
  packedFieldValueToProto :: FieldNumber -> [FieldValue sch ('TPrimitive Int)] -> MessageBuilder
packedFieldValueToProto FieldNumber
fid [FieldValue sch ('TPrimitive Int)]
vs
    = FieldNumber -> [Word64] -> MessageBuilder
forall (f :: * -> *).
Foldable f =>
FieldNumber -> f Word64 -> MessageBuilder
PBEnc.packedVarints FieldNumber
fid ([Word64] -> MessageBuilder) -> [Word64] -> MessageBuilder
forall a b. (a -> b) -> a -> b
$ (FieldValue sch ('TPrimitive Int) -> Word64)
-> [FieldValue sch ('TPrimitive Int)] -> [Word64]
forall a b. (a -> b) -> [a] -> [b]
map (\(FPrimitive t1
i) -> t1 -> Word64
forall a b. (Integral a, Num b) => a -> b
fromIntegral t1
i) [FieldValue sch ('TPrimitive Int)]
vs
  protoToPackedFieldValue :: Parser RawPrimitive [FieldValue sch ('TPrimitive Int)]
protoToPackedFieldValue = (Int -> FieldValue sch ('TPrimitive Int))
-> [Int] -> [FieldValue sch ('TPrimitive Int)]
forall a b. (a -> b) -> [a] -> [b]
map Int -> FieldValue sch ('TPrimitive Int)
forall typeName fieldName t1 (sch :: Schema typeName fieldName).
t1 -> FieldValue sch ('TPrimitive t1)
FPrimitive ([Int] -> [FieldValue sch ('TPrimitive Int)])
-> Parser RawPrimitive [Int]
-> Parser RawPrimitive [FieldValue sch ('TPrimitive Int)]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser RawPrimitive [Int]
forall a. Integral a => Parser RawPrimitive [a]
PBDec.packedVarints

instance ProtoBridgeOneFieldValue sch ('TPrimitive Int32) where
  defaultOneFieldValue :: Maybe (FieldValue sch ('TPrimitive Int32))
defaultOneFieldValue = FieldValue sch ('TPrimitive Int32)
-> Maybe (FieldValue sch ('TPrimitive Int32))
forall a. a -> Maybe a
Just (FieldValue sch ('TPrimitive Int32)
 -> Maybe (FieldValue sch ('TPrimitive Int32)))
-> FieldValue sch ('TPrimitive Int32)
-> Maybe (FieldValue sch ('TPrimitive Int32))
forall a b. (a -> b) -> a -> b
$ Int32 -> FieldValue sch ('TPrimitive Int32)
forall typeName fieldName t1 (sch :: Schema typeName fieldName).
t1 -> FieldValue sch ('TPrimitive t1)
FPrimitive Int32
0
  oneFieldValueToProto :: FieldNumber -> FieldValue sch ('TPrimitive Int32) -> MessageBuilder
oneFieldValueToProto FieldNumber
fid (FPrimitive t1
n) = FieldNumber -> Int32 -> MessageBuilder
PBEnc.int32 FieldNumber
fid t1
Int32
n
  protoToOneFieldValue :: Parser RawPrimitive (FieldValue sch ('TPrimitive Int32))
protoToOneFieldValue = Int32 -> FieldValue sch ('TPrimitive Int32)
forall typeName fieldName t1 (sch :: Schema typeName fieldName).
t1 -> FieldValue sch ('TPrimitive t1)
FPrimitive (Int32 -> FieldValue sch ('TPrimitive Int32))
-> Parser RawPrimitive Int32
-> Parser RawPrimitive (FieldValue sch ('TPrimitive Int32))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser RawPrimitive Int32
PBDec.int32
  supportsPacking :: Proxy (FieldValue sch ('TPrimitive Int32)) -> Bool
supportsPacking Proxy (FieldValue sch ('TPrimitive Int32))
_ = Bool
True
  packedFieldValueToProto :: FieldNumber
-> [FieldValue sch ('TPrimitive Int32)] -> MessageBuilder
packedFieldValueToProto FieldNumber
fid [FieldValue sch ('TPrimitive Int32)]
vs
    = FieldNumber -> [Word64] -> MessageBuilder
forall (f :: * -> *).
Foldable f =>
FieldNumber -> f Word64 -> MessageBuilder
PBEnc.packedVarints FieldNumber
fid ([Word64] -> MessageBuilder) -> [Word64] -> MessageBuilder
forall a b. (a -> b) -> a -> b
$ (FieldValue sch ('TPrimitive Int32) -> Word64)
-> [FieldValue sch ('TPrimitive Int32)] -> [Word64]
forall a b. (a -> b) -> [a] -> [b]
map (\(FPrimitive t1
i) -> t1 -> Word64
forall a b. (Integral a, Num b) => a -> b
fromIntegral t1
i) [FieldValue sch ('TPrimitive Int32)]
vs
  protoToPackedFieldValue :: Parser RawPrimitive [FieldValue sch ('TPrimitive Int32)]
protoToPackedFieldValue = (Int32 -> FieldValue sch ('TPrimitive Int32))
-> [Int32] -> [FieldValue sch ('TPrimitive Int32)]
forall a b. (a -> b) -> [a] -> [b]
map Int32 -> FieldValue sch ('TPrimitive Int32)
forall typeName fieldName t1 (sch :: Schema typeName fieldName).
t1 -> FieldValue sch ('TPrimitive t1)
FPrimitive ([Int32] -> [FieldValue sch ('TPrimitive Int32)])
-> Parser RawPrimitive [Int32]
-> Parser RawPrimitive [FieldValue sch ('TPrimitive Int32)]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser RawPrimitive [Int32]
forall a. Integral a => Parser RawPrimitive [a]
PBDec.packedVarints

instance ProtoBridgeOneFieldValue sch ('TPrimitive Int64) where
  defaultOneFieldValue :: Maybe (FieldValue sch ('TPrimitive Int64))
defaultOneFieldValue = FieldValue sch ('TPrimitive Int64)
-> Maybe (FieldValue sch ('TPrimitive Int64))
forall a. a -> Maybe a
Just (FieldValue sch ('TPrimitive Int64)
 -> Maybe (FieldValue sch ('TPrimitive Int64)))
-> FieldValue sch ('TPrimitive Int64)
-> Maybe (FieldValue sch ('TPrimitive Int64))
forall a b. (a -> b) -> a -> b
$ Int64 -> FieldValue sch ('TPrimitive Int64)
forall typeName fieldName t1 (sch :: Schema typeName fieldName).
t1 -> FieldValue sch ('TPrimitive t1)
FPrimitive Int64
0
  oneFieldValueToProto :: FieldNumber -> FieldValue sch ('TPrimitive Int64) -> MessageBuilder
oneFieldValueToProto FieldNumber
fid (FPrimitive t1
n) = FieldNumber -> Int64 -> MessageBuilder
PBEnc.int64 FieldNumber
fid t1
Int64
n
  protoToOneFieldValue :: Parser RawPrimitive (FieldValue sch ('TPrimitive Int64))
protoToOneFieldValue = Int64 -> FieldValue sch ('TPrimitive Int64)
forall typeName fieldName t1 (sch :: Schema typeName fieldName).
t1 -> FieldValue sch ('TPrimitive t1)
FPrimitive (Int64 -> FieldValue sch ('TPrimitive Int64))
-> Parser RawPrimitive Int64
-> Parser RawPrimitive (FieldValue sch ('TPrimitive Int64))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser RawPrimitive Int64
PBDec.int64
  supportsPacking :: Proxy (FieldValue sch ('TPrimitive Int64)) -> Bool
supportsPacking Proxy (FieldValue sch ('TPrimitive Int64))
_ = Bool
True
  packedFieldValueToProto :: FieldNumber
-> [FieldValue sch ('TPrimitive Int64)] -> MessageBuilder
packedFieldValueToProto FieldNumber
fid [FieldValue sch ('TPrimitive Int64)]
vs
    = FieldNumber -> [Word64] -> MessageBuilder
forall (f :: * -> *).
Foldable f =>
FieldNumber -> f Word64 -> MessageBuilder
PBEnc.packedVarints FieldNumber
fid ([Word64] -> MessageBuilder) -> [Word64] -> MessageBuilder
forall a b. (a -> b) -> a -> b
$ (FieldValue sch ('TPrimitive Int64) -> Word64)
-> [FieldValue sch ('TPrimitive Int64)] -> [Word64]
forall a b. (a -> b) -> [a] -> [b]
map (\(FPrimitive t1
i) -> t1 -> Word64
forall a b. (Integral a, Num b) => a -> b
fromIntegral t1
i) [FieldValue sch ('TPrimitive Int64)]
vs
  protoToPackedFieldValue :: Parser RawPrimitive [FieldValue sch ('TPrimitive Int64)]
protoToPackedFieldValue = (Int64 -> FieldValue sch ('TPrimitive Int64))
-> [Int64] -> [FieldValue sch ('TPrimitive Int64)]
forall a b. (a -> b) -> [a] -> [b]
map Int64 -> FieldValue sch ('TPrimitive Int64)
forall typeName fieldName t1 (sch :: Schema typeName fieldName).
t1 -> FieldValue sch ('TPrimitive t1)
FPrimitive ([Int64] -> [FieldValue sch ('TPrimitive Int64)])
-> Parser RawPrimitive [Int64]
-> Parser RawPrimitive [FieldValue sch ('TPrimitive Int64)]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser RawPrimitive [Int64]
forall a. Integral a => Parser RawPrimitive [a]
PBDec.packedVarints

-- WARNING! These instances may go out of bounds
instance ProtoBridgeOneFieldValue sch ('TPrimitive Integer) where
  defaultOneFieldValue :: Maybe (FieldValue sch ('TPrimitive Integer))
defaultOneFieldValue = FieldValue sch ('TPrimitive Integer)
-> Maybe (FieldValue sch ('TPrimitive Integer))
forall a. a -> Maybe a
Just (FieldValue sch ('TPrimitive Integer)
 -> Maybe (FieldValue sch ('TPrimitive Integer)))
-> FieldValue sch ('TPrimitive Integer)
-> Maybe (FieldValue sch ('TPrimitive Integer))
forall a b. (a -> b) -> a -> b
$ Integer -> FieldValue sch ('TPrimitive Integer)
forall typeName fieldName t1 (sch :: Schema typeName fieldName).
t1 -> FieldValue sch ('TPrimitive t1)
FPrimitive Integer
0
  oneFieldValueToProto :: FieldNumber
-> FieldValue sch ('TPrimitive Integer) -> MessageBuilder
oneFieldValueToProto FieldNumber
fid (FPrimitive t1
n) = FieldNumber -> Int64 -> MessageBuilder
PBEnc.int64 FieldNumber
fid (Integer -> Int64
forall a. Num a => Integer -> a
fromInteger t1
Integer
n)
  protoToOneFieldValue :: Parser RawPrimitive (FieldValue sch ('TPrimitive Integer))
protoToOneFieldValue = Integer -> FieldValue sch ('TPrimitive Integer)
forall typeName fieldName t1 (sch :: Schema typeName fieldName).
t1 -> FieldValue sch ('TPrimitive t1)
FPrimitive (Integer -> FieldValue sch ('TPrimitive Integer))
-> (Int64 -> Integer)
-> Int64
-> FieldValue 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 sch ('TPrimitive Integer))
-> Parser RawPrimitive Int64
-> Parser RawPrimitive (FieldValue sch ('TPrimitive Integer))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser RawPrimitive Int64
PBDec.int64
  supportsPacking :: Proxy (FieldValue sch ('TPrimitive Integer)) -> Bool
supportsPacking Proxy (FieldValue sch ('TPrimitive Integer))
_ = Bool
True
  packedFieldValueToProto :: FieldNumber
-> [FieldValue sch ('TPrimitive Integer)] -> MessageBuilder
packedFieldValueToProto FieldNumber
fid [FieldValue sch ('TPrimitive Integer)]
vs
    = FieldNumber -> [Word64] -> MessageBuilder
forall (f :: * -> *).
Foldable f =>
FieldNumber -> f Word64 -> MessageBuilder
PBEnc.packedVarints FieldNumber
fid ([Word64] -> MessageBuilder) -> [Word64] -> MessageBuilder
forall a b. (a -> b) -> a -> b
$ (FieldValue sch ('TPrimitive Integer) -> Word64)
-> [FieldValue sch ('TPrimitive Integer)] -> [Word64]
forall a b. (a -> b) -> [a] -> [b]
map (\(FPrimitive t1
i) -> t1 -> Word64
forall a b. (Integral a, Num b) => a -> b
fromIntegral t1
i) [FieldValue sch ('TPrimitive Integer)]
vs
  protoToPackedFieldValue :: Parser RawPrimitive [FieldValue sch ('TPrimitive Integer)]
protoToPackedFieldValue = (Integer -> FieldValue sch ('TPrimitive Integer))
-> [Integer] -> [FieldValue sch ('TPrimitive Integer)]
forall a b. (a -> b) -> [a] -> [b]
map Integer -> FieldValue sch ('TPrimitive Integer)
forall typeName fieldName t1 (sch :: Schema typeName fieldName).
t1 -> FieldValue sch ('TPrimitive t1)
FPrimitive ([Integer] -> [FieldValue sch ('TPrimitive Integer)])
-> Parser RawPrimitive [Integer]
-> Parser RawPrimitive [FieldValue sch ('TPrimitive Integer)]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser RawPrimitive [Integer]
forall a. Integral a => Parser RawPrimitive [a]
PBDec.packedVarints

instance ProtoBridgeOneFieldValue sch ('TPrimitive Float) where
  defaultOneFieldValue :: Maybe (FieldValue sch ('TPrimitive Float))
defaultOneFieldValue = FieldValue sch ('TPrimitive Float)
-> Maybe (FieldValue sch ('TPrimitive Float))
forall a. a -> Maybe a
Just (FieldValue sch ('TPrimitive Float)
 -> Maybe (FieldValue sch ('TPrimitive Float)))
-> FieldValue sch ('TPrimitive Float)
-> Maybe (FieldValue sch ('TPrimitive Float))
forall a b. (a -> b) -> a -> b
$ Float -> FieldValue sch ('TPrimitive Float)
forall typeName fieldName t1 (sch :: Schema typeName fieldName).
t1 -> FieldValue sch ('TPrimitive t1)
FPrimitive Float
0
  oneFieldValueToProto :: FieldNumber -> FieldValue sch ('TPrimitive Float) -> MessageBuilder
oneFieldValueToProto FieldNumber
fid (FPrimitive t1
n) = FieldNumber -> Float -> MessageBuilder
PBEnc.float FieldNumber
fid t1
Float
n
  protoToOneFieldValue :: Parser RawPrimitive (FieldValue sch ('TPrimitive Float))
protoToOneFieldValue = Float -> FieldValue sch ('TPrimitive Float)
forall typeName fieldName t1 (sch :: Schema typeName fieldName).
t1 -> FieldValue sch ('TPrimitive t1)
FPrimitive (Float -> FieldValue sch ('TPrimitive Float))
-> Parser RawPrimitive Float
-> Parser RawPrimitive (FieldValue sch ('TPrimitive Float))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser RawPrimitive Float
PBDec.float
  supportsPacking :: Proxy (FieldValue sch ('TPrimitive Float)) -> Bool
supportsPacking Proxy (FieldValue sch ('TPrimitive Float))
_ = Bool
True
  packedFieldValueToProto :: FieldNumber
-> [FieldValue sch ('TPrimitive Float)] -> MessageBuilder
packedFieldValueToProto FieldNumber
fid [FieldValue sch ('TPrimitive Float)]
vs
    = FieldNumber -> [Float] -> MessageBuilder
forall (f :: * -> *).
Foldable f =>
FieldNumber -> f Float -> MessageBuilder
PBEnc.packedFloats FieldNumber
fid ([Float] -> MessageBuilder) -> [Float] -> MessageBuilder
forall a b. (a -> b) -> a -> b
$ (FieldValue sch ('TPrimitive Float) -> Float)
-> [FieldValue sch ('TPrimitive Float)] -> [Float]
forall a b. (a -> b) -> [a] -> [b]
map (\(FPrimitive t1
i) -> t1
Float
i) [FieldValue sch ('TPrimitive Float)]
vs
  protoToPackedFieldValue :: Parser RawPrimitive [FieldValue sch ('TPrimitive Float)]
protoToPackedFieldValue = (Float -> FieldValue sch ('TPrimitive Float))
-> [Float] -> [FieldValue sch ('TPrimitive Float)]
forall a b. (a -> b) -> [a] -> [b]
map Float -> FieldValue sch ('TPrimitive Float)
forall typeName fieldName t1 (sch :: Schema typeName fieldName).
t1 -> FieldValue sch ('TPrimitive t1)
FPrimitive ([Float] -> [FieldValue sch ('TPrimitive Float)])
-> Parser RawPrimitive [Float]
-> Parser RawPrimitive [FieldValue sch ('TPrimitive Float)]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser RawPrimitive [Float]
PBDec.packedFloats

instance ProtoBridgeOneFieldValue sch ('TPrimitive Double) where
  defaultOneFieldValue :: Maybe (FieldValue sch ('TPrimitive Double))
defaultOneFieldValue = FieldValue sch ('TPrimitive Double)
-> Maybe (FieldValue sch ('TPrimitive Double))
forall a. a -> Maybe a
Just (FieldValue sch ('TPrimitive Double)
 -> Maybe (FieldValue sch ('TPrimitive Double)))
-> FieldValue sch ('TPrimitive Double)
-> Maybe (FieldValue sch ('TPrimitive Double))
forall a b. (a -> b) -> a -> b
$ Double -> FieldValue sch ('TPrimitive Double)
forall typeName fieldName t1 (sch :: Schema typeName fieldName).
t1 -> FieldValue sch ('TPrimitive t1)
FPrimitive Double
0
  oneFieldValueToProto :: FieldNumber
-> FieldValue sch ('TPrimitive Double) -> MessageBuilder
oneFieldValueToProto FieldNumber
fid (FPrimitive t1
n) = FieldNumber -> Double -> MessageBuilder
PBEnc.double FieldNumber
fid t1
Double
n
  protoToOneFieldValue :: Parser RawPrimitive (FieldValue sch ('TPrimitive Double))
protoToOneFieldValue = Double -> FieldValue sch ('TPrimitive Double)
forall typeName fieldName t1 (sch :: Schema typeName fieldName).
t1 -> FieldValue sch ('TPrimitive t1)
FPrimitive (Double -> FieldValue sch ('TPrimitive Double))
-> Parser RawPrimitive Double
-> Parser RawPrimitive (FieldValue sch ('TPrimitive Double))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser RawPrimitive Double
PBDec.double
  supportsPacking :: Proxy (FieldValue sch ('TPrimitive Double)) -> Bool
supportsPacking Proxy (FieldValue sch ('TPrimitive Double))
_ = Bool
True
  packedFieldValueToProto :: FieldNumber
-> [FieldValue sch ('TPrimitive Double)] -> MessageBuilder
packedFieldValueToProto FieldNumber
fid [FieldValue sch ('TPrimitive Double)]
vs
    = FieldNumber -> [Double] -> MessageBuilder
forall (f :: * -> *).
Foldable f =>
FieldNumber -> f Double -> MessageBuilder
PBEnc.packedDoubles FieldNumber
fid ([Double] -> MessageBuilder) -> [Double] -> MessageBuilder
forall a b. (a -> b) -> a -> b
$ (FieldValue sch ('TPrimitive Double) -> Double)
-> [FieldValue sch ('TPrimitive Double)] -> [Double]
forall a b. (a -> b) -> [a] -> [b]
map (\(FPrimitive t1
i) -> t1
Double
i) [FieldValue sch ('TPrimitive Double)]
vs
  protoToPackedFieldValue :: Parser RawPrimitive [FieldValue sch ('TPrimitive Double)]
protoToPackedFieldValue = (Double -> FieldValue sch ('TPrimitive Double))
-> [Double] -> [FieldValue sch ('TPrimitive Double)]
forall a b. (a -> b) -> [a] -> [b]
map Double -> FieldValue sch ('TPrimitive Double)
forall typeName fieldName t1 (sch :: Schema typeName fieldName).
t1 -> FieldValue sch ('TPrimitive t1)
FPrimitive ([Double] -> [FieldValue sch ('TPrimitive Double)])
-> Parser RawPrimitive [Double]
-> Parser RawPrimitive [FieldValue sch ('TPrimitive Double)]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser RawPrimitive [Double]
PBDec.packedDoubles

instance ProtoBridgeOneFieldValue sch ('TPrimitive Bool) where
  defaultOneFieldValue :: Maybe (FieldValue sch ('TPrimitive Bool))
defaultOneFieldValue = FieldValue sch ('TPrimitive Bool)
-> Maybe (FieldValue sch ('TPrimitive Bool))
forall a. a -> Maybe a
Just (FieldValue sch ('TPrimitive Bool)
 -> Maybe (FieldValue sch ('TPrimitive Bool)))
-> FieldValue sch ('TPrimitive Bool)
-> Maybe (FieldValue sch ('TPrimitive Bool))
forall a b. (a -> b) -> a -> b
$ Bool -> FieldValue sch ('TPrimitive Bool)
forall typeName fieldName t1 (sch :: Schema typeName fieldName).
t1 -> FieldValue sch ('TPrimitive t1)
FPrimitive Bool
False
  oneFieldValueToProto :: FieldNumber -> FieldValue sch ('TPrimitive Bool) -> MessageBuilder
oneFieldValueToProto FieldNumber
fid (FPrimitive t1
n) = FieldNumber -> t1 -> MessageBuilder
forall e. ProtoEnum e => FieldNumber -> e -> MessageBuilder
PBEnc.enum FieldNumber
fid t1
n
  protoToOneFieldValue :: Parser RawPrimitive (FieldValue sch ('TPrimitive Bool))
protoToOneFieldValue = Bool -> FieldValue sch ('TPrimitive Bool)
forall typeName fieldName t1 (sch :: Schema typeName fieldName).
t1 -> FieldValue sch ('TPrimitive t1)
FPrimitive (Bool -> FieldValue sch ('TPrimitive Bool))
-> Parser RawPrimitive Bool
-> Parser RawPrimitive (FieldValue sch ('TPrimitive Bool))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser RawPrimitive Bool
PBDec.bool
  supportsPacking :: Proxy (FieldValue sch ('TPrimitive Bool)) -> Bool
supportsPacking Proxy (FieldValue sch ('TPrimitive Bool))
_ = Bool
True
  packedFieldValueToProto :: FieldNumber
-> [FieldValue sch ('TPrimitive Bool)] -> MessageBuilder
packedFieldValueToProto FieldNumber
fid [FieldValue sch ('TPrimitive Bool)]
vs
    = FieldNumber -> [Word64] -> MessageBuilder
forall (f :: * -> *).
Foldable f =>
FieldNumber -> f Word64 -> MessageBuilder
PBEnc.packedVarints FieldNumber
fid ([Word64] -> MessageBuilder) -> [Word64] -> MessageBuilder
forall a b. (a -> b) -> a -> b
$ (FieldValue sch ('TPrimitive Bool) -> Word64)
-> [FieldValue sch ('TPrimitive Bool)] -> [Word64]
forall a b. (a -> b) -> [a] -> [b]
map (\(FPrimitive t1
i) -> if t1
Bool
i then Word64
1 else Word64
0) [FieldValue sch ('TPrimitive Bool)]
vs
  protoToPackedFieldValue :: Parser RawPrimitive [FieldValue sch ('TPrimitive Bool)]
protoToPackedFieldValue = (Integer -> FieldValue sch ('TPrimitive Bool))
-> [Integer] -> [FieldValue sch ('TPrimitive Bool)]
forall a b. (a -> b) -> [a] -> [b]
map (\(Integer
i :: Integer) -> Bool -> FieldValue sch ('TPrimitive Bool)
forall typeName fieldName t1 (sch :: Schema typeName fieldName).
t1 -> FieldValue sch ('TPrimitive t1)
FPrimitive (Integer
i Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
/= Integer
0)) ([Integer] -> [FieldValue sch ('TPrimitive Bool)])
-> Parser RawPrimitive [Integer]
-> Parser RawPrimitive [FieldValue sch ('TPrimitive Bool)]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser RawPrimitive [Integer]
forall a. Integral a => Parser RawPrimitive [a]
PBDec.packedVarints

instance ProtoBridgeOneFieldValue sch ('TPrimitive T.Text) where
  defaultOneFieldValue :: Maybe (FieldValue sch ('TPrimitive Text))
defaultOneFieldValue = FieldValue sch ('TPrimitive Text)
-> Maybe (FieldValue sch ('TPrimitive Text))
forall a. a -> Maybe a
Just (FieldValue sch ('TPrimitive Text)
 -> Maybe (FieldValue sch ('TPrimitive Text)))
-> FieldValue sch ('TPrimitive Text)
-> Maybe (FieldValue sch ('TPrimitive Text))
forall a b. (a -> b) -> a -> b
$ Text -> FieldValue sch ('TPrimitive Text)
forall typeName fieldName t1 (sch :: Schema typeName fieldName).
t1 -> FieldValue sch ('TPrimitive t1)
FPrimitive Text
""
  oneFieldValueToProto :: FieldNumber -> FieldValue sch ('TPrimitive Text) -> MessageBuilder
oneFieldValueToProto FieldNumber
fid (FPrimitive t1
n) = FieldNumber -> Text -> MessageBuilder
PBEnc.text FieldNumber
fid (Text -> Text
LT.fromStrict t1
Text
n)
  protoToOneFieldValue :: Parser RawPrimitive (FieldValue sch ('TPrimitive Text))
protoToOneFieldValue = Text -> FieldValue sch ('TPrimitive Text)
forall typeName fieldName t1 (sch :: Schema typeName fieldName).
t1 -> FieldValue sch ('TPrimitive t1)
FPrimitive (Text -> FieldValue sch ('TPrimitive Text))
-> (Text -> Text) -> Text -> FieldValue sch ('TPrimitive Text)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Text
LT.toStrict (Text -> FieldValue sch ('TPrimitive Text))
-> Parser RawPrimitive Text
-> Parser RawPrimitive (FieldValue sch ('TPrimitive Text))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser RawPrimitive Text
PBDec.text
  supportsPacking :: Proxy (FieldValue sch ('TPrimitive Text)) -> Bool
supportsPacking Proxy (FieldValue sch ('TPrimitive Text))
_ = Bool
False
  packedFieldValueToProto :: FieldNumber
-> [FieldValue sch ('TPrimitive Text)] -> MessageBuilder
packedFieldValueToProto = [Char]
-> FieldNumber
-> [FieldValue sch ('TPrimitive Text)]
-> MessageBuilder
forall a. HasCallStack => [Char] -> a
error [Char]
"this is a bug, since we declare we do not support packed encoding"
  protoToPackedFieldValue :: Parser RawPrimitive [FieldValue sch ('TPrimitive Text)]
protoToPackedFieldValue = [Char] -> Parser RawPrimitive [FieldValue sch ('TPrimitive Text)]
forall a. HasCallStack => [Char] -> a
error [Char]
"this is a bug, since we declare we do not support packed encoding"

instance ProtoBridgeOneFieldValue sch ('TPrimitive LT.Text) where
  defaultOneFieldValue :: Maybe (FieldValue sch ('TPrimitive Text))
defaultOneFieldValue = FieldValue sch ('TPrimitive Text)
-> Maybe (FieldValue sch ('TPrimitive Text))
forall a. a -> Maybe a
Just (FieldValue sch ('TPrimitive Text)
 -> Maybe (FieldValue sch ('TPrimitive Text)))
-> FieldValue sch ('TPrimitive Text)
-> Maybe (FieldValue sch ('TPrimitive Text))
forall a b. (a -> b) -> a -> b
$ Text -> FieldValue sch ('TPrimitive Text)
forall typeName fieldName t1 (sch :: Schema typeName fieldName).
t1 -> FieldValue sch ('TPrimitive t1)
FPrimitive Text
""
  oneFieldValueToProto :: FieldNumber -> FieldValue sch ('TPrimitive Text) -> MessageBuilder
oneFieldValueToProto FieldNumber
fid (FPrimitive t1
n) = FieldNumber -> Text -> MessageBuilder
PBEnc.text FieldNumber
fid t1
Text
n
  protoToOneFieldValue :: Parser RawPrimitive (FieldValue sch ('TPrimitive Text))
protoToOneFieldValue = Text -> FieldValue sch ('TPrimitive Text)
forall typeName fieldName t1 (sch :: Schema typeName fieldName).
t1 -> FieldValue sch ('TPrimitive t1)
FPrimitive (Text -> FieldValue sch ('TPrimitive Text))
-> Parser RawPrimitive Text
-> Parser RawPrimitive (FieldValue sch ('TPrimitive Text))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser RawPrimitive Text
PBDec.text
  supportsPacking :: Proxy (FieldValue sch ('TPrimitive Text)) -> Bool
supportsPacking Proxy (FieldValue sch ('TPrimitive Text))
_ = Bool
False
  packedFieldValueToProto :: FieldNumber
-> [FieldValue sch ('TPrimitive Text)] -> MessageBuilder
packedFieldValueToProto = [Char]
-> FieldNumber
-> [FieldValue sch ('TPrimitive Text)]
-> MessageBuilder
forall a. HasCallStack => [Char] -> a
error [Char]
"this is a bug, since we declare we do not support packed encoding"
  protoToPackedFieldValue :: Parser RawPrimitive [FieldValue sch ('TPrimitive Text)]
protoToPackedFieldValue = [Char] -> Parser RawPrimitive [FieldValue sch ('TPrimitive Text)]
forall a. HasCallStack => [Char] -> a
error [Char]
"this is a bug, since we declare we do not support packed encoding"

instance ProtoBridgeOneFieldValue sch ('TPrimitive BS.ByteString) where
  defaultOneFieldValue :: Maybe (FieldValue sch ('TPrimitive ByteString))
defaultOneFieldValue = FieldValue sch ('TPrimitive ByteString)
-> Maybe (FieldValue sch ('TPrimitive ByteString))
forall a. a -> Maybe a
Just (FieldValue sch ('TPrimitive ByteString)
 -> Maybe (FieldValue sch ('TPrimitive ByteString)))
-> FieldValue sch ('TPrimitive ByteString)
-> Maybe (FieldValue sch ('TPrimitive ByteString))
forall a b. (a -> b) -> a -> b
$ ByteString -> FieldValue sch ('TPrimitive ByteString)
forall typeName fieldName t1 (sch :: Schema typeName fieldName).
t1 -> FieldValue sch ('TPrimitive t1)
FPrimitive ByteString
""
  oneFieldValueToProto :: FieldNumber
-> FieldValue sch ('TPrimitive ByteString) -> MessageBuilder
oneFieldValueToProto FieldNumber
fid (FPrimitive t1
n) = FieldNumber -> ByteString -> MessageBuilder
PBEnc.byteString FieldNumber
fid t1
ByteString
n
  protoToOneFieldValue :: Parser RawPrimitive (FieldValue sch ('TPrimitive ByteString))
protoToOneFieldValue = ByteString -> FieldValue sch ('TPrimitive ByteString)
forall typeName fieldName t1 (sch :: Schema typeName fieldName).
t1 -> FieldValue sch ('TPrimitive t1)
FPrimitive (ByteString -> FieldValue sch ('TPrimitive ByteString))
-> Parser RawPrimitive ByteString
-> Parser RawPrimitive (FieldValue sch ('TPrimitive ByteString))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser RawPrimitive ByteString
PBDec.byteString
  supportsPacking :: Proxy (FieldValue sch ('TPrimitive ByteString)) -> Bool
supportsPacking Proxy (FieldValue sch ('TPrimitive ByteString))
_ = Bool
False
  packedFieldValueToProto :: FieldNumber
-> [FieldValue sch ('TPrimitive ByteString)] -> MessageBuilder
packedFieldValueToProto = [Char]
-> FieldNumber
-> [FieldValue sch ('TPrimitive ByteString)]
-> MessageBuilder
forall a. HasCallStack => [Char] -> a
error [Char]
"this is a bug, since we declare we do not support packed encoding"
  protoToPackedFieldValue :: Parser RawPrimitive [FieldValue sch ('TPrimitive ByteString)]
protoToPackedFieldValue = [Char]
-> Parser RawPrimitive [FieldValue sch ('TPrimitive ByteString)]
forall a. HasCallStack => [Char] -> a
error [Char]
"this is a bug, since we declare we do not support packed encoding"

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

instance TypeError ('Text "optionals cannot be nested in protobuf")
         => ProtoBridgeOneFieldValue sch ('TOption t) where
  defaultOneFieldValue :: Maybe (FieldValue sch ('TOption t))
defaultOneFieldValue = [Char] -> Maybe (FieldValue sch ('TOption t))
forall a. HasCallStack => [Char] -> a
error [Char]
"optionals cannot be nested in protobuf"
  oneFieldValueToProto :: FieldNumber -> FieldValue sch ('TOption t) -> MessageBuilder
oneFieldValueToProto = [Char]
-> FieldNumber -> FieldValue sch ('TOption t) -> MessageBuilder
forall a. HasCallStack => [Char] -> a
error [Char]
"optionals cannot be nested in protobuf"
  protoToOneFieldValue :: Parser RawPrimitive (FieldValue sch ('TOption t))
protoToOneFieldValue = [Char] -> Parser RawPrimitive (FieldValue sch ('TOption t))
forall a. HasCallStack => [Char] -> a
error [Char]
"optionals cannot be nested in protobuf"
  supportsPacking :: Proxy (FieldValue sch ('TOption t)) -> Bool
supportsPacking      = [Char] -> Proxy (FieldValue sch ('TOption t)) -> Bool
forall a. HasCallStack => [Char] -> a
error [Char]
"optionals cannot be nested in protobuf"
  packedFieldValueToProto :: FieldNumber -> [FieldValue sch ('TOption t)] -> MessageBuilder
packedFieldValueToProto = [Char]
-> FieldNumber -> [FieldValue sch ('TOption t)] -> MessageBuilder
forall a. HasCallStack => [Char] -> a
error [Char]
"optionals cannot be nested in protobuf"
  protoToPackedFieldValue :: Parser RawPrimitive [FieldValue sch ('TOption t)]
protoToPackedFieldValue = [Char] -> Parser RawPrimitive [FieldValue sch ('TOption t)]
forall a. HasCallStack => [Char] -> a
error [Char]
"optionals cannot be nested in protobuf"

instance TypeError ('Text "lists cannot be nested in protobuf")
         => ProtoBridgeOneFieldValue sch ('TList t) where
  defaultOneFieldValue :: Maybe (FieldValue sch ('TList t))
defaultOneFieldValue = [Char] -> Maybe (FieldValue sch ('TList t))
forall a. HasCallStack => [Char] -> a
error [Char]
"lists cannot be nested in protobuf"
  oneFieldValueToProto :: FieldNumber -> FieldValue sch ('TList t) -> MessageBuilder
oneFieldValueToProto = [Char]
-> FieldNumber -> FieldValue sch ('TList t) -> MessageBuilder
forall a. HasCallStack => [Char] -> a
error [Char]
"lists cannot be nested in protobuf"
  protoToOneFieldValue :: Parser RawPrimitive (FieldValue sch ('TList t))
protoToOneFieldValue = [Char] -> Parser RawPrimitive (FieldValue sch ('TList t))
forall a. HasCallStack => [Char] -> a
error [Char]
"lists cannot be nested in protobuf"
  supportsPacking :: Proxy (FieldValue sch ('TList t)) -> Bool
supportsPacking      = [Char] -> Proxy (FieldValue sch ('TList t)) -> Bool
forall a. HasCallStack => [Char] -> a
error [Char]
"lists cannot be nested in protobuf"
  packedFieldValueToProto :: FieldNumber -> [FieldValue sch ('TList t)] -> MessageBuilder
packedFieldValueToProto = [Char]
-> FieldNumber -> [FieldValue sch ('TList t)] -> MessageBuilder
forall a. HasCallStack => [Char] -> a
error [Char]
"lists cannot be nested in protobuf"
  protoToPackedFieldValue :: Parser RawPrimitive [FieldValue sch ('TList t)]
protoToPackedFieldValue = [Char] -> Parser RawPrimitive [FieldValue sch ('TList t)]
forall a. HasCallStack => [Char] -> a
error [Char]
"lists cannot be nested in protobuf"

instance TypeError ('Text "maps are not currently supported")
         => ProtoBridgeOneFieldValue sch ('TMap k v) where
  defaultOneFieldValue :: Maybe (FieldValue sch ('TMap k v))
defaultOneFieldValue = [Char] -> Maybe (FieldValue sch ('TMap k v))
forall a. HasCallStack => [Char] -> a
error [Char]
"maps are not currently supported"
  oneFieldValueToProto :: FieldNumber -> FieldValue sch ('TMap k v) -> MessageBuilder
oneFieldValueToProto = [Char]
-> FieldNumber -> FieldValue sch ('TMap k v) -> MessageBuilder
forall a. HasCallStack => [Char] -> a
error [Char]
"maps are not currently supported"
  protoToOneFieldValue :: Parser RawPrimitive (FieldValue sch ('TMap k v))
protoToOneFieldValue = [Char] -> Parser RawPrimitive (FieldValue sch ('TMap k v))
forall a. HasCallStack => [Char] -> a
error [Char]
"maps are not currently supported"
  supportsPacking :: Proxy (FieldValue sch ('TMap k v)) -> Bool
supportsPacking      = [Char] -> Proxy (FieldValue sch ('TMap k v)) -> Bool
forall a. HasCallStack => [Char] -> a
error [Char]
"maps are not currently supported"
  packedFieldValueToProto :: FieldNumber -> [FieldValue sch ('TMap k v)] -> MessageBuilder
packedFieldValueToProto = [Char]
-> FieldNumber -> [FieldValue sch ('TMap k v)] -> MessageBuilder
forall a. HasCallStack => [Char] -> a
error [Char]
"maps are not currently supported"
  protoToPackedFieldValue :: Parser RawPrimitive [FieldValue sch ('TMap k v)]
protoToPackedFieldValue = [Char] -> Parser RawPrimitive [FieldValue sch ('TMap k v)]
forall a. HasCallStack => [Char] -> a
error [Char]
"maps are not currently supported"

instance TypeError ('Text "nested unions are not currently supported")
         => ProtoBridgeOneFieldValue sch ('TUnion choices) where
  defaultOneFieldValue :: Maybe (FieldValue sch ('TUnion choices))
defaultOneFieldValue = [Char] -> Maybe (FieldValue sch ('TUnion choices))
forall a. HasCallStack => [Char] -> a
error [Char]
"nested unions are not currently supported"
  oneFieldValueToProto :: FieldNumber -> FieldValue sch ('TUnion choices) -> MessageBuilder
oneFieldValueToProto = [Char]
-> FieldNumber
-> FieldValue sch ('TUnion choices)
-> MessageBuilder
forall a. HasCallStack => [Char] -> a
error [Char]
"nested unions are not currently supported"
  protoToOneFieldValue :: Parser RawPrimitive (FieldValue sch ('TUnion choices))
protoToOneFieldValue = [Char] -> Parser RawPrimitive (FieldValue sch ('TUnion choices))
forall a. HasCallStack => [Char] -> a
error [Char]
"nested unions are not currently supported"
  supportsPacking :: Proxy (FieldValue sch ('TUnion choices)) -> Bool
supportsPacking      = [Char] -> Proxy (FieldValue sch ('TUnion choices)) -> Bool
forall a. HasCallStack => [Char] -> a
error [Char]
"nested unions are not currently supported"
  packedFieldValueToProto :: FieldNumber -> [FieldValue sch ('TUnion choices)] -> MessageBuilder
packedFieldValueToProto = [Char]
-> FieldNumber
-> [FieldValue sch ('TUnion choices)]
-> MessageBuilder
forall a. HasCallStack => [Char] -> a
error [Char]
"nested unions are not currently supported"
  protoToPackedFieldValue :: Parser RawPrimitive [FieldValue sch ('TUnion choices)]
protoToPackedFieldValue = [Char] -> Parser RawPrimitive [FieldValue sch ('TUnion choices)]
forall a. HasCallStack => [Char] -> a
error [Char]
"nested unions are not currently supported"

-- UNIONS
-- ------

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

instance ( ProtoBridgeOneFieldValue sch t, KnownNat thisId
         , ProtoBridgeUnionFieldValue restIds sch ts )
         => ProtoBridgeUnionFieldValue (thisId ': restIds) sch (t ': ts) where
  unionFieldValueToProto :: NS (FieldValue sch) (t : ts) -> MessageBuilder
unionFieldValueToProto (Z FieldValue sch x
v) = FieldNumber -> FieldValue sch x -> MessageBuilder
forall tn fn (sch :: Schema tn fn) (t :: FieldType tn).
ProtoBridgeOneFieldValue sch t =>
FieldNumber -> FieldValue sch t -> MessageBuilder
oneFieldValueToProto FieldNumber
fieldId FieldValue 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 NS (FieldValue sch) xs
v) = NS (FieldValue sch) xs -> MessageBuilder
forall tn fn (ids :: [Nat]) (sch :: Schema tn fn)
       (ts :: [FieldType tn]).
ProtoBridgeUnionFieldValue ids sch ts =>
NS (FieldValue sch) ts -> MessageBuilder
unionFieldValueToProto @_ @_ @restIds NS (FieldValue sch) xs
v
  protoToUnionFieldValue :: Parser RawMessage (NS (FieldValue sch) (t : ts))
protoToUnionFieldValue
    = FieldValue sch t -> NS (FieldValue sch) (t : ts)
forall k (a :: k -> *) (x :: k) (xs :: [k]). a x -> NS a (x : xs)
Z (FieldValue sch t -> NS (FieldValue sch) (t : ts))
-> Parser RawMessage (FieldValue sch t)
-> Parser RawMessage (NS (FieldValue sch) (t : ts))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser RawMessage (FieldValue sch t)
p Parser RawMessage (NS (FieldValue sch) (t : ts))
-> Parser RawMessage (NS (FieldValue sch) (t : ts))
-> Parser RawMessage (NS (FieldValue sch) (t : ts))
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> NS (FieldValue sch) ts -> NS (FieldValue sch) (t : ts)
forall k (a :: k -> *) (xs :: [k]) (x :: k).
NS a xs -> NS a (x : xs)
S (NS (FieldValue sch) ts -> NS (FieldValue sch) (t : ts))
-> Parser RawMessage (NS (FieldValue sch) ts)
-> Parser RawMessage (NS (FieldValue sch) (t : ts))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (sch :: Schema tn fn) (ts :: [FieldType tn]).
ProtoBridgeUnionFieldValue restIds sch ts =>
Parser RawMessage (NS (FieldValue sch) ts)
forall tn fn (ids :: [Nat]) (sch :: Schema tn fn)
       (ts :: [FieldType tn]).
ProtoBridgeUnionFieldValue ids sch ts =>
Parser RawMessage (NS (FieldValue sch) ts)
protoToUnionFieldValue @_ @_ @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)
          p :: Parser RawMessage (FieldValue sch t)
p = case Maybe (FieldValue sch t)
forall tn fn (sch :: Schema tn fn) (t :: FieldType tn).
ProtoBridgeOneFieldValue sch t =>
Maybe (FieldValue sch t)
defaultOneFieldValue of
            Maybe (FieldValue sch t)
Nothing -> do Maybe (FieldValue sch t)
r <- Parser RawPrimitive (Maybe (FieldValue sch t))
-> Maybe (FieldValue sch t)
-> Parser RawField (Maybe (FieldValue sch t))
forall a. Parser RawPrimitive a -> a -> Parser RawField a
one (FieldValue sch t -> Maybe (FieldValue sch t)
forall a. a -> Maybe a
Just (FieldValue sch t -> Maybe (FieldValue sch t))
-> Parser RawPrimitive (FieldValue sch t)
-> Parser RawPrimitive (Maybe (FieldValue sch t))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser RawPrimitive (FieldValue sch t)
forall tn fn (sch :: Schema tn fn) (t :: FieldType tn).
ProtoBridgeOneFieldValue sch t =>
Parser RawPrimitive (FieldValue sch t)
protoToOneFieldValue) Maybe (FieldValue sch t)
forall a. Maybe a
Nothing Parser RawField (Maybe (FieldValue sch t))
-> FieldNumber -> Parser RawMessage (Maybe (FieldValue sch t))
forall a. Parser RawField a -> FieldNumber -> Parser RawMessage a
`at` FieldNumber
fieldId
                          Parser RawMessage (FieldValue sch t)
-> (FieldValue sch t -> Parser RawMessage (FieldValue sch t))
-> Maybe (FieldValue sch t)
-> Parser RawMessage (FieldValue sch t)
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Parser RawMessage (FieldValue sch t)
forall (f :: * -> *) a. Alternative f => f a
empty FieldValue sch t -> Parser RawMessage (FieldValue sch t)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe (FieldValue sch t)
r
            Just FieldValue sch t
d  -> Parser RawPrimitive (FieldValue sch t)
-> FieldValue sch t -> Parser RawField (FieldValue sch t)
forall a. Parser RawPrimitive a -> a -> Parser RawField a
one Parser RawPrimitive (FieldValue sch t)
forall tn fn (sch :: Schema tn fn) (t :: FieldType tn).
ProtoBridgeOneFieldValue sch t =>
Parser RawPrimitive (FieldValue sch t)
protoToOneFieldValue FieldValue sch t
d Parser RawField (FieldValue sch t)
-> FieldNumber -> Parser RawMessage (FieldValue sch t)
forall a. Parser RawField a -> FieldNumber -> Parser RawMessage a
`at` FieldNumber
fieldId Parser RawMessage (FieldValue sch t)
-> Parser RawMessage (FieldValue sch t)
-> Parser RawMessage (FieldValue sch t)
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> FieldValue sch t -> Parser RawMessage (FieldValue sch t)
forall (f :: * -> *) a. Applicative f => a -> f a
pure FieldValue sch t
d