Skip to content

Commit

Permalink
Add flag 'strict-metadata-parsing' to enable use of normal Map instance
Browse files Browse the repository at this point in the history
This is for use in ghcup-metadata validation code
  • Loading branch information
dfordivam committed Nov 3, 2024
1 parent ba1f52b commit d1f92d4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
10 changes: 10 additions & 0 deletions ghcup.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ flag internal-downloader
default: False
manual: True

flag strict-metadata-parsing
description:
Don't ignore unknown keys in metadata. Useful for metadata testing.

default: False
manual: True

flag no-exe
description: Don't build any executables
default: False
Expand Down Expand Up @@ -278,6 +285,9 @@ library
cpp-options: -DBRICK
build-depends: vty ^>=6.0 || ^>=6.1 || ^>=6.2

if (flag(strict-metadata-parsing))
cpp-options: -DSTRICT_METADATA_PARSING

library ghcup-optparse
import: app-common-depends
exposed-modules:
Expand Down
8 changes: 8 additions & 0 deletions lib/GHCup/Types/JSON/MapIgnoreUnknownKeys.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{-# OPTIONS_GHC -Wno-orphans #-}

{-# LANGUAGE CPP #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE UndecidableInstances #-}

Expand All @@ -14,6 +15,12 @@ import qualified Data.Aeson.Key as Key
import qualified Data.Aeson.KeyMap as KeyMap
import qualified Data.Map.Strict as Map

#if defined(STRICT_METADATA_PARSING)
-- | Use the instance of Map
instance (FromJSON (Map.Map k v)) => FromJSON (MapIgnoreUnknownKeys k v) where
parseJSON = fmap MapIgnoreUnknownKeys . parseJSON
#else

-- | Create a Map ignoring KeyValue pair which fail at parse of the key
-- But if the key is parsed, the failures of parsing the value will not be ignored
instance (Ord k, FromJSONKey k, FromJSON v) => FromJSON (MapIgnoreUnknownKeys k v) where
Expand All @@ -32,6 +39,7 @@ instance (Ord k, FromJSONKey k, FromJSON v) => FromJSON (MapIgnoreUnknownKeys k
-- FromJSONKeyCoerce and FromJSONKeyText always parse to Success; hence use instance of Map
_ -> parseJSON (Object obj)
pure $ MapIgnoreUnknownKeys m
#endif

instance (ToJSON (Map.Map k v)) => ToJSON (MapIgnoreUnknownKeys k v) where
toJSON = toJSON . unMapIgnoreUnknownKeys

0 comments on commit d1f92d4

Please sign in to comment.