From 3957272c10a8c1bfe14d4a4f3603b3e87db9d540 Mon Sep 17 00:00:00 2001 From: Lennart Spitzner Date: Mon, 22 May 2017 15:45:14 +0200 Subject: [PATCH] Improve backwards-compat for config (manual FromJson) --- brittany.cabal | 4 ++++ src/Language/Haskell/Brittany/Config/Types.hs | 19 ++++++++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/brittany.cabal b/brittany.cabal index 30881ff..c643db2 100644 --- a/brittany.cabal +++ b/brittany.cabal @@ -107,6 +107,7 @@ library { , semigroups >=0.18.2 && <0.19 , cmdargs >=0.10.14 && <0.11 , czipwith >=1.0.0.0 && <1.1 + , unordered-containers >=0.2.7 && <0.3 } default-extensions: { CPP @@ -167,6 +168,7 @@ executable brittany , semigroups , cmdargs , czipwith + , unordered-containers , hspec >=2.4.1 && <2.5 , filepath >=1.4.1.0 && <1.5 , ghc-boot-th >=8.0.1 && <8.1 @@ -245,6 +247,7 @@ test-suite unittests , semigroups , cmdargs , czipwith + , unordered-containers , hspec >=2.4.1 && <2.5 } ghc-options: -Wall @@ -318,6 +321,7 @@ test-suite littests , semigroups , cmdargs , czipwith + , unordered-containers , hspec >=2.4.1 && <2.5 , parsec >=3.1.11 && <3.2 } diff --git a/src/Language/Haskell/Brittany/Config/Types.hs b/src/Language/Haskell/Brittany/Config/Types.hs index 9455650..bfeec81 100644 --- a/src/Language/Haskell/Brittany/Config/Types.hs +++ b/src/Language/Haskell/Brittany/Config/Types.hs @@ -13,6 +13,7 @@ where import Data.Yaml import qualified Data.Aeson.Types as Aeson +import qualified Data.HashMap.Lazy as HML import GHC.Generics import Data.Data ( Data ) @@ -219,10 +220,26 @@ makeToJSONOption(CPreProcessorConfig) makeToJSONMaybe(CPreProcessorConfig) makeFromJSONOption(CConfig) -makeFromJSONMaybe(CConfig) makeToJSONOption(CConfig) makeToJSONMaybe(CConfig) +-- This custom instance ensures the "omitNothingFields" behaviour not only for +-- leafs, but for nodes of the config as well. This way e.g. "{}" is valid +-- config file content. +instance FromJSON (CConfig Maybe) where + parseJSON (Object v) = Config + <$> v .:? Text.pack "conf_version" + <*> v .:?= Text.pack "conf_debug" + <*> v .:?= Text.pack "conf_layout" + <*> v .:?= Text.pack "conf_errorHandling" + <*> v .:?= Text.pack "conf_forward" + <*> v .:?= Text.pack "conf_preprocessor" + parseJSON invalid = Aeson.typeMismatch "Config" invalid + +-- Pretends that the value is {} when the key is not present. +(.:?=) :: FromJSON a => Object -> Text -> Parser a +o .:?= k = o .:? k >>= maybe (parseJSON (Object HML.empty)) pure + data IndentPolicy = IndentPolicyLeft -- never create a new indentation at more -- than old indentation + amount | IndentPolicyFree -- can create new indentations whereever