@@ -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
@@ -9,10 +10,10 @@ import Control.Applicative ((<|>))
9import Control.Monad
10import Control.Monad
10import Data.Aeson as J
11import Data.Aeson as J
11import Data.Aeson.Types as J
12import Data.Aeson.Types as J
13import Data.HashMap.Strict as H
13import Data.HashMap.Strict as H
14import Data.Kind
14import Data.Kind
15import Data.Maybe
15import Data.Maybe
16+import Data.Schematic.Generator
16import Data.Schematic.Instances ()
17import 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)
@@ -25,7 +26,8 @@ import Data.Vinyl hiding (Dict)
25import qualified Data.Vinyl.TypeLevel as V
26import qualified Data.Vinyl.TypeLevel as V
26import GHC.Exts
27import GHC.Exts
27import GHC.Generics (Generic)
28import GHC.Generics (Generic)
28−import GHC.TypeLits (SomeNat(..), SomeSymbol(..), someSymbolVal, someNatVal)
30+ (SomeNat(..), SomeSymbol(..), someNatVal, someSymbolVal)
29import Prelude as P
31import Prelude as P
30import Test.SmallCheck.Series
32import Test.SmallCheck.Series
@@ -49,33 +51,33 @@ data TextConstraint
49instance SingKind TextConstraint where
51instance SingKind TextConstraint where
50 type Demote TextConstraint = DemotedTextConstraint
52 type Demote TextConstraint = DemotedTextConstraint
51 fromSing = \case
53 fromSing = \case
52− STEq n -> withKnownNat n (DTEq $ natVal n)
54+ STEq n -> withKnownNat n (DTEq . fromIntegral $ natVal n)
53− STLt n -> withKnownNat n (DTLt $ natVal n)
55+ STLt n -> withKnownNat n (DTLt . fromIntegral $ natVal n)
54− STLe n -> withKnownNat n (DTLe $ natVal n)
56+ STLe n -> withKnownNat n (DTLe . fromIntegral $ natVal n)
55− STGt n -> withKnownNat n (DTGt $ natVal n)
57+ STGt n -> withKnownNat n (DTGt . fromIntegral $ natVal n)
56− STGe n -> withKnownNat n (DTGe $ natVal n)
58+ STGe n -> withKnownNat n (DTGe . fromIntegral $ natVal n)
57 STRegex s -> withKnownSymbol s (DTRegex $ T.pack $ symbolVal s)
59 STRegex s -> withKnownSymbol s (DTRegex $ T.pack $ symbolVal s)
58 STEnum s -> let
60 STEnum s -> let
59 d :: Sing (s :: [Symbol]) -> [Text]
61 d :: Sing (s :: [Symbol]) -> [Text]
60− d SNil = []
62+ d SNil = []
61 d (SCons ss@SSym fs) = T.pack (symbolVal ss) : d fs
63 d (SCons ss@SSym fs) = T.pack (symbolVal ss) : d fs
62 in DTEnum $ d s
64 in DTEnum $ d s
63 toSing = \case
65 toSing = \case
64 DTEq n -> case someNatVal n of
66 DTEq n -> case someNatVal n of
65 Just (SomeNat (_ :: Proxy n)) -> SomeSing (STEq (SNat :: Sing n))
67 Just (SomeNat (_ :: Proxy n)) -> SomeSing (STEq (SNat :: Sing n))
66− Nothing -> error "Negative singleton nat"
68+ Nothing -> error "Negative singleton nat"
67 DTLt n -> case someNatVal n of
69 DTLt n -> case someNatVal n of
68 Just (SomeNat (_ :: Proxy n)) -> SomeSing (STLt (SNat :: Sing n))
70 Just (SomeNat (_ :: Proxy n)) -> SomeSing (STLt (SNat :: Sing n))
69− Nothing -> error "Negative singleton nat"
71+ Nothing -> error "Negative singleton nat"
70 DTLe n -> case someNatVal n of
72 DTLe n -> case someNatVal n of
71 Just (SomeNat (_ :: Proxy n)) -> SomeSing (STLe (SNat :: Sing n))
73 Just (SomeNat (_ :: Proxy n)) -> SomeSing (STLe (SNat :: Sing n))
72− Nothing -> error "Negative singleton nat"
74+ Nothing -> error "Negative singleton nat"
73 DTGt n -> case someNatVal n of
75 DTGt n -> case someNatVal n of
74 Just (SomeNat (_ :: Proxy n)) -> SomeSing (STGt (SNat :: Sing n))
76 Just (SomeNat (_ :: Proxy n)) -> SomeSing (STGt (SNat :: Sing n))
75− Nothing -> error "Negative singleton nat"
77+ Nothing -> error "Negative singleton nat"
76 DTGe n -> case someNatVal n of
78 DTGe n -> case someNatVal n of
77 Just (SomeNat (_ :: Proxy n)) -> SomeSing (STGe (SNat :: Sing n))
79 Just (SomeNat (_ :: Proxy n)) -> SomeSing (STGe (SNat :: Sing n))
78− Nothing -> error "Negative singleton nat"
80+ Nothing -> error "Negative singleton nat"
79 DTRegex s -> case someSymbolVal (T.unpack s) of
81 DTRegex s -> case someSymbolVal (T.unpack s) of
80 SomeSymbol (_ :: Proxy n) -> SomeSing (STRegex (SSym :: Sing n))
82 SomeSymbol (_ :: Proxy n) -> SomeSing (STRegex (SSym :: Sing n))
81 DTEnum ss -> case toSing ss of
83 DTEnum ss -> case toSing ss of
@@ -89,7 +91,7 @@ data DemotedTextConstraint
89 | DTGe Integer
91 | DTGe Integer
90 | DTRegex Text
92 | DTRegex Text
91 | DTEnum [Text]
93 | DTEnum [Text]
92− deriving (Generic)
94+ deriving (Generic, Eq, Show)
94data instance Sing (tc :: TextConstraint) where
96data instance Sing (tc :: TextConstraint) where
95 STEq :: Sing n -> Sing ('TEq n)
97 STEq :: Sing n -> Sing ('TEq n)
@@ -130,7 +132,7 @@ data DemotedNumberConstraint
130 | DNGt Integer
132 | DNGt Integer
131 | DNGe Integer
133 | DNGe Integer
132 | DNEq Integer
134 | DNEq Integer
133− deriving (Generic)
135+ deriving (Generic, Eq, Show)
135data instance Sing (nc :: NumberConstraint) where
137data instance Sing (nc :: NumberConstraint) where
136 SNEq :: Sing n -> Sing ('NEq n)
138 SNEq :: Sing n -> Sing ('NEq n)
@@ -154,51 +156,87 @@ instance Eq (Sing ('NGe n)) where _ == _ = True
154instance SingKind NumberConstraint where
156instance SingKind NumberConstraint where
155 type Demote NumberConstraint = DemotedNumberConstraint
157 type Demote NumberConstraint = DemotedNumberConstraint
156 fromSing = \case
158 fromSing = \case
157− SNEq n -> withKnownNat n (DNEq $ natVal n)
159+ SNEq n -> withKnownNat n (DNEq . fromIntegral $ natVal n)
158− SNGt n -> withKnownNat n (DNGt $ natVal n)
160+ SNGt n -> withKnownNat n (DNGt . fromIntegral $ natVal n)
159− SNGe n -> withKnownNat n (DNGe $ natVal n)
161+ SNGe n -> withKnownNat n (DNGe . fromIntegral $ natVal n)
160− SNLt n -> withKnownNat n (DNLt $ natVal n)
162+ SNLt n -> withKnownNat n (DNLt . fromIntegral $ natVal n)
161− SNLe n -> withKnownNat n (DNLe $ natVal n)
163+ SNLe n -> withKnownNat n (DNLe . fromIntegral $ natVal n)
162 toSing = \case
164 toSing = \case
163 DNEq n -> case someNatVal n of
165 DNEq n -> case someNatVal n of
164 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNEq (SNat :: Sing n))
166 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNEq (SNat :: Sing n))
165− Nothing -> error "Negative singleton nat"
167+ Nothing -> error "Negative singleton nat"
166 DNGt n -> case someNatVal n of
168 DNGt n -> case someNatVal n of
167 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNGt (SNat :: Sing n))
169 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNGt (SNat :: Sing n))
168− Nothing -> error "Negative singleton nat"
170+ Nothing -> error "Negative singleton nat"
169 DNGe n -> case someNatVal n of
171 DNGe n -> case someNatVal n of
170 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNGe (SNat :: Sing n))
172 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNGe (SNat :: Sing n))
171− Nothing -> error "Negative singleton nat"
173+ Nothing -> error "Negative singleton nat"
172 DNLt n -> case someNatVal n of
174 DNLt n -> case someNatVal n of
173 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNLt (SNat :: Sing n))
175 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNLt (SNat :: Sing n))
174− Nothing -> error "Negative singleton nat"
176+ Nothing -> error "Negative singleton nat"
175 DNLe n -> case someNatVal n of
177 DNLe n -> case someNatVal n of
176 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNLe (SNat :: Sing n))
178 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNLe (SNat :: Sing n))
177− Nothing -> error "Negative singleton nat"
179+ Nothing -> error "Negative singleton nat"
179data ArrayConstraint
181data ArrayConstraint
180 = AEq Nat
182 = AEq Nat
181 deriving (Generic)
187 deriving (Generic)
183data DemotedArrayConstraint
189data DemotedArrayConstraint
184 = DAEq Integer
190 = DAEq Integer
185− deriving (Generic)
195+ deriving (Generic, Eq, Show)
187data instance Sing (ac :: ArrayConstraint) where
197data instance Sing (ac :: ArrayConstraint) where
188 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)
190instance 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
192instance 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
194instance SingKind ArrayConstraint where
216instance SingKind ArrayConstraint where
195 type Demote ArrayConstraint = DemotedArrayConstraint
217 type Demote ArrayConstraint = DemotedArrayConstraint
196 fromSing = \case
218 fromSing = \case
197− SAEq n -> withKnownNat n (DAEq $ 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)
198 toSing = \case
224 toSing = \case
199 DAEq n -> case someNatVal n of
225 DAEq n -> case someNatVal n of
200 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))
201− Nothing -> error "Negative singleton nat"
239+ Nothing -> error "Negative singleton nat"
203data Schema
241data Schema
204 = SchemaText [TextConstraint]
242 = SchemaText [TextConstraint]
@@ -220,7 +258,7 @@ data DemotedSchema
220 | DSchemaNull
258 | DSchemaNull
221 | DSchemaOptional DemotedSchema
259 | DSchemaOptional DemotedSchema
222 | DSchemaUnion [DemotedSchema]
260 | DSchemaUnion [DemotedSchema]
223− deriving (Generic)
261+ deriving (Generic, Eq, Show)
225data instance Sing (schema :: Schema) where
263data instance Sing (schema :: Schema) where
226 SSchemaText :: Sing tcs -> Sing ('SchemaText tcs)
264 SSchemaText :: Sing tcs -> Sing ('SchemaText tcs)
@@ -325,122 +363,128 @@ type family USubsets (u :: [k]) :: Constraint where
325 USubsets '[] = ()
363 USubsets '[] = ()
326 USubsets (h ': tl) = (USubset tl (h ': tl) (V.RImage tl (h ': tl)), USubsets tl)
364 USubsets (h ': tl) = (USubset tl (h ': tl) (V.RImage tl (h ': tl)), USubsets tl)
328−class JsonRepresentable (schema :: Schema) where
329− type ReprType schema :: *
330− data JsonRepr schema :: *
331− wrap :: ReprType schema -> JsonRepr schema
333−instance JsonRepresentable ('SchemaText cs) where
334− type ReprType ('SchemaText cs) = Text
335− newtype JsonRepr ('SchemaText cs) = ReprText (ReprType ('SchemaText cs))
338−instance JsonRepresentable ('SchemaNumber cs) where
339− type ReprType ('SchemaNumber cs) = Scientific
340− newtype JsonRepr ('SchemaNumber cs) = ReprNumber (ReprType ('SchemaNumber cs))
343−instance JsonRepresentable 'SchemaBoolean where
344− type ReprType 'SchemaBoolean = Bool
345− newtype JsonRepr 'SchemaBoolean = ReprBoolean (ReprType 'SchemaBoolean)
348−instance JsonRepresentable 'SchemaNull where
349− type ReprType 'SchemaNull = ()
350− newtype JsonRepr 'SchemaNull = ReprNull (ReprType 'SchemaNull)
353−instance JsonRepresentable s => JsonRepresentable ('SchemaArray cs s) where
354− type ReprType ('SchemaArray cs s) = V.Vector (JsonRepr s)
355− newtype JsonRepr ('SchemaArray cs s) = ReprArray (ReprType ('SchemaArray cs s))
358−instance JsonRepresentable ('SchemaObject fs) where
359− type ReprType ('SchemaObject fs) = Rec FieldRepr fs
360− newtype JsonRepr ('SchemaObject fs) = ReprObject (ReprType ('SchemaObject fs))
363−instance JsonRepresentable s => JsonRepresentable ('SchemaOptional s) where
364− type ReprType ('SchemaOptional s) = Maybe (JsonRepr s)
365− newtype JsonRepr ('SchemaOptional s) = ReprOptional (ReprType ('SchemaOptional s))
368−instance JsonRepresentable ('SchemaUnion (h ': tl)) where
369− type ReprType ('SchemaUnion (h ': tl)) = Union JsonRepr (h ': tl)
370− newtype JsonRepr ('SchemaUnion (h ': tl)) = ReprUnion (ReprType ('SchemaUnion (h ': tl)))
373−-- data JsonRepr :: Schema -> Type where
366+data JsonRepr :: Schema -> Type where
374−-- ReprText :: Text -> JsonRepr ('SchemaText cs)
367+ ReprText :: Text -> JsonRepr ('SchemaText cs)
375−-- ReprNumber :: Scientific -> JsonRepr ('SchemaNumber cs)
368+ ReprNumber :: Scientific -> JsonRepr ('SchemaNumber cs)
376−-- ReprBoolean :: Bool -> JsonRepr 'SchemaBoolean
369+ ReprBoolean :: Bool -> JsonRepr 'SchemaBoolean
377−-- ReprNull :: JsonRepr 'SchemaNull
370+ ReprNull :: JsonRepr 'SchemaNull
378−-- ReprArray :: V.Vector (JsonRepr s) -> JsonRepr ('SchemaArray cs s)
371+ ReprArray :: V.Vector (JsonRepr s) -> JsonRepr ('SchemaArray cs s)
379−-- ReprObject :: Rec FieldRepr fs -> JsonRepr ('SchemaObject fs)
372+ ReprObject :: Rec FieldRepr fs -> JsonRepr ('SchemaObject fs)
380−-- ReprOptional :: Maybe (JsonRepr s) -> JsonRepr ('SchemaOptional s)
373+ ReprOptional :: Maybe (JsonRepr s) -> JsonRepr ('SchemaOptional s)
381−-- 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
383-- | Move to the union package
401-- | Move to the union package
384−-- instance Show (JsonRepr ('SchemaText cs)) where
402+instance Show (JsonRepr ('SchemaText cs)) where
385−-- show (ReprText t) = "ReprText " P.++ show t
403+ show (ReprText t) = "ReprText " P.++ show t
405+instance Show (JsonRepr ('SchemaNumber cs)) where
406+ show (ReprNumber n) = "ReprNumber " P.++ show n
408+instance Show (JsonRepr 'SchemaBoolean) where
409+ show (ReprBoolean n) = "ReprBoolean " P.++ show n
411+instance Show (JsonRepr 'SchemaNull) where show _ = "ReprNull"
413+instance Show (JsonRepr s) => Show (JsonRepr ('SchemaArray acs s)) where
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
423+instance V.RecAll FieldRepr fs Show => Show (JsonRepr ('SchemaObject fs)) where
424+ show (ReprObject fs) = "ReprObject " P.++ show fs
427+instance Show (JsonRepr s) => Show (JsonRepr ('SchemaOptional s)) where
428+ show (ReprOptional s) = "ReprOptional " P.++ show s
387−-- instance Show (JsonRepr ('SchemaNumber cs)) where
430+instance Show (Union JsonRepr (h ': tl))
388−-- show (ReprNumber n) = "ReprNumber " P.++ show n
431+ => Show (JsonRepr ('SchemaUnion (h ': tl))) where
432+ show (ReprUnion s) = "ReprUnion " P.++ show s
390−-- instance Show (JsonRepr 'SchemaNull) where show _ = "ReprNull"
434+instance Eq (Rec FieldRepr fs) => Eq (JsonRepr ('SchemaObject fs)) where
435+ ReprObject a == ReprObject b = a == b
392−-- instance Show (JsonRepr s) => Show (JsonRepr ('SchemaArray acs s)) where
437+instance Eq (JsonRepr ('SchemaText cs)) where
393−-- show (ReprArray v) = "ReprArray " P.++ show v
438+ ReprText a == ReprText b = a == b
395−-- instance V.RecAll FieldRepr fs Show => Show (JsonRepr ('SchemaObject fs)) where
440+instance Eq (JsonRepr ('SchemaNumber cs)) where
396−-- show (ReprObject fs) = "ReprObject " P.++ show fs
441+ ReprNumber a == ReprNumber b = a == b
398−-- instance Show (JsonRepr s) => Show (JsonRepr ('SchemaOptional s)) where
443+instance Eq (JsonRepr 'SchemaBoolean) where
399−-- show (ReprOptional s) = "ReprOptional " P.++ show s
444+ ReprBoolean a == ReprBoolean b = a == b
401−-- instance (Monad m, Serial m Text)
402−-- => Serial m (JsonRepr ('SchemaText cs)) where
446+instance Eq (JsonRepr 'SchemaNull) where
403−-- series = cons1 ReprText
447+ ReprNull == ReprNull = True
405−-- instance (Monad m, Serial m Scientific)
449+instance Eq (JsonRepr s) => Eq (JsonRepr ('SchemaArray as s)) where
406−-- => Serial m (JsonRepr ('SchemaNumber cs)) where
450+ ReprArray a == ReprArray b = a == b
407−-- series = cons1 ReprNumber
409−-- instance Monad m => Serial m (JsonRepr 'SchemaNull) where
452+instance Eq (JsonRepr s) => Eq (JsonRepr ('SchemaOptional s)) where
410−-- series = cons0 ReprNull
453+ ReprOptional a == ReprOptional b = a == b
412−-- instance (Serial m (V.Vector (JsonRepr s)))
455+instance Eq (Union JsonRepr (h ': tl))
413−-- => Serial m (JsonRepr ('SchemaArray cs s)) where
456+ => Eq (JsonRepr ('SchemaUnion (h ': tl))) where
414−-- series = cons1 ReprArray
457+ ReprUnion a == ReprUnion b = a == b
416−-- instance (Serial m (JsonRepr s))
459+instance Ord (Rec FieldRepr fs) => Ord (JsonRepr ('SchemaObject fs)) where
417−-- => Serial m (JsonRepr ('SchemaOptional s)) where
460+ ReprObject a `compare` ReprObject b = a `compare` b
418−-- series = cons1 ReprOptional
420−-- instance (Monad m, Serial m (Rec FieldRepr fs))
462+instance Ord (JsonRepr ('SchemaText cs)) where
421−-- => Serial m (JsonRepr ('SchemaObject fs)) where
463+ ReprText a `compare` ReprText b = a `compare` b
422−-- series = cons1 ReprObject
424−-- instance Eq (Rec FieldRepr fs) => Eq (JsonRepr ('SchemaObject fs)) where
465+instance Ord (JsonRepr ('SchemaNumber cs)) where
425−-- ReprObject a == ReprObject b = a == b
466+ ReprNumber a `compare` ReprNumber b = a `compare` b
427−-- instance Eq (JsonRepr ('SchemaText cs))
468+instance Ord (JsonRepr 'SchemaBoolean) where
469+ ReprBoolean a `compare` ReprBoolean b = a `compare` b
429−-- instance Eq (JsonRepr ('SchemaNumber cs))
471+instance Ord (JsonRepr 'SchemaNull) where
431−-- instance Eq (JsonRepr 'SchemaNull) where
474+instance Ord (JsonRepr s) => Ord (JsonRepr ('SchemaArray as s)) where
432−-- ReprNull == ReprNull = True
475+ ReprArray a `compare` ReprArray b = a `compare` b
434−-- instance Eq (JsonRepr s) => Eq (JsonRepr ('SchemaArray as s)) where
477+instance Ord (JsonRepr s) => Ord (JsonRepr ('SchemaOptional s)) where
435−-- ReprArray a == ReprArray b = a == b
478+ ReprOptional a `compare` ReprOptional b = a `compare` b
437−-- instance Eq (JsonRepr s) => Eq (JsonRepr ('SchemaOptional s)) where
480+instance Ord (Union JsonRepr (h ': tl))
438−-- ReprOptional a == ReprOptional b = a == b
481+ => Ord (JsonRepr ('SchemaUnion (h ': tl))) where
482+ ReprUnion a `compare` ReprUnion b = a `compare` b
440instance IsList (JsonRepr ('SchemaArray cs s)) where
484instance IsList (JsonRepr ('SchemaArray cs s)) where
441 type Item (JsonRepr ('SchemaArray cs s)) = JsonRepr s
485 type Item (JsonRepr ('SchemaArray cs s)) = JsonRepr s
442− fromList = GHC.Exts.fromList
486+ fromList = ReprArray . GHC.Exts.fromList
443− toList v = GHC.Exts.toList v
487+ toList (ReprArray v) = GHC.Exts.toList v
445instance Num (JsonRepr ('SchemaNumber cs)) where
489instance Num (JsonRepr ('SchemaNumber cs)) where
446 ReprNumber a + ReprNumber b = ReprNumber $ a + b
490 ReprNumber a + ReprNumber b = ReprNumber $ a + b
@@ -451,7 +495,7 @@ instance Num (JsonRepr ('SchemaNumber cs)) where
451 fromInteger = ReprNumber . fromIntegral
495 fromInteger = ReprNumber . fromIntegral
453instance IsString (JsonRepr ('SchemaText cs)) where
497instance IsString (JsonRepr ('SchemaText cs)) where
454− fromString = fromString
498+ fromString = ReprText . fromString
456fromOptional
500fromOptional
457 :: SingI s
501 :: SingI s
@@ -484,7 +528,7 @@ instance SingI schema => J.FromJSON (JsonRepr schema) where
484 SSchemaNumber _ -> withScientific "Number" (pure . ReprNumber) value
528 SSchemaNumber _ -> withScientific "Number" (pure . ReprNumber) value
485 SSchemaBoolean -> ReprBoolean <$> parseJSON value
529 SSchemaBoolean -> ReprBoolean <$> parseJSON value
486 SSchemaNull -> case value of
530 SSchemaNull -> case value of
487− J.Null -> pure (wrap ())
531+ J.Null -> pure ReprNull
488 _ -> typeMismatch "Null" value
532 _ -> typeMismatch "Null" value
489 so@(SSchemaOptional s) -> withSingI s $ ReprOptional <$> fromOptional so value
533 so@(SSchemaOptional s) -> withSingI s $ ReprOptional <$> fromOptional so value
490 SSchemaArray sa sb -> withSingI sa $ withSingI sb
534 SSchemaArray sa sb -> withSingI sa $ withSingI sb
@@ -498,37 +542,37 @@ instance SingI schema => J.FromJSON (JsonRepr schema) where
498 fieldRepr <- case s of
542 fieldRepr <- case s of
499 SSchemaText so -> case H.lookup fieldName h of
543 SSchemaText so -> case H.lookup fieldName h of
500 Just v -> withSingI so $ FieldRepr <$> parseJSON v
544 Just v -> withSingI so $ FieldRepr <$> parseJSON v
501− Nothing -> fail "schematext"
545+ Nothing -> fail $ "No text field: " P.++ show fieldName
502 SSchemaNumber so -> case H.lookup fieldName h of
546 SSchemaNumber so -> case H.lookup fieldName h of
503 Just v -> withSingI so $ FieldRepr <$> parseJSON v
547 Just v -> withSingI so $ FieldRepr <$> parseJSON v
504− Nothing -> fail "schemanumber"
548+ Nothing -> fail $ "No number field: " P.++ show fieldName
505 SSchemaBoolean -> case H.lookup fieldName h of
549 SSchemaBoolean -> case H.lookup fieldName h of
506 Just v -> FieldRepr <$> parseJSON v
550 Just v -> FieldRepr <$> parseJSON v
507− Nothing -> fail "schemaboolean"
551+ Nothing -> fail $ "No boolean field: " P.++ show fieldName
508 SSchemaNull -> case H.lookup fieldName h of
552 SSchemaNull -> case H.lookup fieldName h of
509 Just v -> FieldRepr <$> parseJSON v
553 Just v -> FieldRepr <$> parseJSON v
510− Nothing -> fail "schemanull"
554+ Nothing -> fail $ "No null field: " P.++ show fieldName
511 SSchemaArray sa sb -> case H.lookup fieldName h of
555 SSchemaArray sa sb -> case H.lookup fieldName h of
512 Just v -> withSingI sa $ withSingI sb $ FieldRepr <$> parseJSON v
556 Just v -> withSingI sa $ withSingI sb $ FieldRepr <$> parseJSON v
513− Nothing -> fail "schemaarray"
557+ Nothing -> fail $ "No array field: " P.++ show fieldName
514 SSchemaObject so -> case H.lookup fieldName h of
558 SSchemaObject so -> case H.lookup fieldName h of
515 Just v -> withSingI so $ FieldRepr <$> parseJSON v
559 Just v -> withSingI so $ FieldRepr <$> parseJSON v
516− Nothing -> fail "schemaobject"
560+ Nothing -> fail $ "No object field" P.++ show fieldName
517 SSchemaOptional so -> case H.lookup fieldName h of
561 SSchemaOptional so -> case H.lookup fieldName h of
518 Just v -> withSingI so $ FieldRepr <$> parseJSON v
562 Just v -> withSingI so $ FieldRepr <$> parseJSON v
519− Nothing -> fail "schemaoptional"
563+ Nothing -> withSingI so $ pure $ FieldRepr $ ReprOptional Nothing
520 SSchemaUnion ss -> withSingI ss $ FieldRepr <$> parseUnion ss value
564 SSchemaUnion ss -> withSingI ss $ FieldRepr <$> parseUnion ss value
521 (fieldRepr :&) <$> demoteFields tl h
565 (fieldRepr :&) <$> demoteFields tl h
522 ReprObject <$> withObject "Object" (demoteFields fs) value
566 ReprObject <$> withObject "Object" (demoteFields fs) value
523 SSchemaUnion ss -> parseUnion ss value
567 SSchemaUnion ss -> parseUnion ss value
525−instance J.ToJSON (JsonRepr schema) where
569+instance J.ToJSON (JsonRepr a) where
526− toJSON (ReprNull _) = J.Null
570+ toJSON ReprNull = J.Null
527 toJSON (ReprBoolean b) = J.Bool b
571 toJSON (ReprBoolean b) = J.Bool b
528 toJSON (ReprText t) = J.String t
572 toJSON (ReprText t) = J.String t
529 toJSON (ReprNumber n) = J.Number n
573 toJSON (ReprNumber n) = J.Number n
530 toJSON (ReprOptional s) = case s of
574 toJSON (ReprOptional s) = case s of
531− Just v -> toJSON v
575+ Just v -> toJSON v
532 Nothing -> J.Null
576 Nothing -> J.Null
533 toJSON (ReprArray v) = J.Array $ toJSON <$> v
577 toJSON (ReprArray v) = J.Array $ toJSON <$> v
534 toJSON (ReprObject r) = J.Object . H.fromList . fold $ r
578 toJSON (ReprObject r) = J.Object . H.fromList . fold $ r