~fgaz/haskell-ucl

46b6f2d08f6d3ab7223e36b9055e490e40ef9602 — Francesco Gazzetta 2 years ago 099fe34
Fix null truncating lists and maps

For now we error out, since adding UCLNull will be a breaking change
2 files changed, 13 insertions(+), 14 deletions(-)

M src/Data/UCL.hs
M test/ucl-test.hs
M src/Data/UCL.hs => src/Data/UCL.hs +11 -14
@@ 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

M test/ucl-test.hs => test/ucl-test.hs +2 -0
@@ 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