aboutsummaryrefslogtreecommitdiff
path: root/lib/Malodivo/Types/District.hs
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Malodivo/Types/District.hs')
-rw-r--r--lib/Malodivo/Types/District.hs34
1 files changed, 27 insertions, 7 deletions
diff --git a/lib/Malodivo/Types/District.hs b/lib/Malodivo/Types/District.hs
index 058377b..2e78634 100644
--- a/lib/Malodivo/Types/District.hs
+++ b/lib/Malodivo/Types/District.hs
@@ -18,11 +18,17 @@ Just [Lakos]
module Malodivo.Types.District
( District(..)
+ , DistrictFunds
+ , DistrictInfo(..)
+ , di2df
+ , df2di
) where
+import Control.Arrow ((&&&))
import GHC.Generics (Generic)
import Data.Aeson (FromJSON, ToJSON)
+import qualified Data.HashMap.Strict as HM
import Data.Hashable (Hashable)
-- | District of the Kindom of Malodivo.
@@ -30,10 +36,24 @@ data District
= Palolene
| SouthernPalolene
| Lakos
- deriving ( Eq
- , Hashable
- , Show
- , Generic
- , FromJSON
- , ToJSON
- )
+ deriving (Eq, Hashable, Show, Generic, FromJSON, ToJSON)
+
+-- | Convenient type.
+type DistrictFunds = HM.HashMap District Integer
+
+{-|
+We use this data type instead of 'DistrictFunds' for JSON,
+because maps other than @HashMap Text _@ are hard to decode/encode see
+<https://github.com/bos/aeson/issues/79>. Anyway, we still have type-checked
+typos-proof structure.
+-}
+data DistrictInfo = DistrictInfo
+ { name :: District
+ , amount :: Integer
+ } deriving (Generic, FromJSON, ToJSON)
+
+di2df :: [DistrictInfo] -> DistrictFunds
+di2df = HM.fromListWith (+) . map (name &&& amount)
+
+df2di :: DistrictFunds -> [DistrictInfo]
+df2di = map (\(n, a) -> DistrictInfo {name = n, amount = a}) . HM.toList