Commit f4246b5a authored by Alfredo Di Napoli's avatar Alfredo Di Napoli

HasDbid small refactoring

parent 1829c501
...@@ -137,27 +137,34 @@ class HasDBid a where ...@@ -137,27 +137,34 @@ class HasDBid a where
-- NOTE: We try to use numeric codes for countries -- NOTE: We try to use numeric codes for countries
-- https://en.wikipedia.org/wiki/List_of_ISO_3166_country_codes -- https://en.wikipedia.org/wiki/List_of_ISO_3166_country_codes
-- https://en.wikipedia.org/wiki/ISO_3166-1_numeric#004 -- https://en.wikipedia.org/wiki/ISO_3166-1_numeric#004
dbIds :: [(Lang, Int)] -- The pattern matching ensures this mapping will always be total
dbIds = [ (All, 0 ) -- once we add a new 'Lang'.
, (DE , 276) lang2id :: Map Lang Int
, (EL , 300) lang2id = Map.fromList $ allLangs <&> \lid -> case lid of
, (EN , 2 ) All -> (lid, 0)
, (ES , 724) DE -> (lid, 276)
, (FR , 1 ) EL -> (lid, 300)
, (IT , 380) EN -> (lid, 2)
, (PL , 616) ES -> (lid, 724)
, (PT , 620) FR -> (lid, 1)
, (RU , 643) IT -> (lid, 380)
, (UK , 804) PL -> (lid, 616)
, (ZH , 156) PT -> (lid, 620)
] RU -> (lid, 643)
UK -> (lid, 804)
ZH -> (lid, 156)
-- | /static/ conversion map between an 'Int' and a 'Lang'. Automatically kept up-to-date
-- as it's derived from 'lang2id'.
id2lang :: Map Int Lang
id2lang = Map.fromList . map swap . Map.toList $ lang2id
instance HasDBid Lang where instance HasDBid Lang where
toDBid lang = case Map.lookup lang $ Map.fromList dbIds of -- /NOTE/ this lookup cannot fail because 'dbIds' is defined as a total function
Just la -> la -- over its domain.
Nothing -> panic "[G.Core] Add this lang to DB ids" toDBid lang = lang2id Map.! lang
fromDBid dbId = case Map.lookup dbId $ Map.fromList $ map swap dbIds of fromDBid dbId = case Map.lookup dbId id2lang of
Just la -> la Just la -> la
Nothing -> panic "HasDBid lang, not implemented" Nothing -> panic "HasDBid lang, not implemented"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment