on master help sign in
rename ConstructedAtom to Atomable to better reflect the meat of the module
commite1c1fbdebd38e8d9f2e4b4c08c396ff05d5a83ee authorAgentM <agentm@tanyas-imac.local> dateSat 1 Apr 2017, 15:13 (10 years ago) parent9a2a933
4 files changed+69−11
project-m36.cabal+3−3 src/lib/ProjectM36/ConstructedAtom.hs → src/lib/ProjectM36/Atomable.hs+49−2 src/lib/ProjectM36/Client.hs+1−1 test/Relation/ConstructedAtom.hs → test/Relation/Atomable.hs+16−5
project-m36.cabal+3−3
@@ -100,7 +100,7 @@ Library
100100 ProjectM36.AtomType,
101101 ProjectM36.AtomFunctions.Basic,
102102 ProjectM36.AtomFunctions.Primitive,
103− ProjectM36.ConstructedAtom,
103+ ProjectM36.Atomable,
104104 ProjectM36.DataConstructorDef,
105105 ProjectM36.DataTypes.Basic,
106106 ProjectM36.DataTypes.Day,
@@ -418,10 +418,10 @@ Test-Suite test-isomorphic-schemas
418418 Hs-Source-Dirs: ./src/bin, .
419419 CPP-Options: -DPROJECTM36_VERSION="v0.1"
420420
421−Test-Suite test-constructedatom
421+Test-Suite test-atomable
422422 Default-Language: Haskell2010
423423 type: exitcode-stdio-1.0
424− main-is: test/Relation/ConstructedAtom.hs
424+ main-is: test/Relation/Atomable.hs
425425 Build-Depends: base, HUnit, Cabal, containers, hashable, unordered-containers, mtl, vector, vector-binary-instances, time, hashable-time, bytestring, uuid, stm, deepseq, deepseq-generics, binary, parallel, cassava, attoparsec, gnuplot, directory, temporary, haskeline, megaparsec, text, base64-bytestring, data-interval, filepath, transformers, stm-containers, list-t, websockets, optparse-applicative, network, aeson, uuid-aeson, project-m36 == 0.1, random, MonadRandom
426426 Default-Extensions: OverloadedStrings
427427 GHC-Options: -Wall
src/lib/ProjectM36/ConstructedAtom.hs → src/lib/ProjectM36/Atomable.hsrenamed+49−2
@@ -1,8 +1,9 @@
11{-# LANGUAGE DefaultSignatures, TypeFamilies, TypeOperators, PolyKinds, FlexibleInstances, ScopedTypeVariables, FlexibleContexts, DeriveGeneric, DeriveAnyClass #-}
2−module ProjectM36.ConstructedAtom where
2+module ProjectM36.Atomable where
33--http://stackoverflow.com/questions/13448361/type-families-with-ghc-generics-or-data-data
44--instances to marshal Haskell ADTs to ConstructedAtoms and back
55import ProjectM36.Base
6+import ProjectM36.Relation
67import ProjectM36.DataTypes.Primitive
78import GHC.Generics
89import qualified Data.Map as M
@@ -10,6 +11,9 @@ import qualified Data.Text as T
1011import Control.DeepSeq (NFData)
1112import Data.Binary
1213import Control.Applicative
14+import Data.Time.Calendar
15+import Data.ByteString (ByteString)
16+import Data.Time.Clock
1317
1418--also add haskell scripting atomable support
1519--rename this module to Atomable along with test
@@ -54,13 +58,56 @@ instance Atomable Int where
5458 toAtomType _ = IntAtomType
5559 toDatabaseContextExpr _ = NoOperation
5660
61+instance Atomable Double where
62+ toAtom d = DoubleAtom d
63+ fromAtom (DoubleAtom d) = d
64+ fromAtom _ = error "improper fromAtom"
65+ toAtomType _ = DoubleAtomType
66+ toDatabaseContextExpr _ = NoOperation
67+
5768instance Atomable T.Text where
5869 toAtom t = TextAtom t
5970 fromAtom (TextAtom t) = t
6071 fromAtom _ = error "improper fromAtom"
6172 toAtomType _ = TextAtomType
6273 toDatabaseContextExpr _ = NoOperation
63−
74+
75+instance Atomable Day where
76+ toAtom d = DayAtom d
77+ fromAtom (DayAtom d) = d
78+ fromAtom _ = error "improper fromAtom"
79+ toAtomType _ = DayAtomType
80+ toDatabaseContextExpr _ = NoOperation
81+
82+instance Atomable UTCTime where
83+ toAtom t = DateTimeAtom t
84+ fromAtom (DateTimeAtom t) = t
85+ fromAtom _ = error "improper fromAtom"
86+ toAtomType _ = DateTimeAtomType
87+ toDatabaseContextExpr _ = NoOperation
88+
89+instance Atomable ByteString where
90+ toAtom = ByteStringAtom
91+ fromAtom (ByteStringAtom b) = b
92+ fromAtom _ = error "improper fromAtom"
93+ toAtomType _ = ByteStringAtomType
94+ toDatabaseContextExpr _ = NoOperation
95+
96+instance Atomable Bool where
97+ toAtom = BoolAtom
98+ fromAtom (BoolAtom b) = b
99+ fromAtom _ = error "improper fromAtom"
100+ toAtomType _ = BoolAtomType
101+ toDatabaseContextExpr _ = NoOperation
102+
103+instance Atomable Relation where
104+ toAtom = RelationAtom
105+ fromAtom (RelationAtom r) = r
106+ fromAtom _ = error "improper fromAtom"
107+ --warning: cannot be used with undefined "Relation"
108+ toAtomType rel = RelationAtomType (attributes rel)
109+ toDatabaseContextExpr _ = NoOperation
110+
64111-- Generics
65112class AtomableG g where
66113 --type AtomTG g
src/lib/ProjectM36/Client.hs+1−1
@@ -82,7 +82,7 @@ module ProjectM36.Client
8282import ProjectM36.Base hiding (inclusionDependencies) --defined in this module as well
8383import qualified ProjectM36.Base as B
8484import ProjectM36.Error
85−import ProjectM36.ConstructedAtom
85+import ProjectM36.Atomable
8686import ProjectM36.StaticOptimizer
8787import ProjectM36.Key
8888import qualified ProjectM36.IsomorphicSchema as Schema
test/Relation/ConstructedAtom.hs → test/Relation/Atomable.hsrenamed+16−5
@@ -9,6 +9,7 @@ import TutorialD.Interpreter.TestBase
99import GHC.Generics
1010import ProjectM36.Relation
1111import ProjectM36.Base
12+import Data.Time.Calendar (Day,toGregorian)
1213import Data.Text
1314import qualified Data.Map as M
1415
@@ -36,8 +37,21 @@ main = do
3637 if errors tcounts + failures tcounts > 0 then exitFailure else exitSuccess
3738
3839testList :: Test
39−testList = TestList [testHaskell2DB, testADT1, testADT2, testADT3, testADT4, testADT5]
40+testList = TestList [testHaskell2DB, testADT1, testADT2, testADT3, testADT4, testADT5, testBasicMarshaling]
4041
42+-- test some basic data types like int, day, etc.
43+testBasicMarshaling :: Test
44+testBasicMarshaling = TestCase $ do
45+ assertEqual "to IntAtom" (IntAtom 5) (toAtom (5 :: Int))
46+ assertEqual "from IntAtom" (5 :: Int) (fromAtom (IntAtom 5))
47+
48+ assertEqual "to BoolAtom" (BoolAtom False) (toAtom False)
49+ assertEqual "from BoolAtom" False (fromAtom (BoolAtom False))
50+
51+ let day = fromGregorian 2012 10 19
52+ assertEqual "to DayAtom" (DayAtom day) (toAtom day)
53+ assertEqual "from DayAtom" day (fromAtom (DayAtom day))
54+
4155--test marshaling of Generics-derived Atom to database ADT
4256testHaskell2DB :: Test
4357testHaskell2DB = TestCase $ do
@@ -91,7 +105,4 @@ testADT5 = TestCase $ do
91105 assertEqual "record-based ADT" example (fromAtom (toAtom example))
92106
93107checkExecuteDatabaseContextExpr :: SessionId -> Connection -> DatabaseContextExpr -> IO ()
94−checkExecuteDatabaseContextExpr sessionId dbconn expr = executeDatabaseContextExpr sessionId dbconn expr >>= maybe (pure ()) (\err -> assertFailure (show err))
95−
96−
97−
108+checkExecuteDatabaseContextExpr sessionId dbconn expr = executeDatabaseContextExpr sessionId dbconn expr >>= maybe (pure ()) (\err -> assertFailure (show err))