Improve backwards-compat for config (manual FromJson)

pull/35/head
Lennart Spitzner 2017-05-22 15:45:14 +02:00
parent 8f1e366f9c
commit 3957272c10
2 changed files with 22 additions and 1 deletions

View File

@ -107,6 +107,7 @@ library {
, semigroups >=0.18.2 && <0.19 , semigroups >=0.18.2 && <0.19
, cmdargs >=0.10.14 && <0.11 , cmdargs >=0.10.14 && <0.11
, czipwith >=1.0.0.0 && <1.1 , czipwith >=1.0.0.0 && <1.1
, unordered-containers >=0.2.7 && <0.3
} }
default-extensions: { default-extensions: {
CPP CPP
@ -167,6 +168,7 @@ executable brittany
, semigroups , semigroups
, cmdargs , cmdargs
, czipwith , czipwith
, unordered-containers
, hspec >=2.4.1 && <2.5 , hspec >=2.4.1 && <2.5
, filepath >=1.4.1.0 && <1.5 , filepath >=1.4.1.0 && <1.5
, ghc-boot-th >=8.0.1 && <8.1 , ghc-boot-th >=8.0.1 && <8.1
@ -245,6 +247,7 @@ test-suite unittests
, semigroups , semigroups
, cmdargs , cmdargs
, czipwith , czipwith
, unordered-containers
, hspec >=2.4.1 && <2.5 , hspec >=2.4.1 && <2.5
} }
ghc-options: -Wall ghc-options: -Wall
@ -318,6 +321,7 @@ test-suite littests
, semigroups , semigroups
, cmdargs , cmdargs
, czipwith , czipwith
, unordered-containers
, hspec >=2.4.1 && <2.5 , hspec >=2.4.1 && <2.5
, parsec >=3.1.11 && <3.2 , parsec >=3.1.11 && <3.2
} }

View File

@ -13,6 +13,7 @@ where
import Data.Yaml import Data.Yaml
import qualified Data.Aeson.Types as Aeson import qualified Data.Aeson.Types as Aeson
import qualified Data.HashMap.Lazy as HML
import GHC.Generics import GHC.Generics
import Data.Data ( Data ) import Data.Data ( Data )
@ -219,10 +220,26 @@ makeToJSONOption(CPreProcessorConfig)
makeToJSONMaybe(CPreProcessorConfig) makeToJSONMaybe(CPreProcessorConfig)
makeFromJSONOption(CConfig) makeFromJSONOption(CConfig)
makeFromJSONMaybe(CConfig)
makeToJSONOption(CConfig) makeToJSONOption(CConfig)
makeToJSONMaybe(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 data IndentPolicy = IndentPolicyLeft -- never create a new indentation at more
-- than old indentation + amount -- than old indentation + amount
| IndentPolicyFree -- can create new indentations whereever | IndentPolicyFree -- can create new indentations whereever