Fix boolean parsing
This commit is contained in:
parent
7874b384a5
commit
8e5112ed93
17
lib/Bril.hs
17
lib/Bril.hs
|
|
@ -5,7 +5,7 @@ module Bril (
|
||||||
parseBrilFromPath, parseBrilJSON,
|
parseBrilFromPath, parseBrilJSON,
|
||||||
)where
|
)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 Data.Aeson.Types (Parser, modifyFailure)
|
||||||
import qualified Data.Aeson (Value, Object, Key)
|
import qualified Data.Aeson (Value, Object, Key)
|
||||||
import Data.Aeson.KeyMap
|
import Data.Aeson.KeyMap
|
||||||
|
|
@ -203,19 +203,8 @@ instance FromJSON Type where
|
||||||
|
|
||||||
parseValue :: Type -> Data.Aeson.Value -> Parser Value
|
parseValue :: Type -> Data.Aeson.Value -> Parser Value
|
||||||
parseValue Bool =
|
parseValue Bool =
|
||||||
withText "BoolValue" $ \v ->
|
withBool "BoolValue" (return . BoolValue)
|
||||||
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)
|
|
||||||
parseValue Int =
|
parseValue Int =
|
||||||
withScientific "IntValue" $ \v ->
|
withScientific "IntValue" $ \v ->
|
||||||
if (not (isInteger v))
|
if (not (isInteger v))
|
||||||
|
|
|
||||||
|
|
@ -20,11 +20,9 @@ testTypes = TestCase $ do
|
||||||
testLiterals :: Test
|
testLiterals :: Test
|
||||||
testLiterals = TestCase $ do
|
testLiterals = TestCase $ do
|
||||||
assertEqual "true literal"
|
assertEqual "true literal"
|
||||||
(parseLiteral B.Bool ("true"::Text)) (Success $ B.BoolValue True)
|
(parseLiteral B.Bool True) (Success $ B.BoolValue True)
|
||||||
assertEqual "false literal"
|
assertEqual "false literal"
|
||||||
(parseLiteral B.Bool ("false"::Text)) (Success $ B.BoolValue False)
|
(parseLiteral B.Bool False) (Success $ B.BoolValue False)
|
||||||
assertEqual "wrong bool literal"
|
|
||||||
(parseLiteral B.Bool ("xxx"::Text)) (Error "invalid bool literal: xxx")
|
|
||||||
|
|
||||||
assertEqual "int literal"
|
assertEqual "int literal"
|
||||||
(parseLiteral B.Int (1434 :: Int)) (Success $ B.IntValue 1434)
|
(parseLiteral B.Int (1434 :: Int)) (Success $ B.IntValue 1434)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue