{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiWayIf #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Data.Avro.Internal.Get
where
import qualified Codec.Compression.Zlib as Z
import Control.Monad (replicateM, when)
import Data.Binary.Get (Get)
import qualified Data.Binary.Get as G
import Data.Binary.IEEE754 as IEEE
import Data.Bits
import Data.ByteString (ByteString)
import qualified Data.ByteString.Lazy as BL
import qualified Data.ByteString.Lazy.Char8 as BC
import Data.Int
import qualified Data.Map as Map
import Data.Maybe
import Data.Monoid ((<>))
import qualified Data.Set as Set
import Data.Text (Text)
import qualified Data.Text as Text
import qualified Data.Text.Encoding as Text
import qualified Data.Vector as V
import Prelude as P
import Data.Avro.Internal.DecodeRaw
getBoolean :: Get Bool
getBoolean :: Get Bool
getBoolean =
do Word8
w <- Get Word8
G.getWord8
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$! (Word8
w forall a. Eq a => a -> a -> Bool
== Word8
0x01)
getInt :: Get Int32
getInt :: Get Int32
getInt = forall i. (Bits i, Integral i, DecodeRaw i) => Get i
getZigZag
getLong :: Get Int64
getLong :: Get Int64
getLong = forall i. (Bits i, Integral i, DecodeRaw i) => Get i
getZigZag
getZigZag :: (Bits i, Integral i, DecodeRaw i) => Get i
getZigZag :: forall i. (Bits i, Integral i, DecodeRaw i) => Get i
getZigZag = forall a. DecodeRaw a => Get a
decodeRaw
getBytes :: Get ByteString
getBytes :: Get ByteString
getBytes = Get Int64
getLong forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Int -> Get ByteString
G.getByteString forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (Integral a, Num b) => a -> b
fromIntegral)
getBytesLazy :: Get BL.ByteString
getBytesLazy :: Get ByteString
getBytesLazy = Get Int64
getLong forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Int64 -> Get ByteString
G.getLazyByteString forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (Integral a, Num b) => a -> b
fromIntegral)
getString :: Get Text
getString :: Get Text
getString = do
ByteString
bytes <- Get ByteString
getBytes
case ByteString -> Either UnicodeException Text
Text.decodeUtf8' ByteString
bytes of
Left UnicodeException
unicodeExc -> forall (m :: * -> *) a. MonadFail m => String -> m a
fail (forall a. Show a => a -> String
show UnicodeException
unicodeExc)
Right Text
text -> forall (m :: * -> *) a. Monad m => a -> m a
return Text
text
getFloat :: Get Float
getFloat :: Get Float
getFloat = Word32 -> Float
IEEE.wordToFloat forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get Word32
G.getWord32le
getDouble :: Get Double
getDouble :: Get Double
getDouble = Word64 -> Double
IEEE.wordToDouble forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get Word64
G.getWord64le
decodeBlocks :: Get a -> Get [a]
decodeBlocks :: forall a. Get a -> Get [a]
decodeBlocks Get a
element = do
Int64
count <- Get Int64
getLong
if | Int64
count forall a. Eq a => a -> a -> Bool
== Int64
0 -> forall (m :: * -> *) a. Monad m => a -> m a
return []
| Int64
count forall a. Ord a => a -> a -> Bool
< Int64
0 -> do
Int64
_bytes <- Get Int64
getLong
[a]
items <- forall (m :: * -> *) a. Applicative m => Int -> m a -> m [a]
replicateM (forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ forall a. Num a => a -> a
abs Int64
count) Get a
element'
[a]
rest <- forall a. Get a -> Get [a]
decodeBlocks Get a
element
forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ [a]
items forall a. Semigroup a => a -> a -> a
<> [a]
rest
| Bool
otherwise -> do
[a]
items <- forall (m :: * -> *) a. Applicative m => Int -> m a -> m [a]
replicateM (forall a b. (Integral a, Num b) => a -> b
fromIntegral Int64
count) Get a
element'
[a]
rest <- forall a. Get a -> Get [a]
decodeBlocks Get a
element
forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ [a]
items forall a. Semigroup a => a -> a -> a
<> [a]
rest
where element' :: Get a
element' = do
!a
x <- Get a
element
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
x
sFromIntegral :: forall a b m. (Monad m, Bounded a, Bounded b, Integral a, Integral b) => a -> m b
sFromIntegral :: forall a b (m :: * -> *).
(Monad m, Bounded a, Bounded b, Integral a, Integral b) =>
a -> m b
sFromIntegral a
a
| Integer
aI forall a. Ord a => a -> a -> Bool
> forall a b. (Integral a, Num b) => a -> b
fromIntegral (forall a. Bounded a => a
maxBound :: b) Bool -> Bool -> Bool
||
Integer
aI forall a. Ord a => a -> a -> Bool
< forall a b. (Integral a, Num b) => a -> b
fromIntegral (forall a. Bounded a => a
minBound :: b) = forall a. HasCallStack => String -> a
error String
"Integral overflow."
| Bool
otherwise = forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b. (Integral a, Num b) => a -> b
fromIntegral a
a)
where aI :: Integer
aI = forall a b. (Integral a, Num b) => a -> b
fromIntegral a
a :: Integer