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