Skip to content

Commit

Permalink
Added ParamReader Float and InputValue Float instances
Browse files Browse the repository at this point in the history
This enables float values to be used everywhere where double values previously were possible already
  • Loading branch information
mpscholten committed Aug 8, 2020
1 parent c9cb949 commit 7d563d0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
8 changes: 8 additions & 0 deletions IHP/Controller/Param.hs
Expand Up @@ -18,6 +18,7 @@ import qualified Data.ByteString.Char8 as Char8
import IHP.ValidationSupport
import GHC.TypeLits
import qualified Data.Attoparsec.ByteString.Char8 as Attoparsec
import qualified GHC.Float as Float

-- | Returns a query or body parameter from the current request. The raw string
-- value is parsed before returning it. So the return value type depends on what
Expand Down Expand Up @@ -203,6 +204,13 @@ instance ParamReader Double where
Right value -> Right value
Left error -> Left ("ParamReader Dobule: " <> cs error)

instance ParamReader Float where
{-# INLINE readParameter #-}
readParameter byteString =
case Attoparsec.parseOnly (Attoparsec.double <* Attoparsec.endOfInput) byteString of
Right value -> Right (Float.double2Float value)
Left error -> Left ("ParamReader Dobule: " <> cs error)

instance ParamReader Text where
{-# INLINE readParameter #-}
readParameter byteString = pure (cs byteString)
Expand Down
3 changes: 3 additions & 0 deletions IHP/ModelSupport.hs
Expand Up @@ -63,6 +63,9 @@ instance InputValue Int where
instance InputValue Double where
inputValue = tshow

instance InputValue Float where
inputValue = tshow

instance InputValue Bool where
inputValue True = "on"
inputValue False = "off"
Expand Down

0 comments on commit 7d563d0

Please sign in to comment.