@@ -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
@@ -88,7 +90,7 @@ data DemotedTextConstraint
88 | DTGe Integer
90 | DTGe Integer
89 | DTRegex Text
91 | DTRegex Text
90 | DTEnum [Text]
92 | DTEnum [Text]
91− deriving (Generic)
93+ deriving (Generic, Eq, Show)
93data instance Sing (tc :: TextConstraint) where
95data instance Sing (tc :: TextConstraint) where
94 STEq :: Sing n -> Sing ('TEq n)
96 STEq :: Sing n -> Sing ('TEq n)
@@ -129,7 +131,7 @@ data DemotedNumberConstraint
129 | DNGt Integer
131 | DNGt Integer
130 | DNGe Integer
132 | DNGe Integer
131 | DNEq Integer
133 | DNEq Integer
132− deriving (Generic)
134+ deriving (Generic, Eq, Show)
134data instance Sing (nc :: NumberConstraint) where
136data instance Sing (nc :: NumberConstraint) where
135 SNEq :: Sing n -> Sing ('NEq n)
137 SNEq :: Sing n -> Sing ('NEq n)
@@ -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
@@ -181,7 +183,7 @@ data ArrayConstraint
182data DemotedArrayConstraint
184data DemotedArrayConstraint
183 = DAEq Integer
185 = DAEq Integer
184− deriving (Generic)
186+ deriving (Generic, Eq, Show)
186data instance Sing (ac :: ArrayConstraint) where
188data instance Sing (ac :: ArrayConstraint) where
187 SAEq :: Sing n -> Sing ('AEq n)
189 SAEq :: Sing n -> Sing ('AEq n)
@@ -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]
@@ -219,7 +221,7 @@ data DemotedSchema
219 | DSchemaNull
221 | DSchemaNull
220 | DSchemaOptional DemotedSchema
222 | DSchemaOptional DemotedSchema
221 | DSchemaUnion [DemotedSchema]
223 | DSchemaUnion [DemotedSchema]
222− deriving (Generic)
224+ deriving (Generic, Eq, Show)
224data instance Sing (schema :: Schema) where
226data instance Sing (schema :: Schema) where
225 SSchemaText :: Sing tcs -> Sing ('SchemaText tcs)
227 SSchemaText :: Sing tcs -> Sing ('SchemaText tcs)
@@ -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
@@ -352,29 +379,6 @@ instance V.RecAll FieldRepr fs Show => Show (JsonRepr ('SchemaObject fs)) where
352instance Show (JsonRepr s) => Show (JsonRepr ('SchemaOptional s)) where
379instance Show (JsonRepr s) => Show (JsonRepr ('SchemaOptional s)) where
353 show (ReprOptional s) = "ReprOptional " P.++ show s
380 show (ReprOptional s) = "ReprOptional " P.++ show s
355−instance (Monad m, Serial m Text)
356− => Serial m (JsonRepr ('SchemaText cs)) where
357− series = cons1 ReprText
359−instance (Monad m, Serial m Scientific)
360− => Serial m (JsonRepr ('SchemaNumber cs)) where
361− series = cons1 ReprNumber
363−instance Monad m => Serial m (JsonRepr 'SchemaNull) where
364− series = cons0 ReprNull
366−instance (Serial m (V.Vector (JsonRepr s)))
367− => Serial m (JsonRepr ('SchemaArray cs s)) where
368− series = cons1 ReprArray
370−instance (Serial m (JsonRepr s))
371− => Serial m (JsonRepr ('SchemaOptional s)) where
372− series = cons1 ReprOptional
374−instance (Monad m, Serial m (Rec FieldRepr fs))
375− => Serial m (JsonRepr ('SchemaObject fs)) where
376− series = cons1 ReprObject
378instance Eq (Rec FieldRepr fs) => Eq (JsonRepr ('SchemaObject fs)) where
382instance Eq (Rec FieldRepr fs) => Eq (JsonRepr ('SchemaObject fs)) where
379 ReprObject a == ReprObject b = a == b
383 ReprObject a == ReprObject b = a == b
@@ -502,7 +506,7 @@ instance J.ToJSON (JsonRepr a) where
502 toJSON (ReprText t) = J.String t
506 toJSON (ReprText t) = J.String t
503 toJSON (ReprNumber n) = J.Number n
507 toJSON (ReprNumber n) = J.Number n
504 toJSON (ReprOptional s) = case s of
508 toJSON (ReprOptional s) = case s of
505− Just v -> toJSON v
509+ Just v -> toJSON v
506 Nothing -> J.Null
510 Nothing -> J.Null
507 toJSON (ReprArray v) = J.Array $ toJSON <$> v
511 toJSON (ReprArray v) = J.Array $ toJSON <$> v
508 toJSON (ReprObject r) = J.Object . H.fromList . fold $ r
512 toJSON (ReprObject r) = J.Object . H.fromList . fold $ r