Skip to content

Commit

Permalink
Ensuring defaulted type constraint no longer raises warning
Browse files Browse the repository at this point in the history
  • Loading branch information
brendanhay committed Dec 28, 2014
1 parent 6c6f4f1 commit 9d7aa4c
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions core/src/Network/AWS/Data/Internal/Numeric.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TemplateHaskell #-}

-- Module : Network.AWS.Data.Internal.Numeric
Expand All @@ -13,10 +14,10 @@

module Network.AWS.Data.Internal.Numeric where

import Control.Applicative
import Control.Lens.TH
import Control.Monad
import Data.Aeson.Types
import Data.Monoid
import Data.Scientific
import Network.AWS.Data.Internal.ByteString
import Network.AWS.Data.Internal.Query
Expand All @@ -43,12 +44,16 @@ newtype Nat = Nat { unNat :: Natural }
)

instance FromJSON Nat where
parseJSON = parseJSON >=> \n ->
case floatingOrInteger n of
Left _ -> fail $ "Cannot convert float to Natural: " ++ show n
parseJSON = parseJSON >=> go
where
go n = case floatingOrInteger n of
Left (_ :: Double) -> fail (floatErr n)
Right i
| n < 0 -> fail $ "Cannot convert negative number to Natural: " ++ show n
| otherwise -> pure $ Nat (fromInteger i)
| n < 0 -> fail (negateErr n)
| otherwise -> return . Nat $ fromInteger i

floatErr = mappend "Cannot convert float to Natural: " . show
negateErr = mappend "Cannot convert negative number to Natural: " . show

instance ToJSON Nat where
toJSON = Number . flip scientific 0 . toInteger . unNat
Expand Down

0 comments on commit 9d7aa4c

Please sign in to comment.