Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Map decoding in presence of optional long byte-length marker #193

Merged
merged 3 commits into from
Nov 10, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions src/Data/Avro/Encoding/FromAvro.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module Data.Avro.Encoding.FromAvro
where

import Control.DeepSeq (NFData)
import Control.Monad (forM, replicateM)
import Control.Monad (forM, replicateM, void, when)
import Control.Monad.Identity (Identity (..))
import qualified Data.Avro.Internal.Get as Get
import Data.Avro.Internal.Time
Expand Down Expand Up @@ -195,7 +195,6 @@ instance FromAvro Time.LocalTime where
Right $ millisToLocalTime (toInteger n)
fromAvro x = Left ("Unable to decode LocalTime from: " <> show (describeValue x))
{-# INLINE fromAvro #-}


instance FromAvro a => FromAvro [a] where
fromAvro (Array vec) = mapM fromAvro $ V.toList vec
Expand Down Expand Up @@ -301,22 +300,33 @@ getField env sch = case sch of
v <- getField env t
pure $ Union sch ix v


-- | Read a Map from blocks of KV pairs
getKVBlocks :: HashMap Schema.TypeName ReadSchema -> ReadSchema -> Get [[(Text, Value)]]
getKVBlocks env t = do
blockLength <- abs <$> Get.getLong
if blockLength == 0 then
lengthIndicator <- Get.getLong
if lengthIndicator == 0 then
return []
else do
-- When the block's count is negative, its absolute value is used, and the count is followed immediately by a
-- long block size indicating the number of bytes in the block.
when (lengthIndicator < 0) $ void Get.getLong -- number of bytes in block (ignored)
let blockLength = abs lengthIndicator
vs <- replicateM (fromIntegral blockLength) ((,) <$> Get.getString <*> getField env t)
(vs:) <$> getKVBlocks env t
{-# INLINE getKVBlocks #-}

-- | Read an array from blocks.
getBlocksOf :: HashMap Schema.TypeName ReadSchema -> ReadSchema -> Get [[Value]]
getBlocksOf env t = do
blockLength <- abs <$> Get.getLong
if blockLength == 0 then
lengthIndicator <- Get.getLong
if lengthIndicator == 0 then
return []
else do
-- When the block's count is negative, its absolute value is used, and the count is followed immediately by a
-- long block size indicating the number of bytes in the block.
when (lengthIndicator < 0) $ void Get.getLong -- number of bytes in block (ignored)
let blockLength = abs lengthIndicator
vs <- replicateM (fromIntegral blockLength) (getField env t)
(vs:) <$> getBlocksOf env t

Expand Down
Loading