Skip to content

Commit

Permalink
Avoid loss of precision when parsing numbers (hasura#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
Auke Booij authored and Antoine Leblanc committed Jul 19, 2020
1 parent 4a77332 commit 511980a
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/Language/GraphQL/Draft/Parser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,11 @@ fragmentDefinition = AST.FragmentDefinition
number :: Parser (Either Scientific Integer)
number = do
(numText, num) <- match (tok scientific)
pure $ case Data.Text.find (== '.') numText of
pure $ case Data.Text.find (\c -> c == '.' || c == 'e' || c == 'E') numText of
-- Number specified with decimals and/or scientific notation, so
-- store as a 'Scientific'.
Just _ -> Left num
-- No '.' and not in scientific notation, so safe to convert to integer.
Nothing -> Right (floor num)

-- This will try to pick the first type it can runParser. If you are working with
Expand Down

0 comments on commit 511980a

Please sign in to comment.