hexresearch / hschain

Other
4 stars 0 forks source link

Add derivation of configs using DerivingVia #619

Closed Shimuuar closed 4 years ago

Shimuuar commented 4 years ago

This PR adds new package which allows to easily derive FromJSON instances for configs that provide good error messages and convenient fields.

data Cfg = Cfg
  { cfgPort :: Woth16
  , cfgPathDB :: FilePath
  }
  deriving Generic
  deriving FromJSON via SnakeCase (DropSmart (Config Cfg))

will parse JSON of following form:

{
  "port": 123,
  "path_db": "/tmp"
}

Error messages are bet illustrated by series of building config for hschain-coin-node from scratch:


----------------------------------------------------------------
{}
----------------------------------------------------------------

hschain-coin-node: Could not convert to expected type: Error in $: while parsing NodeConfig defined in main:Main
Records's fields:
    + spec : NodeSpec BData
    + coin : CoinSpecification
    + delays : Configuration Example
    + validator_keys : [PublicKey ((:&) Ed25519 SHA256)]
    + max_h : Maybe Height
    + port : Word16
    + seeds : [NetAddr]
Missing mandatory field "spec"

----------------------------------------------------------------
spec: {}
coin: {}
delays: {}
validator_keys: []
port: 50000
seeds: []
----------------------------------------------------------------

hschain-coin-node: Could not convert to expected type: Error in $: while parsing NodeConfig defined in main:Main
 - while parsing field "spec"
while parsing NodeSpec defined in hschain-examples-0.1-inplace:HSChain.Mock.Types
Records's fields:
    + priv_key : Maybe (PrivValidator ((:&) Ed25519 SHA256))
    + db_name : Maybe [Char]
    + log_file : [ScribeSpec]
    + persist_ival : Maybe Int
Missing mandatory field "log_file"

----------------------------------------------------------------
spec:
  log_file : []
coin: {}
delays: {}
validator_keys: []
port: 50000
seeds: []
----------------------------------------------------------------

 - while parsing field "coin"
while parsing CoinSpecification defined in hschain-examples-0.1-inplace:HSChain.Mock.Types
Records's fields:
    + airdrop : Integer
    + wallets : Int
    + wallets_seed : Int
    + generator_delay : Maybe Int
    + max_mempool_size : Int
Missing mandatory field "airdrop"

----------------------------------------------------------------
spec:
  log_file : []
coin:
  airdrop: 122
  wallets: 100
  wallets_seed: 1233
  max_mempool_size: []
delays: {}
validator_keys: []
port: 50000
seeds: []
----------------------------------------------------------------

hschain-coin-node: Could not convert to expected type: Error in $: while parsing NodeConfig defined in main:Main
 - while parsing field "coin"
while parsing CoinSpecification defined in hschain-examples-0.1-inplace:HSChain.Mock.Types
 - while parsing field "max_mempool_size"
parsing Int failed, expected Number, but encountered Array