data-object switch from To/FromRawObject to To/FromObject

This commit is contained in:
Michael Snoyman 2009-10-15 09:14:21 +02:00
parent 32ae0439f7
commit 971d05050c
6 changed files with 21 additions and 78 deletions

View File

@ -24,7 +24,6 @@ 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.Raw import Data.Object.Raw
import Data.Enumerable import Data.Enumerable
import Control.Monad (when) import Control.Monad (when)
@ -44,10 +43,6 @@ 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,62 +0,0 @@
{-# LANGUAGE TypeSynonymInstances #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE OverlappingInstances #-}
---------------------------------------------------------
--
-- Module : Web.Restful.I18N
-- Copyright : Michael Snoyman
-- License : BSD3
--
-- Maintainer : Michael Snoyman <michael@snoyman.com>
-- Stability : Stable
-- Portability : portable
--
-- Simple method for internationalization.
--
---------------------------------------------------------
module Web.Restful.I18N
( Language
, Translator
, I18N (..)
, translateBS
, NoI18N (..)
) where
import qualified Data.ByteString.Lazy as B
import qualified Data.ByteString as BS
import Data.ByteString.Class
type Language = String
type Translator = [Language] -> B.ByteString
class I18N a where
translate :: a -> Translator
translate a langs = toLazyByteString $ helper langs where
helper [] = defTrans a
helper (l:ls) =
case tryTranslate a l of
Nothing -> helper ls
Just s -> s
defTrans :: a -> String
tryTranslate :: a -> Language -> Maybe String
instance I18N String where
defTrans = id
tryTranslate = const . Just
translateBS :: I18N a => a -> Translator
translateBS a = toLazyByteString . translate a
class NoI18N a where
noTranslate :: a -> Translator
instance NoI18N B.ByteString where
noTranslate = const
instance NoI18N BS.ByteString where
noTranslate = const . toLazyByteString
instance NoI18N String where
noTranslate = const . toLazyByteString

View File

@ -34,23 +34,34 @@ module Web.Restful.Response
, objectResponse , objectResponse
-- * Tests -- * Tests
, testSuite , testSuite
-- * Re-export -- * Translation
, module Web.Restful.I18N , TranslatorBS
, noTranslate
, translateBS
) where ) where
import Data.Time.Clock import Data.Time.Clock
import Data.Object import Data.Object
import Data.Object.Raw import Data.Object.Raw
import Data.Object.Translate
import Data.Object.Instances import Data.Object.Instances
import Data.ByteString.Lazy (ByteString)
import Data.ByteString.Class
import Web.Encodings (formatW3) import Web.Encodings (formatW3)
import Web.Restful.I18N
import Test.Framework (testGroup, Test) import Test.Framework (testGroup, Test)
type ContentType = String type ContentType = String
type Rep = (ContentType, Translator) type TranslatorBS = [Language] -> ByteString
noTranslate :: LazyByteString lbs => lbs -> TranslatorBS
noTranslate lbs = const $ toLazyByteString lbs
translateBS :: CanTranslate t => t -> TranslatorBS
translateBS t langs = toLazyByteString $ translate t langs
type Rep = (ContentType, TranslatorBS)
type Reps = [Rep] type Reps = [Rep]
-- | Something which can be represented as multiple content types. -- | Something which can be represented as multiple content types.
@ -106,14 +117,14 @@ response :: (Monad m, HasReps reps) => reps -> m Reps
response = return . reps response = return . reps
-- | Return a response with an arbitrary content type. -- | Return a response with an arbitrary content type.
genResponse :: (Monad m, NoI18N lbs) genResponse :: (Monad m, LazyByteString lbs)
=> ContentType => ContentType
-> lbs -> lbs
-> m Reps -> m Reps
genResponse ct lbs = return [(ct, noTranslate lbs)] genResponse ct lbs = return [(ct, noTranslate lbs)]
-- | Return a response with a text/html content type. -- | Return a response with a text/html content type.
htmlResponse :: (Monad m, NoI18N lbs) => lbs -> m Reps htmlResponse :: (Monad m, LazyByteString lbs) => lbs -> m Reps
htmlResponse = genResponse "text/html" htmlResponse = genResponse "text/html"
-- | Return a response from an Object. -- | Return a response from an Object.
@ -122,7 +133,7 @@ objectResponse o = return $ reps $ (toObject o :: RawObject)
-- HasReps instances -- HasReps instances
instance HasReps () where instance HasReps () where
reps _ = [("text/plain", translate "")] reps _ = [("text/plain", noTranslate "")]
instance HasReps RawObject where instance HasReps RawObject where
reps o = reps o =
[ ("text/html", noTranslate $ unHtml $ safeFromObject o) [ ("text/html", noTranslate $ unHtml $ safeFromObject o)

View File

@ -31,7 +31,7 @@ data AtomFeed = AtomFeed
} }
instance HasReps AtomFeed where instance HasReps AtomFeed where
reps e = reps e =
[ ("application/atom+xml", translate $ show e) [ ("application/atom+xml", noTranslate $ show e)
] ]
data AtomFeedEntry = AtomFeedEntry data AtomFeedEntry = AtomFeedEntry

View File

@ -79,7 +79,7 @@ instance Show SitemapResponse where
instance HasReps SitemapResponse where instance HasReps SitemapResponse where
reps res = reps res =
[ ("text/xml", translate $ show res) [ ("text/xml", noTranslate $ show res)
] ]
sitemap :: IO [SitemapUrl] -> Handler sitemap :: IO [SitemapUrl] -> Handler

View File

@ -1,5 +1,5 @@
name: restful name: restful
version: 0.1.7 version: 0.1.8
license: BSD3 license: BSD3
license-file: LICENSE license-file: LICENSE
author: Michael Snoyman <michael@snoyman.com> author: Michael Snoyman <michael@snoyman.com>
@ -47,7 +47,6 @@ library
Web.Restful.Handler, Web.Restful.Handler,
Web.Restful.Application, Web.Restful.Application,
Web.Restful.Resource, Web.Restful.Resource,
Web.Restful.I18N,
Data.Object.Instances, Data.Object.Instances,
Hack.Middleware.MethodOverride, Hack.Middleware.MethodOverride,
Web.Restful.Helpers.Auth, Web.Restful.Helpers.Auth,