1 {-# LANGUAGE FlexibleContexts #-}
3 module Configurator ( convert_newtype_list )
6 import Data.Configurator.Types (
12 -- | Configurator helper function. We often want to parse a list of
13 -- \"special\" strings; that is, a list of strings with a little
14 -- more type safett. For example, if we want to read a list of IP
15 -- addresses and a list of usernames, we don't want to confuse the
16 -- two. So, we might wrap them in \"Addresses\" and \"Usernames\"
17 -- newtypes. But then Configurator doesn't know how to parse them
18 -- any more! This function takes the newtype constructor and the
19 -- value and does the obvious thing.
23 -- >>> import Data.Configurator () -- Get predefined 'Configured' instances.
24 -- >>> import Data.Text ( pack )
25 -- >>> import Data.Configurator.Types ( Value( String ) )
26 -- >>> newtype Foo = Foo [String] deriving (Show)
27 -- >>> let s1 = String (pack "foo1")
28 -- >>> let s2 = String (pack "foo2")
29 -- >>> let config = List [s1, s2]
30 -- >>> convert_newtype_list Foo config
31 -- Just (Foo ["foo1","foo2"])
33 convert_newtype_list :: Configured b => ([b] -> a) -> Value -> Maybe a
34 convert_newtype_list ctor (List xs) = fmap ctor (mapM convert xs)
35 convert_newtype_list _ _ = Nothing