@@ -13,6 +13,7 @@ import Data.HashMap.Strict as H
13import Data.Kind
13import Data.Kind
14import Data.Maybe
14import Data.Maybe
15import Data.Schematic.Instances ()
15import Data.Schematic.Instances ()
16+import Data.Schematic.Generator
16import Data.Scientific
17import Data.Scientific
17import Data.Singletons.Prelude.List hiding (All, Union)
18import Data.Singletons.Prelude.List hiding (All, Union)
18import Data.Singletons.TH
19import Data.Singletons.TH
@@ -24,7 +25,8 @@ import Data.Vinyl hiding (Dict)
24import qualified Data.Vinyl.TypeLevel as V
25import qualified Data.Vinyl.TypeLevel as V
25import GHC.Exts
26import GHC.Exts
26import GHC.Generics (Generic)
27import GHC.Generics (Generic)
27−import GHC.TypeLits (SomeNat(..), SomeSymbol(..), someSymbolVal, someNatVal)
29+ (SomeNat(..), SomeSymbol(..), someNatVal, someSymbolVal)
28import Prelude as P
30import Prelude as P
29import Test.SmallCheck.Series
31import Test.SmallCheck.Series
@@ -56,25 +58,25 @@ instance SingKind TextConstraint where
56 STRegex s -> withKnownSymbol s (DTRegex $ T.pack $ symbolVal s)
58 STRegex s -> withKnownSymbol s (DTRegex $ T.pack $ symbolVal s)
57 STEnum s -> let
59 STEnum s -> let
58 d :: Sing (s :: [Symbol]) -> [Text]
60 d :: Sing (s :: [Symbol]) -> [Text]
59− d SNil = []
61+ d SNil = []
60 d (SCons ss@SSym fs) = T.pack (symbolVal ss) : d fs
62 d (SCons ss@SSym fs) = T.pack (symbolVal ss) : d fs
61 in DTEnum $ d s
63 in DTEnum $ d s
62 toSing = \case
64 toSing = \case
63 DTEq n -> case someNatVal n of
65 DTEq n -> case someNatVal n of
64 Just (SomeNat (_ :: Proxy n)) -> SomeSing (STEq (SNat :: Sing n))
66 Just (SomeNat (_ :: Proxy n)) -> SomeSing (STEq (SNat :: Sing n))
65− Nothing -> error "Negative singleton nat"
67+ Nothing -> error "Negative singleton nat"
66 DTLt n -> case someNatVal n of
68 DTLt n -> case someNatVal n of
67 Just (SomeNat (_ :: Proxy n)) -> SomeSing (STLt (SNat :: Sing n))
69 Just (SomeNat (_ :: Proxy n)) -> SomeSing (STLt (SNat :: Sing n))
68− Nothing -> error "Negative singleton nat"
70+ Nothing -> error "Negative singleton nat"
69 DTLe n -> case someNatVal n of
71 DTLe n -> case someNatVal n of
70 Just (SomeNat (_ :: Proxy n)) -> SomeSing (STLe (SNat :: Sing n))
72 Just (SomeNat (_ :: Proxy n)) -> SomeSing (STLe (SNat :: Sing n))
71− Nothing -> error "Negative singleton nat"
73+ Nothing -> error "Negative singleton nat"
72 DTGt n -> case someNatVal n of
74 DTGt n -> case someNatVal n of
73 Just (SomeNat (_ :: Proxy n)) -> SomeSing (STGt (SNat :: Sing n))
75 Just (SomeNat (_ :: Proxy n)) -> SomeSing (STGt (SNat :: Sing n))
74− Nothing -> error "Negative singleton nat"
76+ Nothing -> error "Negative singleton nat"
75 DTGe n -> case someNatVal n of
77 DTGe n -> case someNatVal n of
76 Just (SomeNat (_ :: Proxy n)) -> SomeSing (STGe (SNat :: Sing n))
78 Just (SomeNat (_ :: Proxy n)) -> SomeSing (STGe (SNat :: Sing n))
77− Nothing -> error "Negative singleton nat"
79+ Nothing -> error "Negative singleton nat"
78 DTRegex s -> case someSymbolVal (T.unpack s) of
80 DTRegex s -> case someSymbolVal (T.unpack s) of
79 SomeSymbol (_ :: Proxy n) -> SomeSing (STRegex (SSym :: Sing n))
81 SomeSymbol (_ :: Proxy n) -> SomeSing (STRegex (SSym :: Sing n))
80 DTEnum ss -> case toSing ss of
82 DTEnum ss -> case toSing ss of
@@ -161,19 +163,19 @@ instance SingKind NumberConstraint where
161 toSing = \case
163 toSing = \case
162 DNEq n -> case someNatVal n of
164 DNEq n -> case someNatVal n of
163 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNEq (SNat :: Sing n))
165 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNEq (SNat :: Sing n))
164− Nothing -> error "Negative singleton nat"
166+ Nothing -> error "Negative singleton nat"
165 DNGt n -> case someNatVal n of
167 DNGt n -> case someNatVal n of
166 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNGt (SNat :: Sing n))
168 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNGt (SNat :: Sing n))
167− Nothing -> error "Negative singleton nat"
169+ Nothing -> error "Negative singleton nat"
168 DNGe n -> case someNatVal n of
170 DNGe n -> case someNatVal n of
169 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNGe (SNat :: Sing n))
171 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNGe (SNat :: Sing n))
170− Nothing -> error "Negative singleton nat"
172+ Nothing -> error "Negative singleton nat"
171 DNLt n -> case someNatVal n of
173 DNLt n -> case someNatVal n of
172 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNLt (SNat :: Sing n))
174 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNLt (SNat :: Sing n))
173− Nothing -> error "Negative singleton nat"
175+ Nothing -> error "Negative singleton nat"
174 DNLe n -> case someNatVal n of
176 DNLe n -> case someNatVal n of
175 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNLe (SNat :: Sing n))
177 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNLe (SNat :: Sing n))
176− Nothing -> error "Negative singleton nat"
178+ Nothing -> error "Negative singleton nat"
178data ArrayConstraint
180data ArrayConstraint
179 = AEq Nat
181 = AEq Nat
@@ -197,7 +199,7 @@ instance SingKind ArrayConstraint where
197 toSing = \case
199 toSing = \case
198 DAEq n -> case someNatVal n of
200 DAEq n -> case someNatVal n of
199 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SAEq (SNat :: Sing n))
201 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SAEq (SNat :: Sing n))
200− Nothing -> error "Negative singleton nat"
202+ Nothing -> error "Negative singleton nat"
202data Schema
204data Schema
203 = SchemaText [TextConstraint]
205 = SchemaText [TextConstraint]
@@ -334,6 +336,31 @@ data JsonRepr :: Schema -> Type where
334 ReprOptional :: Maybe (JsonRepr s) -> JsonRepr ('SchemaOptional s)
336 ReprOptional :: Maybe (JsonRepr s) -> JsonRepr ('SchemaOptional s)
335 ReprUnion :: Union JsonRepr (h ': tl) -> JsonRepr ('SchemaUnion (h ': tl))
337 ReprUnion :: Union JsonRepr (h ': tl) -> JsonRepr ('SchemaUnion (h ': tl))
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)
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)
348+instance Monad m => Serial m (JsonRepr 'SchemaNull) where
349+ series = cons0 ReprNull
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)
356+instance (Serial m (JsonRepr s))
357+ => Serial m (JsonRepr ('SchemaOptional s)) where
358+ series = cons1 ReprOptional
360+instance (Monad m, Serial m (Rec FieldRepr fs))
361+ => Serial m (JsonRepr ('SchemaObject fs)) where
362+ series = cons1 ReprObject
337-- | Move to the union package
364-- | Move to the union package
338instance Show (JsonRepr ('SchemaText cs)) where
365instance Show (JsonRepr ('SchemaText cs)) where
339 show (ReprText t) = "ReprText " P.++ show t
366 show (ReprText t) = "ReprText " P.++ show t
@@ -479,7 +506,7 @@ instance J.ToJSON (JsonRepr a) where
479 toJSON (ReprText t) = J.String t
506 toJSON (ReprText t) = J.String t
480 toJSON (ReprNumber n) = J.Number n
507 toJSON (ReprNumber n) = J.Number n
481 toJSON (ReprOptional s) = case s of
508 toJSON (ReprOptional s) = case s of
482− Just v -> toJSON v
509+ Just v -> toJSON v
483 Nothing -> J.Null
510 Nothing -> J.Null
484 toJSON (ReprArray v) = J.Array $ toJSON <$> v
511 toJSON (ReprArray v) = J.Array $ toJSON <$> v
485 toJSON (ReprObject r) = J.Object . H.fromList . fold $ r
512 toJSON (ReprObject r) = J.Object . H.fromList . fold $ r