Skip to content

Commit

Permalink
version 0.1.1.0: lens, json instances (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
Cheshirrrrrr authored and ozzzzz committed Nov 21, 2018
1 parent b150c9b commit 689584b
Show file tree
Hide file tree
Showing 11 changed files with 161 additions and 88 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion bcd-config.cabal
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -37,5 +37,11 @@ library
, containers
, text
, unordered-containers
, aeson
, aeson-casing
, aeson-picker
, lens
default-language: Haskell2010
default-extensions: DeriveGeneric
, OverloadedStrings
, TemplateHaskell
3 changes: 2 additions & 1 deletion src/System/BCD/Config.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module System.BCD.Config
( FromJsonConfig (..)
(
FromJsonConfig (..)
, getConfigText
) where

Expand Down
11 changes: 5 additions & 6 deletions src/System/BCD/Config/BioSources.hs
Original file line number Diff line number Diff line change
@@ -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).
Expand All @@ -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
7 changes: 3 additions & 4 deletions src/System/BCD/Config/FileSystem.hs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{-# LANGUAGE OverloadedStrings #-}

module System.BCD.Config.FileSystem
( FileSystemConfig (..)
(
FileSystemConfig (..)
, FromJsonConfig (..)
) where

Expand All @@ -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
33 changes: 21 additions & 12 deletions src/System/BCD/Config/Mongo.hs
Original file line number Diff line number Diff line change
@@ -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)


Expand All @@ -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"]
37 changes: 24 additions & 13 deletions src/System/BCD/Config/Mysql.hs
Original file line number Diff line number Diff line change
@@ -1,27 +1,38 @@
{-# 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
, _user :: 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"]
39 changes: 25 additions & 14 deletions src/System/BCD/Config/Neo4j.hs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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"]
43 changes: 27 additions & 16 deletions src/System/BCD/Config/Postgres.hs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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"]
32 changes: 21 additions & 11 deletions src/System/BCD/Config/Redis.hs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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"]
31 changes: 21 additions & 10 deletions src/System/BCD/Config/Schrodinger.hs
Original file line number Diff line number Diff line change
@@ -1,25 +1,36 @@
{-# 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
, _port :: Int
, _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"]

0 comments on commit 689584b

Please sign in to comment.