@@ -1,6 +1,7 @@
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
@@ -9,10 +10,10 @@ import Control.Applicative ((<|>))
910import Control.Monad
1011import Data.Aeson as J
1112import Data.Aeson.Types as J
12−import Data.Coerce
1313import Data.HashMap.Strict as H
1414import Data.Kind
1515import Data.Maybe
16+import Data.Schematic.Generator
1617import Data.Schematic.Instances ()
1718import Data.Scientific
1819import Data.Singletons.Prelude.List hiding (All, Union)
@@ -25,7 +26,8 @@ import Data.Vinyl hiding (Dict)
2526import qualified Data.Vinyl.TypeLevel as V
2627import GHC.Exts
2728import GHC.Generics (Generic)
28−import GHC.TypeLits (SomeNat(..), SomeSymbol(..), someSymbolVal, someNatVal)
29+import GHC.TypeLits
30+ (SomeNat(..), SomeSymbol(..), someNatVal, someSymbolVal)
2931import Prelude as P
3032import Test.SmallCheck.Series
3133
@@ -49,33 +51,33 @@ data TextConstraint
4951instance SingKind TextConstraint where
5052 type Demote TextConstraint = DemotedTextConstraint
5153 fromSing = \case
52− STEq n -> withKnownNat n (DTEq $ natVal n)
53− STLt n -> withKnownNat n (DTLt $ natVal n)
54− STLe n -> withKnownNat n (DTLe $ natVal n)
55− STGt n -> withKnownNat n (DTGt $ natVal n)
56− 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)
5759 STRegex s -> withKnownSymbol s (DTRegex $ T.pack $ symbolVal s)
5860 STEnum s -> let
5961 d :: Sing (s :: [Symbol]) -> [Text]
60− d SNil = []
62+ d SNil = []
6163 d (SCons ss@SSym fs) = T.pack (symbolVal ss) : d fs
6264 in DTEnum $ d s
6365 toSing = \case
6466 DTEq n -> case someNatVal n of
6567 Just (SomeNat (_ :: Proxy n)) -> SomeSing (STEq (SNat :: Sing n))
66− Nothing -> error "Negative singleton nat"
68+ Nothing -> error "Negative singleton nat"
6769 DTLt n -> case someNatVal n of
6870 Just (SomeNat (_ :: Proxy n)) -> SomeSing (STLt (SNat :: Sing n))
69− Nothing -> error "Negative singleton nat"
71+ Nothing -> error "Negative singleton nat"
7072 DTLe n -> case someNatVal n of
7173 Just (SomeNat (_ :: Proxy n)) -> SomeSing (STLe (SNat :: Sing n))
72− Nothing -> error "Negative singleton nat"
74+ Nothing -> error "Negative singleton nat"
7375 DTGt n -> case someNatVal n of
7476 Just (SomeNat (_ :: Proxy n)) -> SomeSing (STGt (SNat :: Sing n))
75− Nothing -> error "Negative singleton nat"
77+ Nothing -> error "Negative singleton nat"
7678 DTGe n -> case someNatVal n of
7779 Just (SomeNat (_ :: Proxy n)) -> SomeSing (STGe (SNat :: Sing n))
78− Nothing -> error "Negative singleton nat"
80+ Nothing -> error "Negative singleton nat"
7981 DTRegex s -> case someSymbolVal (T.unpack s) of
8082 SomeSymbol (_ :: Proxy n) -> SomeSing (STRegex (SSym :: Sing n))
8183 DTEnum ss -> case toSing ss of
@@ -89,7 +91,7 @@ data DemotedTextConstraint
8991 | DTGe Integer
9092 | DTRegex Text
9193 | DTEnum [Text]
92− deriving (Generic)
94+ deriving (Generic, Eq, Show)
9395
9496data instance Sing (tc :: TextConstraint) where
9597 STEq :: Sing n -> Sing ('TEq n)
@@ -130,7 +132,7 @@ data DemotedNumberConstraint
130132 | DNGt Integer
131133 | DNGe Integer
132134 | DNEq Integer
133− deriving (Generic)
135+ deriving (Generic, Eq, Show)
134136
135137data instance Sing (nc :: NumberConstraint) where
136138 SNEq :: Sing n -> Sing ('NEq n)
@@ -154,51 +156,87 @@ instance Eq (Sing ('NGe n)) where _ == _ = True
154156instance SingKind NumberConstraint where
155157 type Demote NumberConstraint = DemotedNumberConstraint
156158 fromSing = \case
157− SNEq n -> withKnownNat n (DNEq $ natVal n)
158− SNGt n -> withKnownNat n (DNGt $ natVal n)
159− SNGe n -> withKnownNat n (DNGe $ natVal n)
160− SNLt n -> withKnownNat n (DNLt $ natVal n)
161− 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)
162164 toSing = \case
163165 DNEq n -> case someNatVal n of
164166 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNEq (SNat :: Sing n))
165− Nothing -> error "Negative singleton nat"
167+ Nothing -> error "Negative singleton nat"
166168 DNGt n -> case someNatVal n of
167169 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNGt (SNat :: Sing n))
168− Nothing -> error "Negative singleton nat"
170+ Nothing -> error "Negative singleton nat"
169171 DNGe n -> case someNatVal n of
170172 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNGe (SNat :: Sing n))
171− Nothing -> error "Negative singleton nat"
173+ Nothing -> error "Negative singleton nat"
172174 DNLt n -> case someNatVal n of
173175 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNLt (SNat :: Sing n))
174− Nothing -> error "Negative singleton nat"
176+ Nothing -> error "Negative singleton nat"
175177 DNLe n -> case someNatVal n of
176178 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNLe (SNat :: Sing n))
177− Nothing -> error "Negative singleton nat"
179+ Nothing -> error "Negative singleton nat"
178180
179181data ArrayConstraint
180182 = AEq Nat
183+ | AGt Nat
184+ | AGe Nat
185+ | ALt Nat
186+ | ALe Nat
181187 deriving (Generic)
182188
183189data DemotedArrayConstraint
184190 = DAEq Integer
185− deriving (Generic)
191+ | DAGt Integer
192+ | DAGe Integer
193+ | DALt Integer
194+ | DALe Integer
195+ deriving (Generic, Eq, Show)
186196
187197data instance Sing (ac :: ArrayConstraint) where
188198 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)
189203
190204instance 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
191209
192210instance 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
193215
194216instance SingKind ArrayConstraint where
195217 type Demote ArrayConstraint = DemotedArrayConstraint
196218 fromSing = \case
197− 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)
198224 toSing = \case
199225 DAEq n -> case someNatVal n of
200226 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SAEq (SNat :: Sing n))
201− 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"
202240
203241data Schema
204242 = SchemaText [TextConstraint]
@@ -220,7 +258,7 @@ data DemotedSchema
220258 | DSchemaNull
221259 | DSchemaOptional DemotedSchema
222260 | DSchemaUnion [DemotedSchema]
223− deriving (Generic)
261+ deriving (Generic, Eq, Show)
224262
225263data instance Sing (schema :: Schema) where
226264 SSchemaText :: Sing tcs -> Sing ('SchemaText tcs)
@@ -325,122 +363,128 @@ type family USubsets (u :: [k]) :: Constraint where
325363 USubsets '[] = ()
326364 USubsets (h ': tl) = (USubset tl (h ': tl) (V.RImage tl (h ': tl)), USubsets tl)
327365
328−class JsonRepresentable (schema :: Schema) where
329− type ReprType schema :: *
330− data JsonRepr schema :: *
331− wrap :: ReprType schema -> JsonRepr schema
332−
333−instance JsonRepresentable ('SchemaText cs) where
334− type ReprType ('SchemaText cs) = Text
335− newtype JsonRepr ('SchemaText cs) = ReprText (ReprType ('SchemaText cs))
336− wrap = ReprText
337−
338−instance JsonRepresentable ('SchemaNumber cs) where
339− type ReprType ('SchemaNumber cs) = Scientific
340− newtype JsonRepr ('SchemaNumber cs) = ReprNumber (ReprType ('SchemaNumber cs))
341− wrap = ReprNumber
342−
343−instance JsonRepresentable 'SchemaBoolean where
344− type ReprType 'SchemaBoolean = Bool
345− newtype JsonRepr 'SchemaBoolean = ReprBoolean (ReprType 'SchemaBoolean)
346− wrap = ReprBoolean
347−
348−instance JsonRepresentable 'SchemaNull where
349− type ReprType 'SchemaNull = ()
350− newtype JsonRepr 'SchemaNull = ReprNull (ReprType 'SchemaNull)
351− wrap = ReprNull
352−
353−instance JsonRepresentable s => JsonRepresentable ('SchemaArray cs s) where
354− type ReprType ('SchemaArray cs s) = V.Vector (JsonRepr s)
355− newtype JsonRepr ('SchemaArray cs s) = ReprArray (ReprType ('SchemaArray cs s))
356− wrap = ReprArray
357−
358−instance JsonRepresentable ('SchemaObject fs) where
359− type ReprType ('SchemaObject fs) = Rec FieldRepr fs
360− newtype JsonRepr ('SchemaObject fs) = ReprObject (ReprType ('SchemaObject fs))
361− wrap = ReprObject
362−
363−instance JsonRepresentable s => JsonRepresentable ('SchemaOptional s) where
364− type ReprType ('SchemaOptional s) = Maybe (JsonRepr s)
365− newtype JsonRepr ('SchemaOptional s) = ReprOptional (ReprType ('SchemaOptional s))
366− wrap = ReprOptional
367−
368−instance JsonRepresentable ('SchemaUnion (h ': tl)) where
369− type ReprType ('SchemaUnion (h ': tl)) = Union JsonRepr (h ': tl)
370− newtype JsonRepr ('SchemaUnion (h ': tl)) = ReprUnion (ReprType ('SchemaUnion (h ': tl)))
371− wrap = ReprUnion
372−
373−-- data JsonRepr :: Schema -> Type where
374−-- ReprText :: Text -> JsonRepr ('SchemaText cs)
375−-- ReprNumber :: Scientific -> JsonRepr ('SchemaNumber cs)
376−-- ReprBoolean :: Bool -> JsonRepr 'SchemaBoolean
377−-- ReprNull :: JsonRepr 'SchemaNull
378−-- ReprArray :: V.Vector (JsonRepr s) -> JsonRepr ('SchemaArray cs s)
379−-- ReprObject :: Rec FieldRepr fs -> JsonRepr ('SchemaObject fs)
380−-- ReprOptional :: Maybe (JsonRepr s) -> JsonRepr ('SchemaOptional s)
381−-- ReprUnion :: Union JsonRepr (h ': tl) -> JsonRepr ('SchemaUnion (h ': tl))
366+data JsonRepr :: Schema -> Type where
367+ ReprText :: Text -> JsonRepr ('SchemaText cs)
368+ ReprNumber :: Scientific -> JsonRepr ('SchemaNumber cs)
369+ ReprBoolean :: Bool -> JsonRepr 'SchemaBoolean
370+ ReprNull :: JsonRepr 'SchemaNull
371+ ReprArray :: V.Vector (JsonRepr s) -> JsonRepr ('SchemaArray cs s)
372+ ReprObject :: Rec FieldRepr fs -> JsonRepr ('SchemaObject fs)
373+ 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
382400
383401-- | Move to the union package
384−-- instance Show (JsonRepr ('SchemaText cs)) where
385−-- show (ReprText t) = "ReprText " P.++ show t
402+instance Show (JsonRepr ('SchemaText cs)) where
403+ show (ReprText t) = "ReprText " P.++ show t
404+
405+instance Show (JsonRepr ('SchemaNumber cs)) where
406+ show (ReprNumber n) = "ReprNumber " P.++ show n
407+
408+instance Show (JsonRepr 'SchemaBoolean) where
409+ show (ReprBoolean n) = "ReprBoolean " P.++ show n
410+
411+instance Show (JsonRepr 'SchemaNull) where show _ = "ReprNull"
412+
413+instance Show (JsonRepr s) => Show (JsonRepr ('SchemaArray acs s)) where
414+ show (ReprArray v) = "ReprArray " P.++ show v
415+
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
423+instance V.RecAll FieldRepr fs Show => Show (JsonRepr ('SchemaObject fs)) where
424+ show (ReprObject fs) = "ReprObject " P.++ show fs
425+#endif
426+
427+instance Show (JsonRepr s) => Show (JsonRepr ('SchemaOptional s)) where
428+ show (ReprOptional s) = "ReprOptional " P.++ show s
386429
387−-- instance Show (JsonRepr ('SchemaNumber cs)) where
388−-- show (ReprNumber n) = "ReprNumber " P.++ show n
430+instance Show (Union JsonRepr (h ': tl))
431+ => Show (JsonRepr ('SchemaUnion (h ': tl))) where
432+ show (ReprUnion s) = "ReprUnion " P.++ show s
389433
390−-- instance Show (JsonRepr 'SchemaNull) where show _ = "ReprNull"
434+instance Eq (Rec FieldRepr fs) => Eq (JsonRepr ('SchemaObject fs)) where
435+ ReprObject a == ReprObject b = a == b
391436
392−-- instance Show (JsonRepr s) => Show (JsonRepr ('SchemaArray acs s)) where
393−-- show (ReprArray v) = "ReprArray " P.++ show v
437+instance Eq (JsonRepr ('SchemaText cs)) where
438+ ReprText a == ReprText b = a == b
394439
395−-- instance V.RecAll FieldRepr fs Show => Show (JsonRepr ('SchemaObject fs)) where
396−-- show (ReprObject fs) = "ReprObject " P.++ show fs
440+instance Eq (JsonRepr ('SchemaNumber cs)) where
441+ ReprNumber a == ReprNumber b = a == b
397442
398−-- instance Show (JsonRepr s) => Show (JsonRepr ('SchemaOptional s)) where
399−-- show (ReprOptional s) = "ReprOptional " P.++ show s
443+instance Eq (JsonRepr 'SchemaBoolean) where
444+ ReprBoolean a == ReprBoolean b = a == b
400445
401−-- instance (Monad m, Serial m Text)
402−-- => Serial m (JsonRepr ('SchemaText cs)) where
403−-- series = cons1 ReprText
446+instance Eq (JsonRepr 'SchemaNull) where
447+ ReprNull == ReprNull = True
404448
405−-- instance (Monad m, Serial m Scientific)
406−-- => Serial m (JsonRepr ('SchemaNumber cs)) where
407−-- series = cons1 ReprNumber
449+instance Eq (JsonRepr s) => Eq (JsonRepr ('SchemaArray as s)) where
450+ ReprArray a == ReprArray b = a == b
408451
409−-- instance Monad m => Serial m (JsonRepr 'SchemaNull) where
410−-- series = cons0 ReprNull
452+instance Eq (JsonRepr s) => Eq (JsonRepr ('SchemaOptional s)) where
453+ ReprOptional a == ReprOptional b = a == b
411454
412−-- instance (Serial m (V.Vector (JsonRepr s)))
413−-- => Serial m (JsonRepr ('SchemaArray cs s)) where
414−-- series = cons1 ReprArray
455+instance Eq (Union JsonRepr (h ': tl))
456+ => Eq (JsonRepr ('SchemaUnion (h ': tl))) where
457+ ReprUnion a == ReprUnion b = a == b
415458
416−-- instance (Serial m (JsonRepr s))
417−-- => Serial m (JsonRepr ('SchemaOptional s)) where
418−-- series = cons1 ReprOptional
459+instance Ord (Rec FieldRepr fs) => Ord (JsonRepr ('SchemaObject fs)) where
460+ ReprObject a `compare` ReprObject b = a `compare` b
419461
420−-- instance (Monad m, Serial m (Rec FieldRepr fs))
421−-- => Serial m (JsonRepr ('SchemaObject fs)) where
422−-- series = cons1 ReprObject
462+instance Ord (JsonRepr ('SchemaText cs)) where
463+ ReprText a `compare` ReprText b = a `compare` b
423464
424−-- instance Eq (Rec FieldRepr fs) => Eq (JsonRepr ('SchemaObject fs)) where
425−-- ReprObject a == ReprObject b = a == b
465+instance Ord (JsonRepr ('SchemaNumber cs)) where
466+ ReprNumber a `compare` ReprNumber b = a `compare` b
426467
427−-- instance Eq (JsonRepr ('SchemaText cs))
468+instance Ord (JsonRepr 'SchemaBoolean) where
469+ ReprBoolean a `compare` ReprBoolean b = a `compare` b
428470
429−-- instance Eq (JsonRepr ('SchemaNumber cs))
471+instance Ord (JsonRepr 'SchemaNull) where
472+ compare _ _ = EQ
430473
431−-- instance Eq (JsonRepr 'SchemaNull) where
432−-- ReprNull == ReprNull = True
474+instance Ord (JsonRepr s) => Ord (JsonRepr ('SchemaArray as s)) where
475+ ReprArray a `compare` ReprArray b = a `compare` b
433476
434−-- instance Eq (JsonRepr s) => Eq (JsonRepr ('SchemaArray as s)) where
435−-- ReprArray a == ReprArray b = a == b
477+instance Ord (JsonRepr s) => Ord (JsonRepr ('SchemaOptional s)) where
478+ ReprOptional a `compare` ReprOptional b = a `compare` b
436479
437−-- instance Eq (JsonRepr s) => Eq (JsonRepr ('SchemaOptional s)) where
438−-- ReprOptional a == ReprOptional b = a == b
480+instance Ord (Union JsonRepr (h ': tl))
481+ => Ord (JsonRepr ('SchemaUnion (h ': tl))) where
482+ ReprUnion a `compare` ReprUnion b = a `compare` b
439483
440484instance IsList (JsonRepr ('SchemaArray cs s)) where
441485 type Item (JsonRepr ('SchemaArray cs s)) = JsonRepr s
442− fromList = GHC.Exts.fromList
443− toList v = GHC.Exts.toList v
486+ fromList = ReprArray . GHC.Exts.fromList
487+ toList (ReprArray v) = GHC.Exts.toList v
444488
445489instance Num (JsonRepr ('SchemaNumber cs)) where
446490 ReprNumber a + ReprNumber b = ReprNumber $ a + b
@@ -451,7 +495,7 @@ instance Num (JsonRepr ('SchemaNumber cs)) where
451495 fromInteger = ReprNumber . fromIntegral
452496
453497instance IsString (JsonRepr ('SchemaText cs)) where
454− fromString = fromString
498+ fromString = ReprText . fromString
455499
456500fromOptional
457501 :: SingI s
@@ -484,7 +528,7 @@ instance SingI schema => J.FromJSON (JsonRepr schema) where
484528 SSchemaNumber _ -> withScientific "Number" (pure . ReprNumber) value
485529 SSchemaBoolean -> ReprBoolean <$> parseJSON value
486530 SSchemaNull -> case value of
487− J.Null -> pure (wrap ())
531+ J.Null -> pure ReprNull
488532 _ -> typeMismatch "Null" value
489533 so@(SSchemaOptional s) -> withSingI s $ ReprOptional <$> fromOptional so value
490534 SSchemaArray sa sb -> withSingI sa $ withSingI sb
@@ -498,37 +542,37 @@ instance SingI schema => J.FromJSON (JsonRepr schema) where
498542 fieldRepr <- case s of
499543 SSchemaText so -> case H.lookup fieldName h of
500544 Just v -> withSingI so $ FieldRepr <$> parseJSON v
501− Nothing -> fail "schematext"
545+ Nothing -> fail $ "No text field: " P.++ show fieldName
502546 SSchemaNumber so -> case H.lookup fieldName h of
503547 Just v -> withSingI so $ FieldRepr <$> parseJSON v
504− Nothing -> fail "schemanumber"
548+ Nothing -> fail $ "No number field: " P.++ show fieldName
505549 SSchemaBoolean -> case H.lookup fieldName h of
506550 Just v -> FieldRepr <$> parseJSON v
507− Nothing -> fail "schemaboolean"
551+ Nothing -> fail $ "No boolean field: " P.++ show fieldName
508552 SSchemaNull -> case H.lookup fieldName h of
509553 Just v -> FieldRepr <$> parseJSON v
510− Nothing -> fail "schemanull"
554+ Nothing -> fail $ "No null field: " P.++ show fieldName
511555 SSchemaArray sa sb -> case H.lookup fieldName h of
512556 Just v -> withSingI sa $ withSingI sb $ FieldRepr <$> parseJSON v
513− Nothing -> fail "schemaarray"
557+ Nothing -> fail $ "No array field: " P.++ show fieldName
514558 SSchemaObject so -> case H.lookup fieldName h of
515559 Just v -> withSingI so $ FieldRepr <$> parseJSON v
516− Nothing -> fail "schemaobject"
560+ Nothing -> fail $ "No object field" P.++ show fieldName
517561 SSchemaOptional so -> case H.lookup fieldName h of
518562 Just v -> withSingI so $ FieldRepr <$> parseJSON v
519− Nothing -> fail "schemaoptional"
563+ Nothing -> withSingI so $ pure $ FieldRepr $ ReprOptional Nothing
520564 SSchemaUnion ss -> withSingI ss $ FieldRepr <$> parseUnion ss value
521565 (fieldRepr :&) <$> demoteFields tl h
522566 ReprObject <$> withObject "Object" (demoteFields fs) value
523567 SSchemaUnion ss -> parseUnion ss value
524568
525−instance J.ToJSON (JsonRepr schema) where
526− toJSON (ReprNull _) = J.Null
569+instance J.ToJSON (JsonRepr a) where
570+ toJSON ReprNull = J.Null
527571 toJSON (ReprBoolean b) = J.Bool b
528572 toJSON (ReprText t) = J.String t
529573 toJSON (ReprNumber n) = J.Number n
530574 toJSON (ReprOptional s) = case s of
531− Just v -> toJSON v
575+ Just v -> toJSON v
532576 Nothing -> J.Null
533577 toJSON (ReprArray v) = J.Array $ toJSON <$> v
534578 toJSON (ReprObject r) = J.Object . H.fromList . fold $ r