check for unused (maybe misspelt) keys

This commit is contained in:
Rhiannon Morris 2021-12-03 11:16:05 +01:00
parent 4ce2eb2347
commit 86f05803c7
1 changed files with 28 additions and 5 deletions

View File

@ -23,6 +23,8 @@ import Data.Hashable (Hashable)
import Data.HashSet (HashSet)
import qualified Data.HashSet as HashSet
import qualified Data.Map.Strict as Map
import Data.Set (Set, (\\))
import qualified Data.Set as Set
import Data.Maybe (isJust, isNothing, fromMaybe, mapMaybe)
import Data.List (nub, sortBy)
import Data.Ord (comparing)
@ -241,8 +243,22 @@ addSuffix suf path =
pre ++ suf ++ ext
getKeys :: YAML.Mapping YAML.Pos -> YAML.Parser (Set Text)
getKeys = fmap Set.fromList . traverse (YAML.withStr "key" pure) . Map.keys
checkKeys :: YAML.Mapping YAML.Pos -> Set Text -> YAML.Parser ()
checkKeys mapping wanted = do
keys <- getKeys mapping
let unused = Set.toList $ keys \\ wanted
unless (null unused) do
fail $ "unused keys: " <> show unused
instance FromYAML Info where
parseYAML = YAML.withMap "info" \m ->
parseYAML = YAML.withMap "info" \m -> do
checkKeys m ["date", "sort", "updates", "show-updated", "unlisted", "title",
"artist", "nsfw-only", "tags", "nsfw-tags", "desc",
"nsfw-desc", "bg", "images", "thumb", "links", "extras"]
Info <$> m .: "date"
<*> m .:? "sort" .!= ""
<*> (m .:? "updates" >>= updateList)
@ -264,7 +280,8 @@ instance FromYAML Info where
instance FromYAML Artist where
parseYAML y = justName y <|> withUrl y where
justName = YAML.withStr "name" \name -> pure $ Artist {name, url = Nothing}
withUrl = YAML.withMap "full info" \m ->
withUrl = YAML.withMap "full info" \m -> do
checkKeys m ["name", "url"]
Artist <$> m .: "name" <*> m .:? "url"
instance FromYAML Desc where
@ -292,6 +309,7 @@ unlabelledImage' label' y = asStr y <|> asObj y
pure $ Image {label, path, download = Nothing,
nsfw = False, warning = Nothing}
asObj = YAML.withMap "image info" \m -> do
checkKeys m ["path", "download", "nsfw", "warning"]
path <- m .: "path"
download <- m .:? "download"
nsfw <- m .:? "nsfw" .!= False
@ -313,6 +331,7 @@ instance FromYAML Link where
asStr title = YAML.withStr "url" \url ->
pure $ Link {title, url, nsfw = False}
asObj title = YAML.withMap "link info" \m -> do
checkKeys m ["url", "nsfw"]
url <- m .: "url"
nsfw <- m .:? "nsfw" .!= False
pure $ Link {title, url, nsfw}
@ -328,6 +347,7 @@ updateList =
asDesc date = YAML.withStr "desc" \desc ->
pure $ Update {date, desc, nsfw = False, ignoreSort = False}
asObj date = YAML.withMap "update info" \m -> do
checkKeys m ["desc", "nsfw", "ignore-sort"]
desc <- m .: "desc"
nsfw <- m .:? "nsfw" .!= False
ignoreSort <- m .:? "ignore-sort" .!= False
@ -399,7 +419,8 @@ matchFilters (GalleryFilters {nsfw, artist, require, exclude}) i =
instance FromYAML GalleryInfo where
parseYAML = YAML.withMap "gallery info" \m ->
parseYAML = YAML.withMap "gallery info" \m -> do
checkKeys m ["title", "desc", "prefix", "filters", "hidden"]
GalleryInfo <$> m .: "title"
<*> m .: "desc"
<*> m .: "prefix"
@ -407,7 +428,8 @@ instance FromYAML GalleryInfo where
<*> m .:? "hidden" .!= mempty
instance FromYAML GalleryFilters where
parseYAML = YAML.withMap "gallery filters" \m ->
parseYAML = YAML.withMap "gallery filters" \m -> do
checkKeys m ["nsfw", "artist", "require", "exclude"]
GalleryFilters <$> m .:? "nsfw" .!= AllN
<*> m .:? "artist" .!= AllA
<*> m .:? "require" .!= []
@ -428,7 +450,8 @@ data IndexInfo =
deriving Show
instance FromYAML IndexInfo where
parseYAML = YAML.withMap "index info" \m ->
parseYAML = YAML.withMap "index info" \m -> do
checkKeys m ["title", "desc", "galleries", "links", "footer"]
IndexInfo <$> m .: "title"
<*> m .: "desc"
<*> m .:? "galleries" .!= []