Fix boolean parsing

This commit is contained in:
Enrico Lumetti 2024-10-06 12:07:04 +02:00
parent 7874b384a5
commit 8e5112ed93
2 changed files with 5 additions and 18 deletions

View File

@ -5,7 +5,7 @@ module Bril (
parseBrilFromPath, parseBrilJSON,
)where
import Data.Aeson (FromJSON, parseJSON, withObject, withText, withScientific, (.:), (.:?), eitherDecode)
import Data.Aeson (FromJSON, parseJSON, withObject, withText, withBool, withScientific, (.:), (.:?), eitherDecode)
import Data.Aeson.Types (Parser, modifyFailure)
import qualified Data.Aeson (Value, Object, Key)
import Data.Aeson.KeyMap
@ -203,19 +203,8 @@ instance FromJSON Type where
parseValue :: Type -> Data.Aeson.Value -> Parser Value
parseValue Bool =
withText "BoolValue" $ \v ->
case v of
"true" -> return $ BoolValue True
"false" -> return $ BoolValue False
_ -> fail $ "invalid bool literal: " ++ (T.unpack v)
--parseValue Int =
-- withText "IntValue" $ \v ->
-- case ((signed decimal :: Reader Int64) v) of
-- Left x -> fail $ "invalid int literal: " ++ x
-- Right (val, t) ->
-- if (T.null t)
-- then return $ IntValue val
-- else fail $ "invalid int literal: " ++ (T.unpack v)
withBool "BoolValue" (return . BoolValue)
parseValue Int =
withScientific "IntValue" $ \v ->
if (not (isInteger v))

View File

@ -20,11 +20,9 @@ testTypes = TestCase $ do
testLiterals :: Test
testLiterals = TestCase $ do
assertEqual "true literal"
(parseLiteral B.Bool ("true"::Text)) (Success $ B.BoolValue True)
(parseLiteral B.Bool True) (Success $ B.BoolValue True)
assertEqual "false literal"
(parseLiteral B.Bool ("false"::Text)) (Success $ B.BoolValue False)
assertEqual "wrong bool literal"
(parseLiteral B.Bool ("xxx"::Text)) (Error "invalid bool literal: xxx")
(parseLiteral B.Bool False) (Success $ B.BoolValue False)
assertEqual "int literal"
(parseLiteral B.Int (1434 :: Int)) (Success $ B.IntValue 1434)