From 7f6467b83de4de204981ed15f9467f9d6a967c79 Mon Sep 17 00:00:00 2001 From: Francesco Gazzetta Date: Sun, 7 Feb 2021 17:54:57 +0100 Subject: [PATCH] Use a more precise type for UCLMap key --- src/Data/UCL.hs | 18 ++++++++++-------- test/ucl-test.hs | 2 +- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/Data/UCL.hs b/src/Data/UCL.hs index aadc469..3f9ee63 100644 --- a/src/Data/UCL.hs +++ b/src/Data/UCL.hs @@ -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 diff --git a/test/ucl-test.hs b/test/ucl-test.hs index 19474fc..94aa7b5 100644 --- a/test/ucl-test.hs +++ b/test/ucl-test.hs @@ -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 -- 2.45.2