Commit 9574b051 authored by Toralf Wittner's avatar Toralf Wittner

Decode empty lists, sets and maps properly.

Empty lists, sets and maps in cassandra are represented as 'null'.
Currently we fail to decode these. This commit checks first if the
collection is null and returns an empty one in this case.
parent b6b4c489
name: cql
version: 2.0.0.5
version: 2.0.0.6
synopsis: Cassandra CQL binary protocol.
stability: experimental
license: OtherLicense
......
......@@ -179,6 +179,9 @@ instance (Cql a) => Cql [a] where
------------------------------------------------------------------------------
-- Maybe a
-- | Please note that due to the fact that Cassandra internally represents
-- empty collection type values (i.e. lists, maps and sets) as @null@, we
-- can not distinguish @Just []@ from @Nothing@ on response decoding.
instance (Cql a) => Cql (Maybe a) where
ctype = Tagged (MaybeColumn (untag (ctype :: Tagged a ColumnType)))
toCql = CqlMaybe . fmap toCql
......
......@@ -406,18 +406,19 @@ putNative v@(CqlSet _) = fail $ "putNative: collection type: " ++ show v
putNative v@(CqlMap _) = fail $ "putNative: collection type: " ++ show v
putNative v@(CqlMaybe _) = fail $ "putNative: collection type: " ++ show v
-- Note: Empty lists, maps and sets are represented as null in cassandra.
getValue :: ColumnType -> Get Value
getValue (ListColumn t) = withBytes 4 $ do
getValue (ListColumn t) = CqlList <$> (getList $ do
len <- get :: Get Word16
CqlList <$> replicateM (fromIntegral len) (withBytes 2 (getNative t))
getValue (SetColumn t) = withBytes 4 $ do
replicateM (fromIntegral len) (withBytes 2 (getNative t)))
getValue (SetColumn t) = CqlSet <$> (getList $ do
len <- get :: Get Word16
CqlSet <$> replicateM (fromIntegral len) (withBytes 2 (getNative t))
getValue (MapColumn t u) = withBytes 4 $ do
replicateM (fromIntegral len) (withBytes 2 (getNative t)))
getValue (MapColumn t u) = CqlMap <$> (getList $ do
len <- get :: Get Word16
CqlMap <$> replicateM (fromIntegral len)
((,) <$> withBytes 2 (getNative t) <*> withBytes 2 (getNative u))
getValue (MaybeColumn t) = do
replicateM (fromIntegral len)
((,) <$> withBytes 2 (getNative t) <*> withBytes 2 (getNative u)))
getValue (MaybeColumn t) = do
n <- lookAhead (get :: Get Int32)
if n < 0
then uncheckedSkip 4 >> return (CqlMaybe Nothing)
......@@ -455,6 +456,12 @@ getNative c@(SetColumn _) = fail $ "getNative: collection type: " ++ show c
getNative c@(MapColumn _ _) = fail $ "getNative: collection type: " ++ show c
getNative c@(MaybeColumn _) = fail $ "getNative: collection type: " ++ show c
getList :: Get [a] -> Get [a]
getList m = do
n <- lookAhead (get :: Get Int32)
if n < 0 then uncheckedSkip 4 >> return []
else withBytes 4 m
withBytes :: Int -> Get a -> Get a
withBytes s p = do
n <- case s of
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment