Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only convert Scientific to Integer for nonnegative exponents #29

Merged
merged 3 commits into from
May 12, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/Language/GraphQL/Draft/Parser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import Data.Attoparsec.Text (Parser, anyChar, char, many1,
import qualified Data.Attoparsec.Text as AT
import Data.Char (isAsciiLower, isAsciiUpper,
isDigit)
import Data.Scientific (Scientific)
import Data.Scientific (Scientific, base10Exponent)
import Data.Text (find)

import qualified Language.GraphQL.Draft.Syntax as AST
Expand Down Expand Up @@ -189,8 +189,20 @@ number :: Parser (Either Scientific Integer)
number = do
(numText, num) <- match (tok scientific)
pure $ case Data.Text.find (== '.') numText of
-- Number specified with decimals, so store as a 'Scientific'
abooij marked this conversation as resolved.
Show resolved Hide resolved
Just _ -> Left num
Nothing -> Right (floor num)
-- Even if there is no '.' in the text, the number may still not
-- be integral (e.g. in 3E-7). Conversely, even a number with a
-- negative exponent may still be integral, e.g. 300E-2. But
-- the careful thing to do is to only convert to an 'Integer'
-- for numbers with nonnegative exponents. Note that we can't
-- simply delegate this decision to
-- 'Data.Scientific.floatingOrInteger' since 'Scientific' does
-- not have a 'RealFloat' instance.
Nothing ->
if base10Exponent num >= 0
then Right (floor num)
else Left num

-- This will try to pick the first type it can runParser. If you are working with
-- explicit types use the `typedValue` parser.
Expand Down