From 46b6f2d08f6d3ab7223e36b9055e490e40ef9602 Mon Sep 17 00:00:00 2001 From: Francesco Gazzetta Date: Mon, 14 Mar 2022 18:13:27 +0100 Subject: [PATCH] Fix null truncating lists and maps For now we error out, since adding UCLNull will be a breaking change --- src/Data/UCL.hs | 25 +++++++++++-------------- test/ucl-test.hs | 2 ++ 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/src/Data/UCL.hs b/src/Data/UCL.hs index f1961ea..6abadc8 100644 --- a/src/Data/UCL.hs +++ b/src/Data/UCL.hs @@ -11,7 +11,7 @@ module Data.UCL import Foreign.C ( CUInt(..), CInt(..), CSize(..), CDouble(..), CString, CStringLen , withCString, withCStringLen, peekCString ) -import Foreign.Ptr (Ptr, FunPtr) +import Foreign.Ptr (Ptr, FunPtr, nullPtr) import Foreign.ForeignPtr import qualified Data.Text.Foreign as TF import Data.Text (Text) @@ -172,6 +172,7 @@ foreignToUCL obj = do UCL_TIME -> UCLTime . realToFrac <$> ucl_object_todouble obj -- TODO use Left instead of error UCL_USERDATA -> error "Userdata object" + -- TODO add UCLNull UCL_NULL -> error "Null object" _ -> error "Unknown Type" @@ -184,14 +185,12 @@ uclObjectToMap o = do -- NOTE: the reference count of the returned object is not increased, -- so we don't use ForeignPtr obj <- withForeignPtr it (`ucl_object_iterate_safe` True) - ty <- ucl_object_type obj - case ty of - -- FIXME this is not how we check for end of object - UCL_NULL -> pure m - _ -> do - k <- ucl_object_key obj >>= peekCStringText - v <- foreignToUCL obj - go it $ Map.insert k v m + if obj == nullPtr + then pure m + else do + k <- ucl_object_key obj >>= peekCStringText + v <- foreignToUCL obj + go it $ Map.insert k v m uclArrayToList :: Ptr UCLObject -> IO [UCL] uclArrayToList o = do @@ -202,8 +201,6 @@ uclArrayToList o = do -- NOTE: the reference count of the returned object is not increased -- so we don't use ForeignPtr obj <- withForeignPtr it (`ucl_object_iterate_safe` True) - ty <- ucl_object_type obj - case ty of - -- FIXME this is not how we check for end of object - UCL_NULL -> pure [] - _ -> (:) <$> foreignToUCL obj <*> go it + if obj == nullPtr + then pure [] + else (:) <$> foreignToUCL obj <*> go it diff --git a/test/ucl-test.hs b/test/ucl-test.hs index 5476034..0b55b7a 100644 --- a/test/ucl-test.hs +++ b/test/ucl-test.hs @@ -18,6 +18,8 @@ main = do , ("3",UCLBool True) , ("a",UCLArray [UCLInt 12, UCLInt 34, UCLInt 56]) , ("b",UCLText "foo")])) + -- TODO add UCLNull + --, ("0: false, 1: null, 2: true", UCLMap $ fromList [("0", UCLBool False), ("1", UCLNull), ("2", UCLBool True)]) ] unless success exitFailure -- 2.45.2