packages feed

data-object (empty) → 0.0.0

raw patch · 4 files changed

+145/−0 lines, 4 filesdep +basedep +bytestringdep +bytestring-classsetup-changed

Dependencies added: base, bytestring, bytestring-class

Files

+ Data/Object.hs view
@@ -0,0 +1,90 @@+{-# LANGUAGE FlexibleInstances #-}+{-# LANGUAGE TypeSynonymInstances #-}+{-# LANGUAGE OverlappingInstances #-}+---------------------------------------------------------+--+-- Module        : Web.ClientSession+-- Copyright     : Michael Snoyman+-- License       : BSD3+--+-- Maintainer    : Michael Snoyman <michael@snoyman.com>+-- Stability     : Stable+-- Portability   : portable+--+-- These objects show up in different places, eg JSON, Yaml.+-- By providing a representation in a separate repository,+-- other libraries can share a single representation of+-- these structures.+--+---------------------------------------------------------+module Data.Object+    ( Object (..)+    , FromObject (..)+    , ToObject (..)+    , oLookup+    ) where++import qualified Data.ByteString as B+import Data.ByteString.Class+import Control.Arrow++data Object =+    Mapping [(B.ByteString, Object)]+    | Sequence [Object]+    | Scalar B.ByteString+    deriving (Show)++class ToObject a where+    toObject :: a -> Object++class FromObject a where+    fromObject :: Monad m => Object -> m a++bsFromObject :: (Monad m, StrictByteString bs) => Object -> m bs+bsFromObject (Scalar bs) = return $ fromStrictByteString bs+bsFromObject _ = fail "Attempt to extract a scalar from non-scalar"++instance ToObject String where+    toObject = Scalar . toStrictByteString++instance FromObject String where+    fromObject = bsFromObject++instance ToObject B.ByteString where+    toObject = Scalar++instance FromObject B.ByteString where+    fromObject = bsFromObject++instance ToObject o => ToObject [o] where+    toObject = Sequence . map toObject++instance FromObject o => FromObject [o] where+    fromObject (Sequence os) = mapM fromObject os+    fromObject _ = fail "Attempt to extract a sequence from non-sequence"++instance (StrictByteString bs, ToObject o) => ToObject [(bs, o)] where+    toObject = Mapping . map (toStrictByteString *** toObject)++instance (StrictByteString bs, FromObject o) => FromObject [(bs, o)] where+    fromObject (Mapping pairs) =+        mapM (liftSnd . (fromStrictByteString *** fromObject)) pairs+    fromObject _ = fail "Attempt to extract a mapping from non-mapping"++instance ToObject Object where+    toObject = id++instance FromObject Object where+    fromObject = return++liftSnd :: Monad m => (a, m b) -> m (a, b)+liftSnd (a, b) = b >>= \b' -> return (a, b')++oLookup :: (Monad m, Eq a, Show a, FromObject b)+        => a -- ^ key+        -> [(a, Object)]+        -> m b+oLookup key pairs =+    case lookup key pairs of+        Nothing -> fail $ "Key not found: " ++ show key+        Just x -> fromObject x
+ LICENSE view
@@ -0,0 +1,25 @@+The following license covers this documentation, and the source code, except+where otherwise indicated.++Copyright 2008, Michael Snoyman. All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++* Redistributions of source code must retain the above copyright notice, this+  list of conditions and the following disclaimer.++* Redistributions in binary form must reproduce the above copyright notice,+  this list of conditions and the following disclaimer in the documentation+  and/or other materials provided with the distribution.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS "AS IS" AND ANY EXPRESS OR+IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF+MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO+EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT, INDIRECT,+INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT+NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,+OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF+LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE+OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF+ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ Setup.lhs view
@@ -0,0 +1,7 @@+#!/usr/bin/env runhaskell++> module Main where+> import Distribution.Simple++> main :: IO ()+> main = defaultMain
+ data-object.cabal view
@@ -0,0 +1,23 @@+name:            data-object+version:         0.0.0+license:         BSD3+license-file:    LICENSE+author:          Michael Snoyman <michael@snoyman.com>+maintainer:      Michael Snoyman <michael@snoyman.com>+synopsis:        Represent hierachichal structures, called objects in JSON.+description:     These objects show up in different places, eg JSON, Yaml.+                 By providing a representation in a separate repository,+                 other libraries can share a single representation of+                 these structures.+category:        Data+stability:       unstable+cabal-version:   >= 1.2+build-type:      Simple+homepage:        http://github.com/snoyberg/data-object/tree/master++library+    build-depends:   base >= 4 && < 5,+                     bytestring-class,+                     bytestring >= 0.9.1.4 && < 1+    exposed-modules: Data.Object+    ghc-options:     -Wall