Skip to content

Commit

Permalink
Merge pull request #4 from roman/v0.1.0.0
Browse files Browse the repository at this point in the history
v0.1.0.0
  • Loading branch information
roman authored Apr 17, 2017
2 parents f368b0f + 2326fda commit 74c5ebf
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 8 deletions.
Empty file modified .ghci
100644 → 100755
Empty file.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
2 changes: 1 addition & 1 deletion etc-command-example/etc-command-example.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion etc-plain-example/etc-plain-example.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions etc/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
6 changes: 3 additions & 3 deletions etc/etc.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions etc/src/System/Etc/Internal/Extra/Printer.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
18 changes: 17 additions & 1 deletion etc/src/System/Etc/Spec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)
Expand All @@ -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
28 changes: 26 additions & 2 deletions etc/test/System/Etc/Resolver/DefaultTest.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)


]
4 changes: 4 additions & 0 deletions etc/test/System/Etc/SpecTest.hs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ general_tests =
assertBool "" True
]

#ifdef WITH_CLI
cli_tests :: TestTree
cli_tests =
testGroup "cli"
Expand Down Expand Up @@ -273,6 +274,7 @@ cli_tests =
assertBool "" True

]
#endif

envvar_tests :: TestTree
envvar_tests =
Expand Down Expand Up @@ -330,5 +332,7 @@ tests =
, yaml_tests
#endif
, envvar_tests
#ifdef WITH_CLI
, cli_tests
#endif
]

0 comments on commit 74c5ebf

Please sign in to comment.