From dac64f5718561daf4f298f631097a7a0a02492b2 Mon Sep 17 00:00:00 2001 From: Julian Ospald Date: Mon, 10 Aug 2020 22:22:48 +0200 Subject: [PATCH] Make TarDir backwardscompat --- ghcup-0.0.2.yaml | 1 + lib/GHCup/Types/JSON.hs | 14 +++++++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/ghcup-0.0.2.yaml b/ghcup-0.0.2.yaml index cbf278a..873de2f 100644 --- a/ghcup-0.0.2.yaml +++ b/ghcup-0.0.2.yaml @@ -1,3 +1,4 @@ +# !!! if you use RegexDir, then the version must be bumped !!! --- toolRequirements: GHC: diff --git a/lib/GHCup/Types/JSON.hs b/lib/GHCup/Types/JSON.hs index 1320c5e..c271a08 100644 --- a/lib/GHCup/Types/JSON.hs +++ b/lib/GHCup/Types/JSON.hs @@ -24,6 +24,7 @@ module GHCup.Types.JSON where import GHCup.Types import GHCup.Utils.Prelude +import Control.Applicative ( (<|>) ) import Data.Aeson import Data.Aeson.TH import Data.Aeson.Types @@ -195,5 +196,16 @@ instance FromJSON (Path Rel) where Left e -> fail $ "Failure in HPath Rel (FromJSON)" <> show e -deriveJSON defaultOptions{ sumEncoding = ObjectWithSingleField } ''TarDir +instance ToJSON TarDir where + toJSON (RealDir p) = toJSON p + toJSON (RegexDir r) = object ["RegexDir" .= r] +instance FromJSON TarDir where + parseJSON v = realDir v <|> regexDir v + where + realDir = withText "TarDir" $ \t -> do + fp <- parseJSON (String t) + pure (RealDir fp) + regexDir = withObject "TarDir" $ \o -> do + r <- o .: "RegexDir" + pure $ RegexDir r