@@ -1,351 +1,141 @@
11{-# LANGUAGE AllowAmbiguousTypes #-}
2−{-# LANGUAGE CPP #-}
3−{-# LANGUAGE OverloadedLists #-}
4−{-# LANGUAGE OverloadedStrings #-}
2+{-# LANGUAGE OverloadedLists #-}
3+{-# LANGUAGE OverloadedStrings #-}
54{-# OPTIONS_GHC -fprint-explicit-kinds #-}
65
76module Data.Schematic.Schema where
87
9−import Control.Applicative ((<|>))
10−import Control.Monad
11−import Data.Aeson as J
12−import Data.Aeson.Types as J
13−import Data.HashMap.Strict as H
14−import Data.Kind
15−import Data.Maybe
16−import Data.Schematic.Generator
17−import Data.Schematic.Instances ()
18−import Data.Scientific
19−import Data.Singletons.Prelude.List hiding (All, Union)
20−import Data.Singletons.TH
21−import Data.Singletons.TypeLits
22−import Data.Text as T
23−import Data.Union
24−import Data.Vector as V
25−import Data.Vinyl hiding (Dict)
26−import qualified Data.Vinyl.TypeLevel as V
27−import GHC.Exts
28−import GHC.Generics (Generic)
29−import GHC.TypeLits
30− (SomeNat(..), SomeSymbol(..), someNatVal, someSymbolVal)
31−import Prelude as P
32−import Test.SmallCheck.Series
8+import Control.Applicative
9+import Control.Category ((<<<), (>>>))
10+import Control.Monad
11+import Control.Monad.Validation
12+import Data.Aeson as J
13+import Data.Aeson.Types as J
14+import Data.Eq.Deriving (deriveEq1)
15+import Data.Foldable as F
16+import Data.Functor.Classes
17+import Data.HashMap.Strict as H
18+import Data.Kind
19+import Data.Maybe
20+import Data.Schematic.Instances
21+import Data.Schematic.Utils
22+import Data.Scientific
23+import Data.Singletons.Decide
24+import Data.Singletons.Prelude.List
25+import Data.Singletons.TH
26+import Data.Singletons.TypeLits
27+import Data.Text as T
28+import Data.Vector as V
29+import Data.Vinyl hiding (Dict)
30+import Data.Vinyl.Functor
31+import Data.Vinyl.TypeLevel hiding (Nat)
32+import GHC.Generics (Generic)
33+import Prelude as P
34+import Test.SmallCheck.Series
35+import Text.Show.Deriving (deriveShow1)
3336
3437
3538type family CRepr (s :: Schema) :: Type where
36− CRepr ('SchemaText cs) = TextConstraint
37− CRepr ('SchemaNumber cs) = NumberConstraint
38− CRepr ('SchemaObject fs) = (String, Schema)
39− CRepr ('SchemaArray ar s) = ArrayConstraint
39+ CRepr (SchemaText cs) = TextConstraint
40+ CRepr (SchemaNumber cs) = NumberConstraint
41+ CRepr (SchemaObject fs) = (String, Schema)
42+ CRepr (SchemaArray ar s) = ArrayConstraint
4043
4144data TextConstraint
4245 = TEq Nat
43− | TLt Nat
4446 | TLe Nat
4547 | TGt Nat
46− | TGe Nat
4748 | TRegex Symbol
48− | TEnum [Symbol]
4949 deriving (Generic)
5050
51−instance SingKind TextConstraint where
52− type Demote TextConstraint = DemotedTextConstraint
53− fromSing = \case
54− STEq n -> withKnownNat n (DTEq . fromIntegral $ natVal n)
55− STLt n -> withKnownNat n (DTLt . fromIntegral $ natVal n)
56− STLe n -> withKnownNat n (DTLe . fromIntegral $ natVal n)
57− STGt n -> withKnownNat n (DTGt . fromIntegral $ natVal n)
58− STGe n -> withKnownNat n (DTGe . fromIntegral $ natVal n)
59− STRegex s -> withKnownSymbol s (DTRegex $ T.pack $ symbolVal s)
60− STEnum s -> let
61− d :: Sing (s :: [Symbol]) -> [Text]
62− d SNil = []
63− d (SCons ss@SSym fs) = T.pack (symbolVal ss) : d fs
64− in DTEnum $ d s
65− toSing = \case
66− DTEq n -> case someNatVal n of
67− Just (SomeNat (_ :: Proxy n)) -> SomeSing (STEq (SNat :: Sing n))
68− Nothing -> error "Negative singleton nat"
69− DTLt n -> case someNatVal n of
70− Just (SomeNat (_ :: Proxy n)) -> SomeSing (STLt (SNat :: Sing n))
71− Nothing -> error "Negative singleton nat"
72− DTLe n -> case someNatVal n of
73− Just (SomeNat (_ :: Proxy n)) -> SomeSing (STLe (SNat :: Sing n))
74− Nothing -> error "Negative singleton nat"
75− DTGt n -> case someNatVal n of
76− Just (SomeNat (_ :: Proxy n)) -> SomeSing (STGt (SNat :: Sing n))
77− Nothing -> error "Negative singleton nat"
78− DTGe n -> case someNatVal n of
79− Just (SomeNat (_ :: Proxy n)) -> SomeSing (STGe (SNat :: Sing n))
80− Nothing -> error "Negative singleton nat"
81− DTRegex s -> case someSymbolVal (T.unpack s) of
82− SomeSymbol (_ :: Proxy n) -> SomeSing (STRegex (SSym :: Sing n))
83− DTEnum ss -> case toSing ss of
84− SomeSing l -> SomeSing (STEnum l)
85−
86−data DemotedTextConstraint
87− = DTEq Integer
88− | DTLt Integer
89− | DTLe Integer
90− | DTGt Integer
91− | DTGe Integer
92− | DTRegex Text
93− | DTEnum [Text]
94− deriving (Generic, Eq, Show)
95−
9651data instance Sing (tc :: TextConstraint) where
97− STEq :: Sing n -> Sing ('TEq n)
98− STLt :: Sing n -> Sing ('TLt n)
99− STLe :: Sing n -> Sing ('TLe n)
100− STGt :: Sing n -> Sing ('TGt n)
101− STGe :: Sing n -> Sing ('TGe n)
102− STRegex :: Sing s -> Sing ('TRegex s)
103− STEnum :: Sing ss -> Sing ('TEnum ss)
104−
105−instance (KnownNat n) => SingI ('TEq n) where sing = STEq sing
106−instance (KnownNat n) => SingI ('TGt n) where sing = STGt sing
107−instance (KnownNat n) => SingI ('TGe n) where sing = STGe sing
108−instance (KnownNat n) => SingI ('TLt n) where sing = STLt sing
109−instance (KnownNat n) => SingI ('TLe n) where sing = STLe sing
110−instance (KnownSymbol s, SingI s) => SingI ('TRegex s) where sing = STRegex sing
111−instance (SingI ss) => SingI ('TEnum ss) where sing = STEnum sing
112−
113−instance Eq (Sing ('TEq n)) where _ == _ = True
114−instance Eq (Sing ('TLt n)) where _ == _ = True
115−instance Eq (Sing ('TLe n)) where _ == _ = True
116−instance Eq (Sing ('TGt n)) where _ == _ = True
117−instance Eq (Sing ('TGe n)) where _ == _ = True
118−instance Eq (Sing ('TRegex t)) where _ == _ = True
119−instance Eq (Sing ('TEnum ss)) where _ == _ = True
52+ STEq :: Sing n -> Sing (TEq n)
53+ STLe :: Sing n -> Sing (TLe n)
54+ STGt :: Sing n -> Sing (TGt n)
55+ STRegex :: Sing s -> Sing (TRegex s)
56+
57+instance Known (Sing n) => Known (Sing (TEq n)) where known = STEq known
58+instance Known (Sing n) => Known (Sing (TGt n)) where known = STGt known
59+instance Known (Sing n) => Known (Sing (TLe n)) where known = STLe known
60+instance Known (Sing s) => Known (Sing (TRegex s)) where known = STRegex known
61+
62+instance Eq (Sing (TEq n)) where a == b = True
63+instance Eq (Sing (TLe n)) where a == b = True
64+instance Eq (Sing (TGt n)) where a == b = True
12065
12166data NumberConstraint
12267 = NLe Nat
123− | NLt Nat
12468 | NGt Nat
125− | NGe Nat
12669 | NEq Nat
12770 deriving (Generic)
12871
129−data DemotedNumberConstraint
130− = DNLe Integer
131− | DNLt Integer
132− | DNGt Integer
133− | DNGe Integer
134− | DNEq Integer
135− deriving (Generic, Eq, Show)
136−
13772data instance Sing (nc :: NumberConstraint) where
138− SNEq :: Sing n -> Sing ('NEq n)
139− SNGt :: Sing n -> Sing ('NGt n)
140− SNGe :: Sing n -> Sing ('NGe n)
141− SNLt :: Sing n -> Sing ('NLt n)
142− SNLe :: Sing n -> Sing ('NLe n)
143−
144−instance KnownNat n => SingI ('NEq n) where sing = SNEq sing
145−instance KnownNat n => SingI ('NGt n) where sing = SNGt sing
146−instance KnownNat n => SingI ('NGe n) where sing = SNGe sing
147−instance KnownNat n => SingI ('NLt n) where sing = SNLt sing
148−instance KnownNat n => SingI ('NLe n) where sing = SNLe sing
149−
150−instance Eq (Sing ('NEq n)) where _ == _ = True
151−instance Eq (Sing ('NLt n)) where _ == _ = True
152−instance Eq (Sing ('NLe n)) where _ == _ = True
153−instance Eq (Sing ('NGt n)) where _ == _ = True
154−instance Eq (Sing ('NGe n)) where _ == _ = True
155−
156−instance SingKind NumberConstraint where
157− type Demote NumberConstraint = DemotedNumberConstraint
158− fromSing = \case
159− SNEq n -> withKnownNat n (DNEq . fromIntegral $ natVal n)
160− SNGt n -> withKnownNat n (DNGt . fromIntegral $ natVal n)
161− SNGe n -> withKnownNat n (DNGe . fromIntegral $ natVal n)
162− SNLt n -> withKnownNat n (DNLt . fromIntegral $ natVal n)
163− SNLe n -> withKnownNat n (DNLe . fromIntegral $ natVal n)
164− toSing = \case
165− DNEq n -> case someNatVal n of
166− Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNEq (SNat :: Sing n))
167− Nothing -> error "Negative singleton nat"
168− DNGt n -> case someNatVal n of
169− Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNGt (SNat :: Sing n))
170− Nothing -> error "Negative singleton nat"
171− DNGe n -> case someNatVal n of
172− Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNGe (SNat :: Sing n))
173− Nothing -> error "Negative singleton nat"
174− DNLt n -> case someNatVal n of
175− Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNLt (SNat :: Sing n))
176− Nothing -> error "Negative singleton nat"
177− DNLe n -> case someNatVal n of
178− Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNLe (SNat :: Sing n))
179− Nothing -> error "Negative singleton nat"
73+ SNEq :: Sing n -> Sing (NEq n)
74+ SNGt :: Sing n -> Sing (NGt n)
75+ SNLe :: Sing n -> Sing (NLe n)
76+
77+instance Known (Sing n) => Known (Sing (NEq n)) where known = SNEq known
78+instance Known (Sing n) => Known (Sing (NGt n)) where known = SNGt known
79+instance Known (Sing n) => Known (Sing (NLe n)) where known = SNLe known
80+
81+instance Eq (Sing (NEq n)) where a == b = True
82+instance Eq (Sing (NLe n)) where a == b = True
83+instance Eq (Sing (NGt n)) where a == b = True
18084
18185data ArrayConstraint
18286 = AEq Nat
183− | AGt Nat
184− | AGe Nat
185− | ALt Nat
186− | ALe Nat
18787 deriving (Generic)
18888
189−data DemotedArrayConstraint
190− = DAEq Integer
191− | DAGt Integer
192− | DAGe Integer
193− | DALt Integer
194− | DALe Integer
195− deriving (Generic, Eq, Show)
196−
19789data instance Sing (ac :: ArrayConstraint) where
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)
203−
204−instance 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
209−
210−instance 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
215−
216−instance SingKind ArrayConstraint where
217− type Demote ArrayConstraint = DemotedArrayConstraint
218− fromSing = \case
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)
224− toSing = \case
225− DAEq n -> case someNatVal n of
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))
239− Nothing -> error "Negative singleton nat"
90+ SAEq :: Sing n -> Sing (AEq n)
91+
92+instance Known (Sing n) => Known (Sing (AEq n)) where known = SAEq known
93+
94+instance Eq (Sing (AEq n)) where a == b = True
24095
24196data Schema
24297 = SchemaText [TextConstraint]
243− | SchemaBoolean
24498 | SchemaNumber [NumberConstraint]
24599 | SchemaObject [(Symbol, Schema)]
246100 | SchemaArray [ArrayConstraint] Schema
247101 | SchemaNull
248102 | SchemaOptional Schema
249− | SchemaUnion [Schema]
250103 deriving (Generic)
251104
252−data DemotedSchema
253− = DSchemaText [DemotedTextConstraint]
254− | DSchemaNumber [DemotedNumberConstraint]
255− | DSchemaBoolean
256− | DSchemaObject [(Text, DemotedSchema)]
257− | DSchemaArray [DemotedArrayConstraint] DemotedSchema
258− | DSchemaNull
259− | DSchemaOptional DemotedSchema
260− | DSchemaUnion [DemotedSchema]
261− deriving (Generic, Eq, Show)
262−
263105data instance Sing (schema :: Schema) where
264− SSchemaText :: Sing tcs -> Sing ('SchemaText tcs)
265− SSchemaNumber :: Sing ncs -> Sing ('SchemaNumber ncs)
266− SSchemaBoolean :: Sing 'SchemaBoolean
267− SSchemaArray :: Sing acs -> Sing schema -> Sing ('SchemaArray acs schema)
268− SSchemaObject :: Sing fields -> Sing ('SchemaObject fields)
269− SSchemaOptional :: Sing s -> Sing ('SchemaOptional s)
270− SSchemaNull :: Sing 'SchemaNull
271− SSchemaUnion :: Sing ss -> Sing ('SchemaUnion ss)
272−
273−instance SingI sl => SingI ('SchemaText sl) where
274− sing = SSchemaText sing
275−instance SingI sl => SingI ('SchemaNumber sl) where
276− sing = SSchemaNumber sing
277−instance SingI 'SchemaNull where
278− sing = SSchemaNull
279−instance SingI 'SchemaBoolean where
280− sing = SSchemaBoolean
281−instance (SingI ac, SingI s) => SingI ('SchemaArray ac s) where
282− sing = SSchemaArray sing sing
283−instance SingI stl => SingI ('SchemaObject stl) where
284− sing = SSchemaObject sing
285−instance SingI s => SingI ('SchemaOptional s) where
286− sing = SSchemaOptional sing
287−instance SingI s => SingI ('SchemaUnion s) where
288− sing = SSchemaUnion sing
289−
290−instance Eq (Sing ('SchemaText cs)) where _ == _ = True
291−instance Eq (Sing ('SchemaNumber cs)) where _ == _ = True
292−instance Eq (Sing 'SchemaNull) where _ == _ = True
293−instance Eq (Sing 'SchemaBoolean) where _ == _ = True
294−instance Eq (Sing ('SchemaArray as s)) where _ == _ = True
295−instance Eq (Sing ('SchemaObject cs)) where _ == _ = True
296−instance Eq (Sing ('SchemaOptional s)) where _ == _ = True
297−instance Eq (Sing ('SchemaUnion s)) where _ == _ = True
298−
299−instance SingKind Schema where
300− type Demote Schema = DemotedSchema
301− fromSing = \case
302− SSchemaText cs -> DSchemaText $ fromSing cs
303− SSchemaNumber cs -> DSchemaNumber $ fromSing cs
304− SSchemaBoolean -> DSchemaBoolean
305− SSchemaArray cs s -> DSchemaArray (fromSing cs) (fromSing s)
306− SSchemaOptional s -> DSchemaOptional $ fromSing s
307− SSchemaNull -> DSchemaNull
308− SSchemaObject cs -> DSchemaObject $ fromSing cs
309− SSchemaUnion ss -> DSchemaUnion $ fromSing ss
310− toSing = \case
311− DSchemaText cs -> case toSing cs of
312− SomeSing scs -> SomeSing $ SSchemaText scs
313− DSchemaNumber cs -> case toSing cs of
314− SomeSing scs -> SomeSing $ SSchemaNumber scs
315− DSchemaBoolean -> SomeSing $ SSchemaBoolean
316− DSchemaArray cs sch -> case (toSing cs, toSing sch) of
317− (SomeSing scs, SomeSing ssch) -> SomeSing $ SSchemaArray scs ssch
318− DSchemaOptional sch -> case toSing sch of
319− SomeSing ssch -> SomeSing $ SSchemaOptional ssch
320− DSchemaNull -> SomeSing SSchemaNull
321− DSchemaObject cs -> case toSing cs of
322− SomeSing scs -> SomeSing $ SSchemaObject scs
323− DSchemaUnion ss -> case toSing ss of
324− SomeSing sss -> SomeSing $ SSchemaUnion sss
106+ SSchemaText :: Known (Sing tcs) => Sing tcs -> Sing (SchemaText tcs)
107+ SSchemaNumber :: Known (Sing ncs) => Sing ncs -> Sing (SchemaNumber ncs)
108+ SSchemaArray :: (Known (Sing acs), Known (Sing schema)) => Sing acs -> Sing schema -> Sing (SchemaArray acs schema)
109+ SSchemaObject :: Known (Sing fields) => Sing fields -> Sing (SchemaObject fields)
110+ SSchemaOptional :: Known (Sing s) => Sing s -> Sing (SchemaOptional s)
111+ SSchemaNull :: Sing SchemaNull
112+
113+instance Known (Sing sl) => Known (Sing (SchemaText sl)) where
114+ known = SSchemaText known
115+instance Known (Sing sl) => Known (Sing (SchemaNumber sl)) where
116+ known = SSchemaNumber known
117+instance Known (Sing SchemaNull) where
118+ known = SSchemaNull
119+instance (Known (Sing ac), Known (Sing s)) => Known (Sing (SchemaArray ac s)) where
120+ known = SSchemaArray known known
121+instance Known (Sing stl) => Known (Sing (SchemaObject stl)) where
122+ known = SSchemaObject known
123+instance Known (Sing s) => Known (Sing (SchemaOptional s)) where
124+ known = SSchemaOptional known
125+
126+instance Eq (Sing (SchemaText cs)) where a == b = True
127+instance Eq (Sing (SchemaNumber cs)) where a == b = True
128+instance Eq (Sing SchemaNull) where a == b = True
129+instance Eq (Sing (SchemaArray as s)) where a == b = True
130+instance Eq (Sing (SchemaObject cs)) where a == b = True
131+instance Eq (Sing (SchemaOptional s)) where a == b = True
325132
326133data FieldRepr :: (Symbol, Schema) -> Type where
327− FieldRepr
328− :: (SingI schema, KnownSymbol name)
329− => JsonRepr schema
330− -> FieldRepr '(name, schema)
331−
332−-- | Forgetful Functor Ufr
333−toJsonRepr :: FieldRepr '(fn, sch) -> JsonRepr sch
334−toJsonRepr (FieldRepr x) = x
335−
336−knownFieldName
337− :: forall proxy (fieldName :: Symbol) schema
338− . KnownSymbol fieldName
339− => proxy '(fieldName, schema)
340− -> Text
341−knownFieldName _ = T.pack $ symbolVal (Proxy @fieldName)
342−
343−knownFieldSchema
344− :: forall proxy fieldName schema
345− . SingI schema
346− => proxy '(fieldName, schema)
347− -> Sing schema
348−knownFieldSchema _ = sing
134+ FieldRepr :: KnownSymbol name => JsonRepr schema -> FieldRepr '(name, schema)
135+
136+type family MapSnd (cs :: [(Symbol, Schema)]) :: [Schema] where
137+ MapSnd '[] = '[]
138+ MapSnd ( '(fn, schema) ': as) = schema ': MapSnd as
349139
350140deriving instance Show (JsonRepr schema) => Show (FieldRepr '(name, schema))
351141
@@ -354,225 +144,126 @@ instance Eq (JsonRepr schema) => Eq (FieldRepr '(name, schema)) where
354144
355145instance
356146 ( KnownSymbol name
357− , SingI schema
358147 , Serial m (JsonRepr schema) )
359148 => Serial m (FieldRepr '(name, schema)) where
360149 series = FieldRepr <$> series
361150
362−type family USubsets (u :: [k]) :: Constraint where
363− USubsets '[] = ()
364− USubsets (h ': tl) = (USubset tl (h ': tl) (V.RImage tl (h ': tl)), USubsets tl)
365−
366151data JsonRepr :: Schema -> Type where
367− ReprText :: Text -> JsonRepr ('SchemaText cs)
368− ReprNumber :: Scientific -> JsonRepr ('SchemaNumber cs)
369− ReprBoolean :: Bool -> JsonRepr 'SchemaBoolean
370− ReprNull :: JsonRepr 'SchemaNull
371− ReprArray :: V.Vector (JsonRepr s) -> JsonRepr ('SchemaArray cs s)
372− ReprObject :: Rec FieldRepr fs -> JsonRepr ('SchemaObject fs)
373− ReprOptional :: Maybe (JsonRepr s) -> JsonRepr ('SchemaOptional s)
374− ReprUnion :: Union JsonRepr (h ': tl) -> JsonRepr ('SchemaUnion (h ': tl))
375−
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)
379−
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)
384−
385−instance Monad m => Serial m (JsonRepr 'SchemaNull) where
386− series = cons0 ReprNull
387−
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)
392−
393−instance (Serial m (JsonRepr s))
394− => Serial m (JsonRepr ('SchemaOptional s)) where
395− series = cons1 ReprOptional
396−
397−instance (Monad m, Serial m (Rec FieldRepr fs))
398− => Serial m (JsonRepr ('SchemaObject fs)) where
399− series = cons1 ReprObject
400−
401−-- | Move to the union package
402−instance Show (JsonRepr ('SchemaText cs)) where
152+ ReprText :: Text -> JsonRepr (SchemaText cs)
153+ ReprNumber :: Scientific -> JsonRepr (SchemaNumber cs)
154+ ReprNull :: JsonRepr SchemaNull
155+ ReprArray :: V.Vector (JsonRepr s) -> JsonRepr (SchemaArray cs s)
156+ ReprObject :: Rec FieldRepr fs -> JsonRepr (SchemaObject fs)
157+ ReprOptional :: Maybe (JsonRepr s) -> JsonRepr (SchemaOptional s)
158+
159+instance Show (JsonRepr (SchemaText cs)) where
403160 show (ReprText t) = "ReprText " P.++ show t
404161
405−instance Show (JsonRepr ('SchemaNumber cs)) where
162+instance Show (JsonRepr (SchemaNumber cs)) where
406163 show (ReprNumber n) = "ReprNumber " P.++ show n
407164
408−instance Show (JsonRepr 'SchemaBoolean) where
409− show (ReprBoolean n) = "ReprBoolean " P.++ show n
410−
411−instance Show (JsonRepr 'SchemaNull) where show _ = "ReprNull"
165+instance Show (JsonRepr SchemaNull) where show _ = "ReprNull"
412166
413−instance Show (JsonRepr s) => Show (JsonRepr ('SchemaArray acs s)) where
167+instance Show (JsonRepr s) => Show (JsonRepr (SchemaArray acs s)) where
414168 show (ReprArray v) = "ReprArray " P.++ show v
415169
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
423−instance V.RecAll FieldRepr fs Show => Show (JsonRepr ('SchemaObject fs)) where
170+instance RecAll FieldRepr fs Show => Show (JsonRepr (SchemaObject fs)) where
424171 show (ReprObject fs) = "ReprObject " P.++ show fs
425−#endif
426172
427−instance Show (JsonRepr s) => Show (JsonRepr ('SchemaOptional s)) where
173+instance Show (JsonRepr s) => Show (JsonRepr (SchemaOptional s)) where
428174 show (ReprOptional s) = "ReprOptional " P.++ show s
429175
430−instance Show (Union JsonRepr (h ': tl))
431− => Show (JsonRepr ('SchemaUnion (h ': tl))) where
432− show (ReprUnion s) = "ReprUnion " P.++ show s
176+instance (Monad m, Serial m Text)
177+ => Serial m (JsonRepr (SchemaText cs)) where
178+ series = cons1 ReprText
179+
180+instance (Monad m, Serial m Scientific)
181+ => Serial m (JsonRepr (SchemaNumber cs)) where
182+ series = cons1 ReprNumber
433183
434−instance Eq (Rec FieldRepr fs) => Eq (JsonRepr ('SchemaObject fs)) where
184+instance Monad m => Serial m (JsonRepr SchemaNull) where
185+ series = cons0 ReprNull
186+
187+instance (Serial m (V.Vector (JsonRepr s)))
188+ => Serial m (JsonRepr (SchemaArray cs s)) where
189+ series = cons1 ReprArray
190+
191+instance (Serial m (JsonRepr s))
192+ => Serial m (JsonRepr (SchemaOptional s)) where
193+ series = cons1 ReprOptional
194+
195+instance (Monad m, Serial m (Rec FieldRepr fs))
196+ => Serial m (JsonRepr (SchemaObject fs)) where
197+ series = cons1 ReprObject
198+
199+instance Eq (Rec FieldRepr fs) => Eq (JsonRepr (SchemaObject fs)) where
435200 ReprObject a == ReprObject b = a == b
436201
437−instance Eq (JsonRepr ('SchemaText cs)) where
202+instance Eq (JsonRepr (SchemaText cs)) where
438203 ReprText a == ReprText b = a == b
439204
440−instance Eq (JsonRepr ('SchemaNumber cs)) where
205+instance Eq (JsonRepr (SchemaNumber cs)) where
441206 ReprNumber a == ReprNumber b = a == b
442207
443−instance Eq (JsonRepr 'SchemaBoolean) where
444− ReprBoolean a == ReprBoolean b = a == b
445−
446−instance Eq (JsonRepr 'SchemaNull) where
208+instance Eq (JsonRepr SchemaNull) where
447209 ReprNull == ReprNull = True
448210
449−instance Eq (JsonRepr s) => Eq (JsonRepr ('SchemaArray as s)) where
211+instance Eq (JsonRepr s) => Eq (JsonRepr (SchemaArray as s)) where
450212 ReprArray a == ReprArray b = a == b
451213
452−instance Eq (JsonRepr s) => Eq (JsonRepr ('SchemaOptional s)) where
214+instance Eq (JsonRepr s) => Eq (JsonRepr (SchemaOptional s)) where
453215 ReprOptional a == ReprOptional b = a == b
454216
455−instance Eq (Union JsonRepr (h ': tl))
456− => Eq (JsonRepr ('SchemaUnion (h ': tl))) where
457− ReprUnion a == ReprUnion b = a == b
458−
459−instance Ord (Rec FieldRepr fs) => Ord (JsonRepr ('SchemaObject fs)) where
460− ReprObject a `compare` ReprObject b = a `compare` b
461−
462−instance Ord (JsonRepr ('SchemaText cs)) where
463− ReprText a `compare` ReprText b = a `compare` b
464−
465−instance Ord (JsonRepr ('SchemaNumber cs)) where
466− ReprNumber a `compare` ReprNumber b = a `compare` b
467−
468−instance Ord (JsonRepr 'SchemaBoolean) where
469− ReprBoolean a `compare` ReprBoolean b = a `compare` b
470−
471−instance Ord (JsonRepr 'SchemaNull) where
472− compare _ _ = EQ
473−
474−instance Ord (JsonRepr s) => Ord (JsonRepr ('SchemaArray as s)) where
475− ReprArray a `compare` ReprArray b = a `compare` b
476−
477−instance Ord (JsonRepr s) => Ord (JsonRepr ('SchemaOptional s)) where
478− ReprOptional a `compare` ReprOptional b = a `compare` b
479−
480−instance Ord (Union JsonRepr (h ': tl))
481− => Ord (JsonRepr ('SchemaUnion (h ': tl))) where
482− ReprUnion a `compare` ReprUnion b = a `compare` b
483−
484−instance IsList (JsonRepr ('SchemaArray cs s)) where
485− type Item (JsonRepr ('SchemaArray cs s)) = JsonRepr s
486− fromList = ReprArray . GHC.Exts.fromList
487− toList (ReprArray v) = GHC.Exts.toList v
488−
489−instance Num (JsonRepr ('SchemaNumber cs)) where
490− ReprNumber a + ReprNumber b = ReprNumber $ a + b
491− ReprNumber a - ReprNumber b = ReprNumber $ a - b
492− ReprNumber a * ReprNumber b = ReprNumber $ a * b
493− abs (ReprNumber a) = ReprNumber $ abs a
494− signum (ReprNumber a) = ReprNumber $ signum a
495− fromInteger = ReprNumber . fromIntegral
496−
497−instance IsString (JsonRepr ('SchemaText cs)) where
498− fromString = ReprText . fromString
499−
500217fromOptional
501− :: SingI s
502− => Sing ('SchemaOptional s)
218+ :: (Known (Sing s))
219+ => Sing (SchemaOptional s)
503220 -> J.Value
504221 -> Parser (Maybe (JsonRepr s))
505222fromOptional _ = parseJSON
506223
507−parseUnion
508− :: FromJSON (JsonRepr ('SchemaUnion ss))
509− => sing (ss :: [Schema])
510− -> Value
511− -> Parser (JsonRepr ('SchemaUnion ss))
512−parseUnion _ val = parseJSON val
513−
514−instance FromJSON (Union JsonRepr '[]) where
515− parseJSON = fail "empty union"
516−
517−instance (SingI a, FromJSON (Union JsonRepr as)) => FromJSON (Union JsonRepr (a ': as)) where
518− parseJSON val = (This <$> parseJSON val)
519− <|> (That <$> (parseJSON val :: Parser (Union JsonRepr as)))
520−
521−instance ToJSON (Union JsonRepr as) where
522− toJSON (This fa) = toJSON fa
523− toJSON (That u) = toJSON u
524−
525−instance SingI schema => J.FromJSON (JsonRepr schema) where
526− parseJSON value = case sing :: Sing schema of
527− SSchemaText _ -> withText "String" (pure . ReprText) value
528− SSchemaNumber _ -> withScientific "Number" (pure . ReprNumber) value
529− SSchemaBoolean -> ReprBoolean <$> parseJSON value
530− SSchemaNull -> case value of
224+instance Known (Sing schema) => J.FromJSON (JsonRepr schema) where
225+ parseJSON value = case known :: Sing schema of
226+ SSchemaText _ -> withText "String" (pure . ReprText) value
227+ SSchemaNumber _ -> withScientific "Number" (pure . ReprNumber) value
228+ SSchemaNull -> case value of
531229 J.Null -> pure ReprNull
532230 _ -> typeMismatch "Null" value
533− so@(SSchemaOptional s) -> withSingI s $ ReprOptional <$> fromOptional so value
534− SSchemaArray sa sb -> withSingI sa $ withSingI sb
535− $ withArray "Array" (fmap ReprArray . traverse parseJSON) value
536− SSchemaObject fs -> do
231+ so@(SSchemaOptional _) -> ReprOptional <$> fromOptional so value
232+ SSchemaArray c s -> withArray "Array" (fmap ReprArray . traverse parseJSON) value
233+ SSchemaObject fs -> do
537234 let
538235 demoteFields :: SList s -> H.HashMap Text J.Value -> Parser (Rec FieldRepr s)
539− demoteFields SNil _ = pure RNil
236+ demoteFields SNil h = pure RNil
540237 demoteFields (SCons (STuple2 (n :: Sing fn) s) tl) h = withKnownSymbol n $ do
541238 let fieldName = T.pack $ symbolVal (Proxy @fn)
542239 fieldRepr <- case s of
543− SSchemaText so -> case H.lookup fieldName h of
544− Just v -> withSingI so $ FieldRepr <$> parseJSON v
545− Nothing -> fail $ "No text field: " P.++ show fieldName
546− SSchemaNumber so -> case H.lookup fieldName h of
547− Just v -> withSingI so $ FieldRepr <$> parseJSON v
548− Nothing -> fail $ "No number field: " P.++ show fieldName
549− SSchemaBoolean -> case H.lookup fieldName h of
240+ SSchemaText _ -> case H.lookup fieldName h of
550241 Just v -> FieldRepr <$> parseJSON v
551− Nothing -> fail $ "No boolean field: " P.++ show fieldName
242+ Nothing -> fail "schematext"
243+ SSchemaNumber _ -> case H.lookup fieldName h of
244+ Just v -> FieldRepr <$> parseJSON v
245+ Nothing -> fail "schemanumber"
552246 SSchemaNull -> case H.lookup fieldName h of
553247 Just v -> FieldRepr <$> parseJSON v
554− Nothing -> fail $ "No null field: " P.++ show fieldName
555− SSchemaArray sa sb -> case H.lookup fieldName h of
556− Just v -> withSingI sa $ withSingI sb $ FieldRepr <$> parseJSON v
557− Nothing -> fail $ "No array field: " P.++ show fieldName
558− SSchemaObject so -> case H.lookup fieldName h of
559− Just v -> withSingI so $ FieldRepr <$> parseJSON v
560− Nothing -> fail $ "No object field" P.++ show fieldName
561− SSchemaOptional so -> case H.lookup fieldName h of
562− Just v -> withSingI so $ FieldRepr <$> parseJSON v
563− Nothing -> withSingI so $ pure $ FieldRepr $ ReprOptional Nothing
564− SSchemaUnion ss -> withSingI ss $ FieldRepr <$> parseUnion ss value
248+ Nothing -> fail "schemanull"
249+ SSchemaArray _ _ -> case H.lookup fieldName h of
250+ Just v -> FieldRepr <$> parseJSON v
251+ Nothing -> fail "schemaarray"
252+ SSchemaObject _ -> case H.lookup fieldName h of
253+ Just v -> FieldRepr <$> parseJSON v
254+ Nothing -> fail "schemaobject"
255+ SSchemaOptional s -> case H.lookup fieldName h of
256+ Just v -> FieldRepr <$> parseJSON v
257+ Nothing -> fail "schemaoptional"
565258 (fieldRepr :&) <$> demoteFields tl h
566259 ReprObject <$> withObject "Object" (demoteFields fs) value
567− SSchemaUnion ss -> parseUnion ss value
568260
569261instance J.ToJSON (JsonRepr a) where
570262 toJSON ReprNull = J.Null
571− toJSON (ReprBoolean b) = J.Bool b
572263 toJSON (ReprText t) = J.String t
573264 toJSON (ReprNumber n) = J.Number n
574265 toJSON (ReprOptional s) = case s of
575− Just v -> toJSON v
266+ Just v -> toJSON v
576267 Nothing -> J.Null
577268 toJSON (ReprArray v) = J.Array $ toJSON <$> v
578269 toJSON (ReprObject r) = J.Object . H.fromList . fold $ r
@@ -584,12 +275,11 @@ instance J.ToJSON (JsonRepr a) where
584275 fold :: Rec FieldRepr fs -> [(Text, J.Value)]
585276 fold = \case
586277 RNil -> []
587− fr@(FieldRepr _) :& tl -> (extract fr) : fold tl
588− toJSON (ReprUnion u) = toJSON u
278+ fr@(FieldRepr s) :& tl -> (extract fr) : fold tl
589279
590−class FalseConstraint a
280+class FalseConstraint
591281
592−type family TopLevel (schema :: Schema) :: Constraint where
593− TopLevel ('SchemaArray acs s) = ()
594− TopLevel ('SchemaObject o) = ()
595− TopLevel spec = 'True ~ 'False
282+type family TopLevel (spec :: Schema) :: Constraint where
283+ TopLevel (SchemaArray a e) = ()
284+ TopLevel (SchemaObject o) = ()
285+ TopLevel spec = FalseConstraint