diff --git a/.ghci b/.ghci old mode 100644 new mode 100755 diff --git a/README.md b/README.md index 1b1c6bf..2f9cf49 100644 --- a/README.md +++ b/README.md @@ -149,6 +149,11 @@ function can accept either a JSON or YAML filepath. You can also use the `System.Etc.parseConfigSpec` if you already gather the contents of a spec file from a different source. +_NOTE_: When using `System.Etc.parseConfigSpec` or `System.Etc.readConfigSpec` +and the [CLI cabal feature flag is true](#cli-support), unless you use the +`System.Etc.resolveCommandCli` function, you will have to explicitly declare the +`ConfigSpec` type parameter. + ### YAML support In order to allow `etc` to read from YAML files, you will need to use the `yaml` diff --git a/etc-command-example/etc-command-example.cabal b/etc-command-example/etc-command-example.cabal index 009e731..db01620 100644 --- a/etc-command-example/etc-command-example.cabal +++ b/etc-command-example/etc-command-example.cabal @@ -23,7 +23,7 @@ executable etc-command-example ghc-options: -threaded -rtsopts -with-rtsopts=-N -Wall build-depends: base >=4.7 && <5 - , aeson >=0.11 && <1.2 + , aeson >=0.11 && <1.3 , text >=1.2 && <1.3 , protolude >=0.1 && <0.2 , unordered-containers >=0.2 && <0.3 diff --git a/etc-plain-example/etc-plain-example.cabal b/etc-plain-example/etc-plain-example.cabal index 7132bcf..dd50ad8 100644 --- a/etc-plain-example/etc-plain-example.cabal +++ b/etc-plain-example/etc-plain-example.cabal @@ -23,7 +23,7 @@ executable etc-plain-example ghc-options: -threaded -rtsopts -with-rtsopts=-N -Wall build-depends: base >=4.7 && <5 - , aeson >=0.11 && <1.2 + , aeson >=0.11 && <1.3 , text >=1.2 && <1.3 , protolude >=0.1 && <0.2 , unordered-containers >=0.2 && <0.3 diff --git a/etc/CHANGELOG.md b/etc/CHANGELOG.md index 92b6732..ee47d10 100644 --- a/etc/CHANGELOG.md +++ b/etc/CHANGELOG.md @@ -1,3 +1,10 @@ +0.1.0.0 +---- +* Add support for null values on Default (issue #3) +* If cli cabal flag is false, have `parseConfigSpec` return `ConfigSpec ()` + instead of ambiguous `FromJSON` value (issue #3) +* Bump aeson dependency to `<1.3` + 0.0.0.2 ---- * Rename System.Etc.Internal.Util module to System.Etc.Internal.Extra diff --git a/etc/etc.cabal b/etc/etc.cabal index cf16b69..173c22a 100644 --- a/etc/etc.cabal +++ b/etc/etc.cabal @@ -3,7 +3,7 @@ -- see: https://github.com/sol/hpack name: etc -version: 0.0.0.2 +version: 0.1.0.0 synopsis: Declarative configuration spec for Haskell projects description: Please see README.md category: Configuration, System @@ -50,7 +50,7 @@ library ghc-options: -Wall build-depends: base >=4.7 && <5 - , aeson >=0.11 && <1.2 + , aeson >=0.11 && <1.3 , bytestring >=0.10 && <0.11 , containers >=0.5 && <0.6 , text >=1.2 && <1.3 @@ -113,7 +113,7 @@ test-suite etc-testsuite ghc-options: -Wall build-depends: base >=4.7 && <5 - , aeson >=0.11 && <1.2 + , aeson >=0.11 && <1.3 , bytestring >=0.10 && <0.11 , containers >=0.5 && <0.6 , text >=1.2 && <1.3 diff --git a/etc/src/System/Etc/Internal/Extra/Printer.hs b/etc/src/System/Etc/Internal/Extra/Printer.hs index 34d45c9..d7b4454 100644 --- a/etc/src/System/Etc/Internal/Extra/Printer.hs +++ b/etc/src/System/Etc/Internal/Extra/Printer.hs @@ -21,6 +21,9 @@ import System.Etc.Internal.Types renderJsonValue :: JSON.Value -> (Doc, Int) renderJsonValue value' = case value' of + JSON.Null -> + (text "null", 4) + JSON.String str -> (text $ Text.unpack str, Text.length str) diff --git a/etc/src/System/Etc/Spec.hs b/etc/src/System/Etc/Spec.hs index 9a7d169..58e6680 100644 --- a/etc/src/System/Etc/Spec.hs +++ b/etc/src/System/Etc/Spec.hs @@ -15,7 +15,9 @@ import System.Etc.Internal.Spec.Types as Types import Control.Monad.Catch (MonadCatch (..)) -import qualified Data.Aeson as JSON +#ifdef WITH_CLI +import qualified Data.Aeson as JSON +#endif import qualified Data.Text as Text import qualified Data.Text.IO as Text (readFile) @@ -30,10 +32,18 @@ Parses a text input into a @ConfigSpec@, input can be JSON or YAML (if cabal flag is set). -} +#ifdef WITH_CLI parseConfigSpec :: (MonadCatch m, JSON.FromJSON cmd) => Text -- ^ Text to be parsed -> m (ConfigSpec cmd) -- ^ returns ConfigSpec +#else +parseConfigSpec + :: (MonadCatch m) + => Text -- ^ Text to be parsed + -> m (ConfigSpec ()) -- ^ returns ConfigSpec +#endif + #ifdef WITH_YAML parseConfigSpec input = catch (JSON.parseConfigSpec input) @@ -49,10 +59,16 @@ Reads contents of a file and parses into a @ConfigSpec@, file contents can be either JSON or YAML (if cabal flag is set). -} +#ifdef WITH_CLI readConfigSpec :: JSON.FromJSON cmd => Text -- ^ Filepath where contents are going to be read from and parsed -> IO (ConfigSpec cmd) -- ^ returns ConfigSpec +#else +readConfigSpec + :: Text -- ^ Filepath where contents are going to be read from and parsed + -> IO (ConfigSpec ()) -- ^ returns ConfigSpec +#endif readConfigSpec filepath = do contents <- Text.readFile $ Text.unpack filepath parseConfigSpec contents diff --git a/etc/test/System/Etc/Resolver/DefaultTest.hs b/etc/test/System/Etc/Resolver/DefaultTest.hs index 4a9ac65..dccffc5 100644 --- a/etc/test/System/Etc/Resolver/DefaultTest.hs +++ b/etc/test/System/Etc/Resolver/DefaultTest.hs @@ -5,8 +5,9 @@ module System.Etc.Resolver.DefaultTest (tests) where import Protolude -import Test.Tasty (TestTree, testGroup) -import Test.Tasty.HUnit (assertBool, assertFailure, testCase) +import qualified Data.Aeson as JSON +import Test.Tasty (TestTree, testGroup) +import Test.Tasty.HUnit (assertBool, assertFailure, testCase) import qualified Data.Set as Set @@ -61,4 +62,27 @@ tests = assertBool ("expecting to see entry from env; got " <> show set) (Set.member (Default "hello default") set) + , testCase "default can be a null JSON value" $ do + let + input = + mconcat + [ + "{\"etc/entries\": {" + , " \"greeting\": null}}" + ] + (spec :: ConfigSpec ()) <- parseConfigSpec input + + let + config = + resolveDefault spec + + case getAllConfigSources ["greeting"] config of + Nothing -> + assertFailure ("expecting to get entries for greeting\n" + <> show config) + Just set -> + assertBool ("expecting to see entry from env; got " <> show set) + (Set.member (Default JSON.Null) set) + + ] diff --git a/etc/test/System/Etc/SpecTest.hs b/etc/test/System/Etc/SpecTest.hs index fa501e6..5bca6e5 100644 --- a/etc/test/System/Etc/SpecTest.hs +++ b/etc/test/System/Etc/SpecTest.hs @@ -170,6 +170,7 @@ general_tests = assertBool "" True ] +#ifdef WITH_CLI cli_tests :: TestTree cli_tests = testGroup "cli" @@ -273,6 +274,7 @@ cli_tests = assertBool "" True ] +#endif envvar_tests :: TestTree envvar_tests = @@ -330,5 +332,7 @@ tests = , yaml_tests #endif , envvar_tests +#ifdef WITH_CLI , cli_tests +#endif ]