@@ -1,33 +1,28 @@
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
87
9−import Control.Applicative ((<|>))
8+import Control.Applicative ()
109import Control.Monad
1110import Data.Aeson as J
1211import Data.Aeson.Types as J
1312import Data.HashMap.Strict as H
1413import Data.Kind
1514import Data.Maybe
16−import Data.Schematic.Generator
1715import Data.Schematic.Instances ()
1816import Data.Scientific
19−import Data.Singletons.Prelude.List hiding (All, Union)
17+import Data.Singletons.Prelude.List hiding (All)
2018import Data.Singletons.TH
2119import Data.Singletons.TypeLits
2220import Data.Text as T
23−import Data.Union
2421import Data.Vector as V
2522import Data.Vinyl hiding (Dict)
2623import qualified Data.Vinyl.TypeLevel as V
27−import GHC.Exts
2824import GHC.Generics (Generic)
29−import GHC.TypeLits
30− (SomeNat(..), SomeSymbol(..), someNatVal, someSymbolVal)
25+import GHC.TypeLits (SomeNat(..), SomeSymbol(..), someSymbolVal, someNatVal)
3126import Prelude as P
3227import Test.SmallCheck.Series
3328
@@ -49,38 +44,38 @@ data TextConstraint
4944 deriving (Generic)
5045
5146instance SingKind TextConstraint where
52− type Demote TextConstraint = DemotedTextConstraint
47+ type DemoteRep TextConstraint = DemotedTextConstraint
5348 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)
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)
5954 STRegex s -> withKnownSymbol s (DTRegex $ T.pack $ symbolVal s)
6055 STEnum s -> let
6156 d :: Sing (s :: [Symbol]) -> [Text]
62− d SNil = []
57+ d SNil = []
6358 d (SCons ss@SSym fs) = T.pack (symbolVal ss) : d fs
6459 in DTEnum $ d s
6560 toSing = \case
6661 DTEq n -> case someNatVal n of
6762 Just (SomeNat (_ :: Proxy n)) -> SomeSing (STEq (SNat :: Sing n))
68− Nothing -> error "Negative singleton nat"
63+ Nothing -> error "Negative singleton nat"
6964 DTLt n -> case someNatVal n of
7065 Just (SomeNat (_ :: Proxy n)) -> SomeSing (STLt (SNat :: Sing n))
71− Nothing -> error "Negative singleton nat"
66+ Nothing -> error "Negative singleton nat"
7267 DTLe n -> case someNatVal n of
7368 Just (SomeNat (_ :: Proxy n)) -> SomeSing (STLe (SNat :: Sing n))
74− Nothing -> error "Negative singleton nat"
69+ Nothing -> error "Negative singleton nat"
7570 DTGt n -> case someNatVal n of
7671 Just (SomeNat (_ :: Proxy n)) -> SomeSing (STGt (SNat :: Sing n))
77− Nothing -> error "Negative singleton nat"
72+ Nothing -> error "Negative singleton nat"
7873 DTGe n -> case someNatVal n of
7974 Just (SomeNat (_ :: Proxy n)) -> SomeSing (STGe (SNat :: Sing n))
80− Nothing -> error "Negative singleton nat"
75+ Nothing -> error "Negative singleton nat"
8176 DTRegex s -> case someSymbolVal (T.unpack s) of
8277 SomeSymbol (_ :: Proxy n) -> SomeSing (STRegex (SSym :: Sing n))
83− DTEnum ss -> case toSing ss of
78+ DTEnum ss -> case toSing (T.unpack <$> ss) of
8479 SomeSing l -> SomeSing (STEnum l)
8580
8681data DemotedTextConstraint
@@ -91,7 +86,7 @@ data DemotedTextConstraint
9186 | DTGe Integer
9287 | DTRegex Text
9388 | DTEnum [Text]
94− deriving (Generic, Eq, Show)
89+ deriving (Generic)
9590
9691data instance Sing (tc :: TextConstraint) where
9792 STEq :: Sing n -> Sing ('TEq n)
@@ -132,7 +127,7 @@ data DemotedNumberConstraint
132127 | DNGt Integer
133128 | DNGe Integer
134129 | DNEq Integer
135− deriving (Generic, Eq, Show)
130+ deriving (Generic)
136131
137132data instance Sing (nc :: NumberConstraint) where
138133 SNEq :: Sing n -> Sing ('NEq n)
@@ -154,89 +149,53 @@ instance Eq (Sing ('NGt n)) where _ == _ = True
154149instance Eq (Sing ('NGe n)) where _ == _ = True
155150
156151instance SingKind NumberConstraint where
157− type Demote NumberConstraint = DemotedNumberConstraint
152+ type DemoteRep NumberConstraint = DemotedNumberConstraint
158153 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)
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)
164159 toSing = \case
165160 DNEq n -> case someNatVal n of
166161 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNEq (SNat :: Sing n))
167− Nothing -> error "Negative singleton nat"
162+ Nothing -> error "Negative singleton nat"
168163 DNGt n -> case someNatVal n of
169164 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNGt (SNat :: Sing n))
170− Nothing -> error "Negative singleton nat"
165+ Nothing -> error "Negative singleton nat"
171166 DNGe n -> case someNatVal n of
172167 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNGe (SNat :: Sing n))
173− Nothing -> error "Negative singleton nat"
168+ Nothing -> error "Negative singleton nat"
174169 DNLt n -> case someNatVal n of
175170 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNLt (SNat :: Sing n))
176− Nothing -> error "Negative singleton nat"
171+ Nothing -> error "Negative singleton nat"
177172 DNLe n -> case someNatVal n of
178173 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNLe (SNat :: Sing n))
179− Nothing -> error "Negative singleton nat"
174+ Nothing -> error "Negative singleton nat"
180175
181176data ArrayConstraint
182177 = AEq Nat
183− | AGt Nat
184− | AGe Nat
185− | ALt Nat
186− | ALe Nat
187178 deriving (Generic)
188179
189180data DemotedArrayConstraint
190181 = DAEq Integer
191− | DAGt Integer
192− | DAGe Integer
193− | DALt Integer
194− | DALe Integer
195− deriving (Generic, Eq, Show)
182+ deriving (Generic)
196183
197184data instance Sing (ac :: ArrayConstraint) where
198185 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)
203186
204187instance 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
209188
210189instance 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
215190
216191instance SingKind ArrayConstraint where
217− type Demote ArrayConstraint = DemotedArrayConstraint
192+ type DemoteRep ArrayConstraint = DemotedArrayConstraint
218193 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)
194+ SAEq n -> withKnownNat n (DAEq $ natVal n)
224195 toSing = \case
225196 DAEq n -> case someNatVal n of
226197 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"
198+ Nothing -> error "Negative singleton nat"
240199
241200data Schema
242201 = SchemaText [TextConstraint]
@@ -246,7 +205,6 @@ data Schema
246205 | SchemaArray [ArrayConstraint] Schema
247206 | SchemaNull
248207 | SchemaOptional Schema
249− | SchemaUnion [Schema]
250208 deriving (Generic)
251209
252210data DemotedSchema
@@ -257,8 +215,7 @@ data DemotedSchema
257215 | DSchemaArray [DemotedArrayConstraint] DemotedSchema
258216 | DSchemaNull
259217 | DSchemaOptional DemotedSchema
260− | DSchemaUnion [DemotedSchema]
261− deriving (Generic, Eq, Show)
218+ deriving (Generic)
262219
263220data instance Sing (schema :: Schema) where
264221 SSchemaText :: Sing tcs -> Sing ('SchemaText tcs)
@@ -268,7 +225,6 @@ data instance Sing (schema :: Schema) where
268225 SSchemaObject :: Sing fields -> Sing ('SchemaObject fields)
269226 SSchemaOptional :: Sing s -> Sing ('SchemaOptional s)
270227 SSchemaNull :: Sing 'SchemaNull
271− SSchemaUnion :: Sing ss -> Sing ('SchemaUnion ss)
272228
273229instance SingI sl => SingI ('SchemaText sl) where
274230 sing = SSchemaText sing
@@ -284,8 +240,6 @@ instance SingI stl => SingI ('SchemaObject stl) where
284240 sing = SSchemaObject sing
285241instance SingI s => SingI ('SchemaOptional s) where
286242 sing = SSchemaOptional sing
287−instance SingI s => SingI ('SchemaUnion s) where
288− sing = SSchemaUnion sing
289243
290244instance Eq (Sing ('SchemaText cs)) where _ == _ = True
291245instance Eq (Sing ('SchemaNumber cs)) where _ == _ = True
@@ -294,10 +248,9 @@ instance Eq (Sing 'SchemaBoolean) where _ == _ = True
294248instance Eq (Sing ('SchemaArray as s)) where _ == _ = True
295249instance Eq (Sing ('SchemaObject cs)) where _ == _ = True
296250instance Eq (Sing ('SchemaOptional s)) where _ == _ = True
297−instance Eq (Sing ('SchemaUnion s)) where _ == _ = True
298251
299252instance SingKind Schema where
300− type Demote Schema = DemotedSchema
253+ type DemoteRep Schema = DemotedSchema
301254 fromSing = \case
302255 SSchemaText cs -> DSchemaText $ fromSing cs
303256 SSchemaNumber cs -> DSchemaNumber $ fromSing cs
@@ -305,8 +258,12 @@ instance SingKind Schema where
305258 SSchemaArray cs s -> DSchemaArray (fromSing cs) (fromSing s)
306259 SSchemaOptional s -> DSchemaOptional $ fromSing s
307260 SSchemaNull -> DSchemaNull
308− SSchemaObject cs -> DSchemaObject $ fromSing cs
309− SSchemaUnion ss -> DSchemaUnion $ fromSing ss
261+ SSchemaObject cs -> let
262+ dem :: Sing (s :: [(Symbol, Schema)]) -> [(Text, DemotedSchema)]
263+ dem SNil = []
264+ dem (SCons (STuple2 ss ssch) fs) = withKnownSymbol ss
265+ $ (T.pack (symbolVal ss), fromSing ssch) : dem fs
266+ in DSchemaObject $ dem cs
310267 toSing = \case
311268 DSchemaText cs -> case toSing cs of
312269 SomeSing scs -> SomeSing $ SSchemaText scs
@@ -318,10 +275,8 @@ instance SingKind Schema where
318275 DSchemaOptional sch -> case toSing sch of
319276 SomeSing ssch -> SomeSing $ SSchemaOptional ssch
320277 DSchemaNull -> SomeSing SSchemaNull
321− DSchemaObject cs -> case toSing cs of
278+ DSchemaObject cs -> case toSing ((\(sym,sch) -> (T.unpack sym, sch)) <$> cs) of
322279 SomeSing scs -> SomeSing $ SSchemaObject scs
323− DSchemaUnion ss -> case toSing ss of
324− SomeSing sss -> SomeSing $ SSchemaUnion sss
325280
326281data FieldRepr :: (Symbol, Schema) -> Type where
327282 FieldRepr
@@ -359,10 +314,6 @@ instance
359314 => Serial m (FieldRepr '(name, schema)) where
360315 series = FieldRepr <$> series
361316
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−
366317data JsonRepr :: Schema -> Type where
367318 ReprText :: Text -> JsonRepr ('SchemaText cs)
368319 ReprNumber :: Scientific -> JsonRepr ('SchemaNumber cs)
@@ -371,65 +322,46 @@ data JsonRepr :: Schema -> Type where
371322 ReprArray :: V.Vector (JsonRepr s) -> JsonRepr ('SchemaArray cs s)
372323 ReprObject :: Rec FieldRepr fs -> JsonRepr ('SchemaObject fs)
373324 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)
379325
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
400−
401−-- | Move to the union package
402326instance Show (JsonRepr ('SchemaText cs)) where
403327 show (ReprText t) = "ReprText " P.++ show t
404328
405329instance Show (JsonRepr ('SchemaNumber cs)) where
406330 show (ReprNumber n) = "ReprNumber " P.++ show n
407331
408−instance Show (JsonRepr 'SchemaBoolean) where
409− show (ReprBoolean n) = "ReprBoolean " P.++ show n
410−
411332instance Show (JsonRepr 'SchemaNull) where show _ = "ReprNull"
412333
413334instance Show (JsonRepr s) => Show (JsonRepr ('SchemaArray acs s)) where
414335 show (ReprArray v) = "ReprArray " P.++ show v
415336
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
423337instance V.RecAll FieldRepr fs Show => Show (JsonRepr ('SchemaObject fs)) where
424338 show (ReprObject fs) = "ReprObject " P.++ show fs
425−#endif
426339
427340instance Show (JsonRepr s) => Show (JsonRepr ('SchemaOptional s)) where
428341 show (ReprOptional s) = "ReprOptional " P.++ show s
429342
430−instance Show (Union JsonRepr (h ': tl))
431− => Show (JsonRepr ('SchemaUnion (h ': tl))) where
432− show (ReprUnion s) = "ReprUnion " P.++ show s
343+instance (Monad m, Serial m Text)
344+ => Serial m (JsonRepr ('SchemaText cs)) where
345+ series = cons1 ReprText
346+
347+instance (Monad m, Serial m Scientific)
348+ => Serial m (JsonRepr ('SchemaNumber cs)) where
349+ series = cons1 ReprNumber
350+
351+instance Monad m => Serial m (JsonRepr 'SchemaNull) where
352+ series = cons0 ReprNull
353+
354+instance (Serial m (V.Vector (JsonRepr s)))
355+ => Serial m (JsonRepr ('SchemaArray cs s)) where
356+ series = cons1 ReprArray
357+
358+instance (Serial m (JsonRepr s))
359+ => Serial m (JsonRepr ('SchemaOptional s)) where
360+ series = cons1 ReprOptional
361+
362+instance (Monad m, Serial m (Rec FieldRepr fs))
363+ => Serial m (JsonRepr ('SchemaObject fs)) where
364+ series = cons1 ReprObject
433365
434366instance Eq (Rec FieldRepr fs) => Eq (JsonRepr ('SchemaObject fs)) where
435367 ReprObject a == ReprObject b = a == b
@@ -440,9 +372,6 @@ instance Eq (JsonRepr ('SchemaText cs)) where
440372instance Eq (JsonRepr ('SchemaNumber cs)) where
441373 ReprNumber a == ReprNumber b = a == b
442374
443−instance Eq (JsonRepr 'SchemaBoolean) where
444− ReprBoolean a == ReprBoolean b = a == b
445−
446375instance Eq (JsonRepr 'SchemaNull) where
447376 ReprNull == ReprNull = True
448377
@@ -452,51 +381,6 @@ instance Eq (JsonRepr s) => Eq (JsonRepr ('SchemaArray as s)) where
452381instance Eq (JsonRepr s) => Eq (JsonRepr ('SchemaOptional s)) where
453382 ReprOptional a == ReprOptional b = a == b
454383
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−
500384fromOptional
501385 :: SingI s
502386 => Sing ('SchemaOptional s)
@@ -504,24 +388,6 @@ fromOptional
504388 -> Parser (Maybe (JsonRepr s))
505389fromOptional _ = parseJSON
506390
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−
525391instance SingI schema => J.FromJSON (JsonRepr schema) where
526392 parseJSON value = case sing :: Sing schema of
527393 SSchemaText _ -> withText "String" (pure . ReprText) value
@@ -542,29 +408,27 @@ instance SingI schema => J.FromJSON (JsonRepr schema) where
542408 fieldRepr <- case s of
543409 SSchemaText so -> case H.lookup fieldName h of
544410 Just v -> withSingI so $ FieldRepr <$> parseJSON v
545− Nothing -> fail $ "No text field: " P.++ show fieldName
411+ Nothing -> fail "schematext"
546412 SSchemaNumber so -> case H.lookup fieldName h of
547413 Just v -> withSingI so $ FieldRepr <$> parseJSON v
548− Nothing -> fail $ "No number field: " P.++ show fieldName
414+ Nothing -> fail "schemanumber"
549415 SSchemaBoolean -> case H.lookup fieldName h of
550416 Just v -> FieldRepr <$> parseJSON v
551− Nothing -> fail $ "No boolean field: " P.++ show fieldName
417+ Nothing -> fail "schemaboolean"
552418 SSchemaNull -> case H.lookup fieldName h of
553419 Just v -> FieldRepr <$> parseJSON v
554− Nothing -> fail $ "No null field: " P.++ show fieldName
420+ Nothing -> fail "schemanull"
555421 SSchemaArray sa sb -> case H.lookup fieldName h of
556422 Just v -> withSingI sa $ withSingI sb $ FieldRepr <$> parseJSON v
557− Nothing -> fail $ "No array field: " P.++ show fieldName
423+ Nothing -> fail "schemaarray"
558424 SSchemaObject so -> case H.lookup fieldName h of
559425 Just v -> withSingI so $ FieldRepr <$> parseJSON v
560− Nothing -> fail $ "No object field" P.++ show fieldName
426+ Nothing -> fail "schemaobject"
561427 SSchemaOptional so -> case H.lookup fieldName h of
562428 Just v -> withSingI so $ FieldRepr <$> parseJSON v
563− Nothing -> withSingI so $ pure $ FieldRepr $ ReprOptional Nothing
564− SSchemaUnion ss -> withSingI ss $ FieldRepr <$> parseUnion ss value
429+ Nothing -> fail "schemaoptional"
565430 (fieldRepr :&) <$> demoteFields tl h
566431 ReprObject <$> withObject "Object" (demoteFields fs) value
567− SSchemaUnion ss -> parseUnion ss value
568432
569433instance J.ToJSON (JsonRepr a) where
570434 toJSON ReprNull = J.Null
@@ -572,7 +436,7 @@ instance J.ToJSON (JsonRepr a) where
572436 toJSON (ReprText t) = J.String t
573437 toJSON (ReprNumber n) = J.Number n
574438 toJSON (ReprOptional s) = case s of
575− Just v -> toJSON v
439+ Just v -> toJSON v
576440 Nothing -> J.Null
577441 toJSON (ReprArray v) = J.Array $ toJSON <$> v
578442 toJSON (ReprObject r) = J.Object . H.fromList . fold $ r
@@ -585,7 +449,6 @@ instance J.ToJSON (JsonRepr a) where
585449 fold = \case
586450 RNil -> []
587451 fr@(FieldRepr _) :& tl -> (extract fr) : fold tl
588− toJSON (ReprUnion u) = toJSON u
589452
590453class FalseConstraint a
591454