Skip to content
Browse files

Merge pull request #4 from basvandijk/rational

Changed Number constructor from Integer to Rational
  • Loading branch information...
2 parents 600bd4a + acf1f73 commit 92e594316089e7d7258c364c1bfd1db23c49140c @bos committed Nov 17, 2011
Showing with 53 additions and 17 deletions.
  1. +7 −1 Data/Configurator.hs
  2. +44 −14 Data/Configurator/Instances.hs
  3. +1 −1 Data/Configurator/Parser.hs
  4. +1 −1 Data/Configurator/Types/Internal.hs
View
8 Data/Configurator.hs
@@ -72,6 +72,8 @@ import Data.Maybe (fromMaybe, isJust)
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)
@@ -291,7 +293,11 @@ interpolate s env
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
View
58 Data/Configurator/Instances.hs
@@ -5,9 +5,13 @@ module Data.Configurator.Instances () where
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
@@ -20,42 +24,68 @@ instance Configured Bool where
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
View
2 Data/Configurator/Parser.hs
@@ -84,7 +84,7 @@ value = mconcat [
, string "true" *> pure (Bool True)
, string "false" *> pure (Bool False)
, String <$> string_
- , Number <$> decimal
+ , Number <$> rational
, List <$> brackets '[' ']'
((value <* skipLWS) `sepBy` (char ',' <* skipLWS))
]
View
2 Data/Configurator/Types/Internal.hs
@@ -201,7 +201,7 @@ data Value = Bool Bool
--
-- * @\\u@/xxxx/@\\u@/xxxx/ - Unicode character (as two
-- UTF-16 surrogates)
- | Number Integer
+ | Number Rational
-- ^ Integer.
| List [Value]
-- ^ Heterogeneous list. Represented in a configuration

0 comments on commit 92e5943

Please sign in to comment.
Something went wrong with that request. Please try again.