diff --git a/CHANGELOG.md b/CHANGELOG.md index 63db40a..5c84c20 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [Unreleased] +## [0.1.1.0] - 2018-11-21 +### Added +- Lens for configs; +- Instances are implemented using `Data.Aeson`. + ## [0.1.0.6] - 2018-09-20 ### Fixed - Bugfix with imports diff --git a/bcd-config.cabal b/bcd-config.cabal index 28f44e3..0706e43 100644 --- a/bcd-config.cabal +++ b/bcd-config.cabal @@ -1,5 +1,5 @@ name: bcd-config -version: 0.1.0.6 +version: 0.1.1.0 synopsis: Library to get config. description: Library to get config to different systems homepage: https://github.com/biocad/bcd-config#readme @@ -37,5 +37,11 @@ library , containers , text , unordered-containers + , aeson + , aeson-casing , aeson-picker + , lens default-language: Haskell2010 + default-extensions: DeriveGeneric + , OverloadedStrings + , TemplateHaskell diff --git a/src/System/BCD/Config.hs b/src/System/BCD/Config.hs index 2c4ccb2..8508954 100644 --- a/src/System/BCD/Config.hs +++ b/src/System/BCD/Config.hs @@ -1,5 +1,6 @@ module System.BCD.Config - ( FromJsonConfig (..) + ( + FromJsonConfig (..) , getConfigText ) where diff --git a/src/System/BCD/Config/BioSources.hs b/src/System/BCD/Config/BioSources.hs index 55b907c..5562b82 100644 --- a/src/System/BCD/Config/BioSources.hs +++ b/src/System/BCD/Config/BioSources.hs @@ -1,12 +1,11 @@ -{-# LANGUAGE OverloadedStrings #-} - module System.BCD.Config.BioSources - ( BioSourcesConfig (..) + ( + BioSourcesConfig (..) , FromJsonConfig (..) ) where -import Data.Aeson.Picker ((|--)) -import System.BCD.Config (FromJsonConfig (..), getConfigText) +import Data.Aeson.Picker ((|--)) +import System.BCD.Config (FromJsonConfig (..), getConfigText) -- | This class contains information where to find files that are related to -- semantic common used library [bio-sources](https://github.com/biocad/bio-sources). @@ -16,6 +15,6 @@ newtype BioSourcesConfig = BioSourcesConfig { bioSourcesPath :: FilePath } instance FromJsonConfig BioSourcesConfig where fromJsonConfig = do - jsonText <- getConfigText + jsonText <- getConfigText let path = jsonText |-- ["deploy", "fs", "bio-sources"] pure $ BioSourcesConfig path diff --git a/src/System/BCD/Config/FileSystem.hs b/src/System/BCD/Config/FileSystem.hs index f8589d9..b42917c 100644 --- a/src/System/BCD/Config/FileSystem.hs +++ b/src/System/BCD/Config/FileSystem.hs @@ -1,7 +1,6 @@ -{-# LANGUAGE OverloadedStrings #-} - module System.BCD.Config.FileSystem - ( FileSystemConfig (..) + ( + FileSystemConfig (..) , FromJsonConfig (..) ) where @@ -14,6 +13,6 @@ newtype FileSystemConfig = FileSystemConfig (HashMap String FilePath) instance FromJsonConfig FileSystemConfig where fromJsonConfig = do - jsonText <- getConfigText + jsonText <- getConfigText let fs = jsonText |-- ["deploy", "fs"] pure $ FileSystemConfig fs diff --git a/src/System/BCD/Config/Mongo.hs b/src/System/BCD/Config/Mongo.hs index 4bfc8a4..c10dfbd 100644 --- a/src/System/BCD/Config/Mongo.hs +++ b/src/System/BCD/Config/Mongo.hs @@ -1,13 +1,23 @@ -{-# LANGUAGE OverloadedStrings #-} - module System.BCD.Config.Mongo - ( MongoConfig (..) + ( + MongoConfig (..) , FromJsonConfig (..) + , host + , port + , user + , password + , databases + , descr ) where +import Control.Lens (makeLenses) +import Data.Aeson (FromJSON (..), ToJSON (..), + genericParseJSON, genericToJSON) +import Data.Aeson.Casing (aesonDrop, snakeCase) import Data.Aeson.Picker ((|--)) import Data.Map.Strict (Map) import Data.Text (Text) +import GHC.Generics (Generic) import System.BCD.Config (FromJsonConfig (..), getConfigText) @@ -18,17 +28,16 @@ data MongoConfig = MongoConfig { _host :: String , _databases :: Map String Text , _descr :: String } - deriving (Show, Read, Eq) + deriving (Show, Read, Eq, Generic) +makeLenses ''MongoConfig +instance ToJSON MongoConfig where + toJSON = genericToJSON $ aesonDrop 1 snakeCase +instance FromJSON MongoConfig where + parseJSON = genericParseJSON $ aesonDrop 1 snakeCase instance FromJsonConfig MongoConfig where fromJsonConfig = do - jsonText <- getConfigText - let get field = jsonText |-- ["deploy", "mongo", field] - pure $ MongoConfig (get "host") - (get "port") - (get "user") - (get "password") - (get "databases") - (get "descr") + config <- getConfigText + pure $ config |-- ["deploy", "mongo"] diff --git a/src/System/BCD/Config/Mysql.hs b/src/System/BCD/Config/Mysql.hs index a8cebff..657da1f 100644 --- a/src/System/BCD/Config/Mysql.hs +++ b/src/System/BCD/Config/Mysql.hs @@ -1,12 +1,21 @@ -{-# LANGUAGE OverloadedStrings #-} - module System.BCD.Config.Mysql - ( MysqlConfig (..) + ( + MysqlConfig (..) , FromJsonConfig (..) + , host + , port + , user + , password + , descr ) where -import Data.Aeson.Picker ((|--)) -import System.BCD.Config (FromJsonConfig (..), getConfigText) +import Control.Lens (makeLenses) +import Data.Aeson (FromJSON (..), ToJSON (..), + genericParseJSON, genericToJSON) +import Data.Aeson.Casing (aesonDrop, snakeCase) +import Data.Aeson.Picker ((|--)) +import GHC.Generics (Generic) +import System.BCD.Config (FromJsonConfig (..), getConfigText) data MysqlConfig = MysqlConfig { _host :: String , _port :: Int @@ -14,14 +23,16 @@ data MysqlConfig = MysqlConfig { _host :: String , _password :: String , _descr :: String } - deriving (Show, Read, Eq) + deriving (Show, Read, Eq, Generic) + +makeLenses ''MysqlConfig + +instance ToJSON MysqlConfig where + toJSON = genericToJSON $ aesonDrop 1 snakeCase +instance FromJSON MysqlConfig where + parseJSON = genericParseJSON $ aesonDrop 1 snakeCase instance FromJsonConfig MysqlConfig where fromJsonConfig = do - jsonText <- getConfigText - let get field = jsonText |-- ["deploy", "mysql", field] - pure $ MysqlConfig (get "host") - (get "port") - (get "user") - (get "password") - (get "descr") + config <- getConfigText + pure $ config |-- ["deploy", "mysql"] diff --git a/src/System/BCD/Config/Neo4j.hs b/src/System/BCD/Config/Neo4j.hs index e426bd3..70804b7 100644 --- a/src/System/BCD/Config/Neo4j.hs +++ b/src/System/BCD/Config/Neo4j.hs @@ -1,11 +1,23 @@ -{-# LANGUAGE OverloadedStrings #-} - module System.BCD.Config.Neo4j - ( Neo4jConfig (..) + ( + Neo4jConfig (..) , FromJsonConfig (..) + , host + , port + , user + , password + , stripes + , timeout + , rps + , descr ) where +import Control.Lens (makeLenses) +import Data.Aeson (FromJSON (..), ToJSON (..), + genericParseJSON, genericToJSON) +import Data.Aeson.Casing (aesonDrop, snakeCase) import Data.Aeson.Picker ((|--)) +import GHC.Generics (Generic) import System.BCD.Config (FromJsonConfig (..), getConfigText) data Neo4jConfig = Neo4jConfig { _host :: String @@ -17,17 +29,16 @@ data Neo4jConfig = Neo4jConfig { _host :: String , _rps :: Int , _descr :: String } - deriving (Show, Read, Eq) + deriving (Show, Read, Eq, Generic) + +makeLenses ''Neo4jConfig + +instance ToJSON Neo4jConfig where + toJSON = genericToJSON $ aesonDrop 1 snakeCase +instance FromJSON Neo4jConfig where + parseJSON = genericParseJSON $ aesonDrop 1 snakeCase instance FromJsonConfig Neo4jConfig where fromJsonConfig = do - jsonText <- getConfigText - let get field = jsonText |-- ["deploy", "neo4j", field] - pure $ Neo4jConfig (get "host") - (get "port") - (get "user") - (get "password") - (get "stripes") - (get "timeout") - (get "rps") - (get "descr") + config <- getConfigText + pure $ config |-- ["deploy", "neo4j"] diff --git a/src/System/BCD/Config/Postgres.hs b/src/System/BCD/Config/Postgres.hs index 98da124..dcb6259 100644 --- a/src/System/BCD/Config/Postgres.hs +++ b/src/System/BCD/Config/Postgres.hs @@ -1,14 +1,24 @@ -{-# LANGUAGE OverloadedStrings #-} - module System.BCD.Config.Postgres - ( PostgresConfig (..) + ( + PostgresConfig (..) , FromJsonConfig (..) + , host + , port + , user + , password + , databases + , descr ) where -import Data.Aeson.Picker ((|--)) -import Data.Map.Strict (Map) -import Data.Text (Text) -import System.BCD.Config (FromJsonConfig (..), getConfigText) +import Control.Lens (makeLenses) +import Data.Aeson (FromJSON (..), ToJSON (..), + genericParseJSON, genericToJSON) +import Data.Aeson.Casing (aesonDrop, snakeCase) +import Data.Aeson.Picker ((|--)) +import Data.Map.Strict (Map) +import Data.Text (Text) +import GHC.Generics (Generic) +import System.BCD.Config (FromJsonConfig (..), getConfigText) data PostgresConfig = PostgresConfig { _host :: String , _port :: Int @@ -17,15 +27,16 @@ data PostgresConfig = PostgresConfig { _host :: String , _databases :: Map String Text , _descr :: String } - deriving (Show, Read, Eq) + deriving (Show, Read, Eq, Generic) + +makeLenses ''PostgresConfig + +instance ToJSON PostgresConfig where + toJSON = genericToJSON $ aesonDrop 1 snakeCase +instance FromJSON PostgresConfig where + parseJSON = genericParseJSON $ aesonDrop 1 snakeCase instance FromJsonConfig PostgresConfig where fromJsonConfig = do - jsonText <- getConfigText - let get field = jsonText |-- ["deploy", "postgres", field] - pure $ PostgresConfig (get "host") - (get "port") - (get "user") - (get "password") - (get "databases") - (get "descr") + config <- getConfigText + pure $ config |-- ["deploy", "postgres"] diff --git a/src/System/BCD/Config/Redis.hs b/src/System/BCD/Config/Redis.hs index 78f9afc..c0113df 100644 --- a/src/System/BCD/Config/Redis.hs +++ b/src/System/BCD/Config/Redis.hs @@ -1,11 +1,20 @@ -{-# LANGUAGE OverloadedStrings #-} - module System.BCD.Config.Redis - ( RedisConfig (..) + ( + RedisConfig (..) , FromJsonConfig (..) + , host + , port + , user + , password + , descr ) where +import Control.Lens (makeLenses) +import Data.Aeson (FromJSON (..), ToJSON (..), + genericParseJSON, genericToJSON) +import Data.Aeson.Casing (aesonDrop, snakeCase) import Data.Aeson.Picker ((|--)) +import GHC.Generics (Generic) import System.BCD.Config (FromJsonConfig (..), getConfigText) data RedisConfig = RedisConfig { _host :: String @@ -14,15 +23,16 @@ data RedisConfig = RedisConfig { _host :: String , _password :: String , _descr :: String } - deriving (Show, Read, Eq) + deriving (Show, Read, Eq, Generic) + +makeLenses ''RedisConfig +instance ToJSON RedisConfig where + toJSON = genericToJSON $ aesonDrop 1 snakeCase +instance FromJSON RedisConfig where + parseJSON = genericParseJSON $ aesonDrop 1 snakeCase instance FromJsonConfig RedisConfig where fromJsonConfig = do - jsonText <- getConfigText - let get field = jsonText |-- ["deploy", "redis", field] - pure $ RedisConfig (get "host") - (get "port") - (get "user") - (get "password") - (get "descr") + config <- getConfigText + pure $ config |-- ["deploy", "redis"] diff --git a/src/System/BCD/Config/Schrodinger.hs b/src/System/BCD/Config/Schrodinger.hs index 0f7d02c..4efe1b1 100644 --- a/src/System/BCD/Config/Schrodinger.hs +++ b/src/System/BCD/Config/Schrodinger.hs @@ -1,11 +1,19 @@ -{-# LANGUAGE OverloadedStrings #-} - module System.BCD.Config.Schrodinger - ( SchrodingerConfig (..) + ( + SchrodingerConfig (..) , FromJsonConfig (..) + , host + , port + , user + , password ) where +import Control.Lens (makeLenses) +import Data.Aeson (FromJSON (..), ToJSON (..), + genericParseJSON, genericToJSON) +import Data.Aeson.Casing (aesonDrop, snakeCase) import Data.Aeson.Picker ((|--)) +import GHC.Generics (Generic) import System.BCD.Config (FromJsonConfig (..), getConfigText) data SchrodingerConfig = SchrodingerConfig { _host :: String @@ -13,13 +21,16 @@ data SchrodingerConfig = SchrodingerConfig { _host :: String , _user :: String , _password :: String } - deriving (Show, Read, Eq) + deriving (Show, Read, Eq, Generic) + +makeLenses ''SchrodingerConfig + +instance ToJSON SchrodingerConfig where + toJSON = genericToJSON $ aesonDrop 1 snakeCase +instance FromJSON SchrodingerConfig where + parseJSON = genericParseJSON $ aesonDrop 1 snakeCase instance FromJsonConfig SchrodingerConfig where fromJsonConfig = do - jsonText <- getConfigText - let get field = jsonText |-- ["deploy", "schrodinger", field] - pure $ SchrodingerConfig (get "host") - (get "port") - (get "user") - (get "password") + config <- getConfigText + pure $ config |-- ["deploy", "schrodinger"]