aboutsummaryrefslogtreecommitdiff
path: root/cmd/Main/DistrictInfo.hs
diff options
context:
space:
mode:
Diffstat (limited to 'cmd/Main/DistrictInfo.hs')
-rw-r--r--cmd/Main/DistrictInfo.hs34
1 files changed, 34 insertions, 0 deletions
diff --git a/cmd/Main/DistrictInfo.hs b/cmd/Main/DistrictInfo.hs
new file mode 100644
index 0000000..f4f9ff1
--- /dev/null
+++ b/cmd/Main/DistrictInfo.hs
@@ -0,0 +1,34 @@
+{-# LANGUAGE DeriveAnyClass #-}
+{-# LANGUAGE DeriveGeneric #-}
+
+module Main.DistrictInfo
+ ( DistrictInfo(..)
+ , di2df
+ , df2di
+ ) where
+
+import Control.Arrow ((&&&))
+import GHC.Generics (Generic)
+
+import Data.Aeson (FromJSON, ToJSON)
+import qualified Data.HashMap.Strict as HM
+
+import Malodivo.Budget (DistrictFunds)
+import Malodivo.Types.District (District)
+
+{-|
+We use this data type instead of 'DistrictFunds', 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, and build 'DistrictFunds' out of it.
+-}
+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