@@ -1,6 +1,7 @@
11{-# LANGUAGE AllowAmbiguousTypes #-}
2−{-# LANGUAGE OverloadedLists #-}
3−{-# LANGUAGE OverloadedStrings #-}
2+{-# LANGUAGE CPP #-}
3+{-# LANGUAGE OverloadedLists #-}
4+{-# LANGUAGE OverloadedStrings #-}
45{-# OPTIONS_GHC -fprint-explicit-kinds #-}
56
67module Data.Schematic.Schema where
@@ -12,8 +13,8 @@ import Data.Aeson.Types as J
1213import Data.HashMap.Strict as H
1314import Data.Kind
1415import Data.Maybe
15−import Data.Schematic.Instances ()
1616import Data.Schematic.Generator
17+import Data.Schematic.Instances ()
1718import Data.Scientific
1819import Data.Singletons.Prelude.List hiding (All, Union)
1920import Data.Singletons.TH
@@ -179,27 +180,63 @@ instance SingKind NumberConstraint where
179180
180181data ArrayConstraint
181182 = AEq Nat
183+ | AGt Nat
184+ | AGe Nat
185+ | ALt Nat
186+ | ALe Nat
182187 deriving (Generic)
183188
184189data DemotedArrayConstraint
185190 = DAEq Integer
191+ | DAGt Integer
192+ | DAGe Integer
193+ | DALt Integer
194+ | DALe Integer
186195 deriving (Generic, Eq, Show)
187196
188197data instance Sing (ac :: ArrayConstraint) where
189198 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)
190203
191204instance 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
192209
193210instance 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
194215
195216instance SingKind ArrayConstraint where
196217 type Demote ArrayConstraint = DemotedArrayConstraint
197218 fromSing = \case
198219 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)
199224 toSing = \case
200225 DAEq n -> case someNatVal n of
201226 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SAEq (SNat :: Sing n))
202227 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"
203240
204241data Schema
205242 = SchemaText [TextConstraint]
@@ -368,17 +405,32 @@ instance Show (JsonRepr ('SchemaText cs)) where
368405instance Show (JsonRepr ('SchemaNumber cs)) where
369406 show (ReprNumber n) = "ReprNumber " P.++ show n
370407
408+instance Show (JsonRepr 'SchemaBoolean) where
409+ show (ReprBoolean n) = "ReprBoolean " P.++ show n
410+
371411instance Show (JsonRepr 'SchemaNull) where show _ = "ReprNull"
372412
373413instance Show (JsonRepr s) => Show (JsonRepr ('SchemaArray acs s)) where
374414 show (ReprArray v) = "ReprArray " P.++ show v
375415
416+#if MIN_VERSION_base(4,12,0)
417+instance
418+ ( V.RecAll FieldRepr fs Show, RMap fs, ReifyConstraint Show FieldRepr fs
419+ , RecordToList fs )
420+ => Show (JsonRepr ('SchemaObject fs)) where
421+ show (ReprObject fs) = "ReprObject " P.++ show fs
422+#else
376423instance V.RecAll FieldRepr fs Show => Show (JsonRepr ('SchemaObject fs)) where
377424 show (ReprObject fs) = "ReprObject " P.++ show fs
425+#endif
378426
379427instance Show (JsonRepr s) => Show (JsonRepr ('SchemaOptional s)) where
380428 show (ReprOptional s) = "ReprOptional " P.++ show s
381429
430+instance Show (Union JsonRepr (h ': tl))
431+ => Show (JsonRepr ('SchemaUnion (h ': tl))) where
432+ show (ReprUnion s) = "ReprUnion " P.++ show s
433+
382434instance Eq (Rec FieldRepr fs) => Eq (JsonRepr ('SchemaObject fs)) where
383435 ReprObject a == ReprObject b = a == b
384436
@@ -388,6 +440,9 @@ instance Eq (JsonRepr ('SchemaText cs)) where
388440instance Eq (JsonRepr ('SchemaNumber cs)) where
389441 ReprNumber a == ReprNumber b = a == b
390442
443+instance Eq (JsonRepr 'SchemaBoolean) where
444+ ReprBoolean a == ReprBoolean b = a == b
445+
391446instance Eq (JsonRepr 'SchemaNull) where
392447 ReprNull == ReprNull = True
393448
@@ -397,6 +452,10 @@ instance Eq (JsonRepr s) => Eq (JsonRepr ('SchemaArray as s)) where
397452instance Eq (JsonRepr s) => Eq (JsonRepr ('SchemaOptional s)) where
398453 ReprOptional a == ReprOptional b = a == b
399454
455+instance Eq (Union JsonRepr (h ': tl))
456+ => Eq (JsonRepr ('SchemaUnion (h ': tl))) where
457+ ReprUnion a == ReprUnion b = a == b
458+
400459instance Ord (Rec FieldRepr fs) => Ord (JsonRepr ('SchemaObject fs)) where
401460 ReprObject a `compare` ReprObject b = a `compare` b
402461
@@ -406,6 +465,9 @@ instance Ord (JsonRepr ('SchemaText cs)) where
406465instance Ord (JsonRepr ('SchemaNumber cs)) where
407466 ReprNumber a `compare` ReprNumber b = a `compare` b
408467
468+instance Ord (JsonRepr 'SchemaBoolean) where
469+ ReprBoolean a `compare` ReprBoolean b = a `compare` b
470+
409471instance Ord (JsonRepr 'SchemaNull) where
410472 compare _ _ = EQ
411473
@@ -415,6 +477,10 @@ instance Ord (JsonRepr s) => Ord (JsonRepr ('SchemaArray as s)) where
415477instance Ord (JsonRepr s) => Ord (JsonRepr ('SchemaOptional s)) where
416478 ReprOptional a `compare` ReprOptional b = a `compare` b
417479
480+instance Ord (Union JsonRepr (h ': tl))
481+ => Ord (JsonRepr ('SchemaUnion (h ': tl))) where
482+ ReprUnion a `compare` ReprUnion b = a `compare` b
483+
418484instance IsList (JsonRepr ('SchemaArray cs s)) where
419485 type Item (JsonRepr ('SchemaArray cs s)) = JsonRepr s
420486 fromList = ReprArray . GHC.Exts.fromList