X-Git-Url: https://gitweb.michael.orlitzky.com/?a=blobdiff_plain;f=harbl-cli%2Fsrc%2FConfigurator.hs;fp=harbl-cli%2Fsrc%2FConfigurator.hs;h=f85b2be57b28a0c349513ace29525ec097bff07d;hb=b47aaa60a797aee4ecdcd5535ed40c1a7b15ddce;hp=0000000000000000000000000000000000000000;hpb=6eb7fe0f53b0d8690a821cce9257b8b1a17fdaad;p=dead%2Fharbl.git diff --git a/harbl-cli/src/Configurator.hs b/harbl-cli/src/Configurator.hs new file mode 100644 index 0000000..f85b2be --- /dev/null +++ b/harbl-cli/src/Configurator.hs @@ -0,0 +1,35 @@ +{-# LANGUAGE FlexibleContexts #-} + +module Configurator ( convert_newtype_list ) +where + +import Data.Configurator.Types ( + Configured, + Value( List ), + convert ) + + +-- | Configurator helper function. We often want to parse a list of +-- \"special\" strings; that is, a list of strings with a little +-- more type safett. For example, if we want to read a list of IP +-- addresses and a list of usernames, we don't want to confuse the +-- two. So, we might wrap them in \"Addresses\" and \"Usernames\" +-- newtypes. But then Configurator doesn't know how to parse them +-- any more! This function takes the newtype constructor and the +-- value and does the obvious thing. +-- +-- ==== _Examples_ +-- +-- >>> import Data.Configurator () -- Get predefined 'Configured' instances. +-- >>> import Data.Text ( pack ) +-- >>> import Data.Configurator.Types ( Value( String ) ) +-- >>> newtype Foo = Foo [String] deriving (Show) +-- >>> let s1 = String (pack "foo1") +-- >>> let s2 = String (pack "foo2") +-- >>> let config = List [s1, s2] +-- >>> convert_newtype_list Foo config +-- Just (Foo ["foo1","foo2"]) +-- +convert_newtype_list :: Configured b => ([b] -> a) -> Value -> Maybe a +convert_newtype_list ctor (List xs) = fmap ctor (mapM convert xs) +convert_newtype_list _ _ = Nothing