@@ -1,37 +1,35 @@
1{-# LANGUAGE AllowAmbiguousTypes #-}
1{-# LANGUAGE AllowAmbiguousTypes #-}
3−{-# LANGUAGE OverloadedLists #-}
2+{-# LANGUAGE OverloadedLists #-}
4−{-# LANGUAGE OverloadedStrings #-}
3+{-# LANGUAGE OverloadedStrings #-}
5{-# OPTIONS_GHC -fprint-explicit-kinds #-}
4{-# OPTIONS_GHC -fprint-explicit-kinds #-}
7module Data.Schematic.Schema where
6module Data.Schematic.Schema where
9−import Control.Applicative ((<|>))
8+import Control.Applicative ()
10import Control.Monad
9import Control.Monad
11import Data.Aeson as J
10import Data.Aeson as J
12import Data.Aeson.Types as J
11import Data.Aeson.Types as J
13import Data.HashMap.Strict as H
12import Data.HashMap.Strict as H
14import Data.Kind
13import Data.Kind
15import Data.Maybe
14import Data.Maybe
16−import Data.Schematic.Generator
17import Data.Schematic.Instances ()
15import Data.Schematic.Instances ()
18import Data.Scientific
16import Data.Scientific
19−import Data.Singletons.Prelude.List hiding (All, Union)
17+import Data.Singletons.Prelude.List hiding (All)
20import Data.Singletons.TH
18import Data.Singletons.TH
21import Data.Singletons.TypeLits
19import Data.Singletons.TypeLits
22import Data.Text as T
20import Data.Text as T
24import Data.Vector as V
21import Data.Vector as V
25import Data.Vinyl hiding (Dict)
22import Data.Vinyl hiding (Dict)
26import qualified Data.Vinyl.TypeLevel as V
23import qualified Data.Vinyl.TypeLevel as V
28import GHC.Generics (Generic)
24import GHC.Generics (Generic)
30− (SomeNat(..), SomeSymbol(..), someNatVal, someSymbolVal)
31import Prelude as P
25import Prelude as P
32import Test.SmallCheck.Series
26import Test.SmallCheck.Series
29+type family All (c :: k -> Constraint) (s :: [k]) :: Constraint where
31+ All c (a ': as) = (c a, All c as)
35type family CRepr (s :: Schema) :: Type where
33type family CRepr (s :: Schema) :: Type where
36 CRepr ('SchemaText cs) = TextConstraint
34 CRepr ('SchemaText cs) = TextConstraint
37 CRepr ('SchemaNumber cs) = NumberConstraint
35 CRepr ('SchemaNumber cs) = NumberConstraint
@@ -40,235 +38,77 @@ type family CRepr (s :: Schema) :: Type where
41data TextConstraint
39data TextConstraint
47 | TRegex Symbol
43 | TRegex Symbol
48 | TEnum [Symbol]
44 | TEnum [Symbol]
49 deriving (Generic)
45 deriving (Generic)
51−instance SingKind TextConstraint where
52− type Demote TextConstraint = DemotedTextConstraint
54− STEq n -> withKnownNat n (DTEq . fromIntegral $ natVal n)
55− STLt n -> withKnownNat n (DTLt . fromIntegral $ natVal n)
56− STLe n -> withKnownNat n (DTLe . fromIntegral $ natVal n)
57− STGt n -> withKnownNat n (DTGt . fromIntegral $ natVal n)
58− STGe n -> withKnownNat n (DTGe . fromIntegral $ natVal n)
59− STRegex s -> withKnownSymbol s (DTRegex $ T.pack $ symbolVal s)
61− d :: Sing (s :: [Symbol]) -> [Text]
63− d (SCons ss@SSym fs) = T.pack (symbolVal ss) : d fs
66− DTEq n -> case someNatVal n of
67− Just (SomeNat (_ :: Proxy n)) -> SomeSing (STEq (SNat :: Sing n))
68− Nothing -> error "Negative singleton nat"
69− DTLt n -> case someNatVal n of
70− Just (SomeNat (_ :: Proxy n)) -> SomeSing (STLt (SNat :: Sing n))
71− Nothing -> error "Negative singleton nat"
72− DTLe n -> case someNatVal n of
73− Just (SomeNat (_ :: Proxy n)) -> SomeSing (STLe (SNat :: Sing n))
74− Nothing -> error "Negative singleton nat"
75− DTGt n -> case someNatVal n of
76− Just (SomeNat (_ :: Proxy n)) -> SomeSing (STGt (SNat :: Sing n))
77− Nothing -> error "Negative singleton nat"
78− DTGe n -> case someNatVal n of
79− Just (SomeNat (_ :: Proxy n)) -> SomeSing (STGe (SNat :: Sing n))
80− Nothing -> error "Negative singleton nat"
81− DTRegex s -> case someSymbolVal (T.unpack s) of
82− SomeSymbol (_ :: Proxy n) -> SomeSing (STRegex (SSym :: Sing n))
83− DTEnum ss -> case toSing ss of
84− SomeSing l -> SomeSing (STEnum l)
86−data DemotedTextConstraint
94− deriving (Generic, Eq, Show)
96data instance Sing (tc :: TextConstraint) where
47data instance Sing (tc :: TextConstraint) where
97 STEq :: Sing n -> Sing ('TEq n)
48 STEq :: Sing n -> Sing ('TEq n)
98− STLt :: Sing n -> Sing ('TLt n)
99 STLe :: Sing n -> Sing ('TLe n)
49 STLe :: Sing n -> Sing ('TLe n)
100 STGt :: Sing n -> Sing ('TGt n)
50 STGt :: Sing n -> Sing ('TGt n)
101− STGe :: Sing n -> Sing ('TGe n)
102 STRegex :: Sing s -> Sing ('TRegex s)
51 STRegex :: Sing s -> Sing ('TRegex s)
103− STEnum :: Sing ss -> Sing ('TEnum ss)
52+ STEnum :: All KnownSymbol ss => Sing ss -> Sing ('TEnum ss)
105instance (KnownNat n) => SingI ('TEq n) where sing = STEq sing
54instance (KnownNat n) => SingI ('TEq n) where sing = STEq sing
106instance (KnownNat n) => SingI ('TGt n) where sing = STGt sing
55instance (KnownNat n) => SingI ('TGt n) where sing = STGt sing
107−instance (KnownNat n) => SingI ('TGe n) where sing = STGe sing
108−instance (KnownNat n) => SingI ('TLt n) where sing = STLt sing
109instance (KnownNat n) => SingI ('TLe n) where sing = STLe sing
56instance (KnownNat n) => SingI ('TLe n) where sing = STLe sing
110instance (KnownSymbol s, SingI s) => SingI ('TRegex s) where sing = STRegex sing
57instance (KnownSymbol s, SingI s) => SingI ('TRegex s) where sing = STRegex sing
111−instance (SingI ss) => SingI ('TEnum ss) where sing = STEnum sing
58+instance (All KnownSymbol ss, SingI ss) => SingI ('TEnum ss) where sing = STEnum sing
113instance Eq (Sing ('TEq n)) where _ == _ = True
60instance Eq (Sing ('TEq n)) where _ == _ = True
114−instance Eq (Sing ('TLt n)) where _ == _ = True
115instance Eq (Sing ('TLe n)) where _ == _ = True
61instance Eq (Sing ('TLe n)) where _ == _ = True
116instance Eq (Sing ('TGt n)) where _ == _ = True
62instance Eq (Sing ('TGt n)) where _ == _ = True
117−instance Eq (Sing ('TGe n)) where _ == _ = True
118instance Eq (Sing ('TRegex t)) where _ == _ = True
63instance Eq (Sing ('TRegex t)) where _ == _ = True
119instance Eq (Sing ('TEnum ss)) where _ == _ = True
64instance Eq (Sing ('TEnum ss)) where _ == _ = True
121data NumberConstraint
66data NumberConstraint
122 = NLe Nat
67 = NLe Nat
124 | NGt Nat
68 | NGt Nat
126 | NEq Nat
69 | NEq Nat
127 deriving (Generic)
70 deriving (Generic)
129−data DemotedNumberConstraint
135− deriving (Generic, Eq, Show)
137data instance Sing (nc :: NumberConstraint) where
72data instance Sing (nc :: NumberConstraint) where
138 SNEq :: Sing n -> Sing ('NEq n)
73 SNEq :: Sing n -> Sing ('NEq n)
139 SNGt :: Sing n -> Sing ('NGt n)
74 SNGt :: Sing n -> Sing ('NGt n)
140− SNGe :: Sing n -> Sing ('NGe n)
141− SNLt :: Sing n -> Sing ('NLt n)
142 SNLe :: Sing n -> Sing ('NLe n)
75 SNLe :: Sing n -> Sing ('NLe n)
144instance KnownNat n => SingI ('NEq n) where sing = SNEq sing
77instance KnownNat n => SingI ('NEq n) where sing = SNEq sing
145instance KnownNat n => SingI ('NGt n) where sing = SNGt sing
78instance KnownNat n => SingI ('NGt n) where sing = SNGt sing
146−instance KnownNat n => SingI ('NGe n) where sing = SNGe sing
147−instance KnownNat n => SingI ('NLt n) where sing = SNLt sing
148instance KnownNat n => SingI ('NLe n) where sing = SNLe sing
79instance KnownNat n => SingI ('NLe n) where sing = SNLe sing
150instance Eq (Sing ('NEq n)) where _ == _ = True
81instance Eq (Sing ('NEq n)) where _ == _ = True
151−instance Eq (Sing ('NLt n)) where _ == _ = True
152instance Eq (Sing ('NLe n)) where _ == _ = True
82instance Eq (Sing ('NLe n)) where _ == _ = True
153instance Eq (Sing ('NGt n)) where _ == _ = True
83instance Eq (Sing ('NGt n)) where _ == _ = True
154−instance Eq (Sing ('NGe n)) where _ == _ = True
156−instance SingKind NumberConstraint where
157− type Demote NumberConstraint = DemotedNumberConstraint
159− SNEq n -> withKnownNat n (DNEq . fromIntegral $ natVal n)
160− SNGt n -> withKnownNat n (DNGt . fromIntegral $ natVal n)
161− SNGe n -> withKnownNat n (DNGe . fromIntegral $ natVal n)
162− SNLt n -> withKnownNat n (DNLt . fromIntegral $ natVal n)
163− SNLe n -> withKnownNat n (DNLe . fromIntegral $ natVal n)
165− DNEq n -> case someNatVal n of
166− Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNEq (SNat :: Sing n))
167− Nothing -> error "Negative singleton nat"
168− DNGt n -> case someNatVal n of
169− Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNGt (SNat :: Sing n))
170− Nothing -> error "Negative singleton nat"
171− DNGe n -> case someNatVal n of
172− Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNGe (SNat :: Sing n))
173− Nothing -> error "Negative singleton nat"
174− DNLt n -> case someNatVal n of
175− Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNLt (SNat :: Sing n))
176− Nothing -> error "Negative singleton nat"
177− DNLe n -> case someNatVal n of
178− Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNLe (SNat :: Sing n))
179− Nothing -> error "Negative singleton nat"
181data ArrayConstraint
85data ArrayConstraint
182 = AEq Nat
86 = AEq Nat
187 deriving (Generic)
87 deriving (Generic)
189−data DemotedArrayConstraint
195− deriving (Generic, Eq, Show)
197data instance Sing (ac :: ArrayConstraint) where
89data instance Sing (ac :: ArrayConstraint) where
198 SAEq :: Sing n -> Sing ('AEq n)
90 SAEq :: Sing n -> Sing ('AEq n)
199− SAGt :: Sing n -> Sing ('AGt n)
200− SAGe :: Sing n -> Sing ('AGe n)
201− SALt :: Sing n -> Sing ('ALt n)
202− SALe :: Sing n -> Sing ('ALe n)
204instance KnownNat n => SingI ('AEq n) where sing = SAEq sing
92instance KnownNat n => SingI ('AEq n) where sing = SAEq sing
205−instance KnownNat n => SingI ('AGt n) where sing = SAGt sing
206−instance KnownNat n => SingI ('AGe n) where sing = SAGe sing
207−instance KnownNat n => SingI ('ALt n) where sing = SALt sing
208−instance KnownNat n => SingI ('ALe n) where sing = SALe sing
210instance Eq (Sing ('AEq n)) where _ == _ = True
94instance Eq (Sing ('AEq n)) where _ == _ = True
211−instance Eq (Sing ('AGt n)) where _ == _ = True
212−instance Eq (Sing ('AGe n)) where _ == _ = True
213−instance Eq (Sing ('ALt n)) where _ == _ = True
214−instance Eq (Sing ('ALe n)) where _ == _ = True
216−instance SingKind ArrayConstraint where
217− type Demote ArrayConstraint = DemotedArrayConstraint
219− SAEq n -> withKnownNat n (DAEq . fromIntegral $ natVal n)
220− SAGt n -> withKnownNat n (DAGt . fromIntegral $ natVal n)
221− SAGe n -> withKnownNat n (DAGe . fromIntegral $ natVal n)
222− SALt n -> withKnownNat n (DALt . fromIntegral $ natVal n)
223− SALe n -> withKnownNat n (DALe . fromIntegral $ natVal n)
225− DAEq n -> case someNatVal n of
226− Just (SomeNat (_ :: Proxy n)) -> SomeSing (SAEq (SNat :: Sing n))
227− Nothing -> error "Negative singleton nat"
228− DAGt n -> case someNatVal n of
229− Just (SomeNat (_ :: Proxy n)) -> SomeSing (SAGt (SNat :: Sing n))
230− Nothing -> error "Negative singleton nat"
231− DAGe n -> case someNatVal n of
232− Just (SomeNat (_ :: Proxy n)) -> SomeSing (SAGe (SNat :: Sing n))
233− Nothing -> error "Negative singleton nat"
234− DALt n -> case someNatVal n of
235− Just (SomeNat (_ :: Proxy n)) -> SomeSing (SALt (SNat :: Sing n))
236− Nothing -> error "Negative singleton nat"
237− DALe n -> case someNatVal n of
238− Just (SomeNat (_ :: Proxy n)) -> SomeSing (SALe (SNat :: Sing n))
239− Nothing -> error "Negative singleton nat"
241data Schema
96data Schema
242 = SchemaText [TextConstraint]
97 = SchemaText [TextConstraint]
244 | SchemaNumber [NumberConstraint]
98 | SchemaNumber [NumberConstraint]
245 | SchemaObject [(Symbol, Schema)]
99 | SchemaObject [(Symbol, Schema)]
246 | SchemaArray [ArrayConstraint] Schema
100 | SchemaArray [ArrayConstraint] Schema
247 | SchemaNull
101 | SchemaNull
248 | SchemaOptional Schema
102 | SchemaOptional Schema
249− | SchemaUnion [Schema]
250 deriving (Generic)
103 deriving (Generic)
253− = DSchemaText [DemotedTextConstraint]
254− | DSchemaNumber [DemotedNumberConstraint]
256− | DSchemaObject [(Text, DemotedSchema)]
257− | DSchemaArray [DemotedArrayConstraint] DemotedSchema
259− | DSchemaOptional DemotedSchema
260− | DSchemaUnion [DemotedSchema]
261− deriving (Generic, Eq, Show)
263data instance Sing (schema :: Schema) where
105data instance Sing (schema :: Schema) where
264 SSchemaText :: Sing tcs -> Sing ('SchemaText tcs)
106 SSchemaText :: Sing tcs -> Sing ('SchemaText tcs)
265 SSchemaNumber :: Sing ncs -> Sing ('SchemaNumber ncs)
107 SSchemaNumber :: Sing ncs -> Sing ('SchemaNumber ncs)
266− SSchemaBoolean :: Sing 'SchemaBoolean
267 SSchemaArray :: Sing acs -> Sing schema -> Sing ('SchemaArray acs schema)
108 SSchemaArray :: Sing acs -> Sing schema -> Sing ('SchemaArray acs schema)
268 SSchemaObject :: Sing fields -> Sing ('SchemaObject fields)
109 SSchemaObject :: Sing fields -> Sing ('SchemaObject fields)
269 SSchemaOptional :: Sing s -> Sing ('SchemaOptional s)
110 SSchemaOptional :: Sing s -> Sing ('SchemaOptional s)
270 SSchemaNull :: Sing 'SchemaNull
111 SSchemaNull :: Sing 'SchemaNull
271− SSchemaUnion :: Sing ss -> Sing ('SchemaUnion ss)
273instance SingI sl => SingI ('SchemaText sl) where
113instance SingI sl => SingI ('SchemaText sl) where
274 sing = SSchemaText sing
114 sing = SSchemaText sing
@@ -276,52 +116,19 @@ instance SingI sl => SingI ('SchemaNumber sl) where
276 sing = SSchemaNumber sing
116 sing = SSchemaNumber sing
277instance SingI 'SchemaNull where
117instance SingI 'SchemaNull where
278 sing = SSchemaNull
118 sing = SSchemaNull
279−instance SingI 'SchemaBoolean where
280− sing = SSchemaBoolean
281instance (SingI ac, SingI s) => SingI ('SchemaArray ac s) where
119instance (SingI ac, SingI s) => SingI ('SchemaArray ac s) where
282 sing = SSchemaArray sing sing
120 sing = SSchemaArray sing sing
283instance SingI stl => SingI ('SchemaObject stl) where
121instance SingI stl => SingI ('SchemaObject stl) where
284 sing = SSchemaObject sing
122 sing = SSchemaObject sing
285instance SingI s => SingI ('SchemaOptional s) where
123instance SingI s => SingI ('SchemaOptional s) where
286 sing = SSchemaOptional sing
124 sing = SSchemaOptional sing
287−instance SingI s => SingI ('SchemaUnion s) where
288− sing = SSchemaUnion sing
290instance Eq (Sing ('SchemaText cs)) where _ == _ = True
126instance Eq (Sing ('SchemaText cs)) where _ == _ = True
291instance Eq (Sing ('SchemaNumber cs)) where _ == _ = True
127instance Eq (Sing ('SchemaNumber cs)) where _ == _ = True
292instance Eq (Sing 'SchemaNull) where _ == _ = True
128instance Eq (Sing 'SchemaNull) where _ == _ = True
293−instance Eq (Sing 'SchemaBoolean) where _ == _ = True
294instance Eq (Sing ('SchemaArray as s)) where _ == _ = True
129instance Eq (Sing ('SchemaArray as s)) where _ == _ = True
295instance Eq (Sing ('SchemaObject cs)) where _ == _ = True
130instance Eq (Sing ('SchemaObject cs)) where _ == _ = True
296instance Eq (Sing ('SchemaOptional s)) where _ == _ = True
131instance Eq (Sing ('SchemaOptional s)) where _ == _ = True
297−instance Eq (Sing ('SchemaUnion s)) where _ == _ = True
299−instance SingKind Schema where
300− type Demote Schema = DemotedSchema
302− SSchemaText cs -> DSchemaText $ fromSing cs
303− SSchemaNumber cs -> DSchemaNumber $ fromSing cs
304− SSchemaBoolean -> DSchemaBoolean
305− SSchemaArray cs s -> DSchemaArray (fromSing cs) (fromSing s)
306− SSchemaOptional s -> DSchemaOptional $ fromSing s
307− SSchemaNull -> DSchemaNull
308− SSchemaObject cs -> DSchemaObject $ fromSing cs
309− SSchemaUnion ss -> DSchemaUnion $ fromSing ss
311− DSchemaText cs -> case toSing cs of
312− SomeSing scs -> SomeSing $ SSchemaText scs
313− DSchemaNumber cs -> case toSing cs of
314− SomeSing scs -> SomeSing $ SSchemaNumber scs
315− DSchemaBoolean -> SomeSing $ SSchemaBoolean
316− DSchemaArray cs sch -> case (toSing cs, toSing sch) of
317− (SomeSing scs, SomeSing ssch) -> SomeSing $ SSchemaArray scs ssch
318− DSchemaOptional sch -> case toSing sch of
319− SomeSing ssch -> SomeSing $ SSchemaOptional ssch
320− DSchemaNull -> SomeSing SSchemaNull
321− DSchemaObject cs -> case toSing cs of
322− SomeSing scs -> SomeSing $ SSchemaObject scs
323− DSchemaUnion ss -> case toSing ss of
324− SomeSing sss -> SomeSing $ SSchemaUnion sss
326data FieldRepr :: (Symbol, Schema) -> Type where
133data FieldRepr :: (Symbol, Schema) -> Type where
327 FieldRepr
134 FieldRepr
@@ -359,77 +166,53 @@ instance
359 => Serial m (FieldRepr '(name, schema)) where
166 => Serial m (FieldRepr '(name, schema)) where
360 series = FieldRepr <$> series
167 series = FieldRepr <$> series
362−type family USubsets (u :: [k]) :: Constraint where
364− USubsets (h ': tl) = (USubset tl (h ': tl) (V.RImage tl (h ': tl)), USubsets tl)
366data JsonRepr :: Schema -> Type where
169data JsonRepr :: Schema -> Type where
367 ReprText :: Text -> JsonRepr ('SchemaText cs)
170 ReprText :: Text -> JsonRepr ('SchemaText cs)
368 ReprNumber :: Scientific -> JsonRepr ('SchemaNumber cs)
171 ReprNumber :: Scientific -> JsonRepr ('SchemaNumber cs)
369− ReprBoolean :: Bool -> JsonRepr 'SchemaBoolean
370 ReprNull :: JsonRepr 'SchemaNull
172 ReprNull :: JsonRepr 'SchemaNull
371 ReprArray :: V.Vector (JsonRepr s) -> JsonRepr ('SchemaArray cs s)
173 ReprArray :: V.Vector (JsonRepr s) -> JsonRepr ('SchemaArray cs s)
372 ReprObject :: Rec FieldRepr fs -> JsonRepr ('SchemaObject fs)
174 ReprObject :: Rec FieldRepr fs -> JsonRepr ('SchemaObject fs)
373 ReprOptional :: Maybe (JsonRepr s) -> JsonRepr ('SchemaOptional s)
175 ReprOptional :: Maybe (JsonRepr s) -> JsonRepr ('SchemaOptional s)
374− ReprUnion :: Union JsonRepr (h ': tl) -> JsonRepr ('SchemaUnion (h ': tl))
376−instance (Monad m, Serial m Text, SingI cs)
377− => Serial m (JsonRepr ('SchemaText cs)) where
378− series = decDepth $ fmap ReprText $ textSeries $ fromSing (sing :: Sing cs)
380−instance (Monad m, Serial m Scientific, SingI cs)
381− => Serial m (JsonRepr ('SchemaNumber cs)) where
382− series = decDepth $ fmap ReprNumber
383− $ numberSeries $ fromSing (sing :: Sing cs)
385−instance Monad m => Serial m (JsonRepr 'SchemaNull) where
386− series = cons0 ReprNull
388−instance (Serial m (JsonRepr s), Serial m (V.Vector (JsonRepr s)), SingI cs)
389− => Serial m (JsonRepr ('SchemaArray cs s)) where
390− series = decDepth $ fmap ReprArray
391− $ arraySeries $ fromSing (sing :: Sing cs)
393−instance (Serial m (JsonRepr s))
394− => Serial m (JsonRepr ('SchemaOptional s)) where
395− series = cons1 ReprOptional
397−instance (Monad m, Serial m (Rec FieldRepr fs))
398− => Serial m (JsonRepr ('SchemaObject fs)) where
399− series = cons1 ReprObject
401−-- | Move to the union package
402instance Show (JsonRepr ('SchemaText cs)) where
177instance Show (JsonRepr ('SchemaText cs)) where
403 show (ReprText t) = "ReprText " P.++ show t
178 show (ReprText t) = "ReprText " P.++ show t
405instance Show (JsonRepr ('SchemaNumber cs)) where
180instance Show (JsonRepr ('SchemaNumber cs)) where
406 show (ReprNumber n) = "ReprNumber " P.++ show n
181 show (ReprNumber n) = "ReprNumber " P.++ show n
408−instance Show (JsonRepr 'SchemaBoolean) where
409− show (ReprBoolean n) = "ReprBoolean " P.++ show n
411instance Show (JsonRepr 'SchemaNull) where show _ = "ReprNull"
183instance Show (JsonRepr 'SchemaNull) where show _ = "ReprNull"
413instance Show (JsonRepr s) => Show (JsonRepr ('SchemaArray acs s)) where
185instance Show (JsonRepr s) => Show (JsonRepr ('SchemaArray acs s)) where
414 show (ReprArray v) = "ReprArray " P.++ show v
186 show (ReprArray v) = "ReprArray " P.++ show v
416−#if MIN_VERSION_base(4,12,0)
418− ( V.RecAll FieldRepr fs Show, RMap fs, ReifyConstraint Show FieldRepr fs
420− => Show (JsonRepr ('SchemaObject fs)) where
421− show (ReprObject fs) = "ReprObject " P.++ show fs
423instance V.RecAll FieldRepr fs Show => Show (JsonRepr ('SchemaObject fs)) where
188instance V.RecAll FieldRepr fs Show => Show (JsonRepr ('SchemaObject fs)) where
424 show (ReprObject fs) = "ReprObject " P.++ show fs
189 show (ReprObject fs) = "ReprObject " P.++ show fs
427instance Show (JsonRepr s) => Show (JsonRepr ('SchemaOptional s)) where
191instance Show (JsonRepr s) => Show (JsonRepr ('SchemaOptional s)) where
428 show (ReprOptional s) = "ReprOptional " P.++ show s
192 show (ReprOptional s) = "ReprOptional " P.++ show s
430−instance Show (Union JsonRepr (h ': tl))
194+instance (Monad m, Serial m Text)
431− => Show (JsonRepr ('SchemaUnion (h ': tl))) where
195+ => Serial m (JsonRepr ('SchemaText cs)) where
432− show (ReprUnion s) = "ReprUnion " P.++ show s
196+ series = cons1 ReprText
198+instance (Monad m, Serial m Scientific)
199+ => Serial m (JsonRepr ('SchemaNumber cs)) where
200+ series = cons1 ReprNumber
202+instance Monad m => Serial m (JsonRepr 'SchemaNull) where
203+ series = cons0 ReprNull
205+instance (Serial m (V.Vector (JsonRepr s)))
206+ => Serial m (JsonRepr ('SchemaArray cs s)) where
207+ series = cons1 ReprArray
209+instance (Serial m (JsonRepr s))
210+ => Serial m (JsonRepr ('SchemaOptional s)) where
211+ series = cons1 ReprOptional
213+instance (Monad m, Serial m (Rec FieldRepr fs))
214+ => Serial m (JsonRepr ('SchemaObject fs)) where
215+ series = cons1 ReprObject
434instance Eq (Rec FieldRepr fs) => Eq (JsonRepr ('SchemaObject fs)) where
217instance Eq (Rec FieldRepr fs) => Eq (JsonRepr ('SchemaObject fs)) where
435 ReprObject a == ReprObject b = a == b
218 ReprObject a == ReprObject b = a == b
@@ -440,9 +223,6 @@ instance Eq (JsonRepr ('SchemaText cs)) where
440instance Eq (JsonRepr ('SchemaNumber cs)) where
223instance Eq (JsonRepr ('SchemaNumber cs)) where
441 ReprNumber a == ReprNumber b = a == b
224 ReprNumber a == ReprNumber b = a == b
443−instance Eq (JsonRepr 'SchemaBoolean) where
444− ReprBoolean a == ReprBoolean b = a == b
446instance Eq (JsonRepr 'SchemaNull) where
226instance Eq (JsonRepr 'SchemaNull) where
447 ReprNull == ReprNull = True
227 ReprNull == ReprNull = True
@@ -452,51 +232,6 @@ instance Eq (JsonRepr s) => Eq (JsonRepr ('SchemaArray as s)) where
452instance Eq (JsonRepr s) => Eq (JsonRepr ('SchemaOptional s)) where
232instance Eq (JsonRepr s) => Eq (JsonRepr ('SchemaOptional s)) where
453 ReprOptional a == ReprOptional b = a == b
233 ReprOptional a == ReprOptional b = a == b
455−instance Eq (Union JsonRepr (h ': tl))
456− => Eq (JsonRepr ('SchemaUnion (h ': tl))) where
457− ReprUnion a == ReprUnion b = a == b
459−instance Ord (Rec FieldRepr fs) => Ord (JsonRepr ('SchemaObject fs)) where
460− ReprObject a `compare` ReprObject b = a `compare` b
462−instance Ord (JsonRepr ('SchemaText cs)) where
463− ReprText a `compare` ReprText b = a `compare` b
465−instance Ord (JsonRepr ('SchemaNumber cs)) where
466− ReprNumber a `compare` ReprNumber b = a `compare` b
468−instance Ord (JsonRepr 'SchemaBoolean) where
469− ReprBoolean a `compare` ReprBoolean b = a `compare` b
471−instance Ord (JsonRepr 'SchemaNull) where
474−instance Ord (JsonRepr s) => Ord (JsonRepr ('SchemaArray as s)) where
475− ReprArray a `compare` ReprArray b = a `compare` b
477−instance Ord (JsonRepr s) => Ord (JsonRepr ('SchemaOptional s)) where
478− ReprOptional a `compare` ReprOptional b = a `compare` b
480−instance Ord (Union JsonRepr (h ': tl))
481− => Ord (JsonRepr ('SchemaUnion (h ': tl))) where
482− ReprUnion a `compare` ReprUnion b = a `compare` b
484−instance IsList (JsonRepr ('SchemaArray cs s)) where
485− type Item (JsonRepr ('SchemaArray cs s)) = JsonRepr s
486− fromList = ReprArray . GHC.Exts.fromList
487− toList (ReprArray v) = GHC.Exts.toList v
489−instance Num (JsonRepr ('SchemaNumber cs)) where
490− ReprNumber a + ReprNumber b = ReprNumber $ a + b
491− ReprNumber a - ReprNumber b = ReprNumber $ a - b
492− ReprNumber a * ReprNumber b = ReprNumber $ a * b
493− abs (ReprNumber a) = ReprNumber $ abs a
494− signum (ReprNumber a) = ReprNumber $ signum a
495− fromInteger = ReprNumber . fromIntegral
497−instance IsString (JsonRepr ('SchemaText cs)) where
498− fromString = ReprText . fromString
500fromOptional
235fromOptional
501 :: SingI s
236 :: SingI s
502 => Sing ('SchemaOptional s)
237 => Sing ('SchemaOptional s)
@@ -504,29 +239,10 @@ fromOptional
504 -> Parser (Maybe (JsonRepr s))
239 -> Parser (Maybe (JsonRepr s))
505fromOptional _ = parseJSON
240fromOptional _ = parseJSON
508− :: FromJSON (JsonRepr ('SchemaUnion ss))
509− => sing (ss :: [Schema])
511− -> Parser (JsonRepr ('SchemaUnion ss))
512−parseUnion _ val = parseJSON val
514−instance FromJSON (Union JsonRepr '[]) where
515− parseJSON = fail "empty union"
517−instance (SingI a, FromJSON (Union JsonRepr as)) => FromJSON (Union JsonRepr (a ': as)) where
518− parseJSON val = (This <$> parseJSON val)
519− <|> (That <$> (parseJSON val :: Parser (Union JsonRepr as)))
521−instance ToJSON (Union JsonRepr as) where
522− toJSON (This fa) = toJSON fa
523− toJSON (That u) = toJSON u
525instance SingI schema => J.FromJSON (JsonRepr schema) where
242instance SingI schema => J.FromJSON (JsonRepr schema) where
526 parseJSON value = case sing :: Sing schema of
243 parseJSON value = case sing :: Sing schema of
527 SSchemaText _ -> withText "String" (pure . ReprText) value
244 SSchemaText _ -> withText "String" (pure . ReprText) value
528 SSchemaNumber _ -> withScientific "Number" (pure . ReprNumber) value
245 SSchemaNumber _ -> withScientific "Number" (pure . ReprNumber) value
529− SSchemaBoolean -> ReprBoolean <$> parseJSON value
530 SSchemaNull -> case value of
246 SSchemaNull -> case value of
531 J.Null -> pure ReprNull
247 J.Null -> pure ReprNull
532 _ -> typeMismatch "Null" value
248 _ -> typeMismatch "Null" value
@@ -542,37 +258,31 @@ instance SingI schema => J.FromJSON (JsonRepr schema) where
542 fieldRepr <- case s of
258 fieldRepr <- case s of
543 SSchemaText so -> case H.lookup fieldName h of
259 SSchemaText so -> case H.lookup fieldName h of
544 Just v -> withSingI so $ FieldRepr <$> parseJSON v
260 Just v -> withSingI so $ FieldRepr <$> parseJSON v
545− Nothing -> fail $ "No text field: " P.++ show fieldName
261+ Nothing -> fail "schematext"
546 SSchemaNumber so -> case H.lookup fieldName h of
262 SSchemaNumber so -> case H.lookup fieldName h of
547 Just v -> withSingI so $ FieldRepr <$> parseJSON v
263 Just v -> withSingI so $ FieldRepr <$> parseJSON v
548− Nothing -> fail $ "No number field: " P.++ show fieldName
264+ Nothing -> fail "schemanumber"
549− SSchemaBoolean -> case H.lookup fieldName h of
550− Just v -> FieldRepr <$> parseJSON v
551− Nothing -> fail $ "No boolean field: " P.++ show fieldName
552 SSchemaNull -> case H.lookup fieldName h of
265 SSchemaNull -> case H.lookup fieldName h of
553 Just v -> FieldRepr <$> parseJSON v
266 Just v -> FieldRepr <$> parseJSON v
554− Nothing -> fail $ "No null field: " P.++ show fieldName
267+ Nothing -> fail "schemanull"
555 SSchemaArray sa sb -> case H.lookup fieldName h of
268 SSchemaArray sa sb -> case H.lookup fieldName h of
556 Just v -> withSingI sa $ withSingI sb $ FieldRepr <$> parseJSON v
269 Just v -> withSingI sa $ withSingI sb $ FieldRepr <$> parseJSON v
557− Nothing -> fail $ "No array field: " P.++ show fieldName
270+ Nothing -> fail "schemaarray"
558 SSchemaObject so -> case H.lookup fieldName h of
271 SSchemaObject so -> case H.lookup fieldName h of
559 Just v -> withSingI so $ FieldRepr <$> parseJSON v
272 Just v -> withSingI so $ FieldRepr <$> parseJSON v
560− Nothing -> fail $ "No object field" P.++ show fieldName
273+ Nothing -> fail "schemaobject"
561 SSchemaOptional so -> case H.lookup fieldName h of
274 SSchemaOptional so -> case H.lookup fieldName h of
562 Just v -> withSingI so $ FieldRepr <$> parseJSON v
275 Just v -> withSingI so $ FieldRepr <$> parseJSON v
563− Nothing -> withSingI so $ pure $ FieldRepr $ ReprOptional Nothing
276+ Nothing -> fail "schemaoptional"
564− SSchemaUnion ss -> withSingI ss $ FieldRepr <$> parseUnion ss value
565 (fieldRepr :&) <$> demoteFields tl h
277 (fieldRepr :&) <$> demoteFields tl h
566 ReprObject <$> withObject "Object" (demoteFields fs) value
278 ReprObject <$> withObject "Object" (demoteFields fs) value
567− SSchemaUnion ss -> parseUnion ss value
569instance J.ToJSON (JsonRepr a) where
280instance J.ToJSON (JsonRepr a) where
570 toJSON ReprNull = J.Null
281 toJSON ReprNull = J.Null
571− toJSON (ReprBoolean b) = J.Bool b
572 toJSON (ReprText t) = J.String t
282 toJSON (ReprText t) = J.String t
573 toJSON (ReprNumber n) = J.Number n
283 toJSON (ReprNumber n) = J.Number n
574 toJSON (ReprOptional s) = case s of
284 toJSON (ReprOptional s) = case s of
575− Just v -> toJSON v
285+ Just v -> toJSON v
576 Nothing -> J.Null
286 Nothing -> J.Null
577 toJSON (ReprArray v) = J.Array $ toJSON <$> v
287 toJSON (ReprArray v) = J.Array $ toJSON <$> v
578 toJSON (ReprObject r) = J.Object . H.fromList . fold $ r
288 toJSON (ReprObject r) = J.Object . H.fromList . fold $ r
@@ -585,7 +295,6 @@ instance J.ToJSON (JsonRepr a) where
585 fold = \case
295 fold = \case
586 RNil -> []
296 RNil -> []
587 fr@(FieldRepr _) :& tl -> (extract fr) : fold tl
297 fr@(FieldRepr _) :& tl -> (extract fr) : fold tl
588− toJSON (ReprUnion u) = toJSON u
590class FalseConstraint a
299class FalseConstraint a