@@ -1,7 +1,6 @@
1{-# LANGUAGE AllowAmbiguousTypes #-}
1{-# LANGUAGE AllowAmbiguousTypes #-}
3−{-# LANGUAGE OverloadedLists #-}
2+{-# LANGUAGE OverloadedLists #-}
4−{-# LANGUAGE OverloadedStrings #-}
3+{-# LANGUAGE OverloadedStrings #-}
5{-# OPTIONS_GHC -fprint-explicit-kinds #-}
4{-# OPTIONS_GHC -fprint-explicit-kinds #-}
7module Data.Schematic.Schema where
6module Data.Schematic.Schema where
@@ -13,7 +12,6 @@ import Data.Aeson.Types as J
13import Data.HashMap.Strict as H
12import Data.HashMap.Strict as H
14import Data.Kind
13import Data.Kind
15import Data.Maybe
14import Data.Maybe
16−import Data.Schematic.Generator
17import Data.Schematic.Instances ()
15import Data.Schematic.Instances ()
18import Data.Scientific
16import Data.Scientific
19import Data.Singletons.Prelude.List hiding (All, Union)
17import Data.Singletons.Prelude.List hiding (All, Union)
@@ -26,8 +24,7 @@ import Data.Vinyl hiding (Dict)
26import qualified Data.Vinyl.TypeLevel as V
24import qualified Data.Vinyl.TypeLevel as V
27import GHC.Exts
25import GHC.Exts
28import GHC.Generics (Generic)
26import GHC.Generics (Generic)
30− (SomeNat(..), SomeSymbol(..), someNatVal, someSymbolVal)
27+import GHC.TypeLits (SomeNat(..), SomeSymbol(..), someSymbolVal, someNatVal)
31import Prelude as P
28import Prelude as P
32import Test.SmallCheck.Series
29import Test.SmallCheck.Series
@@ -59,25 +56,25 @@ instance SingKind TextConstraint where
59 STRegex s -> withKnownSymbol s (DTRegex $ T.pack $ symbolVal s)
56 STRegex s -> withKnownSymbol s (DTRegex $ T.pack $ symbolVal s)
60 STEnum s -> let
57 STEnum s -> let
61 d :: Sing (s :: [Symbol]) -> [Text]
58 d :: Sing (s :: [Symbol]) -> [Text]
62− d SNil = []
59+ d SNil = []
63 d (SCons ss@SSym fs) = T.pack (symbolVal ss) : d fs
60 d (SCons ss@SSym fs) = T.pack (symbolVal ss) : d fs
64 in DTEnum $ d s
61 in DTEnum $ d s
65 toSing = \case
62 toSing = \case
66 DTEq n -> case someNatVal n of
63 DTEq n -> case someNatVal n of
67 Just (SomeNat (_ :: Proxy n)) -> SomeSing (STEq (SNat :: Sing n))
64 Just (SomeNat (_ :: Proxy n)) -> SomeSing (STEq (SNat :: Sing n))
68− Nothing -> error "Negative singleton nat"
65+ Nothing -> error "Negative singleton nat"
69 DTLt n -> case someNatVal n of
66 DTLt n -> case someNatVal n of
70 Just (SomeNat (_ :: Proxy n)) -> SomeSing (STLt (SNat :: Sing n))
67 Just (SomeNat (_ :: Proxy n)) -> SomeSing (STLt (SNat :: Sing n))
71− Nothing -> error "Negative singleton nat"
68+ Nothing -> error "Negative singleton nat"
72 DTLe n -> case someNatVal n of
69 DTLe n -> case someNatVal n of
73 Just (SomeNat (_ :: Proxy n)) -> SomeSing (STLe (SNat :: Sing n))
70 Just (SomeNat (_ :: Proxy n)) -> SomeSing (STLe (SNat :: Sing n))
74− Nothing -> error "Negative singleton nat"
71+ Nothing -> error "Negative singleton nat"
75 DTGt n -> case someNatVal n of
72 DTGt n -> case someNatVal n of
76 Just (SomeNat (_ :: Proxy n)) -> SomeSing (STGt (SNat :: Sing n))
73 Just (SomeNat (_ :: Proxy n)) -> SomeSing (STGt (SNat :: Sing n))
77− Nothing -> error "Negative singleton nat"
74+ Nothing -> error "Negative singleton nat"
78 DTGe n -> case someNatVal n of
75 DTGe n -> case someNatVal n of
79 Just (SomeNat (_ :: Proxy n)) -> SomeSing (STGe (SNat :: Sing n))
76 Just (SomeNat (_ :: Proxy n)) -> SomeSing (STGe (SNat :: Sing n))
80− Nothing -> error "Negative singleton nat"
77+ Nothing -> error "Negative singleton nat"
81 DTRegex s -> case someSymbolVal (T.unpack s) of
78 DTRegex s -> case someSymbolVal (T.unpack s) of
82 SomeSymbol (_ :: Proxy n) -> SomeSing (STRegex (SSym :: Sing n))
79 SomeSymbol (_ :: Proxy n) -> SomeSing (STRegex (SSym :: Sing n))
83 DTEnum ss -> case toSing ss of
80 DTEnum ss -> case toSing ss of
@@ -164,79 +161,43 @@ instance SingKind NumberConstraint where
164 toSing = \case
161 toSing = \case
165 DNEq n -> case someNatVal n of
162 DNEq n -> case someNatVal n of
166 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNEq (SNat :: Sing n))
163 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNEq (SNat :: Sing n))
167− Nothing -> error "Negative singleton nat"
164+ Nothing -> error "Negative singleton nat"
168 DNGt n -> case someNatVal n of
165 DNGt n -> case someNatVal n of
169 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNGt (SNat :: Sing n))
166 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNGt (SNat :: Sing n))
170− Nothing -> error "Negative singleton nat"
167+ Nothing -> error "Negative singleton nat"
171 DNGe n -> case someNatVal n of
168 DNGe n -> case someNatVal n of
172 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNGe (SNat :: Sing n))
169 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNGe (SNat :: Sing n))
173− Nothing -> error "Negative singleton nat"
170+ Nothing -> error "Negative singleton nat"
174 DNLt n -> case someNatVal n of
171 DNLt n -> case someNatVal n of
175 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNLt (SNat :: Sing n))
172 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNLt (SNat :: Sing n))
176− Nothing -> error "Negative singleton nat"
173+ Nothing -> error "Negative singleton nat"
177 DNLe n -> case someNatVal n of
174 DNLe n -> case someNatVal n of
178 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNLe (SNat :: Sing n))
175 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNLe (SNat :: Sing n))
179− Nothing -> error "Negative singleton nat"
176+ Nothing -> error "Negative singleton nat"
181data ArrayConstraint
178data ArrayConstraint
182 = AEq Nat
179 = AEq Nat
187 deriving (Generic)
180 deriving (Generic)
189data DemotedArrayConstraint
182data DemotedArrayConstraint
190 = DAEq Integer
183 = DAEq Integer
195 deriving (Generic, Eq, Show)
184 deriving (Generic, Eq, Show)
197data instance Sing (ac :: ArrayConstraint) where
186data instance Sing (ac :: ArrayConstraint) where
198 SAEq :: Sing n -> Sing ('AEq n)
187 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)
204instance KnownNat n => SingI ('AEq n) where sing = SAEq sing
189instance 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
210instance Eq (Sing ('AEq n)) where _ == _ = True
191instance 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
216instance SingKind ArrayConstraint where
193instance SingKind ArrayConstraint where
217 type Demote ArrayConstraint = DemotedArrayConstraint
194 type Demote ArrayConstraint = DemotedArrayConstraint
218 fromSing = \case
195 fromSing = \case
219 SAEq n -> withKnownNat n (DAEq . fromIntegral $ natVal n)
196 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)
224 toSing = \case
197 toSing = \case
225 DAEq n -> case someNatVal n of
198 DAEq n -> case someNatVal n of
226 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SAEq (SNat :: Sing n))
199 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))
239− Nothing -> error "Negative singleton nat"
200+ Nothing -> error "Negative singleton nat"
241data Schema
202data Schema
242 = SchemaText [TextConstraint]
203 = SchemaText [TextConstraint]
@@ -373,31 +334,6 @@ data JsonRepr :: Schema -> Type where
373 ReprOptional :: Maybe (JsonRepr s) -> JsonRepr ('SchemaOptional s)
334 ReprOptional :: Maybe (JsonRepr s) -> JsonRepr ('SchemaOptional s)
374 ReprUnion :: Union JsonRepr (h ': tl) -> JsonRepr ('SchemaUnion (h ': tl))
335 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
401-- | Move to the union package
337-- | Move to the union package
402instance Show (JsonRepr ('SchemaText cs)) where
338instance Show (JsonRepr ('SchemaText cs)) where
403 show (ReprText t) = "ReprText " P.++ show t
339 show (ReprText t) = "ReprText " P.++ show t
@@ -405,32 +341,17 @@ instance Show (JsonRepr ('SchemaText cs)) where
405instance Show (JsonRepr ('SchemaNumber cs)) where
341instance Show (JsonRepr ('SchemaNumber cs)) where
406 show (ReprNumber n) = "ReprNumber " P.++ show n
342 show (ReprNumber n) = "ReprNumber " P.++ show n
408−instance Show (JsonRepr 'SchemaBoolean) where
409− show (ReprBoolean n) = "ReprBoolean " P.++ show n
411instance Show (JsonRepr 'SchemaNull) where show _ = "ReprNull"
344instance Show (JsonRepr 'SchemaNull) where show _ = "ReprNull"
413instance Show (JsonRepr s) => Show (JsonRepr ('SchemaArray acs s)) where
346instance Show (JsonRepr s) => Show (JsonRepr ('SchemaArray acs s)) where
414 show (ReprArray v) = "ReprArray " P.++ show v
347 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
423instance V.RecAll FieldRepr fs Show => Show (JsonRepr ('SchemaObject fs)) where
349instance V.RecAll FieldRepr fs Show => Show (JsonRepr ('SchemaObject fs)) where
424 show (ReprObject fs) = "ReprObject " P.++ show fs
350 show (ReprObject fs) = "ReprObject " P.++ show fs
427instance Show (JsonRepr s) => Show (JsonRepr ('SchemaOptional s)) where
352instance Show (JsonRepr s) => Show (JsonRepr ('SchemaOptional s)) where
428 show (ReprOptional s) = "ReprOptional " P.++ show s
353 show (ReprOptional s) = "ReprOptional " P.++ show s
430−instance Show (Union JsonRepr (h ': tl))
431− => Show (JsonRepr ('SchemaUnion (h ': tl))) where
432− show (ReprUnion s) = "ReprUnion " P.++ show s
434instance Eq (Rec FieldRepr fs) => Eq (JsonRepr ('SchemaObject fs)) where
355instance Eq (Rec FieldRepr fs) => Eq (JsonRepr ('SchemaObject fs)) where
435 ReprObject a == ReprObject b = a == b
356 ReprObject a == ReprObject b = a == b
@@ -440,9 +361,6 @@ instance Eq (JsonRepr ('SchemaText cs)) where
440instance Eq (JsonRepr ('SchemaNumber cs)) where
361instance Eq (JsonRepr ('SchemaNumber cs)) where
441 ReprNumber a == ReprNumber b = a == b
362 ReprNumber a == ReprNumber b = a == b
443−instance Eq (JsonRepr 'SchemaBoolean) where
444− ReprBoolean a == ReprBoolean b = a == b
446instance Eq (JsonRepr 'SchemaNull) where
364instance Eq (JsonRepr 'SchemaNull) where
447 ReprNull == ReprNull = True
365 ReprNull == ReprNull = True
@@ -452,10 +370,6 @@ instance Eq (JsonRepr s) => Eq (JsonRepr ('SchemaArray as s)) where
452instance Eq (JsonRepr s) => Eq (JsonRepr ('SchemaOptional s)) where
370instance Eq (JsonRepr s) => Eq (JsonRepr ('SchemaOptional s)) where
453 ReprOptional a == ReprOptional b = a == b
371 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
459instance Ord (Rec FieldRepr fs) => Ord (JsonRepr ('SchemaObject fs)) where
373instance Ord (Rec FieldRepr fs) => Ord (JsonRepr ('SchemaObject fs)) where
460 ReprObject a `compare` ReprObject b = a `compare` b
374 ReprObject a `compare` ReprObject b = a `compare` b
@@ -465,9 +379,6 @@ instance Ord (JsonRepr ('SchemaText cs)) where
465instance Ord (JsonRepr ('SchemaNumber cs)) where
379instance Ord (JsonRepr ('SchemaNumber cs)) where
466 ReprNumber a `compare` ReprNumber b = a `compare` b
380 ReprNumber a `compare` ReprNumber b = a `compare` b
468−instance Ord (JsonRepr 'SchemaBoolean) where
469− ReprBoolean a `compare` ReprBoolean b = a `compare` b
471instance Ord (JsonRepr 'SchemaNull) where
382instance Ord (JsonRepr 'SchemaNull) where
472 compare _ _ = EQ
383 compare _ _ = EQ
@@ -477,10 +388,6 @@ instance Ord (JsonRepr s) => Ord (JsonRepr ('SchemaArray as s)) where
477instance Ord (JsonRepr s) => Ord (JsonRepr ('SchemaOptional s)) where
388instance Ord (JsonRepr s) => Ord (JsonRepr ('SchemaOptional s)) where
478 ReprOptional a `compare` ReprOptional b = a `compare` b
389 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
484instance IsList (JsonRepr ('SchemaArray cs s)) where
391instance IsList (JsonRepr ('SchemaArray cs s)) where
485 type Item (JsonRepr ('SchemaArray cs s)) = JsonRepr s
392 type Item (JsonRepr ('SchemaArray cs s)) = JsonRepr s
486 fromList = ReprArray . GHC.Exts.fromList
393 fromList = ReprArray . GHC.Exts.fromList
@@ -572,7 +479,7 @@ instance J.ToJSON (JsonRepr a) where
572 toJSON (ReprText t) = J.String t
479 toJSON (ReprText t) = J.String t
573 toJSON (ReprNumber n) = J.Number n
480 toJSON (ReprNumber n) = J.Number n
574 toJSON (ReprOptional s) = case s of
481 toJSON (ReprOptional s) = case s of
575− Just v -> toJSON v
482+ Just v -> toJSON v
576 Nothing -> J.Null
483 Nothing -> J.Null
577 toJSON (ReprArray v) = J.Array $ toJSON <$> v
484 toJSON (ReprArray v) = J.Array $ toJSON <$> v
578 toJSON (ReprObject r) = J.Object . H.fromList . fold $ r
485 toJSON (ReprObject r) = J.Object . H.fromList . fold $ r