Skip to content

Commit ec511a5

Browse files
committed
doh, add contract
1 parent bfcd93b commit ec511a5

1 file changed

Lines changed: 79 additions & 0 deletions

File tree

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
{-# LANGUAGE DeriveGeneric #-}
2+
{-# LANGUAGE TemplateHaskell #-}
3+
{-# LANGUAGE GADTs #-}
4+
5+
module Database.SqlServer.Definition.Contract
6+
(
7+
Contract
8+
) where
9+
10+
import Database.SqlServer.Definition.User (User)
11+
import Database.SqlServer.Definition.MessageType (MessageType)
12+
import Database.SqlServer.Definition.Identifier hiding (unwrap)
13+
import Database.SqlServer.Definition.Entity
14+
15+
import Test.QuickCheck
16+
import Data.DeriveTH
17+
import Text.PrettyPrint
18+
19+
data SentByConstraint = Initiator | Target | Any
20+
21+
isInitiatorOrAny :: SentByConstraint -> Bool
22+
isInitiatorOrAny Initiator = True
23+
isInitiatorOrAny Target = False
24+
isInitiatorOrAny Any = True
25+
26+
derive makeArbitrary ''SentByConstraint
27+
28+
data Constraint = Constraint MessageType SentByConstraint
29+
30+
messageType :: Constraint -> MessageType
31+
messageType (Constraint m _) = m
32+
33+
sentBy :: Constraint -> SentByConstraint
34+
sentBy (Constraint _ s) = s
35+
36+
derive makeArbitrary ''Constraint
37+
38+
data Contract = Contract
39+
{
40+
contractName :: RegularIdentifier
41+
, authorization :: Maybe User
42+
, messageTypes :: [Constraint]
43+
}
44+
45+
renderSentBy :: SentByConstraint -> Doc
46+
renderSentBy Initiator = text "SENT BY INITIATOR"
47+
renderSentBy Target = text "SENT BY TARGET"
48+
renderSentBy Any = text "SENT BY ANY"
49+
50+
renderMessageType :: Constraint -> Doc
51+
renderMessageType (Constraint mt sbc) = renderName mt <+> renderSentBy sbc
52+
53+
renderAuthorization :: User -> Doc
54+
renderAuthorization n = text "AUTHORIZATION" <+> renderName n
55+
56+
renderPrerequisites :: Contract -> Doc
57+
renderPrerequisites c = maybe empty toDoc (authorization c) $+$
58+
text "GO" $+$
59+
vcat (punctuate (text "\nGO\n") $ map (toDoc . messageType) (messageTypes c)) $+$
60+
text "GO\n"
61+
62+
-- The service must have at least one message SENT BY INITIATOR or ANY.
63+
instance Arbitrary Contract where
64+
arbitrary = do
65+
n <- arbitrary
66+
mts <- listOf1 arbitrary `suchThat` any (isInitiatorOrAny . sentBy)
67+
auth <- arbitrary
68+
return $ Contract n auth mts
69+
70+
instance Entity Contract where
71+
name = contractName
72+
toDoc m = renderPrerequisites m $+$
73+
text "CREATE CONTRACT" <+> renderName m $+$
74+
maybe empty renderAuthorization (authorization m) $+$
75+
parens (vcat $ punctuate comma (map renderMessageType (messageTypes m)))
76+
77+
instance Show Contract where
78+
show = show . toDoc
79+

0 commit comments

Comments
 (0)