~fgaz/haskell-ucl

7f6467b83de4de204981ed15f9467f9d6a967c79 — Francesco Gazzetta 3 years ago ad83916
Use a more precise type for UCLMap key
2 files changed, 11 insertions(+), 9 deletions(-)

M src/Data/UCL.hs
M test/ucl-test.hs
M src/Data/UCL.hs => src/Data/UCL.hs +10 -8
@@ 9,7 9,9 @@ module Data.UCL
) where

import Foreign.C
import Foreign.Ptr
  ( CUInt(..), CInt(..), CSize(..), CDouble(..), CString, CStringLen
  , newCString, peekCString )
import Foreign.Ptr (Ptr)
import System.IO.Unsafe (unsafePerformIO)
import qualified Data.Text.Foreign as TF
import Data.Text (Text)


@@ 73,8 75,8 @@ peekCStringText cstr = do
--
-- >>> parseByteString $ fromString "{a: [1,2], b: 3min, a: [4]}"
-- Right (UCLMap (fromList
--   [ (UCLText "a", UCLArray [UCLInt 1, UCLInt 2, UCLInt 4])
--   , (UCLText "b", UCLTime 180s                           )
--   [ ("a", UCLArray [UCLInt 1, UCLInt 2, UCLInt 4])
--   , ("b", UCLTime 180s                           )
--   ]))
--
-- This function is __not__ safe to call on untrusted input: configurations can


@@ 87,8 89,8 @@ parseByteString bs = useAsCStringLen bs parseCStringLen
--
-- >>> parseString "{a: [1,2], 🌅: 3min, a: [4]}"
-- Right (UCLMap (fromList
--   [ (UCLText "a"      , UCLArray [UCLInt 1, UCLInt 2, UCLInt 4])
--   , (UCLText "\127749", UCLTime 180s                           )
--   [ ("a"      , UCLArray [UCLInt 1, UCLInt 2, UCLInt 4])
--   , ("\127749", UCLTime 180s                           )
--   ]))
--
-- This function is __not__ safe to call on untrusted input: configurations can


@@ 123,7 125,7 @@ parseFile s = do
    else Left <$> (ucl_parser_get_error p >>= peekCString)

-- | An UCL object
data UCL = UCLMap (Map UCL UCL)
data UCL = UCLMap (Map Text UCL)
         | UCLArray [UCL]
         | UCLInt Int
         | UCLDouble Double


@@ 147,7 149,7 @@ typedHandleToUCL UCL_USERDATA _   = error "Userdata object"
typedHandleToUCL UCL_NULL     _   = error "Null object"
typedHandleToUCL _            _   = error "Unknown Type"

uclObjectToMap :: UCLObjectHandle -> Map UCL UCL
uclObjectToMap :: UCLObjectHandle -> Map Text UCL
uclObjectToMap o = unsafePerformIO $ do
  iter <- ucl_object_iterate_new o
  go iter Map.empty


@@ 157,7 159,7 @@ uclObjectToMap o = unsafePerformIO $ do
      case ucl_object_type obj of
        UCL_NULL -> pure m
        _        -> go it $ Map.insert (getUclKey obj) (handleToUCL obj) m
    getUclKey obj = UCLText $ unsafePerformIO $ peekCStringText $ ucl_object_key obj
    getUclKey obj = unsafePerformIO $ peekCStringText $ ucl_object_key obj

uclArrayToList :: UCLObjectHandle -> [UCL]
uclArrayToList o = unsafePerformIO $ do

M test/ucl-test.hs => test/ucl-test.hs +1 -1
@@ 13,5 13,5 @@ main = do
  print parsed1
  parsed <- parseString "\"a\": [12,34], 1:2, 1:3, 2:\"ab🌅c\", 3: yes, \"a\": [56]"
  print parsed
  unless (parsed == Right (UCLMap (fromList [(UCLText "1",UCLInt 2),(UCLText "2",UCLText "ab\127749c"),(UCLText "3",UCLBool True),(UCLText "a",UCLArray [UCLInt 12,UCLInt 34])])))
  unless (parsed == Right (UCLMap (fromList [("1",UCLInt 2),("2",UCLText "ab\127749c"),("3",UCLBool True),("a",UCLArray [UCLInt 12,UCLInt 34])])))
     exitFailure