@@ -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,8 +13,8 @@ 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
15−import Data.Schematic.Instances ()
16import Data.Schematic.Generator
16import Data.Schematic.Generator
17+import Data.Schematic.Instances ()
17import Data.Scientific
18import Data.Scientific
18import Data.Singletons.Prelude.List hiding (All, Union)
19import Data.Singletons.Prelude.List hiding (All, Union)
19import Data.Singletons.TH
20import Data.Singletons.TH
@@ -179,27 +180,63 @@ instance SingKind NumberConstraint where
180data ArrayConstraint
181data ArrayConstraint
181 = AEq Nat
182 = AEq Nat
182 deriving (Generic)
187 deriving (Generic)
184data DemotedArrayConstraint
189data DemotedArrayConstraint
185 = DAEq Integer
190 = DAEq Integer
186 deriving (Generic, Eq, Show)
195 deriving (Generic, Eq, Show)
188data instance Sing (ac :: ArrayConstraint) where
197data instance Sing (ac :: ArrayConstraint) where
189 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)
191instance 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
193instance 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
195instance SingKind ArrayConstraint where
216instance SingKind ArrayConstraint where
196 type Demote ArrayConstraint = DemotedArrayConstraint
217 type Demote ArrayConstraint = DemotedArrayConstraint
197 fromSing = \case
218 fromSing = \case
198 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)
199 toSing = \case
224 toSing = \case
200 DAEq n -> case someNatVal n of
225 DAEq n -> case someNatVal n of
201 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SAEq (SNat :: Sing n))
226 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SAEq (SNat :: Sing n))
202 Nothing -> error "Negative singleton nat"
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"
204data Schema
241data Schema
205 = SchemaText [TextConstraint]
242 = SchemaText [TextConstraint]
@@ -368,17 +405,32 @@ instance Show (JsonRepr ('SchemaText cs)) where
368instance Show (JsonRepr ('SchemaNumber cs)) where
405instance Show (JsonRepr ('SchemaNumber cs)) where
369 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
371instance Show (JsonRepr 'SchemaNull) where show _ = "ReprNull"
411instance Show (JsonRepr 'SchemaNull) where show _ = "ReprNull"
373instance Show (JsonRepr s) => Show (JsonRepr ('SchemaArray acs s)) where
413instance Show (JsonRepr s) => Show (JsonRepr ('SchemaArray acs s)) where
374 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
376instance V.RecAll FieldRepr fs Show => Show (JsonRepr ('SchemaObject fs)) where
423instance V.RecAll FieldRepr fs Show => Show (JsonRepr ('SchemaObject fs)) where
377 show (ReprObject fs) = "ReprObject " P.++ show fs
424 show (ReprObject fs) = "ReprObject " P.++ show fs
379instance Show (JsonRepr s) => Show (JsonRepr ('SchemaOptional s)) where
427instance Show (JsonRepr s) => Show (JsonRepr ('SchemaOptional s)) where
380 show (ReprOptional s) = "ReprOptional " P.++ show s
428 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
382instance Eq (Rec FieldRepr fs) => Eq (JsonRepr ('SchemaObject fs)) where
434instance Eq (Rec FieldRepr fs) => Eq (JsonRepr ('SchemaObject fs)) where
383 ReprObject a == ReprObject b = a == b
435 ReprObject a == ReprObject b = a == b
@@ -388,6 +440,9 @@ instance Eq (JsonRepr ('SchemaText cs)) where
388instance Eq (JsonRepr ('SchemaNumber cs)) where
440instance Eq (JsonRepr ('SchemaNumber cs)) where
389 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
391instance Eq (JsonRepr 'SchemaNull) where
446instance Eq (JsonRepr 'SchemaNull) where
392 ReprNull == ReprNull = True
447 ReprNull == ReprNull = True
@@ -397,6 +452,10 @@ instance Eq (JsonRepr s) => Eq (JsonRepr ('SchemaArray as s)) where
397instance Eq (JsonRepr s) => Eq (JsonRepr ('SchemaOptional s)) where
452instance Eq (JsonRepr s) => Eq (JsonRepr ('SchemaOptional s)) where
398 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
400instance Ord (Rec FieldRepr fs) => Ord (JsonRepr ('SchemaObject fs)) where
459instance Ord (Rec FieldRepr fs) => Ord (JsonRepr ('SchemaObject fs)) where
401 ReprObject a `compare` ReprObject b = a `compare` b
460 ReprObject a `compare` ReprObject b = a `compare` b
@@ -406,6 +465,9 @@ instance Ord (JsonRepr ('SchemaText cs)) where
406instance Ord (JsonRepr ('SchemaNumber cs)) where
465instance Ord (JsonRepr ('SchemaNumber cs)) where
407 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
409instance Ord (JsonRepr 'SchemaNull) where
471instance Ord (JsonRepr 'SchemaNull) where
410 compare _ _ = EQ
472 compare _ _ = EQ
@@ -415,6 +477,10 @@ instance Ord (JsonRepr s) => Ord (JsonRepr ('SchemaArray as s)) where
415instance Ord (JsonRepr s) => Ord (JsonRepr ('SchemaOptional s)) where
477instance Ord (JsonRepr s) => Ord (JsonRepr ('SchemaOptional s)) where
416 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
418instance IsList (JsonRepr ('SchemaArray cs s)) where
484instance IsList (JsonRepr ('SchemaArray cs s)) where
419 type Item (JsonRepr ('SchemaArray cs s)) = JsonRepr s
485 type Item (JsonRepr ('SchemaArray cs s)) = JsonRepr s
420 fromList = ReprArray . GHC.Exts.fromList
486 fromList = ReprArray . GHC.Exts.fromList