on master help sign in
fix broken instances of Atomable [a]
aa29193AgentM
src/lib/ProjectM36/Atomable.hs+2−2
@@ -148,10 +148,10 @@ instance (Atomable a, Atomable b) => Atomable (Either a b) where
148--convert to ADT list
148--convert to ADT list
149instance Atomable a => Atomable [a] where
149instance Atomable a => Atomable [a] where
150 toAtom [] = ConstructedAtom "Empty" (listAtomType (toAtomType (Proxy :: Proxy a))) []
150 toAtom [] = ConstructedAtom "Empty" (listAtomType (toAtomType (Proxy :: Proxy a))) []
151− toAtom (x:xs) = ConstructedAtom "Cons" (listAtomType (toAtomType (Proxy :: Proxy a))) (map toAtom (x:xs))
151+ toAtom (x:xs) = ConstructedAtom "Cons" (listAtomType (toAtomType (Proxy :: Proxy a))) (toAtom x: [toAtom xs])
152
152
153 fromAtom (ConstructedAtom "Empty" _ _) = []
153 fromAtom (ConstructedAtom "Empty" _ _) = []
154− fromAtom (ConstructedAtom "Cons" _ (x:xs)) = fromAtom x:map fromAtom xs
154+ fromAtom (ConstructedAtom "Cons" _ (x:y:[])) = fromAtom x:fromAtom y
155 fromAtom _ = error "improper fromAtom [a]"
155 fromAtom _ = error "improper fromAtom [a]"
156
156
157 toAtomType _ = ConstructedAtomType "List" (M.singleton "a" (toAtomType (Proxy :: Proxy a)))
157 toAtomType _ = ConstructedAtomType "List" (M.singleton "a" (toAtomType (Proxy :: Proxy a)))
test/Relation/Atomable.hs+8−2
@@ -1,5 +1,5 @@
1--Test Atomable typeclass which allows users to use existing Haskell datatypes to marshal them to and from the database as ConstructedAtoms.
1--Test Atomable typeclass which allows users to use existing Haskell datatypes to marshal them to and from the database as ConstructedAtoms.
2−{-# LANGUAGE DeriveGeneric, DeriveAnyClass, OverloadedStrings #-}
2+{-# LANGUAGE DeriveGeneric, DeriveAnyClass, OverloadedStrings, TypeApplications #-}
3import Test.HUnit
3import Test.HUnit
4import ProjectM36.Client
4import ProjectM36.Client
5import Data.Binary
5import Data.Binary
@@ -75,7 +75,7 @@ main = do
75 if errors tcounts + failures tcounts > 0 then exitFailure else exitSuccess
75 if errors tcounts + failures tcounts > 0 then exitFailure else exitSuccess
76
76
77testList :: Test
77testList :: Test
78−testList = TestList [testHaskell2DB, testADT1, testADT2, testADT3, testADT4, testADT5, testBasicMarshaling, testListInstance, testNonEmptyInstance, testADT6Maybe, testADT7Either, testNonPrimitiveValues, testRecordType, testManyFields]
78+testList = TestList [testHaskell2DB, testADT1, testADT2, testADT3, testADT4, testADT5, testBasicMarshaling, testSimpleList, testListInstance, testNonEmptyInstance, testADT6Maybe, testADT7Either, testNonPrimitiveValues, testRecordType, testManyFields]
79
79
80-- test some basic data types like int, day, etc.
80-- test some basic data types like int, day, etc.
81testBasicMarshaling :: Test
81testBasicMarshaling :: Test
@@ -160,6 +160,12 @@ testListInstance = TestCase $ do
160 let example = TestListC [3,4,5]
160 let example = TestListC [3,4,5]
161 assertEqual "List instance" example (fromAtom (toAtom example))
161 assertEqual "List instance" example (fromAtom (toAtom example))
162
162
163+testSimpleList :: Test
164+testSimpleList = TestCase $ do
165+ let example = toAtom @[Integer] [1,2,3]
166+ expected = ConstructedAtom "Cons" (ConstructedAtomType "List" (M.fromList [("a",IntegerAtomType)])) [IntegerAtom 1,ConstructedAtom "Cons" (ConstructedAtomType "List" (M.fromList [("a",IntegerAtomType)])) [IntegerAtom 2,ConstructedAtom "Cons" (ConstructedAtomType "List" (M.fromList [("a",IntegerAtomType)])) [IntegerAtom 3,ConstructedAtom "Empty" (ConstructedAtomType "List" (M.fromList [("a",IntegerAtomType)])) []]]]
167+ assertEqual "simple list" expected example
168+
163testNonEmptyInstance :: Test
169testNonEmptyInstance :: Test
164testNonEmptyInstance = TestCase $ do
170testNonEmptyInstance = TestCase $ do
165 let example = TestNonEmptyC [3,4,5]
171 let example = TestNonEmptyC [3,4,5]