@@ -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
@@ -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
@@ -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 ]
@@ -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
@@ -479,7 +506,7 @@ instance J.ToJSON (JsonRepr a) where
479506 toJSON (ReprText t) = J. String t
480507 toJSON (ReprNumber n) = J. Number n
481508 toJSON (ReprOptional s) = case s of
482- Just v -> toJSON v
509+ Just v -> toJSON v
483510 Nothing -> J. Null
484511 toJSON (ReprArray v) = J. Array $ toJSON <$> v
485512 toJSON (ReprObject r) = J. Object . H. fromList . fold $ r
0 commit comments