enummapset 0.6.0.3 → 0.7.0.0
raw patch · 9 files changed
+163/−29 lines, 9 filesdep +aesondep ~basedep ~containersdep ~deepseqPVP ok
version bump matches the API change (PVP)
Dependencies added: aeson
Dependency ranges changed: base, containers, deepseq
API changes (from Hackage documentation)
+ Data.EnumSet: instance Data.Aeson.Types.FromJSON.FromJSON a => Data.Aeson.Types.FromJSON.FromJSON (Data.EnumSet.EnumSet a)
+ Data.EnumSet: instance Data.Aeson.Types.ToJSON.ToJSON (Data.EnumSet.EnumSet a)
Files
- Data/EnumMap.hs +1/−1
- Data/EnumMap/Base.hs +10/−4
- Data/EnumMap/Lazy.hs +1/−1
- Data/EnumMap/Strict.hs +1/−1
- Data/EnumSet.hs +26/−5
- README.md +3/−3
- enummapset.cabal +29/−13
- tests/Json.hs +81/−0
- tests/intset-properties.hs +11/−1
Data/EnumMap.hs view
@@ -8,7 +8,7 @@ -- Module : $Header$ -- Description : Data.IntMap with Enum keys. -- Copyright : (c) 2011-2019 Michal Terepeta--- (c) 2019-2020 Mikolaj Konarski and others (see git history)+-- (c) 2019-2022 Mikolaj Konarski and others (see git history) -- License : BSD3 -- Maintainer : mikolaj.konarski@funktory.com -- Stability : alpha
Data/EnumMap/Base.hs view
@@ -7,7 +7,7 @@ -- Module : $Header$ -- Description : Data.IntMap with Enum keys. -- Copyright : (c) 2011-2019 Michal Terepeta--- (c) 2019-2020 Mikolaj Konarski and others (see git history)+-- (c) 2019-2022 Mikolaj Konarski and others (see git history) -- License : BSD3 -- Maintainer : mikolaj.konarski@funktory.com -- Stability : alpha@@ -180,7 +180,7 @@ import Data.Semigroup ( Semigroup ) import Data.Traversable ( Traversable ) import Data.Typeable ( Typeable )-+import Data.Aeson ( FromJSON(..), ToJSON(..) ) import Text.Read -- | Wrapper for 'IntMap' with 'Enum' keys.@@ -195,8 +195,14 @@ instance (Enum k, Read k, Read a) => Read (EnumMap k a) where readPrec = parens . prec 10 $ do Ident "fromList" <- lexP- list <- readPrec- return (fromList list)+ fromList <$> readPrec++instance (ToJSON a) => ToJSON (EnumMap k a) where+ toJSON = toJSON . unWrap+ toEncoding = toEncoding . unWrap++instance (FromJSON a) => FromJSON (EnumMap k a) where+ parseJSON = fmap (EnumMap . I.fromList) . parseJSON -- -- Conversion to/from 'IntMap'.
Data/EnumMap/Lazy.hs view
@@ -3,7 +3,7 @@ -- Module : $Header$ -- Description : Data.IntMap.Lazy with Enum keys. -- Copyright : (c) 2011-2019 Michal Terepeta--- (c) 2019-2020 Mikolaj Konarski and others (see git history)+-- (c) 2019-2022 Mikolaj Konarski and others (see git history) -- License : BSD3 -- Maintainer : mikolaj.konarski@funktory.com -- Stability : alpha
Data/EnumMap/Strict.hs view
@@ -3,7 +3,7 @@ -- Module : $Header$ -- Description : Data.IntMap.Strict with Enum keys. -- Copyright : (c) 2011-2019 Michal Terepeta--- (c) 2019-2020 Mikolaj Konarski and others (see git history)+-- (c) 2019-2022 Mikolaj Konarski and others (see git history) -- License : BSD3 -- Maintainer : mikolaj.konarski@funktory.com -- Stability : alpha
Data/EnumSet.hs view
@@ -1,11 +1,15 @@ {-# LANGUAGE DeriveDataTypeable #-} {-# LANGUAGE GeneralizedNewtypeDeriving #-}+{-# LANGUAGE DeriveGeneric #-}+{-# LANGUAGE DeriveAnyClass #-}+{-# LANGUAGE StandaloneDeriving #-}+{-# LANGUAGE DerivingStrategies #-} -- | -- Module : $Header$ -- Description : Data.IntSet with Enum elements. -- Copyright : (c) 2011-2019 Michal Terepeta--- (c) 2019-2020 Mikolaj Konarski and others (see git history)+-- (c) 2019-2022 Mikolaj Konarski and others (see git history) -- License : BSD3 -- Maintainer : mikolaj.konarski@funktory.com -- Stability : alpha@@ -105,12 +109,23 @@ import Data.Monoid ( Monoid ) import Data.Semigroup ( Semigroup ) import Data.Typeable ( Typeable )-+import Data.Aeson+ ( FromJSON(parseJSON), ToJSON(toEncoding, toJSON) ) import Text.Read+import GHC.Generics (Generic) -- | Wrapper for 'IntSet' with 'Enum' elements. newtype EnumSet k = EnumSet { unWrap :: IntSet }- deriving (Eq, Semigroup, Monoid, Ord, Typeable, NFData)+ deriving stock+ ( Eq+ , Ord+ , Typeable+ )+ deriving newtype+ ( Semigroup+ , Monoid+ , NFData+ ) instance (Enum k, Show k) => Show (EnumSet k) where showsPrec p ks = showParen (p > 10) $@@ -119,8 +134,14 @@ instance (Enum k, Read k) => Read (EnumSet k) where readPrec = parens . prec 10 $ do Ident "fromList" <- lexP- list <- readPrec- return (fromList list)+ fromList <$> readPrec++instance ToJSON (EnumSet a) where+ toJSON = toJSON . unWrap+ toEncoding = toEncoding . unWrap++instance (FromJSON a) => FromJSON (EnumSet a) where+ parseJSON = fmap (EnumSet . I.fromList) . parseJSON -- -- Conversion to/from 'IntSet'.
README.md view
@@ -1,7 +1,7 @@ Enummapset ========== -[](https://travis-ci.org/Mikolaj/enummapset)+[](https://github.com/Mikolaj/enummapset/actions/workflows/haskell-ci.yml) [](https://hackage.haskell.org/package/enummapset) This package contains simple wrappers around 'Data.IntMap' and@@ -11,6 +11,6 @@ Copyright --------- -Copyright (c) 2011-2019 Michal Terepeta +Copyright (c) 2011-2019 Michal Terepeta -Copyright (c) 2019-2020 Mikolaj Konarski and others (see git history)+Copyright (c) 2019-2022 Mikolaj Konarski and others (see git history)
enummapset.cabal view
@@ -1,5 +1,5 @@ name: enummapset-version: 0.6.0.3+version: 0.7.0.0 synopsis: IntMap and IntSet with Enum keys/elements. description: This package contains simple wrappers around 'Data.IntMap' and 'Data.IntSet' with 'Enum' keys and elements respectively.@@ -8,8 +8,14 @@ bug-reports: https://github.com/Mikolaj/enummapset/issues license: BSD3 license-file: LICENSE-tested-with: GHC==7.8.4, GHC==7.10.3, GHC==8.0.2, GHC==8.2.2, GHC==8.4.4, GHC==8.6.5, GHC==8.8.3, GHC==8.10.1-author: Michal Terepeta+tested-with:+ GHC==8.4.4+ , GHC==8.6.5+ , GHC==8.8.4+ , GHC==8.10.7+ , GHC==9.0.2+ , GHC==9.2.2+author: Michal Terepeta and others maintainer: Mikolaj Konarski <mikolaj.konarski@funktory.com> category: Data Structures build-type: Simple@@ -38,9 +44,10 @@ build-depends: base >= 4.6 && < 5,- containers >= 0.5.2 && < 0.7,+ aeson,+ containers >= 0.5.11 && < 0.7, semigroups >=0.1 && <1.0,- deepseq+ deepseq >= 1.2 && < 1.5 if flag(debug) ghc-options: -Wall@@ -56,11 +63,14 @@ main-is: intset-properties.hs other-modules: Data.EnumSet+ Data.EnumMap.Lazy+ Data.EnumMap.Base+ Data.EnumMap IntSetValidity+ Json type: exitcode-stdio-1.0 cpp-options: -DTESTING - build-depends: base >= 4.6 && < 5, array >= 0.4.0.0, deepseq >= 1.2 && < 1.5, ghc-prim ghc-options: -O2 default-language: Haskell2010 other-extensions: CPP, BangPatterns,@@ -68,10 +78,16 @@ KindSignatures, TypeFamilies, UndecidableInstances build-depends:- containers,- semigroups,- HUnit,- QuickCheck >= 2.7.1,- test-framework,- test-framework-hunit,- test-framework-quickcheck2+ base,+ aeson,+ array >= 0.4.0.0,+ containers,+ deepseq,+ ghc-prim,+ semigroups,++ HUnit,+ QuickCheck >= 2.7.1,+ test-framework,+ test-framework-hunit,+ test-framework-quickcheck2
+ tests/Json.hs view
@@ -0,0 +1,81 @@+{-# LANGUAGE DerivingStrategies #-}+{-# LANGUAGE DeriveGeneric #-}+{-# LANGUAGE DeriveAnyClass #-}+{-# LANGUAGE OverloadedStrings #-}+{-# LANGUAGE TypeApplications #-}+{-# LANGUAGE RankNTypes #-}++module Json where++import Data.Aeson ( decode, encode, FromJSON, ToJSON )+import Data.EnumSet ( EnumSet)+import Data.EnumMap ( EnumMap)+import qualified Data.EnumSet as ES+import qualified Data.EnumMap as EM+import Test.HUnit ( Assertion, (@?=) )+import GHC.Generics (Generic)+import Test.QuickCheck ( Property, (===))++data Creature = Cat | Dog | Fish | Wolf+ deriving stock (Eq, Show, Enum, Ord, Generic)+ deriving anyclass (ToJSON, FromJSON)++creature :: [Creature]+creature = [Cat, Dog, Fish, Dog]++creature2 :: [Creature]+creature2 = [Cat, Wolf]++creatureM :: [(Creature,String)]+creatureM = [(Cat,"C"), (Dog,"D")]++creatureMRev :: [(Creature,String)]+creatureMRev = [(Dog,"D"), (Cat,"C")]++creatureMDup :: [(Creature, String)]+creatureMDup = [(Cat,"C"), (Dog,"D"),(Dog,"DD")]++test_encode_set :: Assertion+test_encode_set = do+ encode (ES.fromList creature) @?= "[0,1,2]"+ encode (ES.fromList creature2) @?= "[0,3]"++test_decode_set :: Assertion+test_decode_set = do+ decode "[0,1,2]" @?=+ (Just (ES.fromList creature) :: Maybe (EnumSet Creature))+ decode "[0,3]" @?=+ (Just (ES.fromList creature2) :: Maybe (EnumSet Creature))++test_encode_map :: Assertion+test_encode_map = do+ encode (EM.fromSet id $ ES.fromList creature)+ @?= "[[0,\"Cat\"],[1,\"Dog\"],[2,\"Fish\"]]"+ encode (EM.fromSet id $ ES.fromList creature2) @?= "[[0,\"Cat\"],[3,\"Wolf\"]]"+ encode (EM.fromList creatureM) @?= "[[0,\"C\"],[1,\"D\"]]"+ encode (EM.fromList creatureMRev) @?= "[[0,\"C\"],[1,\"D\"]]"+ encode (EM.fromList creatureMDup) @?= "[[0,\"C\"],[1,\"DD\"]]"++test_decode_map :: Assertion+test_decode_map = do+ decode "[[0,\"Cat\"],[1,\"Dog\"],[2,\"Fish\"]]" @?=+ ((Just $ EM.fromSet id $ ES.fromList creature) :: Maybe (EnumMap Creature Creature))+ (EM.keys <$> decode @(EnumMap Creature Creature) "[[0,\"Cat\"],[1,\"Dog\"],[2,\"Fish\"]]")+ @?= Just [Cat,Dog,Fish]+ EM.toList <$> decode "[[0,\"C\"],[1,\"D\"]]" @?=+ (Just creatureM :: Maybe [(Creature, String)])++prop_json_trip_set :: [Int] -> Property+prop_json_trip_set is =+ let setNums = ES.fromList is+ in decode (encode setNums) === Just setNums++prop_json_trip_map :: [Int] -> Property+prop_json_trip_map is =+ let mapNums = EM.fromSet show $ ES.fromList is+ in decode (encode mapNums) === Just mapNums++prop_json_trip_map2 :: [(Int,String)] -> Property+prop_json_trip_map2 is =+ let mapNums = EM.fromList is+ in decode (encode mapNums) === Just mapNums
tests/intset-properties.hs view
@@ -1,6 +1,10 @@ -- Copied from https://github.com/haskell/containers and tweaked slithgly {-# LANGUAGE CPP #-}++import qualified IntSetValidity as IntSetValidity (valid)+import Json+ import Data.Bits (popCount, (.&.)) import Data.EnumSet import Data.IntSet.Internal (IntSet (Bin))@@ -9,7 +13,6 @@ import Data.Monoid (mempty) import qualified Data.Set as Set import Data.Word (Word)-import qualified IntSetValidity as IntSetValidity (valid) import Prelude hiding (filter, foldl, foldr, lookup, map, null) import Test.Framework import Test.Framework.Providers.HUnit@@ -26,6 +29,13 @@ , testCase "lookupLE" test_lookupLE , testCase "lookupGE" test_lookupGE , testCase "split" test_split+ , testCase "encode set" test_encode_set+ , testCase "encode map" test_encode_map+ , testCase "decode set" test_decode_set+ , testCase "decode map" test_decode_map+ , testProperty "prop_json_trip_set" prop_json_trip_set+ , testProperty "prop_json_trip_map" prop_json_trip_map+ , testProperty "prop_json_trip_map2" prop_json_trip_map2 , testProperty "prop_Valid" prop_Valid , testProperty "prop_EmptyValid" prop_EmptyValid , testProperty "prop_SingletonValid" prop_SingletonValid