Commits

Bryan O'Sullivan committed c555e52 Merge

Merge pull request #12 from basvandijk/configured-list

Allow instance Configured [a] without OverlappingInstances

  • Participants
  • Parent commits 7a7cc15, a620980

Comments (0)

Files changed (5)

File Data/Configurator/Instances.hs

     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

File Data/Configurator/Types.hs

     , Config
     , Name
     , Value(..)
-    , Configured(..)
+    , Configured, convert
     , Worth(..)
     -- * Exceptions
     , ConfigError(..)

File Data/Configurator/Types/Internal.hs

 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)

File tests/Test.hs

     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
     assertEqual "notify happened" r1 (Just ())
     r2 <- takeMVarTimeout 2000 wongly
     assertEqual "notify not happened" r2 Nothing
-

File tests/resources/pathological.cfg

 
 ba = "$(HOME)"
 
+xs = [1,2,3]
+
+c = "x"