on master help sign in
new mess
commitc73af4407aac6725693680d8e4537b60b2c75bde authorDenis Redozubov <denis.redozubov@gmail.com> dateFri 5 May 2017, 23:06 (9 years ago) parent871411a
2 files changed+12−49
schematic.cabal+7 src/Data/Schematic/Schema.hs+5−49
schematic.cabal+7
@@ -18,10 +18,12 @@ cabal-version: >=1.10
1818library
1919 exposed-modules: Data.Schematic
2020 , Data.Schematic.Schema
21+ , Data.Schematic.Validation
2122 -- ghc-options:
2223 -- other-modules:
2324 default-extensions: ConstraintKinds
2425 , DataKinds
26+ , DefaultSignatures
2527 , DeriveFunctor
2628 , DeriveFoldable
2729 , DeriveDataTypeable
@@ -31,12 +33,15 @@ library
3133 , GADTs
3234 , KindSignatures
3335 , InstanceSigs
36+ , LambdaCase
3437 , MultiParamTypeClasses
3538 , OverloadedStrings
3639 , PolyKinds
3740 , QuasiQuotes
3841 , PartialTypeSignatures
42+ , RankNTypes
3943 , RecordWildCards
44+ , StandaloneDeriving
4045 , ScopedTypeVariables
4146 , TemplateHaskell
4247 , TypeApplications
@@ -44,8 +49,10 @@ library
4449 , TypeInType
4550 , TypeOperators
4651 , TypeSynonymInstances
52+ , UndecidableInstances
4753 build-depends: base >=4.9 && <4.10
4854 , regex-compat
55+ , singletons
4956 , text
5057 , vinyl
5158 hs-source-dirs: src
src/Data/Schematic/Schema.hs+5−49
@@ -8,28 +8,11 @@ module Data.Schematic.Schema where
88
99import Data.Kind hiding (Type)
1010import Data.Proxy
11+import Data.Schematic.Validation
1112import Data.Text (Text)
1213import GHC.TypeLits (KnownNat, natVal, KnownSymbol, symbolVal, Symbol, Nat)
1314
1415
15−-- | Type Tags
16−data JType
17− = JText
18− | JNumber
19− | JArray
20− | JObject
21− | JNull
22−
23−type family Repr (ty :: JType) :: * where
24− Repr 'JText = String -- because of matchRegex
25− Repr 'JNumber = Double
26−
27−type family CRepr (jty :: JType) :: * where
28− CRepr JText = TextConstraint
29− CRepr JNumber = NumberConstraint
30− CRepr JObject = (String, Schema)
31− CRepr JArray = ArrayConstraint
32−
3316class Constrained (ty :: JType) c where
3417 cRep :: Proxy ty -> Proxy c -> CRepr ty
3518
@@ -92,45 +75,17 @@ instance
9275 fieldName = symbolVal (Proxy @field)
9376 fieldSchema = constructor (Proxy @jty) $ schema (Proxy @jty) (Proxy @cs)
9477
95−data TextConstraint
96− = LengthEq Integer
97− | LengthLe Integer
98− | LengthGt Integer
99− deriving (Show, Eq)
100−
101−data NumberConstraint
102− = Le Integer
103− | Gt Integer
104− | Eq Integer
105− deriving (Show, Eq)
106−
107−data ArrayConstraint
108− = LengthArrEq Integer
109− deriving (Show, Eq)
110−
111−data Schema
112− = SchemaText [TextConstraint]
113− | SchemaNumber [NumberConstraint]
114− | SchemaObject [(String, Schema)]
115− | SchemaArray [ArrayConstraint] Schema
116− deriving (Show, Eq)
117−
118−-- | Apply constraint for each element of list
119−-- type family AllSatisfy (c :: k -> Constraint) (s :: [k]) :: Constraint where
120−-- AllSatisfy c '[] = ()
121−-- AllSatisfy c (a ': as) = (c a, AllSatisfy c as)
122−
12378class SchemaConstructor jty where
12479 constructor :: Proxy jty -> [CRepr jty] -> Schema
12580
12681instance SchemaConstructor 'JText where
127− constructor _ = SchemaText
82+ constructor _ = SchemaText SJText
12883
12984instance SchemaConstructor 'JNumber where
130− constructor _ = SchemaNumber
85+ constructor _ = SchemaNumber SJNumber
13186
13287instance SchemaConstructor 'JObject where
133− constructor _ = SchemaObject
88+ constructor _ = SchemaObject SJObject
13489
13590class Schematic ty where
13691 type JT (ty :: k) :: JType
@@ -174,6 +129,7 @@ instance
174129 type JT (SArray cs s) = 'JArray
175130 build _ =
176131 SchemaArray
132+ SJArray
177133 (schema (Proxy @'JArray) (Proxy @cs))
178134 (build (Proxy @s))
179135