summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDanny Navarro <j@dannynavarro.net>2017-02-26 17:22:06 -0300
committerDanny Navarro <j@dannynavarro.net>2017-02-28 17:22:06 -0300
commite521d92c7f5bfcce3f3c9e255bfd6a4902cb1809 (patch)
tree07ad19dfc82fb72e24dfba805f49b2b597cdc452
parent1b8fca3658215c69402e2bc0f0c46d28e46d70e2 (diff)
downloadgraphql-e521d92c7f5bfcce3f3c9e255bfd6a4902cb1809.tar.gz
Use builtin scientific `toBoundInteger` to check for Int32 bounds
-rw-r--r--Data/GraphQL/Parser.hs20
1 files changed, 9 insertions, 11 deletions
diff --git a/Data/GraphQL/Parser.hs b/Data/GraphQL/Parser.hs
index 29a051d..df91e99 100644
--- a/Data/GraphQL/Parser.hs
+++ b/Data/GraphQL/Parser.hs
@@ -9,10 +9,9 @@ import Control.Applicative ((<|>), Alternative, empty, many, optional)
import Control.Monad (when)
import Data.Char (isDigit, isSpace)
import Data.Foldable (traverse_)
-import Data.Int (Int32)
import Data.Monoid ((<>))
import Data.List.NonEmpty (NonEmpty((:|)))
-import Data.Scientific (floatingOrInteger)
+import Data.Scientific (floatingOrInteger, scientific, toBoundedInteger)
import Data.Text (Text, append)
import Data.Attoparsec.Combinator (lookAhead)
@@ -26,10 +25,10 @@ import Data.Attoparsec.Text
, manyTill
, option
, peekChar
- , scientific
, takeWhile
, takeWhile1
)
+import qualified Data.Attoparsec.Text as Attoparsec (scientific)
import Data.GraphQL.AST
@@ -145,14 +144,13 @@ value = ValueVariable <$> variable
<|> False <$ tok "false"
floatOrInt32Value :: Parser Value
- floatOrInt32Value = do
- n <- scientific
- case (floatingOrInteger n :: Either Double Integer) of
- Left dbl -> return $ ValueFloat dbl
- Right i ->
- if i < (-2147483648) || i >= 2147483648
- then fail "Integer value is out of range."
- else return $ ValueInt (fromIntegral i :: Int32)
+ floatOrInt32Value =
+ Attoparsec.scientific >>=
+ either (pure . ValueFloat)
+ (maybe (fail "Integer value is out of range.")
+ (pure . ValueInt)
+ . toBoundedInteger . (`scientific` 1))
+ . floatingOrInteger
-- TODO: Escape characters. Look at `jsstring_` in aeson package.
stringValue :: Parser Text