@@ -1,28 +1,33 @@
1{-# LANGUAGE AllowAmbiguousTypes #-}
1{-# LANGUAGE AllowAmbiguousTypes #-}
2−{-# LANGUAGE OverloadedLists #-}
3+{-# LANGUAGE OverloadedLists #-}
3−{-# LANGUAGE OverloadedStrings #-}
4+{-# LANGUAGE OverloadedStrings #-}
4{-# OPTIONS_GHC -fprint-explicit-kinds #-}
5{-# OPTIONS_GHC -fprint-explicit-kinds #-}
6module Data.Schematic.Schema where
7module Data.Schematic.Schema where
8−import Control.Applicative ()
9+import Control.Applicative ((<|>))
9import Control.Monad
10import Control.Monad
10import Data.Aeson as J
11import Data.Aeson as J
11import Data.Aeson.Types as J
12import Data.Aeson.Types as J
12import Data.HashMap.Strict as H
13import Data.HashMap.Strict as H
13import Data.Kind
14import Data.Kind
14import Data.Maybe
15import Data.Maybe
16+import Data.Schematic.Generator
15import Data.Schematic.Instances ()
17import Data.Schematic.Instances ()
16import Data.Scientific
18import Data.Scientific
17−import Data.Singletons.Prelude.List hiding (All)
19+import Data.Singletons.Prelude.List hiding (All, Union)
18import Data.Singletons.TH
20import Data.Singletons.TH
19import Data.Singletons.TypeLits
21import Data.Singletons.TypeLits
20import Data.Text as T
22import Data.Text as T
21import Data.Vector as V
24import Data.Vector as V
22import Data.Vinyl hiding (Dict)
25import Data.Vinyl hiding (Dict)
23import qualified Data.Vinyl.TypeLevel as V
26import qualified Data.Vinyl.TypeLevel as V
24import GHC.Generics (Generic)
28import GHC.Generics (Generic)
25−import GHC.TypeLits (SomeNat(..), SomeSymbol(..), someSymbolVal, someNatVal)
30+ (SomeNat(..), SomeSymbol(..), someNatVal, someSymbolVal)
26import Prelude as P
31import Prelude as P
27import Test.SmallCheck.Series
32import Test.SmallCheck.Series
@@ -44,38 +49,38 @@ data TextConstraint
44 deriving (Generic)
49 deriving (Generic)
46instance SingKind TextConstraint where
51instance SingKind TextConstraint where
47− type DemoteRep TextConstraint = DemotedTextConstraint
52+ type Demote TextConstraint = DemotedTextConstraint
48 fromSing = \case
53 fromSing = \case
49− STEq n -> withKnownNat n (DTEq $ natVal n)
54+ STEq n -> withKnownNat n (DTEq . fromIntegral $ natVal n)
50− STLt n -> withKnownNat n (DTLt $ natVal n)
55+ STLt n -> withKnownNat n (DTLt . fromIntegral $ natVal n)
51− STLe n -> withKnownNat n (DTLe $ natVal n)
56+ STLe n -> withKnownNat n (DTLe . fromIntegral $ natVal n)
52− STGt n -> withKnownNat n (DTGt $ natVal n)
57+ STGt n -> withKnownNat n (DTGt . fromIntegral $ natVal n)
53− STGe n -> withKnownNat n (DTGe $ natVal n)
58+ STGe n -> withKnownNat n (DTGe . fromIntegral $ natVal n)
54 STRegex s -> withKnownSymbol s (DTRegex $ T.pack $ symbolVal s)
59 STRegex s -> withKnownSymbol s (DTRegex $ T.pack $ symbolVal s)
55 STEnum s -> let
60 STEnum s -> let
56 d :: Sing (s :: [Symbol]) -> [Text]
61 d :: Sing (s :: [Symbol]) -> [Text]
57− d SNil = []
62+ d SNil = []
58 d (SCons ss@SSym fs) = T.pack (symbolVal ss) : d fs
63 d (SCons ss@SSym fs) = T.pack (symbolVal ss) : d fs
59 in DTEnum $ d s
64 in DTEnum $ d s
60 toSing = \case
65 toSing = \case
61 DTEq n -> case someNatVal n of
66 DTEq n -> case someNatVal n of
62 Just (SomeNat (_ :: Proxy n)) -> SomeSing (STEq (SNat :: Sing n))
67 Just (SomeNat (_ :: Proxy n)) -> SomeSing (STEq (SNat :: Sing n))
63− Nothing -> error "Negative singleton nat"
68+ Nothing -> error "Negative singleton nat"
64 DTLt n -> case someNatVal n of
69 DTLt n -> case someNatVal n of
65 Just (SomeNat (_ :: Proxy n)) -> SomeSing (STLt (SNat :: Sing n))
70 Just (SomeNat (_ :: Proxy n)) -> SomeSing (STLt (SNat :: Sing n))
66− Nothing -> error "Negative singleton nat"
71+ Nothing -> error "Negative singleton nat"
67 DTLe n -> case someNatVal n of
72 DTLe n -> case someNatVal n of
68 Just (SomeNat (_ :: Proxy n)) -> SomeSing (STLe (SNat :: Sing n))
73 Just (SomeNat (_ :: Proxy n)) -> SomeSing (STLe (SNat :: Sing n))
69− Nothing -> error "Negative singleton nat"
74+ Nothing -> error "Negative singleton nat"
70 DTGt n -> case someNatVal n of
75 DTGt n -> case someNatVal n of
71 Just (SomeNat (_ :: Proxy n)) -> SomeSing (STGt (SNat :: Sing n))
76 Just (SomeNat (_ :: Proxy n)) -> SomeSing (STGt (SNat :: Sing n))
72− Nothing -> error "Negative singleton nat"
77+ Nothing -> error "Negative singleton nat"
73 DTGe n -> case someNatVal n of
78 DTGe n -> case someNatVal n of
74 Just (SomeNat (_ :: Proxy n)) -> SomeSing (STGe (SNat :: Sing n))
79 Just (SomeNat (_ :: Proxy n)) -> SomeSing (STGe (SNat :: Sing n))
75− Nothing -> error "Negative singleton nat"
80+ Nothing -> error "Negative singleton nat"
76 DTRegex s -> case someSymbolVal (T.unpack s) of
81 DTRegex s -> case someSymbolVal (T.unpack s) of
77 SomeSymbol (_ :: Proxy n) -> SomeSing (STRegex (SSym :: Sing n))
82 SomeSymbol (_ :: Proxy n) -> SomeSing (STRegex (SSym :: Sing n))
78− DTEnum ss -> case toSing (T.unpack <$> ss) of
83+ DTEnum ss -> case toSing ss of
79 SomeSing l -> SomeSing (STEnum l)
84 SomeSing l -> SomeSing (STEnum l)
81data DemotedTextConstraint
86data DemotedTextConstraint
@@ -86,7 +91,7 @@ data DemotedTextConstraint
86 | DTGe Integer
91 | DTGe Integer
87 | DTRegex Text
92 | DTRegex Text
88 | DTEnum [Text]
93 | DTEnum [Text]
89− deriving (Generic)
94+ deriving (Generic, Eq, Show)
91data instance Sing (tc :: TextConstraint) where
96data instance Sing (tc :: TextConstraint) where
92 STEq :: Sing n -> Sing ('TEq n)
97 STEq :: Sing n -> Sing ('TEq n)
@@ -127,7 +132,7 @@ data DemotedNumberConstraint
127 | DNGt Integer
132 | DNGt Integer
128 | DNGe Integer
133 | DNGe Integer
129 | DNEq Integer
134 | DNEq Integer
130− deriving (Generic)
135+ deriving (Generic, Eq, Show)
132data instance Sing (nc :: NumberConstraint) where
137data instance Sing (nc :: NumberConstraint) where
133 SNEq :: Sing n -> Sing ('NEq n)
138 SNEq :: Sing n -> Sing ('NEq n)
@@ -149,53 +154,89 @@ instance Eq (Sing ('NGt n)) where _ == _ = True
149instance Eq (Sing ('NGe n)) where _ == _ = True
154instance Eq (Sing ('NGe n)) where _ == _ = True
151instance SingKind NumberConstraint where
156instance SingKind NumberConstraint where
152− type DemoteRep NumberConstraint = DemotedNumberConstraint
157+ type Demote NumberConstraint = DemotedNumberConstraint
153 fromSing = \case
158 fromSing = \case
154− SNEq n -> withKnownNat n (DNEq $ natVal n)
159+ SNEq n -> withKnownNat n (DNEq . fromIntegral $ natVal n)
155− SNGt n -> withKnownNat n (DNGt $ natVal n)
160+ SNGt n -> withKnownNat n (DNGt . fromIntegral $ natVal n)
156− SNGe n -> withKnownNat n (DNGe $ natVal n)
161+ SNGe n -> withKnownNat n (DNGe . fromIntegral $ natVal n)
157− SNLt n -> withKnownNat n (DNLt $ natVal n)
162+ SNLt n -> withKnownNat n (DNLt . fromIntegral $ natVal n)
158− SNLe n -> withKnownNat n (DNLe $ natVal n)
163+ SNLe n -> withKnownNat n (DNLe . fromIntegral $ natVal n)
159 toSing = \case
164 toSing = \case
160 DNEq n -> case someNatVal n of
165 DNEq n -> case someNatVal n of
161 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNEq (SNat :: Sing n))
166 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNEq (SNat :: Sing n))
162− Nothing -> error "Negative singleton nat"
167+ Nothing -> error "Negative singleton nat"
163 DNGt n -> case someNatVal n of
168 DNGt n -> case someNatVal n of
164 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNGt (SNat :: Sing n))
169 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNGt (SNat :: Sing n))
165− Nothing -> error "Negative singleton nat"
170+ Nothing -> error "Negative singleton nat"
166 DNGe n -> case someNatVal n of
171 DNGe n -> case someNatVal n of
167 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNGe (SNat :: Sing n))
172 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNGe (SNat :: Sing n))
168− Nothing -> error "Negative singleton nat"
173+ Nothing -> error "Negative singleton nat"
169 DNLt n -> case someNatVal n of
174 DNLt n -> case someNatVal n of
170 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNLt (SNat :: Sing n))
175 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNLt (SNat :: Sing n))
171− Nothing -> error "Negative singleton nat"
176+ Nothing -> error "Negative singleton nat"
172 DNLe n -> case someNatVal n of
177 DNLe n -> case someNatVal n of
173 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNLe (SNat :: Sing n))
178 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNLe (SNat :: Sing n))
174− Nothing -> error "Negative singleton nat"
179+ Nothing -> error "Negative singleton nat"
176data ArrayConstraint
181data ArrayConstraint
177 = AEq Nat
182 = AEq Nat
178 deriving (Generic)
187 deriving (Generic)
180data DemotedArrayConstraint
189data DemotedArrayConstraint
181 = DAEq Integer
190 = DAEq Integer
182− deriving (Generic)
195+ deriving (Generic, Eq, Show)
184data instance Sing (ac :: ArrayConstraint) where
197data instance Sing (ac :: ArrayConstraint) where
185 SAEq :: Sing n -> Sing ('AEq n)
198 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)
187instance KnownNat n => SingI ('AEq n) where sing = SAEq sing
204instance 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
189instance Eq (Sing ('AEq n)) where _ == _ = True
210instance 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
191instance SingKind ArrayConstraint where
216instance SingKind ArrayConstraint where
192− type DemoteRep ArrayConstraint = DemotedArrayConstraint
217+ type Demote ArrayConstraint = DemotedArrayConstraint
193 fromSing = \case
218 fromSing = \case
194− SAEq n -> withKnownNat n (DAEq $ natVal n)
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)
195 toSing = \case
224 toSing = \case
196 DAEq n -> case someNatVal n of
225 DAEq n -> case someNatVal n of
197 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SAEq (SNat :: Sing n))
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))
198− Nothing -> error "Negative singleton nat"
239+ Nothing -> error "Negative singleton nat"
200data Schema
241data Schema
201 = SchemaText [TextConstraint]
242 = SchemaText [TextConstraint]
@@ -205,6 +246,7 @@ data Schema
205 | SchemaArray [ArrayConstraint] Schema
246 | SchemaArray [ArrayConstraint] Schema
206 | SchemaNull
247 | SchemaNull
207 | SchemaOptional Schema
248 | SchemaOptional Schema
249+ | SchemaUnion [Schema]
208 deriving (Generic)
250 deriving (Generic)
210data DemotedSchema
252data DemotedSchema
@@ -215,7 +257,8 @@ data DemotedSchema
215 | DSchemaArray [DemotedArrayConstraint] DemotedSchema
257 | DSchemaArray [DemotedArrayConstraint] DemotedSchema
216 | DSchemaNull
258 | DSchemaNull
217 | DSchemaOptional DemotedSchema
259 | DSchemaOptional DemotedSchema
260+ | DSchemaUnion [DemotedSchema]
218− deriving (Generic)
261+ deriving (Generic, Eq, Show)
220data instance Sing (schema :: Schema) where
263data instance Sing (schema :: Schema) where
221 SSchemaText :: Sing tcs -> Sing ('SchemaText tcs)
264 SSchemaText :: Sing tcs -> Sing ('SchemaText tcs)
@@ -225,6 +268,7 @@ data instance Sing (schema :: Schema) where
225 SSchemaObject :: Sing fields -> Sing ('SchemaObject fields)
268 SSchemaObject :: Sing fields -> Sing ('SchemaObject fields)
226 SSchemaOptional :: Sing s -> Sing ('SchemaOptional s)
269 SSchemaOptional :: Sing s -> Sing ('SchemaOptional s)
227 SSchemaNull :: Sing 'SchemaNull
270 SSchemaNull :: Sing 'SchemaNull
271+ SSchemaUnion :: Sing ss -> Sing ('SchemaUnion ss)
229instance SingI sl => SingI ('SchemaText sl) where
273instance SingI sl => SingI ('SchemaText sl) where
230 sing = SSchemaText sing
274 sing = SSchemaText sing
@@ -240,6 +284,8 @@ instance SingI stl => SingI ('SchemaObject stl) where
240 sing = SSchemaObject sing
284 sing = SSchemaObject sing
241instance SingI s => SingI ('SchemaOptional s) where
285instance SingI s => SingI ('SchemaOptional s) where
242 sing = SSchemaOptional sing
286 sing = SSchemaOptional sing
287+instance SingI s => SingI ('SchemaUnion s) where
288+ sing = SSchemaUnion sing
244instance Eq (Sing ('SchemaText cs)) where _ == _ = True
290instance Eq (Sing ('SchemaText cs)) where _ == _ = True
245instance Eq (Sing ('SchemaNumber cs)) where _ == _ = True
291instance Eq (Sing ('SchemaNumber cs)) where _ == _ = True
@@ -248,9 +294,10 @@ instance Eq (Sing 'SchemaBoolean) where _ == _ = True
248instance Eq (Sing ('SchemaArray as s)) where _ == _ = True
294instance Eq (Sing ('SchemaArray as s)) where _ == _ = True
249instance Eq (Sing ('SchemaObject cs)) where _ == _ = True
295instance Eq (Sing ('SchemaObject cs)) where _ == _ = True
250instance Eq (Sing ('SchemaOptional s)) where _ == _ = True
296instance Eq (Sing ('SchemaOptional s)) where _ == _ = True
297+instance Eq (Sing ('SchemaUnion s)) where _ == _ = True
252instance SingKind Schema where
299instance SingKind Schema where
253− type DemoteRep Schema = DemotedSchema
300+ type Demote Schema = DemotedSchema
254 fromSing = \case
301 fromSing = \case
255 SSchemaText cs -> DSchemaText $ fromSing cs
302 SSchemaText cs -> DSchemaText $ fromSing cs
256 SSchemaNumber cs -> DSchemaNumber $ fromSing cs
303 SSchemaNumber cs -> DSchemaNumber $ fromSing cs
@@ -258,12 +305,8 @@ instance SingKind Schema where
258 SSchemaArray cs s -> DSchemaArray (fromSing cs) (fromSing s)
305 SSchemaArray cs s -> DSchemaArray (fromSing cs) (fromSing s)
259 SSchemaOptional s -> DSchemaOptional $ fromSing s
306 SSchemaOptional s -> DSchemaOptional $ fromSing s
260 SSchemaNull -> DSchemaNull
307 SSchemaNull -> DSchemaNull
261− SSchemaObject cs -> let
308+ SSchemaObject cs -> DSchemaObject $ fromSing cs
262− dem :: Sing (s :: [(Symbol, Schema)]) -> [(Text, DemotedSchema)]
309+ SSchemaUnion ss -> DSchemaUnion $ fromSing ss
264− dem (SCons (STuple2 ss ssch) fs) = withKnownSymbol ss
265− $ (T.pack (symbolVal ss), fromSing ssch) : dem fs
266− in DSchemaObject $ dem cs
267 toSing = \case
310 toSing = \case
268 DSchemaText cs -> case toSing cs of
311 DSchemaText cs -> case toSing cs of
269 SomeSing scs -> SomeSing $ SSchemaText scs
312 SomeSing scs -> SomeSing $ SSchemaText scs
@@ -275,8 +318,10 @@ instance SingKind Schema where
275 DSchemaOptional sch -> case toSing sch of
318 DSchemaOptional sch -> case toSing sch of
276 SomeSing ssch -> SomeSing $ SSchemaOptional ssch
319 SomeSing ssch -> SomeSing $ SSchemaOptional ssch
277 DSchemaNull -> SomeSing SSchemaNull
320 DSchemaNull -> SomeSing SSchemaNull
278− DSchemaObject cs -> case toSing ((\(sym,sch) -> (T.unpack sym, sch)) <$> cs) of
321+ DSchemaObject cs -> case toSing cs of
279 SomeSing scs -> SomeSing $ SSchemaObject scs
322 SomeSing scs -> SomeSing $ SSchemaObject scs
323+ DSchemaUnion ss -> case toSing ss of
324+ SomeSing sss -> SomeSing $ SSchemaUnion sss
281data FieldRepr :: (Symbol, Schema) -> Type where
326data FieldRepr :: (Symbol, Schema) -> Type where
282 FieldRepr
327 FieldRepr
@@ -314,6 +359,10 @@ instance
314 => Serial m (FieldRepr '(name, schema)) where
359 => Serial m (FieldRepr '(name, schema)) where
315 series = FieldRepr <$> series
360 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)
317data JsonRepr :: Schema -> Type where
366data JsonRepr :: Schema -> Type where
318 ReprText :: Text -> JsonRepr ('SchemaText cs)
367 ReprText :: Text -> JsonRepr ('SchemaText cs)
319 ReprNumber :: Scientific -> JsonRepr ('SchemaNumber cs)
368 ReprNumber :: Scientific -> JsonRepr ('SchemaNumber cs)
@@ -322,46 +371,65 @@ data JsonRepr :: Schema -> Type where
322 ReprArray :: V.Vector (JsonRepr s) -> JsonRepr ('SchemaArray cs s)
371 ReprArray :: V.Vector (JsonRepr s) -> JsonRepr ('SchemaArray cs s)
323 ReprObject :: Rec FieldRepr fs -> JsonRepr ('SchemaObject fs)
372 ReprObject :: Rec FieldRepr fs -> JsonRepr ('SchemaObject fs)
324 ReprOptional :: Maybe (JsonRepr s) -> JsonRepr ('SchemaOptional s)
373 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
326instance Show (JsonRepr ('SchemaText cs)) where
402instance Show (JsonRepr ('SchemaText cs)) where
327 show (ReprText t) = "ReprText " P.++ show t
403 show (ReprText t) = "ReprText " P.++ show t
329instance Show (JsonRepr ('SchemaNumber cs)) where
405instance Show (JsonRepr ('SchemaNumber cs)) where
330 show (ReprNumber n) = "ReprNumber " P.++ show n
406 show (ReprNumber n) = "ReprNumber " P.++ show n
408+instance Show (JsonRepr 'SchemaBoolean) where
409+ show (ReprBoolean n) = "ReprBoolean " P.++ show n
332instance Show (JsonRepr 'SchemaNull) where show _ = "ReprNull"
411instance Show (JsonRepr 'SchemaNull) where show _ = "ReprNull"
334instance Show (JsonRepr s) => Show (JsonRepr ('SchemaArray acs s)) where
413instance Show (JsonRepr s) => Show (JsonRepr ('SchemaArray acs s)) where
335 show (ReprArray v) = "ReprArray " P.++ show v
414 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
337instance V.RecAll FieldRepr fs Show => Show (JsonRepr ('SchemaObject fs)) where
423instance V.RecAll FieldRepr fs Show => Show (JsonRepr ('SchemaObject fs)) where
338 show (ReprObject fs) = "ReprObject " P.++ show fs
424 show (ReprObject fs) = "ReprObject " P.++ show fs
340instance Show (JsonRepr s) => Show (JsonRepr ('SchemaOptional s)) where
427instance Show (JsonRepr s) => Show (JsonRepr ('SchemaOptional s)) where
341 show (ReprOptional s) = "ReprOptional " P.++ show s
428 show (ReprOptional s) = "ReprOptional " P.++ show s
343−instance (Monad m, Serial m Text)
430+instance Show (Union JsonRepr (h ': tl))
344− => Serial m (JsonRepr ('SchemaText cs)) where
431+ => Show (JsonRepr ('SchemaUnion (h ': tl))) where
345− series = cons1 ReprText
432+ show (ReprUnion s) = "ReprUnion " P.++ show s
347−instance (Monad m, Serial m Scientific)
348− => Serial m (JsonRepr ('SchemaNumber cs)) where
349− series = cons1 ReprNumber
351−instance Monad m => Serial m (JsonRepr 'SchemaNull) where
352− series = cons0 ReprNull
354−instance (Serial m (V.Vector (JsonRepr s)))
355− => Serial m (JsonRepr ('SchemaArray cs s)) where
356− series = cons1 ReprArray
358−instance (Serial m (JsonRepr s))
359− => Serial m (JsonRepr ('SchemaOptional s)) where
360− series = cons1 ReprOptional
362−instance (Monad m, Serial m (Rec FieldRepr fs))
363− => Serial m (JsonRepr ('SchemaObject fs)) where
364− series = cons1 ReprObject
366instance Eq (Rec FieldRepr fs) => Eq (JsonRepr ('SchemaObject fs)) where
434instance Eq (Rec FieldRepr fs) => Eq (JsonRepr ('SchemaObject fs)) where
367 ReprObject a == ReprObject b = a == b
435 ReprObject a == ReprObject b = a == b
@@ -372,6 +440,9 @@ instance Eq (JsonRepr ('SchemaText cs)) where
372instance Eq (JsonRepr ('SchemaNumber cs)) where
440instance Eq (JsonRepr ('SchemaNumber cs)) where
373 ReprNumber a == ReprNumber b = a == b
441 ReprNumber a == ReprNumber b = a == b
443+instance Eq (JsonRepr 'SchemaBoolean) where
444+ ReprBoolean a == ReprBoolean b = a == b
375instance Eq (JsonRepr 'SchemaNull) where
446instance Eq (JsonRepr 'SchemaNull) where
376 ReprNull == ReprNull = True
447 ReprNull == ReprNull = True
@@ -381,6 +452,51 @@ instance Eq (JsonRepr s) => Eq (JsonRepr ('SchemaArray as s)) where
381instance Eq (JsonRepr s) => Eq (JsonRepr ('SchemaOptional s)) where
452instance Eq (JsonRepr s) => Eq (JsonRepr ('SchemaOptional s)) where
382 ReprOptional a == ReprOptional b = a == b
453 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
384fromOptional
500fromOptional
385 :: SingI s
501 :: SingI s
386 => Sing ('SchemaOptional s)
502 => Sing ('SchemaOptional s)
@@ -388,6 +504,24 @@ fromOptional
388 -> Parser (Maybe (JsonRepr s))
504 -> Parser (Maybe (JsonRepr s))
389fromOptional _ = parseJSON
505fromOptional _ = 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
391instance SingI schema => J.FromJSON (JsonRepr schema) where
525instance SingI schema => J.FromJSON (JsonRepr schema) where
392 parseJSON value = case sing :: Sing schema of
526 parseJSON value = case sing :: Sing schema of
393 SSchemaText _ -> withText "String" (pure . ReprText) value
527 SSchemaText _ -> withText "String" (pure . ReprText) value
@@ -408,27 +542,29 @@ instance SingI schema => J.FromJSON (JsonRepr schema) where
408 fieldRepr <- case s of
542 fieldRepr <- case s of
409 SSchemaText so -> case H.lookup fieldName h of
543 SSchemaText so -> case H.lookup fieldName h of
410 Just v -> withSingI so $ FieldRepr <$> parseJSON v
544 Just v -> withSingI so $ FieldRepr <$> parseJSON v
411− Nothing -> fail "schematext"
545+ Nothing -> fail $ "No text field: " P.++ show fieldName
412 SSchemaNumber so -> case H.lookup fieldName h of
546 SSchemaNumber so -> case H.lookup fieldName h of
413 Just v -> withSingI so $ FieldRepr <$> parseJSON v
547 Just v -> withSingI so $ FieldRepr <$> parseJSON v
414− Nothing -> fail "schemanumber"
548+ Nothing -> fail $ "No number field: " P.++ show fieldName
415 SSchemaBoolean -> case H.lookup fieldName h of
549 SSchemaBoolean -> case H.lookup fieldName h of
416 Just v -> FieldRepr <$> parseJSON v
550 Just v -> FieldRepr <$> parseJSON v
417− Nothing -> fail "schemaboolean"
551+ Nothing -> fail $ "No boolean field: " P.++ show fieldName
418 SSchemaNull -> case H.lookup fieldName h of
552 SSchemaNull -> case H.lookup fieldName h of
419 Just v -> FieldRepr <$> parseJSON v
553 Just v -> FieldRepr <$> parseJSON v
420− Nothing -> fail "schemanull"
554+ Nothing -> fail $ "No null field: " P.++ show fieldName
421 SSchemaArray sa sb -> case H.lookup fieldName h of
555 SSchemaArray sa sb -> case H.lookup fieldName h of
422 Just v -> withSingI sa $ withSingI sb $ FieldRepr <$> parseJSON v
556 Just v -> withSingI sa $ withSingI sb $ FieldRepr <$> parseJSON v
423− Nothing -> fail "schemaarray"
557+ Nothing -> fail $ "No array field: " P.++ show fieldName
424 SSchemaObject so -> case H.lookup fieldName h of
558 SSchemaObject so -> case H.lookup fieldName h of
425 Just v -> withSingI so $ FieldRepr <$> parseJSON v
559 Just v -> withSingI so $ FieldRepr <$> parseJSON v
426− Nothing -> fail "schemaobject"
560+ Nothing -> fail $ "No object field" P.++ show fieldName
427 SSchemaOptional so -> case H.lookup fieldName h of
561 SSchemaOptional so -> case H.lookup fieldName h of
428 Just v -> withSingI so $ FieldRepr <$> parseJSON v
562 Just v -> withSingI so $ FieldRepr <$> parseJSON v
429− Nothing -> fail "schemaoptional"
563+ Nothing -> withSingI so $ pure $ FieldRepr $ ReprOptional Nothing
564+ SSchemaUnion ss -> withSingI ss $ FieldRepr <$> parseUnion ss value
430 (fieldRepr :&) <$> demoteFields tl h
565 (fieldRepr :&) <$> demoteFields tl h
431 ReprObject <$> withObject "Object" (demoteFields fs) value
566 ReprObject <$> withObject "Object" (demoteFields fs) value
567+ SSchemaUnion ss -> parseUnion ss value
433instance J.ToJSON (JsonRepr a) where
569instance J.ToJSON (JsonRepr a) where
434 toJSON ReprNull = J.Null
570 toJSON ReprNull = J.Null
@@ -436,7 +572,7 @@ instance J.ToJSON (JsonRepr a) where
436 toJSON (ReprText t) = J.String t
572 toJSON (ReprText t) = J.String t
437 toJSON (ReprNumber n) = J.Number n
573 toJSON (ReprNumber n) = J.Number n
438 toJSON (ReprOptional s) = case s of
574 toJSON (ReprOptional s) = case s of
439− Just v -> toJSON v
575+ Just v -> toJSON v
440 Nothing -> J.Null
576 Nothing -> J.Null
441 toJSON (ReprArray v) = J.Array $ toJSON <$> v
577 toJSON (ReprArray v) = J.Array $ toJSON <$> v
442 toJSON (ReprObject r) = J.Object . H.fromList . fold $ r
578 toJSON (ReprObject r) = J.Object . H.fromList . fold $ r
@@ -449,6 +585,7 @@ instance J.ToJSON (JsonRepr a) where
449 fold = \case
585 fold = \case
450 RNil -> []
586 RNil -> []
451 fr@(FieldRepr _) :& tl -> (extract fr) : fold tl
587 fr@(FieldRepr _) :& tl -> (extract fr) : fold tl
588+ toJSON (ReprUnion u) = toJSON u
453class FalseConstraint a
590class FalseConstraint a