Permalink
Browse files

First version

  • Loading branch information...
1 parent 789671d commit 0201643ad26dd43a41418f039d29b1d398ee4750 @snoyberg committed Jul 27, 2009
Showing with 158 additions and 0 deletions.
  1. +2 −0 .gitignore
  2. +90 −0 Data/Object.hs
  3. +25 −0 LICENSE
  4. +1 −0 README
  5. +7 −0 Setup.lhs
  6. +23 −0 data-object.cabal
  7. +10 −0 test.hs
View
@@ -0,0 +1,2 @@
+dist
+*.swp
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
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.
View
@@ -0,0 +1 @@
+Represent hierachichal structures, called objects in JSON.
View
@@ -0,0 +1,7 @@
+#!/usr/bin/env runhaskell
+
+> module Main where
+> import Distribution.Simple
+
+> main :: IO ()
+> main = defaultMain
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
View
@@ -0,0 +1,10 @@
+import Data.Object
+
+test = toObject
+ [ ("foo", ["bar", "baz"])
+ , ("bin", ["bin1", "bin2"])
+ ]
+main = do
+ print test
+ bin <- fromObject test >>= oLookup "bin" :: IO [String]
+ print bin

0 comments on commit 0201643

Please sign in to comment.