@@ -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)
@@ -24,7 +26,8 @@ import Data.Vinyl hiding (Dict)
24import qualified Data.Vinyl.TypeLevel as V
26import qualified Data.Vinyl.TypeLevel as V
25import GHC.Exts
27import GHC.Exts
26import GHC.Generics (Generic)
28import GHC.Generics (Generic)
27−import GHC.TypeLits (SomeNat(..), SomeSymbol(..), someSymbolVal, someNatVal)
30+ (SomeNat(..), SomeSymbol(..), someNatVal, someSymbolVal)
28import Prelude as P
31import Prelude as P
29import Test.SmallCheck.Series
32import Test.SmallCheck.Series
@@ -56,25 +59,25 @@ instance SingKind TextConstraint where
56 STRegex s -> withKnownSymbol s (DTRegex $ T.pack $ symbolVal s)
59 STRegex s -> withKnownSymbol s (DTRegex $ T.pack $ symbolVal s)
57 STEnum s -> let
60 STEnum s -> let
58 d :: Sing (s :: [Symbol]) -> [Text]
61 d :: Sing (s :: [Symbol]) -> [Text]
59− d SNil = []
62+ d SNil = []
60 d (SCons ss@SSym fs) = T.pack (symbolVal ss) : d fs
63 d (SCons ss@SSym fs) = T.pack (symbolVal ss) : d fs
61 in DTEnum $ d s
64 in DTEnum $ d s
62 toSing = \case
65 toSing = \case
63 DTEq n -> case someNatVal n of
66 DTEq n -> case someNatVal n of
64 Just (SomeNat (_ :: Proxy n)) -> SomeSing (STEq (SNat :: Sing n))
67 Just (SomeNat (_ :: Proxy n)) -> SomeSing (STEq (SNat :: Sing n))
65− Nothing -> error "Negative singleton nat"
68+ Nothing -> error "Negative singleton nat"
66 DTLt n -> case someNatVal n of
69 DTLt n -> case someNatVal n of
67 Just (SomeNat (_ :: Proxy n)) -> SomeSing (STLt (SNat :: Sing n))
70 Just (SomeNat (_ :: Proxy n)) -> SomeSing (STLt (SNat :: Sing n))
68− Nothing -> error "Negative singleton nat"
71+ Nothing -> error "Negative singleton nat"
69 DTLe n -> case someNatVal n of
72 DTLe n -> case someNatVal n of
70 Just (SomeNat (_ :: Proxy n)) -> SomeSing (STLe (SNat :: Sing n))
73 Just (SomeNat (_ :: Proxy n)) -> SomeSing (STLe (SNat :: Sing n))
71− Nothing -> error "Negative singleton nat"
74+ Nothing -> error "Negative singleton nat"
72 DTGt n -> case someNatVal n of
75 DTGt n -> case someNatVal n of
73 Just (SomeNat (_ :: Proxy n)) -> SomeSing (STGt (SNat :: Sing n))
76 Just (SomeNat (_ :: Proxy n)) -> SomeSing (STGt (SNat :: Sing n))
74− Nothing -> error "Negative singleton nat"
77+ Nothing -> error "Negative singleton nat"
75 DTGe n -> case someNatVal n of
78 DTGe n -> case someNatVal n of
76 Just (SomeNat (_ :: Proxy n)) -> SomeSing (STGe (SNat :: Sing n))
79 Just (SomeNat (_ :: Proxy n)) -> SomeSing (STGe (SNat :: Sing n))
77− Nothing -> error "Negative singleton nat"
80+ Nothing -> error "Negative singleton nat"
78 DTRegex s -> case someSymbolVal (T.unpack s) of
81 DTRegex s -> case someSymbolVal (T.unpack s) of
79 SomeSymbol (_ :: Proxy n) -> SomeSing (STRegex (SSym :: Sing n))
82 SomeSymbol (_ :: Proxy n) -> SomeSing (STRegex (SSym :: Sing n))
80 DTEnum ss -> case toSing ss of
83 DTEnum ss -> case toSing ss of
@@ -161,43 +164,79 @@ instance SingKind NumberConstraint where
161 toSing = \case
164 toSing = \case
162 DNEq n -> case someNatVal n of
165 DNEq n -> case someNatVal n of
163 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNEq (SNat :: Sing n))
166 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNEq (SNat :: Sing n))
164− Nothing -> error "Negative singleton nat"
167+ Nothing -> error "Negative singleton nat"
165 DNGt n -> case someNatVal n of
168 DNGt n -> case someNatVal n of
166 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNGt (SNat :: Sing n))
169 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNGt (SNat :: Sing n))
167− Nothing -> error "Negative singleton nat"
170+ Nothing -> error "Negative singleton nat"
168 DNGe n -> case someNatVal n of
171 DNGe n -> case someNatVal n of
169 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNGe (SNat :: Sing n))
172 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNGe (SNat :: Sing n))
170− Nothing -> error "Negative singleton nat"
173+ Nothing -> error "Negative singleton nat"
171 DNLt n -> case someNatVal n of
174 DNLt n -> case someNatVal n of
172 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNLt (SNat :: Sing n))
175 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNLt (SNat :: Sing n))
173− Nothing -> error "Negative singleton nat"
176+ Nothing -> error "Negative singleton nat"
174 DNLe n -> case someNatVal n of
177 DNLe n -> case someNatVal n of
175 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNLe (SNat :: Sing n))
178 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNLe (SNat :: Sing n))
176− Nothing -> error "Negative singleton nat"
179+ Nothing -> error "Negative singleton nat"
178data ArrayConstraint
181data ArrayConstraint
179 = AEq Nat
182 = AEq Nat
180 deriving (Generic)
187 deriving (Generic)
182data DemotedArrayConstraint
189data DemotedArrayConstraint
183 = DAEq Integer
190 = DAEq Integer
184 deriving (Generic, Eq, Show)
195 deriving (Generic, Eq, Show)
186data instance Sing (ac :: ArrayConstraint) where
197data instance Sing (ac :: ArrayConstraint) where
187 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)
189instance 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
191instance 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
193instance SingKind ArrayConstraint where
216instance SingKind ArrayConstraint where
194 type Demote ArrayConstraint = DemotedArrayConstraint
217 type Demote ArrayConstraint = DemotedArrayConstraint
195 fromSing = \case
218 fromSing = \case
196 SAEq n -> withKnownNat n (DAEq . fromIntegral $ 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)
197 toSing = \case
224 toSing = \case
198 DAEq n -> case someNatVal n of
225 DAEq n -> case someNatVal n of
199 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))
200− Nothing -> error "Negative singleton nat"
239+ Nothing -> error "Negative singleton nat"
202data Schema
241data Schema
203 = SchemaText [TextConstraint]
242 = SchemaText [TextConstraint]
@@ -334,6 +373,31 @@ data JsonRepr :: Schema -> Type where
334 ReprOptional :: Maybe (JsonRepr s) -> JsonRepr ('SchemaOptional s)
373 ReprOptional :: Maybe (JsonRepr s) -> JsonRepr ('SchemaOptional s)
335 ReprUnion :: Union JsonRepr (h ': tl) -> JsonRepr ('SchemaUnion (h ': tl))
374 ReprUnion :: Union JsonRepr (h ': tl) -> JsonRepr ('SchemaUnion (h ': tl))
376+instance (Monad m, Serial m Text, SingI cs)
377+ => Serial m (JsonRepr ('SchemaText cs)) where
378+ series = decDepth $ fmap ReprText $ textSeries $ fromSing (sing :: Sing cs)
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)
385+instance Monad m => Serial m (JsonRepr 'SchemaNull) where
386+ series = cons0 ReprNull
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
337-- | Move to the union package
401-- | Move to the union package
338instance Show (JsonRepr ('SchemaText cs)) where
402instance Show (JsonRepr ('SchemaText cs)) where
339 show (ReprText t) = "ReprText " P.++ show t
403 show (ReprText t) = "ReprText " P.++ show t
@@ -341,39 +405,31 @@ instance Show (JsonRepr ('SchemaText cs)) where
341instance Show (JsonRepr ('SchemaNumber cs)) where
405instance Show (JsonRepr ('SchemaNumber cs)) where
342 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
344instance Show (JsonRepr 'SchemaNull) where show _ = "ReprNull"
411instance Show (JsonRepr 'SchemaNull) where show _ = "ReprNull"
346instance Show (JsonRepr s) => Show (JsonRepr ('SchemaArray acs s)) where
413instance Show (JsonRepr s) => Show (JsonRepr ('SchemaArray acs s)) where
347 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
349instance V.RecAll FieldRepr fs Show => Show (JsonRepr ('SchemaObject fs)) where
423instance V.RecAll FieldRepr fs Show => Show (JsonRepr ('SchemaObject fs)) where
350 show (ReprObject fs) = "ReprObject " P.++ show fs
424 show (ReprObject fs) = "ReprObject " P.++ show fs
352instance Show (JsonRepr s) => Show (JsonRepr ('SchemaOptional s)) where
427instance Show (JsonRepr s) => Show (JsonRepr ('SchemaOptional s)) where
353 show (ReprOptional s) = "ReprOptional " P.++ show s
428 show (ReprOptional s) = "ReprOptional " P.++ show s
355−instance (Monad m, Serial m Text)
430+instance Show (Union JsonRepr (h ': tl))
356− => Serial m (JsonRepr ('SchemaText cs)) where
431+ => Show (JsonRepr ('SchemaUnion (h ': tl))) where
357− series = cons1 ReprText
432+ show (ReprUnion s) = "ReprUnion " P.++ show s
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
434instance Eq (Rec FieldRepr fs) => Eq (JsonRepr ('SchemaObject fs)) where
379 ReprObject a == ReprObject b = a == b
435 ReprObject a == ReprObject b = a == b
@@ -384,6 +440,9 @@ instance Eq (JsonRepr ('SchemaText cs)) where
384instance Eq (JsonRepr ('SchemaNumber cs)) where
440instance Eq (JsonRepr ('SchemaNumber cs)) where
385 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
387instance Eq (JsonRepr 'SchemaNull) where
446instance Eq (JsonRepr 'SchemaNull) where
388 ReprNull == ReprNull = True
447 ReprNull == ReprNull = True
@@ -393,6 +452,10 @@ instance Eq (JsonRepr s) => Eq (JsonRepr ('SchemaArray as s)) where
393instance Eq (JsonRepr s) => Eq (JsonRepr ('SchemaOptional s)) where
452instance Eq (JsonRepr s) => Eq (JsonRepr ('SchemaOptional s)) where
394 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
396instance Ord (Rec FieldRepr fs) => Ord (JsonRepr ('SchemaObject fs)) where
459instance Ord (Rec FieldRepr fs) => Ord (JsonRepr ('SchemaObject fs)) where
397 ReprObject a `compare` ReprObject b = a `compare` b
460 ReprObject a `compare` ReprObject b = a `compare` b
@@ -402,6 +465,9 @@ instance Ord (JsonRepr ('SchemaText cs)) where
402instance Ord (JsonRepr ('SchemaNumber cs)) where
465instance Ord (JsonRepr ('SchemaNumber cs)) where
403 ReprNumber a `compare` ReprNumber b = a `compare` b
466 ReprNumber a `compare` ReprNumber b = a `compare` b
468+instance Ord (JsonRepr 'SchemaBoolean) where
469+ ReprBoolean a `compare` ReprBoolean b = a `compare` b
405instance Ord (JsonRepr 'SchemaNull) where
471instance Ord (JsonRepr 'SchemaNull) where
406 compare _ _ = EQ
472 compare _ _ = EQ
@@ -411,6 +477,10 @@ instance Ord (JsonRepr s) => Ord (JsonRepr ('SchemaArray as s)) where
411instance Ord (JsonRepr s) => Ord (JsonRepr ('SchemaOptional s)) where
477instance Ord (JsonRepr s) => Ord (JsonRepr ('SchemaOptional s)) where
412 ReprOptional a `compare` ReprOptional b = a `compare` b
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
414instance IsList (JsonRepr ('SchemaArray cs s)) where
484instance IsList (JsonRepr ('SchemaArray cs s)) where
415 type Item (JsonRepr ('SchemaArray cs s)) = JsonRepr s
485 type Item (JsonRepr ('SchemaArray cs s)) = JsonRepr s
416 fromList = ReprArray . GHC.Exts.fromList
486 fromList = ReprArray . GHC.Exts.fromList
@@ -502,7 +572,7 @@ instance J.ToJSON (JsonRepr a) where
502 toJSON (ReprText t) = J.String t
572 toJSON (ReprText t) = J.String t
503 toJSON (ReprNumber n) = J.Number n
573 toJSON (ReprNumber n) = J.Number n
504 toJSON (ReprOptional s) = case s of
574 toJSON (ReprOptional s) = case s of
505− Just v -> toJSON v
575+ Just v -> toJSON v
506 Nothing -> J.Null
576 Nothing -> J.Null
507 toJSON (ReprArray v) = J.Array $ toJSON <$> v
577 toJSON (ReprArray v) = J.Array $ toJSON <$> v
508 toJSON (ReprObject r) = J.Object . H.fromList . fold $ r
578 toJSON (ReprObject r) = J.Object . H.fromList . fold $ r