-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathSchema.hs
174 lines (139 loc) · 4.44 KB
/
Schema.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}
module RON.Schema
( Alias (..),
CaseTransform (..),
Declaration (..),
Field (..),
XField,
FieldAnnotations (..),
defaultFieldAnnotations,
MergeStrategy (..),
Opaque (..),
opaqueAtoms,
opaqueAtoms_,
opaqueObject,
OpaqueAnnotations (..),
defaultOpaqueAnnotations,
RonType (..),
Schema,
Stage (..),
Struct (..),
StructAnnotations (..),
defaultStructAnnotations,
StructEncoding (..),
StructLww,
StructSet,
TAtom (..),
TEnum (..),
TObject (..),
TypeExpr (..),
TypeName,
UseType,
)
where
import qualified Data.Text as Text
import RON.Prelude
data Stage = Parsed | Resolved | Equipped
type TypeName = Text
data TypeExpr = Use TypeName | Apply TypeName [TypeExpr]
deriving (Show)
data TAtom = TAFloat | TAInteger | TAString | TAUuid | TObjectRef RonType
deriving (Show)
data RonType
= TAtom TAtom
| TEnum TEnum
| TObject TObject
| TOpaqueAtoms Opaque
deriving (Show)
data TEnum = Enum {name :: Text, items :: [Text]}
deriving (Show)
data TObject
= TOpaqueObject Opaque
| TORSet RonType
| TORSetMap RonType RonType
| TRga RonType
| TStructLww (StructLww Resolved)
| TStructSet (StructSet Resolved)
| TVersionVector
deriving (Show)
data StructEncoding = SELww | SESet
data Struct (encoding :: StructEncoding) stage = Struct
{ name :: Text
, fields :: Map Text (Field stage)
, annotations :: StructAnnotations
}
deriving instance Show (Field stage) => Show (Struct encoding stage)
type StructLww = Struct SELww
type StructSet = Struct SESet
data StructAnnotations
= StructAnnotations
{ haskellFieldPrefix :: Text,
haskellFieldCaseTransform :: Maybe CaseTransform
}
deriving (Show)
defaultStructAnnotations :: StructAnnotations
defaultStructAnnotations = StructAnnotations
{ haskellFieldPrefix = Text.empty,
haskellFieldCaseTransform = Nothing
}
data CaseTransform = TitleCase
deriving (Show)
data Field (stage :: Stage) = Field
{ mergeStrategy :: MergeStrategy
-- ^ Used only for 'StructSet'; for 'StructLww' must be 'LWW'
, ronType :: UseType stage
, annotations :: FieldAnnotations
, ext :: XField stage
}
deriving instance
(Show (UseType stage), Show (XField stage)) => Show (Field stage)
data FieldAnnotations = FieldAnnotations deriving (Show)
defaultFieldAnnotations :: FieldAnnotations
defaultFieldAnnotations = FieldAnnotations
type family XField (stage :: Stage)
type instance XField Parsed = ()
type instance XField Resolved = ()
type family UseType (stage :: Stage) where
UseType Parsed = TypeExpr
UseType Resolved = RonType
UseType Equipped = RonType
data Declaration stage
= DAlias (Alias stage)
| DEnum TEnum
| DOpaqueAtoms Opaque
| DOpaqueObject Opaque
| DStructLww (StructLww stage)
| DStructSet (StructSet stage)
deriving instance
(Show (Alias stage), Show (StructLww stage), Show (StructSet stage)) =>
Show (Declaration stage)
type family Schema (stage :: Stage) where
Schema Parsed = [Declaration 'Parsed]
Schema Resolved = Map TypeName (Declaration 'Resolved)
newtype OpaqueAnnotations = OpaqueAnnotations {haskellType :: Maybe Text}
deriving (Show)
defaultOpaqueAnnotations :: OpaqueAnnotations
defaultOpaqueAnnotations = OpaqueAnnotations {haskellType = Nothing}
data Opaque = Opaque {name :: Text, annotations :: OpaqueAnnotations}
deriving (Show)
opaqueObject :: Text -> OpaqueAnnotations -> RonType
opaqueObject tyname = TObject . TOpaqueObject . Opaque tyname
opaqueAtoms :: Text -> OpaqueAnnotations -> RonType
opaqueAtoms tyname = TOpaqueAtoms . Opaque tyname
opaqueAtoms_ :: Text -> RonType
opaqueAtoms_ tyname = TOpaqueAtoms $ Opaque tyname defaultOpaqueAnnotations
data Alias stage = Alias {name :: Text, target :: UseType stage}
deriving instance Show (UseType stage) => Show (Alias stage)
data MergeStrategy
= LWW -- ^ Keep the last value, use 'Nothing' if no values
| Max -- ^ Keep the maximum value, use 'Nothing' if no values
| Min -- ^ Keep the minimum value, use 'Nothing' if no values
| Set -- ^ Keep all values, use 'mempty' if no values
| Monoid -- ^ Merge all values, use 'mempty' if no values
-- TODO Semigroup -- ^ Merge all values, use 'Nothing' if no values
deriving (Eq, Show)