Permalink
Browse files

Merge pull request #12 from basvandijk/configured-list

Allow instance Configured [a] without OverlappingInstances
  • Loading branch information...
2 parents 2cd1d44 + 79846aa commit 4e64dd2d9467fc7867a9f7844a1d760fa50abc9d @bos committed Apr 28, 2014
@@ -91,8 +91,11 @@ instance Configured T.Text where
convert (String v) = Just v
convert _ = Nothing
-instance Configured [Char] where
- convert = fmap T.unpack . convert
+instance Configured Char where
+ convert (String txt) | T.length txt == 1 = Just $ T.head txt
+ convert _ = Nothing
+
+ convertList = fmap T.unpack . convert
instance Configured L.Text where
convert = fmap L.fromStrict . convert
@@ -14,7 +14,7 @@ module Data.Configurator.Types
, Config
, Name
, Value(..)
- , Configured(..)
+ , Configured, convert
, Worth(..)
-- * Exceptions
, ConfigError(..)
@@ -131,6 +131,13 @@ instance Hashable Pattern where
class Configured a where
convert :: Value -> Maybe a
+ convertList :: Value -> Maybe [a]
+ convertList (List xs) = mapM convert xs
+ convertList _ = Nothing
+
+instance Configured a => Configured [a] where
+ convert = convertList
+
-- | An error occurred while processing a configuration file.
data ConfigError = ParseError FilePath String
deriving (Show, Typeable)
View
@@ -133,10 +133,19 @@ typesTest = withLoad [Required "resources/pathological.cfg"] $ \ cfg -> do
assertEqual "word64" asWord64 (Just 1)
asTextBad <- lookup cfg "aa" :: IO (Maybe Text)
- assertEqual "word64" asTextBad Nothing
+ assertEqual "bad text" asTextBad Nothing
asTextGood <- lookup cfg "ab" :: IO (Maybe Text)
- assertEqual "word64" asTextGood (Just "foo")
+ assertEqual "good text" asTextGood (Just "foo")
+
+ asStringGood <- lookup cfg "ab" :: IO (Maybe String)
+ assertEqual "string" asStringGood (Just "foo")
+
+ asInts <- lookup cfg "xs" :: IO (Maybe [Int])
+ assertEqual "ints" asInts (Just [1,2,3])
+
+ asChar <- lookup cfg "c" :: IO (Maybe Char)
+ assertEqual "char" asChar (Just 'x')
interpTest :: Assertion
interpTest = withLoad [Required "resources/pathological.cfg"] $ \ cfg -> do
@@ -165,4 +174,3 @@ reloadTest = withReload [Required "resources/pathological.cfg"] $ \[Just f] cfg
assertEqual "notify happened" r1 (Just ())
r2 <- takeMVarTimeout 2000 wongly
assertEqual "notify not happened" r2 Nothing
-
@@ -34,3 +34,6 @@ ag { q-e { i_u9 { a=false}}}
ba = "$(HOME)"
+xs = [1,2,3]
+
+c = "x"

2 comments on commit 4e64dd2

@bos any chance of getting a new release pushed to hackage with this change?

@bos ping?

Please sign in to comment.