packages feed

conferer-hedis (empty) → 0.2.0.0

raw patch · 7 files changed

+191/−0 lines, 7 filesdep +basedep +confererdep +conferer-hedissetup-changed

Dependencies added: base, conferer, conferer-hedis, hedis, hspec, text

Files

+ LICENSE view
@@ -0,0 +1,21 @@+MIT License++Copyright (c) 2019 Lucas David Traverso++Permission is hereby granted, free of charge, to any person obtaining a copy+of this software and associated documentation files (the "Software"), to deal+in the Software without restriction, including without limitation the rights+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell+copies of the Software, and to permit persons to whom the Software is+furnished to do so, subject to the following conditions:++The above copyright notice and this permission notice shall be included in all+copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE+SOFTWARE.
+ README.md view
@@ -0,0 +1,1 @@+# conferer-hedis
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ conferer-hedis.cabal view
@@ -0,0 +1,57 @@+cabal-version: 1.18++-- This file has been generated from package.yaml by hpack version 0.31.2.+--+-- see: https://github.com/sol/hpack+--+-- hash: 1ca6d2eb45eb5c28e844e351918ffe93f8d006f0d26a03bbe39d6ea9b0316c68++name:           conferer-hedis+version:        0.2.0.0+synopsis:       conferer's FromConfig instances for hedis settings++description:    Library to abstract the parsing of many haskell config values from different config sources+category:       Configuration+homepage:       https://github.com/ludat/conferer#readme+author:         Lucas David Traverso+maintainer:     lucas6246@gmail.com+copyright:      MIT+license:        BSD3+license-file:   LICENSE+build-type:     Simple+extra-doc-files:+    README.md+    LICENSE++library+  exposed-modules:+      Conferer.FetchFromConfig.Hedis+  other-modules:+      Paths_conferer_hedis+  hs-source-dirs:+      src+  default-extensions: OverloadedStrings LambdaCase QuasiQuotes ScopedTypeVariables+  build-depends:+      base >=4.3 && <5+    , conferer >=0.2.0.0 && <0.3.0.0+    , hedis >=0.7.0 && <1.0.0+    , text >=1.1 && <1.3+  default-language: Haskell2010++test-suite conferer-hedis-test+  type: exitcode-stdio-1.0+  main-is: Spec.hs+  other-modules:+      Conferer.FetchFromConfig.HedisSpec+      Paths_conferer_hedis+  hs-source-dirs:+      test+  default-extensions: OverloadedStrings LambdaCase QuasiQuotes ScopedTypeVariables+  build-depends:+      base >=4.3 && <5+    , conferer >=0.2.0.0 && <0.3.0.0+    , conferer-hedis+    , hedis >=0.7.0 && <1.0.0+    , hspec+    , text >=1.1 && <1.3+  default-language: Haskell2010
+ src/Conferer/FetchFromConfig/Hedis.hs view
@@ -0,0 +1,60 @@+{-# LANGUAGE FlexibleInstances #-}+{-# LANGUAGE TypeApplications #-}+module Conferer.FetchFromConfig.Hedis+  (+  -- * How to use this+  -- | FetchFromConfig instance for hedis server settings+  --+  -- @+  -- import Conferer+  -- import Conferer.FetchFromConfig.Hedis ()+  --+  -- main = do+  --   config <- 'defaultConfig' \"awesomeapp\"+  --   hedisSettings <- 'getFromConfig' \"hedis\" config+  -- @+  ) where++import Conferer.Core+import Conferer.Types+import Conferer.FetchFromConfig.Basics+import Data.Maybe (catMaybes)+import qualified Database.Redis as Redis+import Data.String (fromString)+import Data.Text (unpack)+import Data.Maybe (fromMaybe)+import Text.Read (readMaybe)+import Data.Proxy (Proxy(..))+import Data.Typeable (typeRep)+import Control.Exception (throwIO)++instance FetchFromConfig Redis.PortID where+  fetch = fetchFromConfigWith (\t -> do+      case readMaybe $ unpack t of+        Just n -> return $ Redis.PortNumber n+        Nothing ->+          return $ Redis.UnixSocket $ unpack t+    )++instance DefaultConfig Redis.ConnectInfo where+  configDef = Redis.defaultConnectInfo++instance FetchFromConfig Redis.ConnectInfo where+  fetch key config = do+    redisConfig <- getKey key config+      >>= \case+        Just connectionString -> +          case Redis.parseConnectInfo $ unpack connectionString of+            Right con -> return $ con+            Left e -> +                throwIO $ ConfigParsingError key connectionString (typeRep (Proxy :: Proxy (Redis.ConnectInfo)))+        Nothing -> +          pure configDef+            >>= findKeyAndApplyConfig config key "host" (\v c -> c { Redis.connectHost = v })+            >>= findKeyAndApplyConfig config key "port" (\v c -> c { Redis.connectPort = v })+            >>= findKeyAndApplyConfig config key "auth" (\v c -> c { Redis.connectAuth = v })++    pure redisConfig+      >>= findKeyAndApplyConfig config key "maxConnections" (\v c -> c { Redis.connectMaxConnections = v })+      >>= findKeyAndApplyConfig config key "maxIdleTime" (\v c -> c { Redis.connectMaxConnections = v })+      >>= (return . return)
+ test/Conferer/FetchFromConfig/HedisSpec.hs view
@@ -0,0 +1,49 @@+module Conferer.FetchFromConfig.HedisSpec where++import           Test.Hspec+import           Conferer.Types+import           Data.Text+import           Conferer+import           Conferer.FetchFromConfig.Hedis ()+import qualified Database.Redis as Redis++configWith :: [(Key, Text)] -> IO Config+configWith keyValues = emptyConfig & addProvider (mkMapProvider keyValues)++portAndHostShouldBe :: Maybe Redis.ConnectInfo -> (Redis.PortID, Int) -> Expectation+portAndHostShouldBe fetchedSettings (port, host) = do+  Redis.connectPort <$> fetchedSettings `shouldBe` Just port+  Redis.connectMaxConnections <$> fetchedSettings `shouldBe` Just host++spec :: Spec+spec = do+  let defaultPort = Redis.connectPort configDef+      defaultHost = Redis.connectMaxConnections configDef+  describe "fetching a hedis configuration without overriding anything" $ do+    it "returns hedis default config" $ do+      let config = emptyConfig+      fetchedValue <- fetch "." config+      fetchedValue `portAndHostShouldBe` (defaultPort, defaultHost)+  describe "fetching a hedis configuration overriding its port" $ do+    it "returns a hedis config with its port set to the overriden one" $ do+      config <- configWith [("hedis.port", "9999")]+      fetchedValue <- fetch "hedis" config+      fetchedValue `portAndHostShouldBe` (Redis.PortNumber 9999, defaultHost)+  describe "fetching a hedis configuration overriding its host" $ do+    it "returns a hedis config with its host set to the overriden one" $ do+      config <- configWith [("hedis.maxConnections", "6")]+      fetchedValue <- fetch "hedis" config+      fetchedValue `portAndHostShouldBe` (defaultPort, 6)+  describe "fetching a hedis configuration overriding its host" $ do+    it "returns a hedis config with its host set to the overriden one" $ do+      config <- configWith [("hedis", "redis://username:password@host:42")]+      fetchedValue <- fetch "hedis" config+      fetchedValue `portAndHostShouldBe` (Redis.PortNumber 42, defaultHost)+  describe "fetching a hedis configuration overriding its host" $ do+    it "returns a hedis config with its host set to the overriden one" $ do+      config <- configWith +        [ ("hedis", "redis://username:password@host:42")+        , ("hedis.maxConnections", "70")+        ]+      fetchedValue <- fetch "hedis" config+      fetchedValue `portAndHostShouldBe` (Redis.PortNumber 42, 70)
+ test/Spec.hs view
@@ -0,0 +1,1 @@+{-# OPTIONS_GHC -F -pgmF hspec-discover #-}