-
Notifications
You must be signed in to change notification settings - Fork 4
/
Person3.hs
86 lines (75 loc) · 2.47 KB
/
Person3.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE OverloadedLists #-}
{-# LANGUAGE OverloadedStrings #-}
module Person3 where
import Control.Applicative
import Data.Generics.Labels ()
import GHC.Generics
import qualified Generics.SOP as SOP
import Person
import Person2
import Schemas
-- | v3 adds recursive field 'spouse', which leads to cycles
data Person3 = Person3
{ name :: String
, age :: Int
, addresses :: [String]
, spouse :: Maybe Person3 -- new recursive field
, religion :: Maybe Religion
, education :: [Education]
}
deriving (Generic, Eq, Show)
deriving anyclass (SOP.Generic, SOP.HasDatatypeInfo)
instance HasSchema Person3 where
schema = named "Person3"
$ record
$ Person3 <$> field "name" Person3.name
<*> field "age" Person3.age
<*> field "addresses" Person3.addresses
<*> optField "spouse" Person3.spouse
<*> optField "religion" Person3.religion
<*> (field "studies" Person3.education <|> field "education" Person3.education)
laura3, pepe3 :: Person3
-- pepe3 has a cycle with laura3
pepe3 = Person3
"Pepe"
38
["2 Edward Square", "La Mar 10"]
(Just laura3)
Nothing
[PhD "Computer Science", Degree "Engineering"]
-- laura3 has a cycle with pepe3
laura3 = pepe3 { name = "Laura"
, spouse = Just pepe3
, education = [Degree "English"]
, addresses = ["2 Edward Square"]
, religion = Just Catholic
}
martin :: Person3
martin = Person3 "Martin" 10 [] Nothing Nothing []
-- >>> import qualified Data.ByteString.Lazy.Char8 as B
-- >>> import Data.Aeson.Encode.Pretty
-- >>> B.putStrLn $ encodePretty $ finiteEncode 4 laura3
-- {
-- "L": {
-- "spouse": {
-- "L": {}
-- },
-- "religion": "Catholic",
-- "addresses": [
-- "2 Edward Square"
-- ],
-- "age": 38,
-- "studies": {
-- "Degree": "English"
-- },
-- "name": "Laura"
-- }
-- }
-- Unpacking infinite data is not supported currently
-- >>> decode @Person3 (finiteEncode 4 pepe3)
-- Left (MissingRecordField {name = "name", context = ["spouse"]})