@@ -1,28 +1,33 @@
11{-# LANGUAGE AllowAmbiguousTypes #-}
2−{-# LANGUAGE OverloadedLists #-}
3−{-# LANGUAGE OverloadedStrings #-}
2+{-# LANGUAGE CPP #-}
3+{-# LANGUAGE OverloadedLists #-}
4+{-# LANGUAGE OverloadedStrings #-}
45{-# OPTIONS_GHC -fprint-explicit-kinds #-}
56
67module Data.Schematic.Schema where
78
8−import Control.Applicative ()
9+import Control.Applicative ((<|>))
910import Control.Monad
1011import Data.Aeson as J
1112import Data.Aeson.Types as J
1213import Data.HashMap.Strict as H
1314import Data.Kind
1415import Data.Maybe
16+import Data.Schematic.Generator
1517import Data.Schematic.Instances ()
1618import Data.Scientific
17−import Data.Singletons.Prelude.List hiding (All)
19+import Data.Singletons.Prelude.List hiding (All, Union)
1820import Data.Singletons.TH
1921import Data.Singletons.TypeLits
2022import Data.Text as T
23+import Data.Union
2124import Data.Vector as V
2225import Data.Vinyl hiding (Dict)
2326import qualified Data.Vinyl.TypeLevel as V
27+import GHC.Exts
2428import GHC.Generics (Generic)
25−import GHC.TypeLits (SomeNat(..), SomeSymbol(..), someSymbolVal, someNatVal)
29+import GHC.TypeLits
30+ (SomeNat(..), SomeSymbol(..), someNatVal, someSymbolVal)
2631import Prelude as P
2732import Test.SmallCheck.Series
2833
@@ -44,38 +49,38 @@ data TextConstraint
4449 deriving (Generic)
4550
4651instance SingKind TextConstraint where
47− type DemoteRep TextConstraint = DemotedTextConstraint
52+ type Demote TextConstraint = DemotedTextConstraint
4853 fromSing = \case
49− STEq n -> withKnownNat n (DTEq $ natVal n)
50− STLt n -> withKnownNat n (DTLt $ natVal n)
51− STLe n -> withKnownNat n (DTLe $ natVal n)
52− STGt n -> withKnownNat n (DTGt $ natVal n)
53− STGe n -> withKnownNat n (DTGe $ natVal n)
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)
5459 STRegex s -> withKnownSymbol s (DTRegex $ T.pack $ symbolVal s)
5560 STEnum s -> let
5661 d :: Sing (s :: [Symbol]) -> [Text]
57− d SNil = []
62+ d SNil = []
5863 d (SCons ss@SSym fs) = T.pack (symbolVal ss) : d fs
5964 in DTEnum $ d s
6065 toSing = \case
6166 DTEq n -> case someNatVal n of
6267 Just (SomeNat (_ :: Proxy n)) -> SomeSing (STEq (SNat :: Sing n))
63− Nothing -> error "Negative singleton nat"
68+ Nothing -> error "Negative singleton nat"
6469 DTLt n -> case someNatVal n of
6570 Just (SomeNat (_ :: Proxy n)) -> SomeSing (STLt (SNat :: Sing n))
66− Nothing -> error "Negative singleton nat"
71+ Nothing -> error "Negative singleton nat"
6772 DTLe n -> case someNatVal n of
6873 Just (SomeNat (_ :: Proxy n)) -> SomeSing (STLe (SNat :: Sing n))
69− Nothing -> error "Negative singleton nat"
74+ Nothing -> error "Negative singleton nat"
7075 DTGt n -> case someNatVal n of
7176 Just (SomeNat (_ :: Proxy n)) -> SomeSing (STGt (SNat :: Sing n))
72− Nothing -> error "Negative singleton nat"
77+ Nothing -> error "Negative singleton nat"
7378 DTGe n -> case someNatVal n of
7479 Just (SomeNat (_ :: Proxy n)) -> SomeSing (STGe (SNat :: Sing n))
75− Nothing -> error "Negative singleton nat"
80+ Nothing -> error "Negative singleton nat"
7681 DTRegex s -> case someSymbolVal (T.unpack s) of
7782 SomeSymbol (_ :: Proxy n) -> SomeSing (STRegex (SSym :: Sing n))
78− DTEnum ss -> case toSing (T.unpack <$> ss) of
83+ DTEnum ss -> case toSing ss of
7984 SomeSing l -> SomeSing (STEnum l)
8085
8186data DemotedTextConstraint
@@ -86,7 +91,7 @@ data DemotedTextConstraint
8691 | DTGe Integer
8792 | DTRegex Text
8893 | DTEnum [Text]
89− deriving (Generic)
94+ deriving (Generic, Eq, Show)
9095
9196data instance Sing (tc :: TextConstraint) where
9297 STEq :: Sing n -> Sing ('TEq n)
@@ -127,7 +132,7 @@ data DemotedNumberConstraint
127132 | DNGt Integer
128133 | DNGe Integer
129134 | DNEq Integer
130− deriving (Generic)
135+ deriving (Generic, Eq, Show)
131136
132137data instance Sing (nc :: NumberConstraint) where
133138 SNEq :: Sing n -> Sing ('NEq n)
@@ -149,79 +154,121 @@ instance Eq (Sing ('NGt n)) where _ == _ = True
149154instance Eq (Sing ('NGe n)) where _ == _ = True
150155
151156instance SingKind NumberConstraint where
152− type DemoteRep NumberConstraint = DemotedNumberConstraint
157+ type Demote NumberConstraint = DemotedNumberConstraint
153158 fromSing = \case
154− SNEq n -> withKnownNat n (DNEq $ natVal n)
155− SNGt n -> withKnownNat n (DNGt $ natVal n)
156− SNGe n -> withKnownNat n (DNGe $ natVal n)
157− SNLt n -> withKnownNat n (DNLt $ natVal n)
158− SNLe n -> withKnownNat n (DNLe $ natVal n)
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)
159164 toSing = \case
160165 DNEq n -> case someNatVal n of
161166 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNEq (SNat :: Sing n))
162− Nothing -> error "Negative singleton nat"
167+ Nothing -> error "Negative singleton nat"
163168 DNGt n -> case someNatVal n of
164169 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNGt (SNat :: Sing n))
165− Nothing -> error "Negative singleton nat"
170+ Nothing -> error "Negative singleton nat"
166171 DNGe n -> case someNatVal n of
167172 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNGe (SNat :: Sing n))
168− Nothing -> error "Negative singleton nat"
173+ Nothing -> error "Negative singleton nat"
169174 DNLt n -> case someNatVal n of
170175 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNLt (SNat :: Sing n))
171− Nothing -> error "Negative singleton nat"
176+ Nothing -> error "Negative singleton nat"
172177 DNLe n -> case someNatVal n of
173178 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNLe (SNat :: Sing n))
174− Nothing -> error "Negative singleton nat"
179+ Nothing -> error "Negative singleton nat"
175180
176181data ArrayConstraint
177182 = AEq Nat
183+ | AGt Nat
184+ | AGe Nat
185+ | ALt Nat
186+ | ALe Nat
178187 deriving (Generic)
179188
180189data DemotedArrayConstraint
181190 = DAEq Integer
182− deriving (Generic)
191+ | DAGt Integer
192+ | DAGe Integer
193+ | DALt Integer
194+ | DALe Integer
195+ deriving (Generic, Eq, Show)
183196
184197data instance Sing (ac :: ArrayConstraint) where
185198 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)
186203
187204instance 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
188209
189210instance 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
190215
191216instance SingKind ArrayConstraint where
192− type DemoteRep ArrayConstraint = DemotedArrayConstraint
217+ type Demote ArrayConstraint = DemotedArrayConstraint
193218 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)
195224 toSing = \case
196225 DAEq n -> case someNatVal n of
197226 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SAEq (SNat :: Sing n))
198− Nothing -> error "Negative singleton nat"
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"
199240
200241data Schema
201242 = SchemaText [TextConstraint]
243+ | SchemaBoolean
202244 | SchemaNumber [NumberConstraint]
203245 | SchemaObject [(Symbol, Schema)]
204246 | SchemaArray [ArrayConstraint] Schema
205247 | SchemaNull
206248 | SchemaOptional Schema
249+ | SchemaUnion [Schema]
207250 deriving (Generic)
208251
209252data DemotedSchema
210253 = DSchemaText [DemotedTextConstraint]
211254 | DSchemaNumber [DemotedNumberConstraint]
255+ | DSchemaBoolean
212256 | DSchemaObject [(Text, DemotedSchema)]
213257 | DSchemaArray [DemotedArrayConstraint] DemotedSchema
214258 | DSchemaNull
215259 | DSchemaOptional DemotedSchema
216− deriving (Generic)
260+ | DSchemaUnion [DemotedSchema]
261+ deriving (Generic, Eq, Show)
217262
218263data instance Sing (schema :: Schema) where
219264 SSchemaText :: Sing tcs -> Sing ('SchemaText tcs)
220265 SSchemaNumber :: Sing ncs -> Sing ('SchemaNumber ncs)
266+ SSchemaBoolean :: Sing 'SchemaBoolean
221267 SSchemaArray :: Sing acs -> Sing schema -> Sing ('SchemaArray acs schema)
222268 SSchemaObject :: Sing fields -> Sing ('SchemaObject fields)
223269 SSchemaOptional :: Sing s -> Sing ('SchemaOptional s)
224270 SSchemaNull :: Sing 'SchemaNull
271+ SSchemaUnion :: Sing ss -> Sing ('SchemaUnion ss)
225272
226273instance SingI sl => SingI ('SchemaText sl) where
227274 sing = SSchemaText sing
@@ -229,46 +276,52 @@ instance SingI sl => SingI ('SchemaNumber sl) where
229276 sing = SSchemaNumber sing
230277instance SingI 'SchemaNull where
231278 sing = SSchemaNull
279+instance SingI 'SchemaBoolean where
280+ sing = SSchemaBoolean
232281instance (SingI ac, SingI s) => SingI ('SchemaArray ac s) where
233282 sing = SSchemaArray sing sing
234283instance SingI stl => SingI ('SchemaObject stl) where
235284 sing = SSchemaObject sing
236285instance SingI s => SingI ('SchemaOptional s) where
237286 sing = SSchemaOptional sing
287+instance SingI s => SingI ('SchemaUnion s) where
288+ sing = SSchemaUnion sing
238289
239290instance Eq (Sing ('SchemaText cs)) where _ == _ = True
240291instance Eq (Sing ('SchemaNumber cs)) where _ == _ = True
241292instance Eq (Sing 'SchemaNull) where _ == _ = True
293+instance Eq (Sing 'SchemaBoolean) where _ == _ = True
242294instance Eq (Sing ('SchemaArray as s)) where _ == _ = True
243295instance Eq (Sing ('SchemaObject cs)) where _ == _ = True
244296instance Eq (Sing ('SchemaOptional s)) where _ == _ = True
297+instance Eq (Sing ('SchemaUnion s)) where _ == _ = True
245298
246299instance SingKind Schema where
247− type DemoteRep Schema = DemotedSchema
300+ type Demote Schema = DemotedSchema
248301 fromSing = \case
249302 SSchemaText cs -> DSchemaText $ fromSing cs
250303 SSchemaNumber cs -> DSchemaNumber $ fromSing cs
304+ SSchemaBoolean -> DSchemaBoolean
251305 SSchemaArray cs s -> DSchemaArray (fromSing cs) (fromSing s)
252306 SSchemaOptional s -> DSchemaOptional $ fromSing s
253307 SSchemaNull -> DSchemaNull
254− SSchemaObject cs -> let
255− dem :: Sing (s :: [(Symbol, Schema)]) -> [(Text, DemotedSchema)]
256− dem SNil = []
257− dem (SCons (STuple2 ss ssch) fs) = withKnownSymbol ss
258− $ (T.pack (symbolVal ss), fromSing ssch) : dem fs
259− in DSchemaObject $ dem cs
308+ SSchemaObject cs -> DSchemaObject $ fromSing cs
309+ SSchemaUnion ss -> DSchemaUnion $ fromSing ss
260310 toSing = \case
261311 DSchemaText cs -> case toSing cs of
262312 SomeSing scs -> SomeSing $ SSchemaText scs
263313 DSchemaNumber cs -> case toSing cs of
264314 SomeSing scs -> SomeSing $ SSchemaNumber scs
315+ DSchemaBoolean -> SomeSing $ SSchemaBoolean
265316 DSchemaArray cs sch -> case (toSing cs, toSing sch) of
266317 (SomeSing scs, SomeSing ssch) -> SomeSing $ SSchemaArray scs ssch
267318 DSchemaOptional sch -> case toSing sch of
268319 SomeSing ssch -> SomeSing $ SSchemaOptional ssch
269320 DSchemaNull -> SomeSing SSchemaNull
270− DSchemaObject cs -> case toSing ((\(sym,sch) -> (T.unpack sym, sch)) <$> cs) of
321+ DSchemaObject cs -> case toSing cs of
271322 SomeSing scs -> SomeSing $ SSchemaObject scs
323+ DSchemaUnion ss -> case toSing ss of
324+ SomeSing sss -> SomeSing $ SSchemaUnion sss
272325
273326data FieldRepr :: (Symbol, Schema) -> Type where
274327 FieldRepr
@@ -306,53 +359,77 @@ instance
306359 => Serial m (FieldRepr '(name, schema)) where
307360 series = FieldRepr <$> series
308361
362+type family USubsets (u :: [k]) :: Constraint where
363+ USubsets '[] = ()
364+ USubsets (h ': tl) = (USubset tl (h ': tl) (V.RImage tl (h ': tl)), USubsets tl)
365+
309366data JsonRepr :: Schema -> Type where
310367 ReprText :: Text -> JsonRepr ('SchemaText cs)
311368 ReprNumber :: Scientific -> JsonRepr ('SchemaNumber cs)
369+ ReprBoolean :: Bool -> JsonRepr 'SchemaBoolean
312370 ReprNull :: JsonRepr 'SchemaNull
313371 ReprArray :: V.Vector (JsonRepr s) -> JsonRepr ('SchemaArray cs s)
314372 ReprObject :: Rec FieldRepr fs -> JsonRepr ('SchemaObject fs)
315373 ReprOptional :: Maybe (JsonRepr s) -> JsonRepr ('SchemaOptional s)
374+ ReprUnion :: Union JsonRepr (h ': tl) -> JsonRepr ('SchemaUnion (h ': tl))
375+
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)
379+
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)
384+
385+instance Monad m => Serial m (JsonRepr 'SchemaNull) where
386+ series = cons0 ReprNull
387+
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)
392+
393+instance (Serial m (JsonRepr s))
394+ => Serial m (JsonRepr ('SchemaOptional s)) where
395+ series = cons1 ReprOptional
396+
397+instance (Monad m, Serial m (Rec FieldRepr fs))
398+ => Serial m (JsonRepr ('SchemaObject fs)) where
399+ series = cons1 ReprObject
316400
401+-- | Move to the union package
317402instance Show (JsonRepr ('SchemaText cs)) where
318403 show (ReprText t) = "ReprText " P.++ show t
319404
320405instance Show (JsonRepr ('SchemaNumber cs)) where
321406 show (ReprNumber n) = "ReprNumber " P.++ show n
322407
408+instance Show (JsonRepr 'SchemaBoolean) where
409+ show (ReprBoolean n) = "ReprBoolean " P.++ show n
410+
323411instance Show (JsonRepr 'SchemaNull) where show _ = "ReprNull"
324412
325413instance Show (JsonRepr s) => Show (JsonRepr ('SchemaArray acs s)) where
326414 show (ReprArray v) = "ReprArray " P.++ show v
327415
416+#if MIN_VERSION_base(4,12,0)
417+instance
418+ ( V.RecAll FieldRepr fs Show, RMap fs, ReifyConstraint Show FieldRepr fs
419+ , RecordToList fs )
420+ => Show (JsonRepr ('SchemaObject fs)) where
421+ show (ReprObject fs) = "ReprObject " P.++ show fs
422+#else
328423instance V.RecAll FieldRepr fs Show => Show (JsonRepr ('SchemaObject fs)) where
329424 show (ReprObject fs) = "ReprObject " P.++ show fs
425+#endif
330426
331427instance Show (JsonRepr s) => Show (JsonRepr ('SchemaOptional s)) where
332428 show (ReprOptional s) = "ReprOptional " P.++ show s
333429
334−instance (Monad m, Serial m Text)
335− => Serial m (JsonRepr ('SchemaText cs)) where
336− series = cons1 ReprText
337−
338−instance (Monad m, Serial m Scientific)
339− => Serial m (JsonRepr ('SchemaNumber cs)) where
340− series = cons1 ReprNumber
341−
342−instance Monad m => Serial m (JsonRepr 'SchemaNull) where
343− series = cons0 ReprNull
344−
345−instance (Serial m (V.Vector (JsonRepr s)))
346− => Serial m (JsonRepr ('SchemaArray cs s)) where
347− series = cons1 ReprArray
348−
349−instance (Serial m (JsonRepr s))
350− => Serial m (JsonRepr ('SchemaOptional s)) where
351− series = cons1 ReprOptional
352−
353−instance (Monad m, Serial m (Rec FieldRepr fs))
354− => Serial m (JsonRepr ('SchemaObject fs)) where
355− series = cons1 ReprObject
430+instance Show (Union JsonRepr (h ': tl))
431+ => Show (JsonRepr ('SchemaUnion (h ': tl))) where
432+ show (ReprUnion s) = "ReprUnion " P.++ show s
356433
357434instance Eq (Rec FieldRepr fs) => Eq (JsonRepr ('SchemaObject fs)) where
358435 ReprObject a == ReprObject b = a == b
@@ -363,6 +440,9 @@ instance Eq (JsonRepr ('SchemaText cs)) where
363440instance Eq (JsonRepr ('SchemaNumber cs)) where
364441 ReprNumber a == ReprNumber b = a == b
365442
443+instance Eq (JsonRepr 'SchemaBoolean) where
444+ ReprBoolean a == ReprBoolean b = a == b
445+
366446instance Eq (JsonRepr 'SchemaNull) where
367447 ReprNull == ReprNull = True
368448
@@ -372,6 +452,51 @@ instance Eq (JsonRepr s) => Eq (JsonRepr ('SchemaArray as s)) where
372452instance Eq (JsonRepr s) => Eq (JsonRepr ('SchemaOptional s)) where
373453 ReprOptional a == ReprOptional b = a == b
374454
455+instance Eq (Union JsonRepr (h ': tl))
456+ => Eq (JsonRepr ('SchemaUnion (h ': tl))) where
457+ ReprUnion a == ReprUnion b = a == b
458+
459+instance Ord (Rec FieldRepr fs) => Ord (JsonRepr ('SchemaObject fs)) where
460+ ReprObject a `compare` ReprObject b = a `compare` b
461+
462+instance Ord (JsonRepr ('SchemaText cs)) where
463+ ReprText a `compare` ReprText b = a `compare` b
464+
465+instance Ord (JsonRepr ('SchemaNumber cs)) where
466+ ReprNumber a `compare` ReprNumber b = a `compare` b
467+
468+instance Ord (JsonRepr 'SchemaBoolean) where
469+ ReprBoolean a `compare` ReprBoolean b = a `compare` b
470+
471+instance Ord (JsonRepr 'SchemaNull) where
472+ compare _ _ = EQ
473+
474+instance Ord (JsonRepr s) => Ord (JsonRepr ('SchemaArray as s)) where
475+ ReprArray a `compare` ReprArray b = a `compare` b
476+
477+instance Ord (JsonRepr s) => Ord (JsonRepr ('SchemaOptional s)) where
478+ ReprOptional a `compare` ReprOptional b = a `compare` b
479+
480+instance Ord (Union JsonRepr (h ': tl))
481+ => Ord (JsonRepr ('SchemaUnion (h ': tl))) where
482+ ReprUnion a `compare` ReprUnion b = a `compare` b
483+
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
488+
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
496+
497+instance IsString (JsonRepr ('SchemaText cs)) where
498+ fromString = ReprText . fromString
499+
375500fromOptional
376501 :: SingI s
377502 => Sing ('SchemaOptional s)
@@ -379,10 +504,29 @@ fromOptional
379504 -> Parser (Maybe (JsonRepr s))
380505fromOptional _ = parseJSON
381506
507+parseUnion
508+ :: FromJSON (JsonRepr ('SchemaUnion ss))
509+ => sing (ss :: [Schema])
510+ -> Value
511+ -> Parser (JsonRepr ('SchemaUnion ss))
512+parseUnion _ val = parseJSON val
513+
514+instance FromJSON (Union JsonRepr '[]) where
515+ parseJSON = fail "empty union"
516+
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)))
520+
521+instance ToJSON (Union JsonRepr as) where
522+ toJSON (This fa) = toJSON fa
523+ toJSON (That u) = toJSON u
524+
382525instance SingI schema => J.FromJSON (JsonRepr schema) where
383526 parseJSON value = case sing :: Sing schema of
384527 SSchemaText _ -> withText "String" (pure . ReprText) value
385528 SSchemaNumber _ -> withScientific "Number" (pure . ReprNumber) value
529+ SSchemaBoolean -> ReprBoolean <$> parseJSON value
386530 SSchemaNull -> case value of
387531 J.Null -> pure ReprNull
388532 _ -> typeMismatch "Null" value
@@ -398,31 +542,37 @@ instance SingI schema => J.FromJSON (JsonRepr schema) where
398542 fieldRepr <- case s of
399543 SSchemaText so -> case H.lookup fieldName h of
400544 Just v -> withSingI so $ FieldRepr <$> parseJSON v
401− Nothing -> fail "schematext"
545+ Nothing -> fail $ "No text field: " P.++ show fieldName
402546 SSchemaNumber so -> case H.lookup fieldName h of
403547 Just v -> withSingI so $ FieldRepr <$> parseJSON v
404− Nothing -> fail "schemanumber"
548+ Nothing -> fail $ "No number field: " P.++ show fieldName
549+ SSchemaBoolean -> case H.lookup fieldName h of
550+ Just v -> FieldRepr <$> parseJSON v
551+ Nothing -> fail $ "No boolean field: " P.++ show fieldName
405552 SSchemaNull -> case H.lookup fieldName h of
406553 Just v -> FieldRepr <$> parseJSON v
407− Nothing -> fail "schemanull"
554+ Nothing -> fail $ "No null field: " P.++ show fieldName
408555 SSchemaArray sa sb -> case H.lookup fieldName h of
409556 Just v -> withSingI sa $ withSingI sb $ FieldRepr <$> parseJSON v
410− Nothing -> fail "schemaarray"
557+ Nothing -> fail $ "No array field: " P.++ show fieldName
411558 SSchemaObject so -> case H.lookup fieldName h of
412559 Just v -> withSingI so $ FieldRepr <$> parseJSON v
413− Nothing -> fail "schemaobject"
560+ Nothing -> fail $ "No object field" P.++ show fieldName
414561 SSchemaOptional so -> case H.lookup fieldName h of
415562 Just v -> withSingI so $ FieldRepr <$> parseJSON v
416− Nothing -> fail "schemaoptional"
563+ Nothing -> withSingI so $ pure $ FieldRepr $ ReprOptional Nothing
564+ SSchemaUnion ss -> withSingI ss $ FieldRepr <$> parseUnion ss value
417565 (fieldRepr :&) <$> demoteFields tl h
418566 ReprObject <$> withObject "Object" (demoteFields fs) value
567+ SSchemaUnion ss -> parseUnion ss value
419568
420569instance J.ToJSON (JsonRepr a) where
421570 toJSON ReprNull = J.Null
571+ toJSON (ReprBoolean b) = J.Bool b
422572 toJSON (ReprText t) = J.String t
423573 toJSON (ReprNumber n) = J.Number n
424574 toJSON (ReprOptional s) = case s of
425− Just v -> toJSON v
575+ Just v -> toJSON v
426576 Nothing -> J.Null
427577 toJSON (ReprArray v) = J.Array $ toJSON <$> v
428578 toJSON (ReprObject r) = J.Object . H.fromList . fold $ r
@@ -435,6 +585,7 @@ instance J.ToJSON (JsonRepr a) where
435585 fold = \case
436586 RNil -> []
437587 fr@(FieldRepr _) :& tl -> (extract fr) : fold tl
588+ toJSON (ReprUnion u) = toJSON u
438589
439590class FalseConstraint a
440591