@@ -65,26 +65,40 @@ uRelationLikeClass = BiClassInfo { className = ''URelationLike
6565-- | Make an instance of 'NodeLike' class.
6666-- Only data types with one constructor are currently supported.
6767-- Each field is transformed into 'Text' key and its value is transformed into a 'Value'.
68- -- For example, we have a structure
68+ -- For example, we have a structure and define an instance:
6969--
70- -- > data Foo = Bar { baz :: Double
71- -- > , quux :: Text
72- -- > , quuz :: Int
73- -- > }
70+ -- >>> :{
71+ -- data Foo = Bar
72+ -- { baz :: Double
73+ -- , quux :: Text
74+ -- , quuz :: Maybe Int
75+ -- } deriving (Show)
76+ -- makeNodeLike ''Foo
77+ -- :}
7478--
75- -- You can make it instance of 'NodeLike' by writing
79+ -- Then you may create example and convert it to and from Node:
7680--
77- -- > makeNodeLike ''Foo
81+ -- >>> let foo = Bar 42.0 "Loren ipsum" (Just 7)
82+ -- >>> toNode foo
83+ -- Node {nodeIdentity = -1, labels = ["Foo"], nodeProps = fromList [("baz",F 42.0),("quux",T "Loren ipsum"),("quuz",I 7)]}
84+ -- >>> fromNode . toNode $ foo :: Foo
85+ -- Bar {baz = 42.0, quux = "Loren ipsum", quuz = Just 7}
7886--
79- -- Then you may create example and convert it into from from Node :
87+ -- 'Maybe' fields are handled correctly :
8088--
81- -- > ghci> :set -XOverloadedStrings
82- -- > ghci> let foo = Bar 42.0 "Loren ipsum" 7
83- -- > ghci> toNode foo
84- -- > Node {nodeIdentity = -1, labels = ["Foo"], nodeProps = fromList [("baz",F 42.0),("quux",T "Loren ipsum"),("quuz",I 7)]}
85- -- > ghci> fromNode . toNode $ foo :: Foo
86- -- > Bar {baz = 42.0, quux = "Loren ipsum", quuz = 7}
89+ -- >>> let bar = Bar 42.0 "Hello world" Nothing
90+ -- >>> toNode bar
91+ -- Node {nodeIdentity = -1, labels = ["Foo"], nodeProps = fromList [("baz",F 42.0),("quux",T "Hello world"),("quuz",N ())]}
92+ -- >>> :{
93+ -- let barNode = Node
94+ -- { nodeIdentity = -1
95+ -- , labels = ["Foo"]
96+ -- , nodeProps = fromList [("baz", F 42.0), ("quux", T "Hello world")] -- No "quuz" here
97+ -- }
98+ -- :}
8799--
100+ -- >>> fromNode barNode :: Foo
101+ -- Bar {baz = 42.0, quux = "Hello world", quuz = Nothing}
88102makeNodeLike :: Name -> Q [Dec ]
89103makeNodeLike name = makeBiClassInstance nodeLikeClass name id
90104
@@ -308,3 +322,9 @@ getProp container (fieldName, fieldMaybe) | fieldMaybe && fieldName `notMember`
308322
309323unpackError :: Show c => c -> String -> a
310324unpackError container label = error $ $ currentLoc ++ " could not unpack " ++ label ++ " from " ++ show container
325+
326+ {- $setup
327+ >>> :set -XTemplateHaskell
328+ >>> :set -XOverloadedStrings
329+ >>> import Data.Text (Text)
330+ -}
0 commit comments