Use builtin scientific toBoundInteger
to check for Int32 bounds
This commit is contained in:
parent
1b8fca3658
commit
e521d92c7f
@ -9,10 +9,9 @@ import Control.Applicative ((<|>), Alternative, empty, many, optional)
|
|||||||
import Control.Monad (when)
|
import Control.Monad (when)
|
||||||
import Data.Char (isDigit, isSpace)
|
import Data.Char (isDigit, isSpace)
|
||||||
import Data.Foldable (traverse_)
|
import Data.Foldable (traverse_)
|
||||||
import Data.Int (Int32)
|
|
||||||
import Data.Monoid ((<>))
|
import Data.Monoid ((<>))
|
||||||
import Data.List.NonEmpty (NonEmpty((:|)))
|
import Data.List.NonEmpty (NonEmpty((:|)))
|
||||||
import Data.Scientific (floatingOrInteger)
|
import Data.Scientific (floatingOrInteger, scientific, toBoundedInteger)
|
||||||
|
|
||||||
import Data.Text (Text, append)
|
import Data.Text (Text, append)
|
||||||
import Data.Attoparsec.Combinator (lookAhead)
|
import Data.Attoparsec.Combinator (lookAhead)
|
||||||
@ -26,10 +25,10 @@ import Data.Attoparsec.Text
|
|||||||
, manyTill
|
, manyTill
|
||||||
, option
|
, option
|
||||||
, peekChar
|
, peekChar
|
||||||
, scientific
|
|
||||||
, takeWhile
|
, takeWhile
|
||||||
, takeWhile1
|
, takeWhile1
|
||||||
)
|
)
|
||||||
|
import qualified Data.Attoparsec.Text as Attoparsec (scientific)
|
||||||
|
|
||||||
import Data.GraphQL.AST
|
import Data.GraphQL.AST
|
||||||
|
|
||||||
@ -145,14 +144,13 @@ value = ValueVariable <$> variable
|
|||||||
<|> False <$ tok "false"
|
<|> False <$ tok "false"
|
||||||
|
|
||||||
floatOrInt32Value :: Parser Value
|
floatOrInt32Value :: Parser Value
|
||||||
floatOrInt32Value = do
|
floatOrInt32Value =
|
||||||
n <- scientific
|
Attoparsec.scientific >>=
|
||||||
case (floatingOrInteger n :: Either Double Integer) of
|
either (pure . ValueFloat)
|
||||||
Left dbl -> return $ ValueFloat dbl
|
(maybe (fail "Integer value is out of range.")
|
||||||
Right i ->
|
(pure . ValueInt)
|
||||||
if i < (-2147483648) || i >= 2147483648
|
. toBoundedInteger . (`scientific` 1))
|
||||||
then fail "Integer value is out of range."
|
. floatingOrInteger
|
||||||
else return $ ValueInt (fromIntegral i :: Int32)
|
|
||||||
|
|
||||||
-- TODO: Escape characters. Look at `jsstring_` in aeson package.
|
-- TODO: Escape characters. Look at `jsstring_` in aeson package.
|
||||||
stringValue :: Parser Text
|
stringValue :: Parser Text
|
||||||
|
Loading…
Reference in New Issue
Block a user