on master help sign in
Merge pull request #363 from phadej/nats-old

Use nats package to provided Natural instances with old bases

commitf69ed10b1b2486d7fa00eb31b166671e3c67ed46 authorAdam Bergmark <adam@bergmark.nl> dateTue 1 Mar 2016, 18:11 (11 years ago) parent692d3ddfb299ae
4 files changed+20−4
Data/Aeson/Types/Instances.hs+1−3 aeson.cabal+8−1 tests/Instances.hs+9 tests/Properties.hs+2
Data/Aeson/Types/Instances.hs+1−3
@@ -86,6 +86,7 @@ import Data.Word (Word8, Word16, Word32, Word64)
8686import Data.Version (Version, showVersion, parseVersion)
8787import Text.ParserCombinators.ReadP (readP_to_S)
8888import Foreign.Storable (Storable)
89+import Numeric.Natural (Natural)
8990import Prelude hiding (foldr)
9091import qualified Data.Aeson.Encode.Builder as E
9192import qualified Data.Aeson.Parser.Time as Time
@@ -111,7 +112,6 @@ import qualified Data.Vector.Storable as VS
111112import qualified Data.Vector.Unboxed as VU
112113
113114#if MIN_VERSION_base(4,8,0)
114−import Numeric.Natural
115115#else
116116import Control.Applicative ((<$>), (<*>), pure)
117117import Data.Monoid (mempty)
@@ -353,7 +353,6 @@ instance FromJSON Integer where
353353 parseJSON = withScientific "Integral" $ pure . truncate
354354 {-# INLINE parseJSON #-}
355355
356−#if MIN_VERSION_base(4,8,0)
357356instance ToJSON Natural where
358357 toJSON = toJSON . toInteger
359358 {-# INLINE toJSON #-}
@@ -366,7 +365,6 @@ instance FromJSON Natural where
366365 if Scientific.coefficient s < 0
367366 then fail $ "Expected a Natural number but got the negative number: " <> show s
368367 else pure $ truncate s
369−#endif
370368
371369instance ToJSON Int8 where
372370 toJSON = Number . fromIntegral
aeson.cabal+8−1
@@ -107,9 +107,13 @@ library
107107 vector >= 0.7.1
108108
109109 if !impl(ghc >= 8.0)
110− -- `Data.Semigroup` is available in base only since GHC 8.0
110+ -- `Data.Semigroup` is available in base only since GHC 8.0 / base 4.9
111111 build-depends: semigroups >= 0.16.1
112112
113+ if !impl(ghc >= 7.10)
114+ -- `Numeric.Natural` is available in base only since GHC 7.10 / base 4.8
115+ build-depends: nats >=1 && <1.1
116+
113117 if flag(old-locale)
114118 build-depends: time < 1.5, old-locale
115119 else
@@ -167,6 +171,9 @@ test-suite tests
167171 else
168172 build-depends: time >= 1.5
169173
174+ if !impl(ghc >= 7.10)
175+ build-depends: nats >=1 && <1.1
176+
170177source-repository head
171178 type: git
172179 location: git://github.com/bos/aeson.git
tests/Instances.hs+9
@@ -21,6 +21,10 @@ import Functions
2121
2222import Test.QuickCheck.Instances ()
2323
24+#if !MIN_VERSION_base(4,8,0) && !MIN_VERSION_QuickCheck(2,8,3)
25+import Numeric.Natural
26+#endif
27+
2428-- "System" types.
2529
2630instance Arbitrary DotNetTime where
@@ -145,3 +149,8 @@ instance Arbitrary Version where
145149makeVersion :: [Int] -> Version
146150makeVersion b = Version b []
147151#endif
152+
153+#if !MIN_VERSION_base(4,8,0) && !MIN_VERSION_QuickCheck(2,8,3)
154+instance Arbitrary Natural where
155+ arbitrary = fromInteger . abs <$> arbitrary
156+#endif
tests/Properties.hs+2
@@ -12,6 +12,7 @@ import Data.Int (Int8)
1212import Data.Time (Day, LocalTime, NominalDiffTime, TimeOfDay, UTCTime,
1313 ZonedTime)
1414import Data.Version (Version)
15+import Numeric.Natural (Natural)
1516import Encoders
1617import Instances ()
1718import Test.Framework (Test, testGroup)
@@ -139,6 +140,7 @@ tests = testGroup "properties" [
139140 , testProperty "ZonedTime" $ roundTripEq (undefined :: ZonedTime)
140141 , testProperty "NominalDiffTime" $ roundTripEq (undefined :: NominalDiffTime)
141142 , testProperty "Version" $ roundTripEq (undefined :: Version)
143+ , testProperty "Natural" $ roundTripEq (undefined :: Natural)
142144 , testGroup "ghcGenerics" [
143145 testProperty "OneConstructor" $ roundTripEq OneConstructor
144146 , testProperty "Product2" $ roundTripEq (undefined :: Product2 Int Bool)