@@ -1,6 +1,7 @@
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
@@ -12,6 +13,7 @@ import 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
17import Data.Singletons.Prelude.List hiding (All, Union)
19import Data.Singletons.Prelude.List hiding (All, Union)
@@ -22,8 +24,10 @@ import Data.Union
22import Data.Vector as V
24import Data.Vector as V
23import Data.Vinyl hiding (Dict)
25import Data.Vinyl hiding (Dict)
24import qualified Data.Vinyl.TypeLevel as V
26import qualified Data.Vinyl.TypeLevel as V
25import GHC.Generics (Generic)
28import GHC.Generics (Generic)
26−import GHC.TypeLits (SomeNat(..), SomeSymbol(..), someSymbolVal, someNatVal)
30+ (SomeNat(..), SomeSymbol(..), someNatVal, someSymbolVal)
27import Prelude as P
31import Prelude as P
28import Test.SmallCheck.Series
32import Test.SmallCheck.Series
@@ -47,33 +51,33 @@ data TextConstraint
47instance SingKind TextConstraint where
51instance SingKind TextConstraint where
48 type Demote TextConstraint = DemotedTextConstraint
52 type Demote TextConstraint = DemotedTextConstraint
49 fromSing = \case
53 fromSing = \case
50− STEq n -> withKnownNat n (DTEq $ natVal n)
54+ STEq n -> withKnownNat n (DTEq . fromIntegral $ natVal n)
51− STLt n -> withKnownNat n (DTLt $ natVal n)
55+ STLt n -> withKnownNat n (DTLt . fromIntegral $ natVal n)
52− STLe n -> withKnownNat n (DTLe $ natVal n)
56+ STLe n -> withKnownNat n (DTLe . fromIntegral $ natVal n)
53− STGt n -> withKnownNat n (DTGt $ natVal n)
57+ STGt n -> withKnownNat n (DTGt . fromIntegral $ natVal n)
54− STGe n -> withKnownNat n (DTGe $ natVal n)
58+ STGe n -> withKnownNat n (DTGe . fromIntegral $ natVal n)
55 STRegex s -> withKnownSymbol s (DTRegex $ T.pack $ symbolVal s)
59 STRegex s -> withKnownSymbol s (DTRegex $ T.pack $ symbolVal s)
56 STEnum s -> let
60 STEnum s -> let
57 d :: Sing (s :: [Symbol]) -> [Text]
61 d :: Sing (s :: [Symbol]) -> [Text]
58− d SNil = []
62+ d SNil = []
59 d (SCons ss@SSym fs) = T.pack (symbolVal ss) : d fs
63 d (SCons ss@SSym fs) = T.pack (symbolVal ss) : d fs
60 in DTEnum $ d s
64 in DTEnum $ d s
61 toSing = \case
65 toSing = \case
62 DTEq n -> case someNatVal n of
66 DTEq n -> case someNatVal n of
63 Just (SomeNat (_ :: Proxy n)) -> SomeSing (STEq (SNat :: Sing n))
67 Just (SomeNat (_ :: Proxy n)) -> SomeSing (STEq (SNat :: Sing n))
64− Nothing -> error "Negative singleton nat"
68+ Nothing -> error "Negative singleton nat"
65 DTLt n -> case someNatVal n of
69 DTLt n -> case someNatVal n of
66 Just (SomeNat (_ :: Proxy n)) -> SomeSing (STLt (SNat :: Sing n))
70 Just (SomeNat (_ :: Proxy n)) -> SomeSing (STLt (SNat :: Sing n))
67− Nothing -> error "Negative singleton nat"
71+ Nothing -> error "Negative singleton nat"
68 DTLe n -> case someNatVal n of
72 DTLe n -> case someNatVal n of
69 Just (SomeNat (_ :: Proxy n)) -> SomeSing (STLe (SNat :: Sing n))
73 Just (SomeNat (_ :: Proxy n)) -> SomeSing (STLe (SNat :: Sing n))
70− Nothing -> error "Negative singleton nat"
74+ Nothing -> error "Negative singleton nat"
71 DTGt n -> case someNatVal n of
75 DTGt n -> case someNatVal n of
72 Just (SomeNat (_ :: Proxy n)) -> SomeSing (STGt (SNat :: Sing n))
76 Just (SomeNat (_ :: Proxy n)) -> SomeSing (STGt (SNat :: Sing n))
73− Nothing -> error "Negative singleton nat"
77+ Nothing -> error "Negative singleton nat"
74 DTGe n -> case someNatVal n of
78 DTGe n -> case someNatVal n of
75 Just (SomeNat (_ :: Proxy n)) -> SomeSing (STGe (SNat :: Sing n))
79 Just (SomeNat (_ :: Proxy n)) -> SomeSing (STGe (SNat :: Sing n))
76− Nothing -> error "Negative singleton nat"
80+ Nothing -> error "Negative singleton nat"
77 DTRegex s -> case someSymbolVal (T.unpack s) of
81 DTRegex s -> case someSymbolVal (T.unpack s) of
78 SomeSymbol (_ :: Proxy n) -> SomeSing (STRegex (SSym :: Sing n))
82 SomeSymbol (_ :: Proxy n) -> SomeSing (STRegex (SSym :: Sing n))
79 DTEnum ss -> case toSing ss of
83 DTEnum ss -> case toSing ss of
@@ -87,7 +91,7 @@ data DemotedTextConstraint
87 | DTGe Integer
91 | DTGe Integer
88 | DTRegex Text
92 | DTRegex Text
89 | DTEnum [Text]
93 | DTEnum [Text]
90− deriving (Generic)
94+ deriving (Generic, Eq, Show)
92data instance Sing (tc :: TextConstraint) where
96data instance Sing (tc :: TextConstraint) where
93 STEq :: Sing n -> Sing ('TEq n)
97 STEq :: Sing n -> Sing ('TEq n)
@@ -128,7 +132,7 @@ data DemotedNumberConstraint
128 | DNGt Integer
132 | DNGt Integer
129 | DNGe Integer
133 | DNGe Integer
130 | DNEq Integer
134 | DNEq Integer
131− deriving (Generic)
135+ deriving (Generic, Eq, Show)
133data instance Sing (nc :: NumberConstraint) where
137data instance Sing (nc :: NumberConstraint) where
134 SNEq :: Sing n -> Sing ('NEq n)
138 SNEq :: Sing n -> Sing ('NEq n)
@@ -152,51 +156,87 @@ instance Eq (Sing ('NGe n)) where _ == _ = True
152instance SingKind NumberConstraint where
156instance SingKind NumberConstraint where
153 type Demote NumberConstraint = DemotedNumberConstraint
157 type Demote NumberConstraint = DemotedNumberConstraint
154 fromSing = \case
158 fromSing = \case
155− SNEq n -> withKnownNat n (DNEq $ natVal n)
159+ SNEq n -> withKnownNat n (DNEq . fromIntegral $ natVal n)
156− SNGt n -> withKnownNat n (DNGt $ natVal n)
160+ SNGt n -> withKnownNat n (DNGt . fromIntegral $ natVal n)
157− SNGe n -> withKnownNat n (DNGe $ natVal n)
161+ SNGe n -> withKnownNat n (DNGe . fromIntegral $ natVal n)
158− SNLt n -> withKnownNat n (DNLt $ natVal n)
162+ SNLt n -> withKnownNat n (DNLt . fromIntegral $ natVal n)
159− SNLe n -> withKnownNat n (DNLe $ natVal n)
163+ SNLe n -> withKnownNat n (DNLe . fromIntegral $ natVal n)
160 toSing = \case
164 toSing = \case
161 DNEq n -> case someNatVal n of
165 DNEq n -> case someNatVal n of
162 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNEq (SNat :: Sing n))
166 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNEq (SNat :: Sing n))
163− Nothing -> error "Negative singleton nat"
167+ Nothing -> error "Negative singleton nat"
164 DNGt n -> case someNatVal n of
168 DNGt n -> case someNatVal n of
165 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNGt (SNat :: Sing n))
169 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNGt (SNat :: Sing n))
166− Nothing -> error "Negative singleton nat"
170+ Nothing -> error "Negative singleton nat"
167 DNGe n -> case someNatVal n of
171 DNGe n -> case someNatVal n of
168 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNGe (SNat :: Sing n))
172 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNGe (SNat :: Sing n))
169− Nothing -> error "Negative singleton nat"
173+ Nothing -> error "Negative singleton nat"
170 DNLt n -> case someNatVal n of
174 DNLt n -> case someNatVal n of
171 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNLt (SNat :: Sing n))
175 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNLt (SNat :: Sing n))
172− Nothing -> error "Negative singleton nat"
176+ Nothing -> error "Negative singleton nat"
173 DNLe n -> case someNatVal n of
177 DNLe n -> case someNatVal n of
174 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNLe (SNat :: Sing n))
178 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNLe (SNat :: Sing n))
175− Nothing -> error "Negative singleton nat"
179+ Nothing -> error "Negative singleton nat"
177data ArrayConstraint
181data ArrayConstraint
178 = AEq Nat
182 = AEq Nat
179 deriving (Generic)
187 deriving (Generic)
181data DemotedArrayConstraint
189data DemotedArrayConstraint
182 = DAEq Integer
190 = DAEq Integer
183− deriving (Generic)
195+ deriving (Generic, Eq, Show)
185data instance Sing (ac :: ArrayConstraint) where
197data instance Sing (ac :: ArrayConstraint) where
186 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)
188instance 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
190instance 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
192instance SingKind ArrayConstraint where
216instance SingKind ArrayConstraint where
193 type Demote ArrayConstraint = DemotedArrayConstraint
217 type Demote ArrayConstraint = DemotedArrayConstraint
194 fromSing = \case
218 fromSing = \case
195− 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)
196 toSing = \case
224 toSing = \case
197 DAEq n -> case someNatVal n of
225 DAEq n -> case someNatVal n of
198 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))
199− Nothing -> error "Negative singleton nat"
239+ Nothing -> error "Negative singleton nat"
201data Schema
241data Schema
202 = SchemaText [TextConstraint]
242 = SchemaText [TextConstraint]
@@ -218,7 +258,7 @@ data DemotedSchema
218 | DSchemaNull
258 | DSchemaNull
219 | DSchemaOptional DemotedSchema
259 | DSchemaOptional DemotedSchema
220 | DSchemaUnion [DemotedSchema]
260 | DSchemaUnion [DemotedSchema]
221− deriving (Generic)
261+ deriving (Generic, Eq, Show)
223data instance Sing (schema :: Schema) where
263data instance Sing (schema :: Schema) where
224 SSchemaText :: Sing tcs -> Sing ('SchemaText tcs)
264 SSchemaText :: Sing tcs -> Sing ('SchemaText tcs)
@@ -319,6 +359,10 @@ instance
319 => Serial m (FieldRepr '(name, schema)) where
359 => Serial m (FieldRepr '(name, schema)) where
320 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)
322data JsonRepr :: Schema -> Type where
366data JsonRepr :: Schema -> Type where
323 ReprText :: Text -> JsonRepr ('SchemaText cs)
367 ReprText :: Text -> JsonRepr ('SchemaText cs)
324 ReprNumber :: Scientific -> JsonRepr ('SchemaNumber cs)
368 ReprNumber :: Scientific -> JsonRepr ('SchemaNumber cs)
@@ -329,56 +373,63 @@ data JsonRepr :: Schema -> Type where
329 ReprOptional :: Maybe (JsonRepr s) -> JsonRepr ('SchemaOptional s)
373 ReprOptional :: Maybe (JsonRepr s) -> JsonRepr ('SchemaOptional s)
330 ReprUnion :: Union JsonRepr (h ': tl) -> JsonRepr ('SchemaUnion (h ': tl))
374 ReprUnion :: Union JsonRepr (h ': tl) -> JsonRepr ('SchemaUnion (h ': tl))
332−-- | Move to the different package
376+instance (Monad m, Serial m Text, SingI cs)
333−type family UnionAll (f :: u -> *) (rs :: [u]) (c :: * -> Constraint) :: Constraint where
377+ => Serial m (JsonRepr ('SchemaText cs)) where
334− UnionAll f '[] c = ()
378+ series = decDepth $ fmap ReprText $ textSeries $ fromSing (sing :: Sing cs)
335− UnionAll f (r ': rs) c = (c (f r), UnionAll f rs c)
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)
337−-- instance (UnionAll f as Show) => Show (Union f as) where
385+instance Monad m => Serial m (JsonRepr 'SchemaNull) where
338−-- show (This fa) = "This " P.++ show fa
386+ series = cons0 ReprNull
339−-- show (That u) = "That " P.++ show u
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
341instance Show (JsonRepr ('SchemaText cs)) where
402instance Show (JsonRepr ('SchemaText cs)) where
342 show (ReprText t) = "ReprText " P.++ show t
403 show (ReprText t) = "ReprText " P.++ show t
344instance Show (JsonRepr ('SchemaNumber cs)) where
405instance Show (JsonRepr ('SchemaNumber cs)) where
345 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
347instance Show (JsonRepr 'SchemaNull) where show _ = "ReprNull"
411instance Show (JsonRepr 'SchemaNull) where show _ = "ReprNull"
349instance Show (JsonRepr s) => Show (JsonRepr ('SchemaArray acs s)) where
413instance Show (JsonRepr s) => Show (JsonRepr ('SchemaArray acs s)) where
350 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
352instance V.RecAll FieldRepr fs Show => Show (JsonRepr ('SchemaObject fs)) where
423instance V.RecAll FieldRepr fs Show => Show (JsonRepr ('SchemaObject fs)) where
353 show (ReprObject fs) = "ReprObject " P.++ show fs
424 show (ReprObject fs) = "ReprObject " P.++ show fs
355instance Show (JsonRepr s) => Show (JsonRepr ('SchemaOptional s)) where
427instance Show (JsonRepr s) => Show (JsonRepr ('SchemaOptional s)) where
356 show (ReprOptional s) = "ReprOptional " P.++ show s
428 show (ReprOptional s) = "ReprOptional " P.++ show s
358−deriving instance UnionAll f as Show => Show (Union f as)
430+instance Show (Union JsonRepr (h ': tl))
359−
431+ => Show (JsonRepr ('SchemaUnion (h ': tl))) where
360−instance (Monad m, Serial m Text)
432+ show (ReprUnion s) = "ReprUnion " P.++ show s
361− => Serial m (JsonRepr ('SchemaText cs)) where
362− series = cons1 ReprText
364−instance (Monad m, Serial m Scientific)
365− => Serial m (JsonRepr ('SchemaNumber cs)) where
366− series = cons1 ReprNumber
368−instance Monad m => Serial m (JsonRepr 'SchemaNull) where
369− series = cons0 ReprNull
371−instance (Serial m (V.Vector (JsonRepr s)))
372− => Serial m (JsonRepr ('SchemaArray cs s)) where
373− series = cons1 ReprArray
375−instance (Serial m (JsonRepr s))
376− => Serial m (JsonRepr ('SchemaOptional s)) where
377− series = cons1 ReprOptional
379−instance (Monad m, Serial m (Rec FieldRepr fs))
380− => Serial m (JsonRepr ('SchemaObject fs)) where
381− series = cons1 ReprObject
383instance Eq (Rec FieldRepr fs) => Eq (JsonRepr ('SchemaObject fs)) where
434instance Eq (Rec FieldRepr fs) => Eq (JsonRepr ('SchemaObject fs)) where
384 ReprObject a == ReprObject b = a == b
435 ReprObject a == ReprObject b = a == b
@@ -389,6 +440,9 @@ instance Eq (JsonRepr ('SchemaText cs)) where
389instance Eq (JsonRepr ('SchemaNumber cs)) where
440instance Eq (JsonRepr ('SchemaNumber cs)) where
390 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
392instance Eq (JsonRepr 'SchemaNull) where
446instance Eq (JsonRepr 'SchemaNull) where
393 ReprNull == ReprNull = True
447 ReprNull == ReprNull = True
@@ -398,6 +452,51 @@ instance Eq (JsonRepr s) => Eq (JsonRepr ('SchemaArray as s)) where
398instance Eq (JsonRepr s) => Eq (JsonRepr ('SchemaOptional s)) where
452instance Eq (JsonRepr s) => Eq (JsonRepr ('SchemaOptional s)) where
399 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
401fromOptional
500fromOptional
402 :: SingI s
501 :: SingI s
403 => Sing ('SchemaOptional s)
502 => Sing ('SchemaOptional s)
@@ -443,25 +542,25 @@ instance SingI schema => J.FromJSON (JsonRepr schema) where
443 fieldRepr <- case s of
542 fieldRepr <- case s of
444 SSchemaText so -> case H.lookup fieldName h of
543 SSchemaText so -> case H.lookup fieldName h of
445 Just v -> withSingI so $ FieldRepr <$> parseJSON v
544 Just v -> withSingI so $ FieldRepr <$> parseJSON v
446− Nothing -> fail "schematext"
545+ Nothing -> fail $ "No text field: " P.++ show fieldName
447 SSchemaNumber so -> case H.lookup fieldName h of
546 SSchemaNumber so -> case H.lookup fieldName h of
448 Just v -> withSingI so $ FieldRepr <$> parseJSON v
547 Just v -> withSingI so $ FieldRepr <$> parseJSON v
449− Nothing -> fail "schemanumber"
548+ Nothing -> fail $ "No number field: " P.++ show fieldName
450 SSchemaBoolean -> case H.lookup fieldName h of
549 SSchemaBoolean -> case H.lookup fieldName h of
451 Just v -> FieldRepr <$> parseJSON v
550 Just v -> FieldRepr <$> parseJSON v
452− Nothing -> fail "schemaboolean"
551+ Nothing -> fail $ "No boolean field: " P.++ show fieldName
453 SSchemaNull -> case H.lookup fieldName h of
552 SSchemaNull -> case H.lookup fieldName h of
454 Just v -> FieldRepr <$> parseJSON v
553 Just v -> FieldRepr <$> parseJSON v
455− Nothing -> fail "schemanull"
554+ Nothing -> fail $ "No null field: " P.++ show fieldName
456 SSchemaArray sa sb -> case H.lookup fieldName h of
555 SSchemaArray sa sb -> case H.lookup fieldName h of
457 Just v -> withSingI sa $ withSingI sb $ FieldRepr <$> parseJSON v
556 Just v -> withSingI sa $ withSingI sb $ FieldRepr <$> parseJSON v
458− Nothing -> fail "schemaarray"
557+ Nothing -> fail $ "No array field: " P.++ show fieldName
459 SSchemaObject so -> case H.lookup fieldName h of
558 SSchemaObject so -> case H.lookup fieldName h of
460 Just v -> withSingI so $ FieldRepr <$> parseJSON v
559 Just v -> withSingI so $ FieldRepr <$> parseJSON v
461− Nothing -> fail "schemaobject"
560+ Nothing -> fail $ "No object field" P.++ show fieldName
462 SSchemaOptional so -> case H.lookup fieldName h of
561 SSchemaOptional so -> case H.lookup fieldName h of
463 Just v -> withSingI so $ FieldRepr <$> parseJSON v
562 Just v -> withSingI so $ FieldRepr <$> parseJSON v
464− Nothing -> fail "schemaoptional"
563+ Nothing -> withSingI so $ pure $ FieldRepr $ ReprOptional Nothing
465 SSchemaUnion ss -> withSingI ss $ FieldRepr <$> parseUnion ss value
564 SSchemaUnion ss -> withSingI ss $ FieldRepr <$> parseUnion ss value
466 (fieldRepr :&) <$> demoteFields tl h
565 (fieldRepr :&) <$> demoteFields tl h
467 ReprObject <$> withObject "Object" (demoteFields fs) value
566 ReprObject <$> withObject "Object" (demoteFields fs) value
@@ -473,7 +572,7 @@ instance J.ToJSON (JsonRepr a) where
473 toJSON (ReprText t) = J.String t
572 toJSON (ReprText t) = J.String t
474 toJSON (ReprNumber n) = J.Number n
573 toJSON (ReprNumber n) = J.Number n
475 toJSON (ReprOptional s) = case s of
574 toJSON (ReprOptional s) = case s of
476− Just v -> toJSON v
575+ Just v -> toJSON v
477 Nothing -> J.Null
576 Nothing -> J.Null
478 toJSON (ReprArray v) = J.Array $ toJSON <$> v
577 toJSON (ReprArray v) = J.Array $ toJSON <$> v
479 toJSON (ReprObject r) = J.Object . H.fromList . fold $ r
578 toJSON (ReprObject r) = J.Object . H.fromList . fold $ r