Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/pr/1144'
Browse files Browse the repository at this point in the history
  • Loading branch information
hasufell committed Nov 19, 2024
2 parents 0dfd6e6 + a1facfa commit 93dfbce
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 11 deletions.
11 changes: 11 additions & 0 deletions ghcup.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,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 @@ -144,6 +151,7 @@ library
GHCup.Stack
GHCup.Types
GHCup.Types.JSON
GHCup.Types.JSON.MapIgnoreUnknownKeys
GHCup.Types.JSON.Utils
GHCup.Types.JSON.Versions
GHCup.Types.Optics
Expand Down Expand Up @@ -284,6 +292,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
5 changes: 2 additions & 3 deletions lib/GHCup/Download.hs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ getDownloadsF pfreq@(PlatformRequest arch plat _) = do
pure (GHCupInfo mempty ghcupDownloads' Nothing)
where
fromDownloadInfo :: DownloadInfo -> VersionInfo
fromDownloadInfo dli = let aspec = M.singleton arch (M.singleton plat (M.singleton Nothing dli))
fromDownloadInfo dli = let aspec = MapIgnoreUnknownKeys $ M.singleton arch (MapIgnoreUnknownKeys $ M.singleton plat (M.singleton Nothing dli))
in VersionInfo [] Nothing Nothing Nothing Nothing aspec Nothing Nothing Nothing Nothing

fromStackDownloadInfo :: MonadThrow m => Stack.GHCDownloadInfo -> m DownloadInfo
Expand Down Expand Up @@ -403,7 +403,7 @@ getDownloadInfo' t v = do

let distro_preview f g =
let platformVersionSpec =
preview (ix t % ix v % viArch % ix a % ix (f p)) dls
preview (ix t % ix v % viArch % to unMapIgnoreUnknownKeys % ix a % to unMapIgnoreUnknownKeys % ix (f p)) dls
mv' = g mv
in fmap snd
. find
Expand Down Expand Up @@ -889,4 +889,3 @@ applyMirrors (DM ms) uri@(URI { uriAuthority = Just (Authority { authorityHost =
Just (DownloadMirror auth Nothing) ->
uri { uriAuthority = Just auth }
applyMirrors _ uri = uri

2 changes: 1 addition & 1 deletion lib/GHCup/Requirements.hs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ getCommonRequirements pr tr =

distro_preview f g =
let platformVersionSpec =
preview (ix GHC % ix Nothing % ix (f pr)) tr
preview (ix GHC % ix Nothing % to unMapIgnoreUnknownKeys % ix (f pr)) tr
mv' = g pr
in fmap snd
. find
Expand Down
14 changes: 8 additions & 6 deletions lib/GHCup/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE DuplicateRecordFields #-}

{-|
Expand Down Expand Up @@ -39,7 +38,6 @@ import Data.Time.Calendar ( Day )
import Data.Text ( Text )
import Data.Versions
import GHC.IO.Exception ( ExitCode )
import Optics ( makeLenses )
import Text.PrettyPrint.HughesPJClass (Pretty, pPrint, text)
import URI.ByteString
#if defined(BRICK)
Expand Down Expand Up @@ -91,7 +89,7 @@ instance NFData GHCupInfo

type ToolRequirements = Map Tool ToolReqVersionSpec
type ToolReqVersionSpec = Map (Maybe Version) PlatformReqSpec
type PlatformReqSpec = Map Platform PlatformReqVersionSpec
type PlatformReqSpec = MapIgnoreUnknownKeys Platform PlatformReqVersionSpec
type PlatformReqVersionSpec = Map (Maybe VersionRange) Requirements


Expand All @@ -116,8 +114,8 @@ instance NFData Requirements
-- of nested maps.
type GHCupDownloads = Map Tool ToolVersionSpec
type ToolVersionSpec = Map GHCTargetVersion VersionInfo
type ArchitectureSpec = Map Architecture PlatformSpec
type PlatformSpec = Map Platform PlatformVersionSpec
type ArchitectureSpec = MapIgnoreUnknownKeys Architecture PlatformSpec
type PlatformSpec = MapIgnoreUnknownKeys Platform PlatformVersionSpec
type PlatformVersionSpec = Map (Maybe VersionRange) DownloadInfo


Expand Down Expand Up @@ -808,7 +806,6 @@ data CapturedProcess = CapturedProcess
}
deriving (Eq, Show)

makeLenses ''CapturedProcess


data InstallDir = IsolateDir FilePath
Expand Down Expand Up @@ -863,3 +860,8 @@ data VersionPattern = CabalVer
| S String
deriving (Eq, Show)

-- | Map with custom FromJSON instance which ignores unknown keys
newtype MapIgnoreUnknownKeys k v = MapIgnoreUnknownKeys { unMapIgnoreUnknownKeys :: Map k v }
deriving (Eq, Show, GHC.Generic)

instance (NFData k, NFData v) => NFData (MapIgnoreUnknownKeys k v)
1 change: 1 addition & 0 deletions lib/GHCup/Types/JSON.hs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ module GHCup.Types.JSON where

import GHCup.Types
import GHCup.Types.Stack (SetupInfo)
import GHCup.Types.JSON.MapIgnoreUnknownKeys ()
import GHCup.Types.JSON.Utils
import GHCup.Types.JSON.Versions ()
import GHCup.Prelude.MegaParsec
Expand Down
45 changes: 45 additions & 0 deletions lib/GHCup/Types/JSON/MapIgnoreUnknownKeys.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{-# OPTIONS_GHC -Wno-orphans #-}

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

module GHCup.Types.JSON.MapIgnoreUnknownKeys where

import GHCup.Types

import Data.Aeson hiding (Key)
import Data.Aeson.Types hiding (Key)

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
parseJSON = withObject "MapIgnoreUnknownKeys" $ \obj -> do
m <- case fromJSONKey of
FromJSONKeyTextParser f ->
let doParse k v m = case parseMaybe f (Key.toText k) of
Just k' -> Map.insert k' <$> parseJSON v <*> m
Nothing -> m
in KeyMap.foldrWithKey doParse (pure Map.empty) obj
FromJSONKeyValue f ->
let doParse k v m = case parseMaybe f (toJSON k) of
Just k' -> Map.insert k' <$> parseJSON v <*> m
Nothing -> m
in KeyMap.foldrWithKey doParse (pure Map.empty) obj
-- 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
2 changes: 2 additions & 0 deletions lib/GHCup/Types/Optics.hs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ makeLenses ''GHCTargetVersion

makeLenses ''GHCupInfo

makeLenses ''CapturedProcess

uriSchemeL' :: Lens' (URIRef Absolute) Scheme
uriSchemeL' = lensVL uriSchemeL

Expand Down
7 changes: 6 additions & 1 deletion test/ghcup-test/GHCup/ArbitraryTypes.hs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,11 @@ instance {-# OVERLAPS #-} Arbitrary v => Arbitrary (M.Map (Maybe Version) v) whe
instance {-# OVERLAPS #-} Arbitrary v => Arbitrary (M.Map Platform v) where
arbitrary = resize 8 $ M.fromList <$> arbitrary

instance {-# OVERLAPS #-} Arbitrary v => Arbitrary (MapIgnoreUnknownKeys Platform v) where
arbitrary = resize 8 $ MapIgnoreUnknownKeys . M.fromList <$> arbitrary

instance {-# OVERLAPS #-} Arbitrary v => Arbitrary (MapIgnoreUnknownKeys Architecture v) where
arbitrary = resize 8 $ MapIgnoreUnknownKeys . M.fromList <$> arbitrary

instance {-# OVERLAPS #-} Arbitrary v => Arbitrary (M.Map (Maybe Versioning) v) where
arbitrary = resize 8 $ M.fromList <$> arbitrary

0 comments on commit 93dfbce

Please sign in to comment.