@@ -13,6 +13,7 @@ import Data.HashMap.Strict as H
1313import Data.Kind
1414import Data.Maybe
1515import Data.Schematic.Instances ()
16+import Data.Schematic.Generator
1617import Data.Scientific
1718import Data.Singletons.Prelude.List hiding (All, Union)
1819import Data.Singletons.TH
@@ -24,7 +25,8 @@ import Data.Vinyl hiding (Dict)
2425import qualified Data.Vinyl.TypeLevel as V
2526import GHC.Exts
2627import GHC.Generics (Generic)
27−import GHC.TypeLits (SomeNat(..), SomeSymbol(..), someSymbolVal, someNatVal)
28+import GHC.TypeLits
29+ (SomeNat(..), SomeSymbol(..), someNatVal, someSymbolVal)
2830import Prelude as P
2931import Test.SmallCheck.Series
3032
@@ -56,25 +58,25 @@ instance SingKind TextConstraint where
5658 STRegex s -> withKnownSymbol s (DTRegex $ T.pack $ symbolVal s)
5759 STEnum s -> let
5860 d :: Sing (s :: [Symbol]) -> [Text]
59− d SNil = []
61+ d SNil = []
6062 d (SCons ss@SSym fs) = T.pack (symbolVal ss) : d fs
6163 in DTEnum $ d s
6264 toSing = \case
6365 DTEq n -> case someNatVal n of
6466 Just (SomeNat (_ :: Proxy n)) -> SomeSing (STEq (SNat :: Sing n))
65− Nothing -> error "Negative singleton nat"
67+ Nothing -> error "Negative singleton nat"
6668 DTLt n -> case someNatVal n of
6769 Just (SomeNat (_ :: Proxy n)) -> SomeSing (STLt (SNat :: Sing n))
68− Nothing -> error "Negative singleton nat"
70+ Nothing -> error "Negative singleton nat"
6971 DTLe n -> case someNatVal n of
7072 Just (SomeNat (_ :: Proxy n)) -> SomeSing (STLe (SNat :: Sing n))
71− Nothing -> error "Negative singleton nat"
73+ Nothing -> error "Negative singleton nat"
7274 DTGt n -> case someNatVal n of
7375 Just (SomeNat (_ :: Proxy n)) -> SomeSing (STGt (SNat :: Sing n))
74− Nothing -> error "Negative singleton nat"
76+ Nothing -> error "Negative singleton nat"
7577 DTGe n -> case someNatVal n of
7678 Just (SomeNat (_ :: Proxy n)) -> SomeSing (STGe (SNat :: Sing n))
77− Nothing -> error "Negative singleton nat"
79+ Nothing -> error "Negative singleton nat"
7880 DTRegex s -> case someSymbolVal (T.unpack s) of
7981 SomeSymbol (_ :: Proxy n) -> SomeSing (STRegex (SSym :: Sing n))
8082 DTEnum ss -> case toSing ss of
@@ -88,7 +90,7 @@ data DemotedTextConstraint
8890 | DTGe Integer
8991 | DTRegex Text
9092 | DTEnum [Text]
91− deriving (Generic)
93+ deriving (Generic, Eq, Show)
9294
9395data instance Sing (tc :: TextConstraint) where
9496 STEq :: Sing n -> Sing ('TEq n)
@@ -129,7 +131,7 @@ data DemotedNumberConstraint
129131 | DNGt Integer
130132 | DNGe Integer
131133 | DNEq Integer
132− deriving (Generic)
134+ deriving (Generic, Eq, Show)
133135
134136data instance Sing (nc :: NumberConstraint) where
135137 SNEq :: Sing n -> Sing ('NEq n)
@@ -161,19 +163,19 @@ instance SingKind NumberConstraint where
161163 toSing = \case
162164 DNEq n -> case someNatVal n of
163165 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNEq (SNat :: Sing n))
164− Nothing -> error "Negative singleton nat"
166+ Nothing -> error "Negative singleton nat"
165167 DNGt n -> case someNatVal n of
166168 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNGt (SNat :: Sing n))
167− Nothing -> error "Negative singleton nat"
169+ Nothing -> error "Negative singleton nat"
168170 DNGe n -> case someNatVal n of
169171 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNGe (SNat :: Sing n))
170− Nothing -> error "Negative singleton nat"
172+ Nothing -> error "Negative singleton nat"
171173 DNLt n -> case someNatVal n of
172174 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNLt (SNat :: Sing n))
173− Nothing -> error "Negative singleton nat"
175+ Nothing -> error "Negative singleton nat"
174176 DNLe n -> case someNatVal n of
175177 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNLe (SNat :: Sing n))
176− Nothing -> error "Negative singleton nat"
178+ Nothing -> error "Negative singleton nat"
177179
178180data ArrayConstraint
179181 = AEq Nat
@@ -181,7 +183,7 @@ data ArrayConstraint
181183
182184data DemotedArrayConstraint
183185 = DAEq Integer
184− deriving (Generic)
186+ deriving (Generic, Eq, Show)
185187
186188data instance Sing (ac :: ArrayConstraint) where
187189 SAEq :: Sing n -> Sing ('AEq n)
@@ -197,7 +199,7 @@ instance SingKind ArrayConstraint where
197199 toSing = \case
198200 DAEq n -> case someNatVal n of
199201 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SAEq (SNat :: Sing n))
200− Nothing -> error "Negative singleton nat"
202+ Nothing -> error "Negative singleton nat"
201203
202204data Schema
203205 = SchemaText [TextConstraint]
@@ -219,7 +221,7 @@ data DemotedSchema
219221 | DSchemaNull
220222 | DSchemaOptional DemotedSchema
221223 | DSchemaUnion [DemotedSchema]
222− deriving (Generic)
224+ deriving (Generic, Eq, Show)
223225
224226data instance Sing (schema :: Schema) where
225227 SSchemaText :: Sing tcs -> Sing ('SchemaText tcs)
@@ -334,6 +336,31 @@ data JsonRepr :: Schema -> Type where
334336 ReprOptional :: Maybe (JsonRepr s) -> JsonRepr ('SchemaOptional s)
335337 ReprUnion :: Union JsonRepr (h ': tl) -> JsonRepr ('SchemaUnion (h ': tl))
336338
339+instance (Monad m, Serial m Text, SingI cs)
340+ => Serial m (JsonRepr ('SchemaText cs)) where
341+ series = decDepth $ fmap ReprText $ textSeries $ fromSing (sing :: Sing cs)
342+
343+instance (Monad m, Serial m Scientific, SingI cs)
344+ => Serial m (JsonRepr ('SchemaNumber cs)) where
345+ series = decDepth $ fmap ReprNumber
346+ $ numberSeries $ fromSing (sing :: Sing cs)
347+
348+instance Monad m => Serial m (JsonRepr 'SchemaNull) where
349+ series = cons0 ReprNull
350+
351+instance (Serial m (JsonRepr s), Serial m (V.Vector (JsonRepr s)), SingI cs)
352+ => Serial m (JsonRepr ('SchemaArray cs s)) where
353+ series = decDepth $ fmap ReprArray
354+ $ arraySeries $ fromSing (sing :: Sing cs)
355+
356+instance (Serial m (JsonRepr s))
357+ => Serial m (JsonRepr ('SchemaOptional s)) where
358+ series = cons1 ReprOptional
359+
360+instance (Monad m, Serial m (Rec FieldRepr fs))
361+ => Serial m (JsonRepr ('SchemaObject fs)) where
362+ series = cons1 ReprObject
363+
337364-- | Move to the union package
338365instance Show (JsonRepr ('SchemaText cs)) where
339366 show (ReprText t) = "ReprText " P.++ show t
@@ -352,29 +379,6 @@ instance V.RecAll FieldRepr fs Show => Show (JsonRepr ('SchemaObject fs)) where
352379instance Show (JsonRepr s) => Show (JsonRepr ('SchemaOptional s)) where
353380 show (ReprOptional s) = "ReprOptional " P.++ show s
354381
355−instance (Monad m, Serial m Text)
356− => Serial m (JsonRepr ('SchemaText cs)) where
357− series = cons1 ReprText
358−
359−instance (Monad m, Serial m Scientific)
360− => Serial m (JsonRepr ('SchemaNumber cs)) where
361− series = cons1 ReprNumber
362−
363−instance Monad m => Serial m (JsonRepr 'SchemaNull) where
364− series = cons0 ReprNull
365−
366−instance (Serial m (V.Vector (JsonRepr s)))
367− => Serial m (JsonRepr ('SchemaArray cs s)) where
368− series = cons1 ReprArray
369−
370−instance (Serial m (JsonRepr s))
371− => Serial m (JsonRepr ('SchemaOptional s)) where
372− series = cons1 ReprOptional
373−
374−instance (Monad m, Serial m (Rec FieldRepr fs))
375− => Serial m (JsonRepr ('SchemaObject fs)) where
376− series = cons1 ReprObject
377−
378382instance Eq (Rec FieldRepr fs) => Eq (JsonRepr ('SchemaObject fs)) where
379383 ReprObject a == ReprObject b = a == b
380384
@@ -502,7 +506,7 @@ instance J.ToJSON (JsonRepr a) where
502506 toJSON (ReprText t) = J.String t
503507 toJSON (ReprNumber n) = J.Number n
504508 toJSON (ReprOptional s) = case s of
505− Just v -> toJSON v
509+ Just v -> toJSON v
506510 Nothing -> J.Null
507511 toJSON (ReprArray v) = J.Array $ toJSON <$> v
508512 toJSON (ReprObject r) = J.Object . H.fromList . fold $ r