@@ -1,7 +1,6 @@
11{-# LANGUAGE AllowAmbiguousTypes #-}
2−{-# LANGUAGE CPP #-}
3−{-# LANGUAGE OverloadedLists #-}
4−{-# LANGUAGE OverloadedStrings #-}
2+{-# LANGUAGE OverloadedLists #-}
3+{-# LANGUAGE OverloadedStrings #-}
54{-# OPTIONS_GHC -fprint-explicit-kinds #-}
65
76module Data.Schematic.Schema where
@@ -10,10 +9,10 @@ import Control.Applicative ((<|>))
109import Control.Monad
1110import Data.Aeson as J
1211import 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
1716import Data.Schematic.Instances ()
1817import Data.Scientific
1918import Data.Singletons.Prelude.List hiding (All, Union)
@@ -26,8 +25,7 @@ import Data.Vinyl hiding (Dict)
2625import qualified Data.Vinyl.TypeLevel as V
2726import GHC.Exts
2827import GHC.Generics (Generic)
29−import GHC.TypeLits
30− (SomeNat(..), SomeSymbol(..), someNatVal, someSymbolVal)
28+import GHC.TypeLits (SomeNat(..), SomeSymbol(..), someSymbolVal, someNatVal)
3129import Prelude as P
3230import Test.SmallCheck.Series
3331
@@ -51,33 +49,33 @@ data TextConstraint
5149instance SingKind TextConstraint where
5250 type Demote TextConstraint = DemotedTextConstraint
5351 fromSing = \case
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)
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)
5957 STRegex s -> withKnownSymbol s (DTRegex $ T.pack $ symbolVal s)
6058 STEnum s -> let
6159 d :: Sing (s :: [Symbol]) -> [Text]
62− d SNil = []
60+ d SNil = []
6361 d (SCons ss@SSym fs) = T.pack (symbolVal ss) : d fs
6462 in DTEnum $ d s
6563 toSing = \case
6664 DTEq n -> case someNatVal n of
6765 Just (SomeNat (_ :: Proxy n)) -> SomeSing (STEq (SNat :: Sing n))
68− Nothing -> error "Negative singleton nat"
66+ Nothing -> error "Negative singleton nat"
6967 DTLt n -> case someNatVal n of
7068 Just (SomeNat (_ :: Proxy n)) -> SomeSing (STLt (SNat :: Sing n))
71− Nothing -> error "Negative singleton nat"
69+ Nothing -> error "Negative singleton nat"
7270 DTLe n -> case someNatVal n of
7371 Just (SomeNat (_ :: Proxy n)) -> SomeSing (STLe (SNat :: Sing n))
74− Nothing -> error "Negative singleton nat"
72+ Nothing -> error "Negative singleton nat"
7573 DTGt n -> case someNatVal n of
7674 Just (SomeNat (_ :: Proxy n)) -> SomeSing (STGt (SNat :: Sing n))
77− Nothing -> error "Negative singleton nat"
75+ Nothing -> error "Negative singleton nat"
7876 DTGe n -> case someNatVal n of
7977 Just (SomeNat (_ :: Proxy n)) -> SomeSing (STGe (SNat :: Sing n))
80− Nothing -> error "Negative singleton nat"
78+ Nothing -> error "Negative singleton nat"
8179 DTRegex s -> case someSymbolVal (T.unpack s) of
8280 SomeSymbol (_ :: Proxy n) -> SomeSing (STRegex (SSym :: Sing n))
8381 DTEnum ss -> case toSing ss of
@@ -91,7 +89,7 @@ data DemotedTextConstraint
9189 | DTGe Integer
9290 | DTRegex Text
9391 | DTEnum [Text]
94− deriving (Generic, Eq, Show)
92+ deriving (Generic)
9593
9694data instance Sing (tc :: TextConstraint) where
9795 STEq :: Sing n -> Sing ('TEq n)
@@ -132,7 +130,7 @@ data DemotedNumberConstraint
132130 | DNGt Integer
133131 | DNGe Integer
134132 | DNEq Integer
135− deriving (Generic, Eq, Show)
133+ deriving (Generic)
136134
137135data instance Sing (nc :: NumberConstraint) where
138136 SNEq :: Sing n -> Sing ('NEq n)
@@ -156,87 +154,51 @@ instance Eq (Sing ('NGe n)) where _ == _ = True
156154instance SingKind NumberConstraint where
157155 type Demote NumberConstraint = DemotedNumberConstraint
158156 fromSing = \case
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)
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)
164162 toSing = \case
165163 DNEq n -> case someNatVal n of
166164 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNEq (SNat :: Sing n))
167− Nothing -> error "Negative singleton nat"
165+ Nothing -> error "Negative singleton nat"
168166 DNGt n -> case someNatVal n of
169167 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNGt (SNat :: Sing n))
170− Nothing -> error "Negative singleton nat"
168+ Nothing -> error "Negative singleton nat"
171169 DNGe n -> case someNatVal n of
172170 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNGe (SNat :: Sing n))
173− Nothing -> error "Negative singleton nat"
171+ Nothing -> error "Negative singleton nat"
174172 DNLt n -> case someNatVal n of
175173 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNLt (SNat :: Sing n))
176− Nothing -> error "Negative singleton nat"
174+ Nothing -> error "Negative singleton nat"
177175 DNLe n -> case someNatVal n of
178176 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNLe (SNat :: Sing n))
179− Nothing -> error "Negative singleton nat"
177+ Nothing -> error "Negative singleton nat"
180178
181179data ArrayConstraint
182180 = AEq Nat
183− | AGt Nat
184− | AGe Nat
185− | ALt Nat
186− | ALe Nat
187181 deriving (Generic)
188182
189183data DemotedArrayConstraint
190184 = DAEq Integer
191− | DAGt Integer
192− | DAGe Integer
193− | DALt Integer
194− | DALe Integer
195− deriving (Generic, Eq, Show)
185+ deriving (Generic)
196186
197187data instance Sing (ac :: ArrayConstraint) where
198188 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)
203189
204190instance 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
209191
210192instance 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
215193
216194instance SingKind ArrayConstraint where
217195 type Demote ArrayConstraint = DemotedArrayConstraint
218196 fromSing = \case
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)
197+ SAEq n -> withKnownNat n (DAEq $ natVal n)
224198 toSing = \case
225199 DAEq n -> case someNatVal n of
226200 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"
201+ Nothing -> error "Negative singleton nat"
240202
241203data Schema
242204 = SchemaText [TextConstraint]
@@ -258,7 +220,7 @@ data DemotedSchema
258220 | DSchemaNull
259221 | DSchemaOptional DemotedSchema
260222 | DSchemaUnion [DemotedSchema]
261− deriving (Generic, Eq, Show)
223+ deriving (Generic)
262224
263225data instance Sing (schema :: Schema) where
264226 SSchemaText :: Sing tcs -> Sing ('SchemaText tcs)
@@ -363,128 +325,122 @@ type family USubsets (u :: [k]) :: Constraint where
363325 USubsets '[] = ()
364326 USubsets (h ': tl) = (USubset tl (h ': tl) (V.RImage tl (h ': tl)), USubsets tl)
365327
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
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))
400382
401383-- | Move to the union package
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
384+-- instance Show (JsonRepr ('SchemaText cs)) where
385+-- show (ReprText t) = "ReprText " P.++ show t
429386
430−instance Show (Union JsonRepr (h ': tl))
431− => Show (JsonRepr ('SchemaUnion (h ': tl))) where
432− show (ReprUnion s) = "ReprUnion " P.++ show s
387+-- instance Show (JsonRepr ('SchemaNumber cs)) where
388+-- show (ReprNumber n) = "ReprNumber " P.++ show n
433389
434−instance Eq (Rec FieldRepr fs) => Eq (JsonRepr ('SchemaObject fs)) where
435− ReprObject a == ReprObject b = a == b
390+-- instance Show (JsonRepr 'SchemaNull) where show _ = "ReprNull"
436391
437−instance Eq (JsonRepr ('SchemaText cs)) where
438− ReprText a == ReprText b = a == b
392+-- instance Show (JsonRepr s) => Show (JsonRepr ('SchemaArray acs s)) where
393+-- show (ReprArray v) = "ReprArray " P.++ show v
439394
440−instance Eq (JsonRepr ('SchemaNumber cs)) where
441− ReprNumber a == ReprNumber b = a == b
395+-- instance V.RecAll FieldRepr fs Show => Show (JsonRepr ('SchemaObject fs)) where
396+-- show (ReprObject fs) = "ReprObject " P.++ show fs
442397
443−instance Eq (JsonRepr 'SchemaBoolean) where
444− ReprBoolean a == ReprBoolean b = a == b
398+-- instance Show (JsonRepr s) => Show (JsonRepr ('SchemaOptional s)) where
399+-- show (ReprOptional s) = "ReprOptional " P.++ show s
445400
446−instance Eq (JsonRepr 'SchemaNull) where
447− ReprNull == ReprNull = True
401+-- instance (Monad m, Serial m Text)
402+-- => Serial m (JsonRepr ('SchemaText cs)) where
403+-- series = cons1 ReprText
448404
449−instance Eq (JsonRepr s) => Eq (JsonRepr ('SchemaArray as s)) where
450− ReprArray a == ReprArray b = a == b
405+-- instance (Monad m, Serial m Scientific)
406+-- => Serial m (JsonRepr ('SchemaNumber cs)) where
407+-- series = cons1 ReprNumber
451408
452−instance Eq (JsonRepr s) => Eq (JsonRepr ('SchemaOptional s)) where
453− ReprOptional a == ReprOptional b = a == b
409+-- instance Monad m => Serial m (JsonRepr 'SchemaNull) where
410+-- series = cons0 ReprNull
454411
455−instance Eq (Union JsonRepr (h ': tl))
456− => Eq (JsonRepr ('SchemaUnion (h ': tl))) where
457− ReprUnion a == ReprUnion b = a == b
412+-- instance (Serial m (V.Vector (JsonRepr s)))
413+-- => Serial m (JsonRepr ('SchemaArray cs s)) where
414+-- series = cons1 ReprArray
458415
459−instance Ord (Rec FieldRepr fs) => Ord (JsonRepr ('SchemaObject fs)) where
460− ReprObject a `compare` ReprObject b = a `compare` b
416+-- instance (Serial m (JsonRepr s))
417+-- => Serial m (JsonRepr ('SchemaOptional s)) where
418+-- series = cons1 ReprOptional
461419
462−instance Ord (JsonRepr ('SchemaText cs)) where
463− ReprText a `compare` ReprText b = a `compare` b
420+-- instance (Monad m, Serial m (Rec FieldRepr fs))
421+-- => Serial m (JsonRepr ('SchemaObject fs)) where
422+-- series = cons1 ReprObject
464423
465−instance Ord (JsonRepr ('SchemaNumber cs)) where
466− ReprNumber a `compare` ReprNumber b = a `compare` b
424+-- instance Eq (Rec FieldRepr fs) => Eq (JsonRepr ('SchemaObject fs)) where
425+-- ReprObject a == ReprObject b = a == b
467426
468−instance Ord (JsonRepr 'SchemaBoolean) where
469− ReprBoolean a `compare` ReprBoolean b = a `compare` b
427+-- instance Eq (JsonRepr ('SchemaText cs))
470428
471−instance Ord (JsonRepr 'SchemaNull) where
472− compare _ _ = EQ
429+-- instance Eq (JsonRepr ('SchemaNumber cs))
473430
474−instance Ord (JsonRepr s) => Ord (JsonRepr ('SchemaArray as s)) where
475− ReprArray a `compare` ReprArray b = a `compare` b
431+-- instance Eq (JsonRepr 'SchemaNull) where
432+-- ReprNull == ReprNull = True
476433
477−instance Ord (JsonRepr s) => Ord (JsonRepr ('SchemaOptional s)) where
478− ReprOptional a `compare` ReprOptional b = a `compare` b
434+-- instance Eq (JsonRepr s) => Eq (JsonRepr ('SchemaArray as s)) where
435+-- ReprArray a == ReprArray b = a == b
479436
480−instance Ord (Union JsonRepr (h ': tl))
481− => Ord (JsonRepr ('SchemaUnion (h ': tl))) where
482− ReprUnion a `compare` ReprUnion b = a `compare` b
437+-- instance Eq (JsonRepr s) => Eq (JsonRepr ('SchemaOptional s)) where
438+-- ReprOptional a == ReprOptional b = a == b
483439
484440instance IsList (JsonRepr ('SchemaArray cs s)) where
485441 type Item (JsonRepr ('SchemaArray cs s)) = JsonRepr s
486− fromList = ReprArray . GHC.Exts.fromList
487− toList (ReprArray v) = GHC.Exts.toList v
442+ fromList = GHC.Exts.fromList
443+ toList v = GHC.Exts.toList v
488444
489445instance Num (JsonRepr ('SchemaNumber cs)) where
490446 ReprNumber a + ReprNumber b = ReprNumber $ a + b
@@ -495,7 +451,7 @@ instance Num (JsonRepr ('SchemaNumber cs)) where
495451 fromInteger = ReprNumber . fromIntegral
496452
497453instance IsString (JsonRepr ('SchemaText cs)) where
498− fromString = ReprText . fromString
454+ fromString = fromString
499455
500456fromOptional
501457 :: SingI s
@@ -528,7 +484,7 @@ instance SingI schema => J.FromJSON (JsonRepr schema) where
528484 SSchemaNumber _ -> withScientific "Number" (pure . ReprNumber) value
529485 SSchemaBoolean -> ReprBoolean <$> parseJSON value
530486 SSchemaNull -> case value of
531− J.Null -> pure ReprNull
487+ J.Null -> pure (wrap ())
532488 _ -> typeMismatch "Null" value
533489 so@(SSchemaOptional s) -> withSingI s $ ReprOptional <$> fromOptional so value
534490 SSchemaArray sa sb -> withSingI sa $ withSingI sb
@@ -542,37 +498,37 @@ instance SingI schema => J.FromJSON (JsonRepr schema) where
542498 fieldRepr <- case s of
543499 SSchemaText so -> case H.lookup fieldName h of
544500 Just v -> withSingI so $ FieldRepr <$> parseJSON v
545− Nothing -> fail $ "No text field: " P.++ show fieldName
501+ Nothing -> fail "schematext"
546502 SSchemaNumber so -> case H.lookup fieldName h of
547503 Just v -> withSingI so $ FieldRepr <$> parseJSON v
548− Nothing -> fail $ "No number field: " P.++ show fieldName
504+ Nothing -> fail "schemanumber"
549505 SSchemaBoolean -> case H.lookup fieldName h of
550506 Just v -> FieldRepr <$> parseJSON v
551− Nothing -> fail $ "No boolean field: " P.++ show fieldName
507+ Nothing -> fail "schemaboolean"
552508 SSchemaNull -> case H.lookup fieldName h of
553509 Just v -> FieldRepr <$> parseJSON v
554− Nothing -> fail $ "No null field: " P.++ show fieldName
510+ Nothing -> fail "schemanull"
555511 SSchemaArray sa sb -> case H.lookup fieldName h of
556512 Just v -> withSingI sa $ withSingI sb $ FieldRepr <$> parseJSON v
557− Nothing -> fail $ "No array field: " P.++ show fieldName
513+ Nothing -> fail "schemaarray"
558514 SSchemaObject so -> case H.lookup fieldName h of
559515 Just v -> withSingI so $ FieldRepr <$> parseJSON v
560− Nothing -> fail $ "No object field" P.++ show fieldName
516+ Nothing -> fail "schemaobject"
561517 SSchemaOptional so -> case H.lookup fieldName h of
562518 Just v -> withSingI so $ FieldRepr <$> parseJSON v
563− Nothing -> withSingI so $ pure $ FieldRepr $ ReprOptional Nothing
519+ Nothing -> fail "schemaoptional"
564520 SSchemaUnion ss -> withSingI ss $ FieldRepr <$> parseUnion ss value
565521 (fieldRepr :&) <$> demoteFields tl h
566522 ReprObject <$> withObject "Object" (demoteFields fs) value
567523 SSchemaUnion ss -> parseUnion ss value
568524
569−instance J.ToJSON (JsonRepr a) where
570− toJSON ReprNull = J.Null
525+instance J.ToJSON (JsonRepr schema) where
526+ toJSON (ReprNull _) = J.Null
571527 toJSON (ReprBoolean b) = J.Bool b
572528 toJSON (ReprText t) = J.String t
573529 toJSON (ReprNumber n) = J.Number n
574530 toJSON (ReprOptional s) = case s of
575− Just v -> toJSON v
531+ Just v -> toJSON v
576532 Nothing -> J.Null
577533 toJSON (ReprArray v) = J.Array $ toJSON <$> v
578534 toJSON (ReprObject r) = J.Object . H.fromList . fold $ r