Skip to content

Commit

Permalink
Merge pull request #43 from biocad/maksbotan/version-0.0.1.6
Browse files Browse the repository at this point in the history
version 0.0.1.6: fix makeNodeLike for Maybe fields
  • Loading branch information
maksbotan authored Dec 26, 2020
2 parents d970e62 + 48013e4 commit 279193e
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 16 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

## [0.0.1.6] - 2020-12-26
### Fixed
- Fix `makeNodeLike` for `Maybe` fields, bug introduced in previous version.

## [0.0.1.5] - 2020-12-22
### Fixed
- Compatibility of `makeNodeLike` / `makeURelationLike` with `DuplicateRecordFields`, thanks to
Expand Down
2 changes: 1 addition & 1 deletion hasbolt-extras.cabal
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: hasbolt-extras
version: 0.0.1.5
version: 0.0.1.6
synopsis: Extras for hasbolt library
description: Extras for hasbolt library
homepage: https://github.com/biocad/hasbolt-extras#readme
Expand Down
48 changes: 34 additions & 14 deletions src/Database/Bolt/Extras/Template/Internal/Converters.hs
Original file line number Diff line number Diff line change
Expand Up @@ -65,26 +65,40 @@ uRelationLikeClass = BiClassInfo { className = ''URelationLike
-- | Make an instance of 'NodeLike' class.
-- Only data types with one constructor are currently supported.
-- Each field is transformed into 'Text' key and its value is transformed into a 'Value'.
-- For example, we have a structure
-- For example, we have a structure and define an instance:
--
-- > data Foo = Bar { baz :: Double
-- > , quux :: Text
-- > , quuz :: Int
-- > }
-- >>> :{
-- data Foo = Bar
-- { baz :: Double
-- , quux :: Text
-- , quuz :: Maybe Int
-- } deriving (Show)
-- makeNodeLike ''Foo
-- :}
--
-- You can make it instance of 'NodeLike' by writing
-- Then you may create example and convert it to and from Node:
--
-- > makeNodeLike ''Foo
-- >>> let foo = Bar 42.0 "Loren ipsum" (Just 7)
-- >>> toNode foo
-- Node {nodeIdentity = -1, labels = ["Foo"], nodeProps = fromList [("baz",F 42.0),("quux",T "Loren ipsum"),("quuz",I 7)]}
-- >>> fromNode . toNode $ foo :: Foo
-- Bar {baz = 42.0, quux = "Loren ipsum", quuz = Just 7}
--
-- Then you may create example and convert it into from from Node:
-- 'Maybe' fields are handled correctly:
--
-- > ghci> :set -XOverloadedStrings
-- > ghci> let foo = Bar 42.0 "Loren ipsum" 7
-- > ghci> toNode foo
-- > Node {nodeIdentity = -1, labels = ["Foo"], nodeProps = fromList [("baz",F 42.0),("quux",T "Loren ipsum"),("quuz",I 7)]}
-- > ghci> fromNode . toNode $ foo :: Foo
-- > Bar {baz = 42.0, quux = "Loren ipsum", quuz = 7}
-- >>> let bar = Bar 42.0 "Hello world" Nothing
-- >>> toNode bar
-- Node {nodeIdentity = -1, labels = ["Foo"], nodeProps = fromList [("baz",F 42.0),("quux",T "Hello world"),("quuz",N ())]}
-- >>> :{
-- let barNode = Node
-- { nodeIdentity = -1
-- , labels = ["Foo"]
-- , nodeProps = fromList [("baz", F 42.0), ("quux", T "Hello world")] -- No "quuz" here
-- }
-- :}
--
-- >>> fromNode barNode :: Foo
-- Bar {baz = 42.0, quux = "Hello world", quuz = Nothing}
makeNodeLike :: Name -> Q [Dec]
makeNodeLike name = makeBiClassInstance nodeLikeClass name id

Expand Down Expand Up @@ -308,3 +322,9 @@ getProp container (fieldName, fieldMaybe) | fieldMaybe && fieldName `notMember`

unpackError :: Show c => c -> String -> a
unpackError container label = error $ $currentLoc ++ " could not unpack " ++ label ++ " from " ++ show container

{- $setup
>>> :set -XTemplateHaskell
>>> :set -XOverloadedStrings
>>> import Data.Text (Text)
-}
8 changes: 7 additions & 1 deletion test/Doctest.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,16 @@ import Test.DocTest (doctest)
-- Taken from https://github.com/kowainik/membrain/blob/master/test/Doctest.hs

main :: IO ()
main =
main = do
doctest
[ "-isrc"
, "src/Database/Bolt/Extras/DSL/Typed.hs"
, "src/Database/Bolt/Extras/DSL/Typed/Types.hs"
, "src/Database/Bolt/Extras/DSL/Typed/Parameters.hs"
]
-- This has to be run separately due to some complications with TH and/or internal modules
-- See here: https://github.com/sol/doctest/issues/160
doctest
[ "-isrc"
, "src/Database/Bolt/Extras/Template/Internal/Converters.hs"
]

0 comments on commit 279193e

Please sign in to comment.