config-schema: Schema definitions for the config-value package

[ language, library ] [ Propose Tags ] [ Report a vulnerability ]

This package makes it possible to define schemas for use when loading configuration files using the config-value format. These schemas can be used to process a configuration file into a Haskell value or to automatically generate documentation for the file format.


[Skip to Readme]

Downloads

Note: This package has metadata revisions in the cabal description newer than included in the tarball. To unpack the package including the revisions, use 'cabal get'.

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

Versions [RSS] 0.1.0.0, 0.2.0.0, 0.3.0.0, 0.3.1.0, 0.3.1.1, 0.4.0.0, 0.4.1.0, 0.5.0.0, 0.5.0.1, 1.0.0.0, 1.1.0.0, 1.2.0.0, 1.2.1.0, 1.2.2.0, 1.3.0.0
Change log ChangeLog.md
Dependencies base (>=4.9 && <4.21), config-value (>=0.8.3 && <0.9), containers (>=0.5 && <0.8), free (>=4.12 && <5.3), kan-extensions (>=5.0.2 && <5.3), pretty (>=1.1.2 && <1.2), semigroupoids (>=5.1 && <6.1), text (>=1.2 && <2.2), transformers (>=0.4 && <0.7) [details]
Tested with ghc ==8.0.2, ghc ==8.2.2, ghc ==8.4.4, ghc ==8.6.5, ghc ==8.8.4, ghc ==8.10.2
License ISC
Copyright Eric Mertens 2017
Author Eric Mertens
Maintainer emertens@gmail.com
Revised Revision 5 made by EricMertens at 2024-05-29T16:13:01Z
Category Language
Home page https://github.com/glguy/config-schema
Bug tracker https://github.com/glguy/config-schema/issues
Source repo head: git clone https://github.com/glguy/config-schema
Uploaded by EricMertens at 2022-08-10T22:30:19Z
Distributions Arch:1.3.0.0, Debian:1.2.0.0, NixOS:1.3.0.0
Reverse Dependencies 3 direct, 1 indirect [details]
Downloads 11245 total (78 in the last 30 days)
Rating 2.0 (votes: 1) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2022-08-11 [all 1 reports]

Readme for config-schema-1.3.0.0

[back to package description]

config-schema

Hackage Build Status

Live Demo

The config-value and config-schema packages are available in a live demo.

About

This package allows the user to define configuration schemas suitable for matching against configuration files written in the config-value format. These schemas allow the user to extract an arbitrary Haskell value from an interpretation of a configuration file. It also allows the user to programatically generate documentation for the configuration files accepted by the loader.

{-# Language OverloadedStrings, ApplicativeDo #-}
module Example where

import qualified Data.Text as Text
import           Data.Text (Text)
import           Data.Monoid ((<>))
import           Data.Functor.Alt ((<!>))
import           Data.List.NonEmpty (NonEmpty)

import           Config
import           Config.Schema

exampleFile :: Text
exampleFile =
  " name: \"Johny Appleseed\" \n\
  \ age : 99                  \n\
  \ happy: yes                \n\
  \ kids:                     \n\
  \   * name: \"Bob\"         \n\
  \   * name: \"Tom\"         \n"

exampleValue :: Value Position
Right exampleValue = parse exampleFile

exampleSpec :: ValueSpec Text
exampleSpec = sectionsSpec "" $
  do name  <- reqSection  "name" "Full name"
     age   <- reqSection  "age"  "Age of user"
     happy <- optSection' "happy" yesOrNo
              "Current happiness status"
     kids  <- reqSection' "kids"  (oneOrList kidSpec)
              "All children's names"

     return $
       let happyText = case happy of Just True  -> " and is happy"
                                     Just False -> " and is not happy"
                                     Nothing    -> " and is private"

       in name <> " is " <> Text.pack (show (age::Integer)) <>
             " years old and has kids " <>
             Text.intercalate ", " kids <>
             happyText

kidSpec :: ValueSpec Text
kidSpec = sectionsSpec "kid" (reqSection "name" "Kid's name")


-- | Matches the 'yes' and 'no' atoms
yesOrNo :: ValueSpec Bool
yesOrNo = True  <$ atomSpec "yes" <!>
          False <$ atomSpec "no"


printDoc :: IO ()
printDoc = print (generateDocs exampleSpec)
-- *Example> printDoc
-- Top-level configuration file fields:
--     name: REQUIRED text
--        Full name
--     age: REQUIRED integer
--        Age of user
--     happy: `yes` or `no`
--        Current happiness status
--     kids: REQUIRED kid or list of kid
--        All children
--
-- kid
--     name: REQUIRED text
--        Kid's name

example :: Either (NonEmpty (LoadError Position)) Text
example = loadValue exampleSpec exampleValue
-- *Example> exampleVal
-- Right "Johny Appleseed is 99 years old and has kids Bob, Tom and is happy"