File: Person.hs

package info (click to toggle)
haskell-haxr 3000.11.5.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 232 kB
  • sloc: haskell: 1,539; makefile: 16
file content (22 lines) | stat: -rw-r--r-- 864 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
-- | This module demonstrates how to handle heterogeneous structs.
--   See person_server.hs and person_client.hs for examples.
module Person where

import Network.XmlRpc.Internals

-- | Record type used to represent the struct in Haskell.
data Person = Person { name :: String, age :: Int, spouse :: Maybe String } deriving Show

-- | Converts a Person to and from a heterogeneous struct.
--   Uses the existing instance of XmlRpcType for [(String,Value)]
instance XmlRpcType Person where
    toValue p = toValue $ [("name",toValue (name p)),
			   ("age", toValue (age p))]
                           ++ maybe [] ((:[]) . (,) "spouse" . toValue) (spouse p)
    fromValue v = do
		  t <- fromValue v
		  n <- getField "name" t
		  a <- getField "age" t
		  s <- getFieldMaybe "spouse" t
		  return Person { name = n, age = a, spouse = s }
    getType _ = TStruct