Skip to content

Commit

Permalink
Merge
Browse files Browse the repository at this point in the history
  • Loading branch information
bos committed Nov 17, 2011
2 parents 2dad1d1 + 92e5943 commit 36dcf66
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 17 deletions.
8 changes: 7 additions & 1 deletion Data/Configurator.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
58 changes: 44 additions & 14 deletions Data/Configurator/Instances.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Data/Configurator/Parser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
]
Expand Down
2 changes: 1 addition & 1 deletion Data/Configurator/Types/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 36dcf66

Please sign in to comment.