Bryan O'Sullivan avatar Bryan O'Sullivan committed 599cab8 Merge

Merge

Comments (0)

Files changed (4)

Data/Configurator.hs

 import Data.Monoid (mconcat)
 import Data.Text.Lazy.Builder (fromString, fromText, toLazyText)
 import Data.Text.Lazy.Builder.Int (decimal)
+import Data.Text.Lazy.Builder.RealFloat (realFloat)
+import Data.Ratio (denominator, numerator)
 import Prelude hiding (catch, lookup)
 import System.Environment (getEnv)
 import System.IO (hPutStrLn, stderr)
   interpret (Interpolate name) =
       case H.lookup name env of
         Just (String x) -> return (fromText x)
-        Just (Number n) -> return (decimal n)
+        Just (Number r)
+            | denominator r == 1 -> return (decimal $ numerator r)
+            | otherwise -> return $ realFloat (fromRational r :: Double)
+                           -- TODO: Use a dedicated Builder for Rationals instead of
+                           -- using realFloat on a Double.
         Just _          -> error "type error"
         _ -> do
           e <- try . getEnv . T.unpack $ name

Data/Configurator/Instances.hs

 
 import Control.Applicative
 import Data.Configurator.Types.Internal
+import Data.Complex (Complex)
+import Data.Fixed (Fixed, HasResolution)
 import Data.Int (Int8, Int16, Int32, Int64)
 import Data.Text.Encoding (encodeUtf8)
+import Data.Ratio (Ratio, denominator, numerator)
 import Data.Word (Word, Word8, Word16, Word32, Word64)
+import Foreign.C.Types (CDouble, CFloat)
 import qualified Data.ByteString as B
 import qualified Data.ByteString.Lazy as LB
 import qualified Data.Text as T
     convert (Bool v) = Just v
     convert _        = Nothing
 
-convertNumber :: (Num a) => Value -> Maybe a
-convertNumber (Number v) = Just (fromIntegral v)
-convertNumber _          = Nothing
+convertNumberToNum :: (Num a) => Value -> Maybe a
+convertNumberToNum (Number r)
+    | denominator r == 1 = Just $ fromInteger $ numerator r
+convertNumberToNum _ = Nothing
 
 instance Configured Int where
-    convert = convertNumber
+    convert = convertNumberToNum
 
 instance Configured Integer where
-    convert = convertNumber
+    convert = convertNumberToNum
 
 instance Configured Int8 where
-    convert = convertNumber
+    convert = convertNumberToNum
 
 instance Configured Int16 where
-    convert = convertNumber
+    convert = convertNumberToNum
 
 instance Configured Int32 where
-    convert = convertNumber
+    convert = convertNumberToNum
 
 instance Configured Int64 where
-    convert = convertNumber
+    convert = convertNumberToNum
 
 instance Configured Word where
-    convert = convertNumber
+    convert = convertNumberToNum
 
 instance Configured Word8 where
-    convert = convertNumber
+    convert = convertNumberToNum
 
 instance Configured Word16 where
-    convert = convertNumber
+    convert = convertNumberToNum
 
 instance Configured Word32 where
-    convert = convertNumber
+    convert = convertNumberToNum
 
 instance Configured Word64 where
-    convert = convertNumber
+    convert = convertNumberToNum
+
+convertNumberToFractional :: (Fractional a) => Value -> Maybe a
+convertNumberToFractional (Number r) = Just $ fromRational r
+convertNumberToFractional _ = Nothing
+
+instance Configured Double where
+    convert = convertNumberToFractional
+
+instance Configured Float where
+    convert = convertNumberToFractional
+
+instance Configured CDouble where
+    convert = convertNumberToFractional
+
+instance Configured CFloat where
+    convert = convertNumberToFractional
+
+instance Integral a => Configured (Ratio a) where
+    convert = convertNumberToFractional
+
+instance RealFloat a => Configured (Complex a) where
+    convert = convertNumberToFractional
+
+instance HasResolution a => Configured (Fixed a) where
+    convert = convertNumberToFractional
 
 instance Configured T.Text where
     convert (String v) = Just v

Data/Configurator/Parser.hs

         , string "true" *> pure (Bool True)
         , string "false" *> pure (Bool False)
         , String <$> string_
-        , Number <$> decimal
+        , Number <$> rational
         , List <$> brackets '[' ']'
                    ((value <* skipLWS) `sepBy` (char ',' <* skipLWS))
         ]

Data/Configurator/Types/Internal.hs

            --
            -- * @\\u@/xxxx/@\\u@/xxxx/ - Unicode character (as two
            --   UTF-16 surrogates)
-           | Number Integer
+           | Number Rational
            -- ^ Integer.
            | List [Value]
            -- ^ Heterogeneous list.  Represented in a configuration
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.