From e18beedbaf70a7b186980c3ce2b7f473ff84664f Mon Sep 17 00:00:00 2001 From: Maxim Koltsov Date: Sat, 26 Dec 2020 13:06:31 +0300 Subject: [PATCH 1/2] Add doctests for TH generators --- .../Extras/Template/Internal/Converters.hs | 48 +++++++++++++------ test/Doctest.hs | 8 +++- 2 files changed, 41 insertions(+), 15 deletions(-) diff --git a/src/Database/Bolt/Extras/Template/Internal/Converters.hs b/src/Database/Bolt/Extras/Template/Internal/Converters.hs index 01b5c6e..4f26b3d 100644 --- a/src/Database/Bolt/Extras/Template/Internal/Converters.hs +++ b/src/Database/Bolt/Extras/Template/Internal/Converters.hs @@ -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 @@ -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) +-} diff --git a/test/Doctest.hs b/test/Doctest.hs index aad0b10..c7dce26 100644 --- a/test/Doctest.hs +++ b/test/Doctest.hs @@ -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" + ] From 48013e4a981d38d58f7261872d02b83093ab2a91 Mon Sep 17 00:00:00 2001 From: Maxim Koltsov Date: Sat, 26 Dec 2020 13:21:08 +0300 Subject: [PATCH 2/2] version 0.0.1.6: fix makeNodeLike for Maybe fields --- CHANGELOG.md | 4 ++++ hasbolt-extras.cabal | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fd050be..b25428b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/hasbolt-extras.cabal b/hasbolt-extras.cabal index ad5c490..05280f8 100644 --- a/hasbolt-extras.cabal +++ b/hasbolt-extras.cabal @@ -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