Switch from To/FromRawObjec to To/FromObject

This commit is contained in:
Michael Snoyman 2009-10-15 08:58:41 +02:00
parent 564d1431df
commit 32ae0439f7
3 changed files with 18 additions and 9 deletions

View File

@ -20,6 +20,7 @@ module Data.Object.Instances
) where ) where
import Data.Object import Data.Object
import Data.Object.Raw
import qualified Data.ByteString.Lazy as B import qualified Data.ByteString.Lazy as B
import Data.ByteString.Class import Data.ByteString.Class
import Web.Encodings (encodeJson) import Web.Encodings (encodeJson)
@ -32,7 +33,7 @@ newtype Json = Json { unJson :: B.ByteString }
instance SafeFromObject Json where instance SafeFromObject Json where
safeFromObject = Json . helper where safeFromObject = Json . helper where
helper :: RawObject -> B.ByteString helper :: RawObject -> B.ByteString
helper (Scalar s) = B.concat helper (Scalar (Raw s)) = B.concat
[ toLazyByteString "\"" [ toLazyByteString "\""
, encodeJson $ fromLazyByteString s , encodeJson $ fromLazyByteString s
, toLazyByteString "\"" , toLazyByteString "\""
@ -47,8 +48,8 @@ instance SafeFromObject Json where
, B.intercalate (toLazyByteString ",") $ map helper2 m , B.intercalate (toLazyByteString ",") $ map helper2 m
, toLazyByteString "}" , toLazyByteString "}"
] ]
helper2 :: (B.ByteString, RawObject) -> B.ByteString helper2 :: (Raw, RawObject) -> B.ByteString
helper2 (k, v) = B.concat helper2 (Raw k, v) = B.concat
[ toLazyByteString "\"" [ toLazyByteString "\""
, encodeJson $ fromLazyByteString k , encodeJson $ fromLazyByteString k
, toLazyByteString "\":" , toLazyByteString "\":"
@ -73,7 +74,7 @@ instance SafeFromObject Html where
, toLazyByteString "</body></html>" , toLazyByteString "</body></html>"
] where ] where
helper :: RawObject -> B.ByteString helper :: RawObject -> B.ByteString
helper (Scalar s) = B.concat helper (Scalar (Raw s)) = B.concat
[ toLazyByteString "<p>" [ toLazyByteString "<p>"
, toLazyByteString s , toLazyByteString s
, toLazyByteString "</p>" , toLazyByteString "</p>"
@ -88,8 +89,8 @@ instance SafeFromObject Html where
toLazyByteString "<dl>" : toLazyByteString "<dl>" :
map helper2 m ++ map helper2 m ++
[ toLazyByteString "</dl>" ] [ toLazyByteString "</dl>" ]
helper2 :: (B.ByteString, RawObject) -> B.ByteString helper2 :: (Raw, RawObject) -> B.ByteString
helper2 (k, v) = B.concat helper2 (Raw k, v) = B.concat
[ toLazyByteString "<dt>" [ toLazyByteString "<dt>"
, toLazyByteString k , toLazyByteString k
, toLazyByteString "</dt><dd>" , toLazyByteString "</dt><dd>"

View File

@ -2,6 +2,7 @@
{-# LANGUAGE TypeSynonymInstances #-} {-# LANGUAGE TypeSynonymInstances #-}
{-# LANGUAGE ExistentialQuantification #-} {-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE FlexibleContexts #-}
--------------------------------------------------------- ---------------------------------------------------------
-- --
-- Module : Web.Restful.Application -- Module : Web.Restful.Application
@ -24,6 +25,7 @@ module Web.Restful.Application
import Web.Encodings import Web.Encodings
import qualified Data.ByteString.Lazy as B import qualified Data.ByteString.Lazy as B
import Data.Object import Data.Object
import Data.Object.Raw
import Data.Enumerable import Data.Enumerable
import Control.Monad (when) import Control.Monad (when)
@ -42,6 +44,10 @@ import Web.Restful.Definitions
import Web.Restful.Constants import Web.Restful.Constants
import Web.Restful.Resource import Web.Restful.Resource
-- FIXME move to Data.Object.Raw
toRawObject :: ToObject o Raw Raw => o -> RawObject
toRawObject = toObject
-- | A data type that can be turned into a Hack application. -- | A data type that can be turned into a Hack application.
class ResourceName a => RestfulApp a where class ResourceName a => RestfulApp a where
-- | The encryption key to be used for encrypting client sessions. -- | The encryption key to be used for encrypting client sessions.

View File

@ -1,4 +1,5 @@
{-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE TypeSynonymInstances #-} {-# LANGUAGE TypeSynonymInstances #-}
--------------------------------------------------------- ---------------------------------------------------------
-- --
@ -38,7 +39,8 @@ module Web.Restful.Response
) where ) where
import Data.Time.Clock import Data.Time.Clock
import Data.Object hiding (testSuite) import Data.Object
import Data.Object.Raw
import Data.Object.Instances import Data.Object.Instances
import Web.Encodings (formatW3) import Web.Encodings (formatW3)
@ -115,8 +117,8 @@ htmlResponse :: (Monad m, NoI18N lbs) => lbs -> m Reps
htmlResponse = genResponse "text/html" htmlResponse = genResponse "text/html"
-- | Return a response from an Object. -- | Return a response from an Object.
objectResponse :: (Monad m, ToRawObject o) => o -> m Reps objectResponse :: (Monad m, ToObject o Raw Raw) => o -> m Reps
objectResponse = return . reps . toRawObject objectResponse o = return $ reps $ (toObject o :: RawObject)
-- HasReps instances -- HasReps instances
instance HasReps () where instance HasReps () where