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 Data.HashSet (HashSet)
import qualified Data.HashSet as HashSet import qualified Data.HashSet as HashSet
import qualified Data.Map.Strict as Map 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.Maybe (isJust, isNothing, fromMaybe, mapMaybe)
import Data.List (nub, sortBy) import Data.List (nub, sortBy)
import Data.Ord (comparing) import Data.Ord (comparing)
@ -241,8 +243,22 @@ addSuffix suf path =
pre ++ suf ++ ext 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 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" Info <$> m .: "date"
<*> m .:? "sort" .!= "" <*> m .:? "sort" .!= ""
<*> (m .:? "updates" >>= updateList) <*> (m .:? "updates" >>= updateList)
@ -264,7 +280,8 @@ instance FromYAML Info where
instance FromYAML Artist where instance FromYAML Artist where
parseYAML y = justName y <|> withUrl y where parseYAML y = justName y <|> withUrl y where
justName = YAML.withStr "name" \name -> pure $ Artist {name, url = Nothing} 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" Artist <$> m .: "name" <*> m .:? "url"
instance FromYAML Desc where instance FromYAML Desc where
@ -292,6 +309,7 @@ unlabelledImage' label' y = asStr y <|> asObj y
pure $ Image {label, path, download = Nothing, pure $ Image {label, path, download = Nothing,
nsfw = False, warning = Nothing} nsfw = False, warning = Nothing}
asObj = YAML.withMap "image info" \m -> do asObj = YAML.withMap "image info" \m -> do
checkKeys m ["path", "download", "nsfw", "warning"]
path <- m .: "path" path <- m .: "path"
download <- m .:? "download" download <- m .:? "download"
nsfw <- m .:? "nsfw" .!= False nsfw <- m .:? "nsfw" .!= False
@ -313,6 +331,7 @@ instance FromYAML Link where
asStr title = YAML.withStr "url" \url -> asStr title = YAML.withStr "url" \url ->
pure $ Link {title, url, nsfw = False} pure $ Link {title, url, nsfw = False}
asObj title = YAML.withMap "link info" \m -> do asObj title = YAML.withMap "link info" \m -> do
checkKeys m ["url", "nsfw"]
url <- m .: "url" url <- m .: "url"
nsfw <- m .:? "nsfw" .!= False nsfw <- m .:? "nsfw" .!= False
pure $ Link {title, url, nsfw} pure $ Link {title, url, nsfw}
@ -328,6 +347,7 @@ updateList =
asDesc date = YAML.withStr "desc" \desc -> asDesc date = YAML.withStr "desc" \desc ->
pure $ Update {date, desc, nsfw = False, ignoreSort = False} pure $ Update {date, desc, nsfw = False, ignoreSort = False}
asObj date = YAML.withMap "update info" \m -> do asObj date = YAML.withMap "update info" \m -> do
checkKeys m ["desc", "nsfw", "ignore-sort"]
desc <- m .: "desc" desc <- m .: "desc"
nsfw <- m .:? "nsfw" .!= False nsfw <- m .:? "nsfw" .!= False
ignoreSort <- m .:? "ignore-sort" .!= False ignoreSort <- m .:? "ignore-sort" .!= False
@ -399,7 +419,8 @@ matchFilters (GalleryFilters {nsfw, artist, require, exclude}) i =
instance FromYAML GalleryInfo where 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" GalleryInfo <$> m .: "title"
<*> m .: "desc" <*> m .: "desc"
<*> m .: "prefix" <*> m .: "prefix"
@ -407,7 +428,8 @@ instance FromYAML GalleryInfo where
<*> m .:? "hidden" .!= mempty <*> m .:? "hidden" .!= mempty
instance FromYAML GalleryFilters where 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 GalleryFilters <$> m .:? "nsfw" .!= AllN
<*> m .:? "artist" .!= AllA <*> m .:? "artist" .!= AllA
<*> m .:? "require" .!= [] <*> m .:? "require" .!= []
@ -428,7 +450,8 @@ data IndexInfo =
deriving Show deriving Show
instance FromYAML IndexInfo where 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" IndexInfo <$> m .: "title"
<*> m .: "desc" <*> m .: "desc"
<*> m .:? "galleries" .!= [] <*> m .:? "galleries" .!= []