-
Notifications
You must be signed in to change notification settings - Fork 39
/
Reflection.hs
45 lines (35 loc) · 1.08 KB
/
Reflection.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
module Reflection where
import Data.Aeson (FromJSON, ToJSON)
import GHC.Generics
--import JsonPersistence -- use this to use JSON serialization/deserialization
import SimplePersistence (Id, Entity, getId, persist, retrieve)
data User = User {
userId :: Id
, name :: String
, email :: String
} deriving (Show, Read, Generic, ToJSON, FromJSON)
instance Entity User where
getId = userId
data Post = Post {
postId :: Id
, userRef :: Id
, text :: String
} deriving (Show, Read, Generic, ToJSON, FromJSON)
instance Entity Post where
getId = postId
retrieveUser :: Id -> IO User
retrieveUser = retrieve
retrievePost :: Id -> IO Post
retrievePost = retrieve
reflectionDemo = do
putStrLn "Reflection"
let user = User "1" "Heinz Meier" "hm@meier.com"
let post = Post "4711" "1" "My name is Heinz, this is my first post"
persist user
persist post
user' <- retrieve "1" :: IO User
user' <- retrieveUser "1"
print user'
retrievePost "4711" >>= print