on master help sign in
oops- fix critical bug in Atomable list instance

I'm glad I made a test!

commit463bb374b80e10e2c58f0d5f4941d25ff680be5f authorAgentM <agentm@themactionfaction.com> dateSat 15 Apr 2017, 16:09 (10 years ago) parentec5b8c3
3 files changed+12−3
docs/new_datatypes.markdown+1−1 src/lib/ProjectM36/Atomable.hs+1−1 test/Relation/Atomable.hs+10−1
docs/new_datatypes.markdown+1−1
@@ -104,6 +104,6 @@ The essential ```Atomable``` typeclass functions are:
104104| ```toAtomType :: a -> AtomType``` | generate a database AtomType for a Haskell datatype |
105105| ```toDatabaseContextExpr :: a -> DatabaseContextExpr``` | generate a ```DatabaseContextExpr``` which can be executed against a database context in order to add the new type |
106106
107−In addition, basic Haskell data types like ```Int```, ```Double```, ```Text```, ```Day```, ```Bool```, ```UTCTime```, ```ByteString```, and more already have Atomable instances ready-to-go. In addition, an ```Atomable``` instance is available for lists: ```Atomable a => [a]```, though one should be very careful not to use this in a common anti-pattern. Typically, it makes more sense to store one-to-many relationships as discrete relation variables; when storing them as lists, one gives up the various query capabilities and optimizations of the relational algebra.
107+In addition, basic Haskell data types like ```Int```, ```Double```, ```Text```, ```Day```, ```Bool```, ```UTCTime```, and ```ByteString``` already have Atomable instances ready-to-go. In addition, an ```Atomable``` instance is available for lists: ```Atomable a => [a]```, though one should be very careful not to use this in a common anti-pattern. Typically, it makes more sense to store one-to-many relationships as discrete relation variables; when storing them as lists, one gives up the various query capabilities and optimizations of the relational algebra.
108108
109109To be clear, these data types are not black boxes once stored in the database. These new types can be scanned and manipulated by Haskell scripts which can be created at runtime. For more information, read about [```AtomFunction```s](/docs/atomfunctions.markdown).
src/lib/ProjectM36/Atomable.hs+1−1
@@ -112,7 +112,7 @@ instance Atomable Relation where
112112--convert to ADT list
113113instance Atomable a => Atomable [a] where
114114 toAtom [] = ConstructedAtom "Empty" (listAtomType (toAtomType (undefined :: a))) []
115− toAtom (x:xs) = ConstructedAtom "Cons" (listAtomType (toAtomType x)) (map toAtom xs)
115+ toAtom (x:xs) = ConstructedAtom "Cons" (listAtomType (toAtomType x)) (map toAtom (x:xs))
116116
117117 fromAtom (ConstructedAtom "Empty" _ _) = []
118118 fromAtom (ConstructedAtom "Cons" _ (x:xs)) = fromAtom x:map fromAtom xs
test/Relation/Atomable.hs+10−1
@@ -26,6 +26,9 @@ data Test4T = Test4Ca Int |
2626 Test4Cb Int
2727 deriving (Show, Generic, Eq, Binary, NFData, Atomable)
2828
29+data TestListT = TestListC [Int]
30+ deriving (Show, Generic, Eq, Binary, NFData, Atomable)
31+
2932data Test5T = Test5C {
3033 con1 :: Int,
3134 con2 :: Int
@@ -37,7 +40,7 @@ main = do
3740 if errors tcounts + failures tcounts > 0 then exitFailure else exitSuccess
3841
3942testList :: Test
40−testList = TestList [testHaskell2DB, testADT1, testADT2, testADT3, testADT4, testADT5, testBasicMarshaling]
43+testList = TestList [testHaskell2DB, testADT1, testADT2, testADT3, testADT4, testADT5, testBasicMarshaling, testListInstance]
4144
4245-- test some basic data types like int, day, etc.
4346testBasicMarshaling :: Test
@@ -106,3 +109,9 @@ testADT5 = TestCase $ do
106109
107110checkExecuteDatabaseContextExpr :: SessionId -> Connection -> DatabaseContextExpr -> IO ()
108111checkExecuteDatabaseContextExpr sessionId dbconn expr = executeDatabaseContextExpr sessionId dbconn expr >>= maybe (pure ()) (\err -> assertFailure (show err))
112+
113+testListInstance :: Test
114+testListInstance = TestCase $ do
115+ let example = TestListC [3,4,5]
116+ traceShowM (toAtom example)
117+ assertEqual "List instance" example (fromAtom (toAtom example))