@@ -1,35 +1,36 @@
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
8−import Control.Applicative
9+import Control.Applicative ((<|>))
9−import Control.Monad
10+import Control.Monad
10−import Data.Aeson as J
11+import Data.Aeson as J
11−import Data.Aeson.Types as J
12+import Data.Aeson.Types as J
12−import Data.HashMap.Strict as H
13+import Data.HashMap.Strict as H
13−import Data.Kind
14+import Data.Kind
14−import Data.Maybe
15+import Data.Maybe
16+import Data.Schematic.Generator
15−import Data.Schematic.Instances ()
17+import Data.Schematic.Instances ()
16−import Data.Schematic.Utils
17−import Data.Scientific
18+import Data.Scientific
18−import Data.Singletons.Prelude.List hiding (All)
19+import Data.Singletons.Prelude.List hiding (All, Union)
19−import Data.Singletons.TH
20+import Data.Singletons.TH
20−import Data.Singletons.TypeLits
21+import Data.Singletons.TypeLits
21−import Data.Text as T
22+import Data.Text as T
22−import Data.Vector as V
24+import Data.Vector as V
23−import Data.Vinyl hiding (Dict)
25+import Data.Vinyl hiding (Dict)
24−import Data.Vinyl.TypeLevel hiding (Nat)
26+import qualified Data.Vinyl.TypeLevel as V
25−import GHC.Generics (Generic)
28+import GHC.Generics (Generic)
30+ (SomeNat(..), SomeSymbol(..), someNatVal, someSymbolVal)
26−import Prelude as P
31+import Prelude as P
27−import Test.SmallCheck.Series
32+import Test.SmallCheck.Series
30−type family All (c :: k -> Constraint) (s :: [k]) :: Constraint where
32− All c (a ': as) = (c a, All c as)
34type family CRepr (s :: Schema) :: Type where
35type family CRepr (s :: Schema) :: Type where
35 CRepr ('SchemaText cs) = TextConstraint
36 CRepr ('SchemaText cs) = TextConstraint
@@ -39,104 +40,299 @@ type family CRepr (s :: Schema) :: Type where
40data TextConstraint
41data TextConstraint
44 | TRegex Symbol
47 | TRegex Symbol
45 | TEnum [Symbol]
48 | TEnum [Symbol]
46 deriving (Generic)
49 deriving (Generic)
51+instance SingKind TextConstraint where
52+ type Demote TextConstraint = DemotedTextConstraint
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)
61+ d :: Sing (s :: [Symbol]) -> [Text]
63+ d (SCons ss@SSym fs) = T.pack (symbolVal ss) : d fs
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)
86+data DemotedTextConstraint
94+ deriving (Generic, Eq, Show)
48data instance Sing (tc :: TextConstraint) where
96data instance Sing (tc :: TextConstraint) where
49− STEq :: (KnownNat n) => Sing n -> Sing ('TEq n)
97+ STEq :: Sing n -> Sing ('TEq n)
98+ STLt :: Sing n -> Sing ('TLt n)
50− STLe :: (KnownNat n) => Sing n -> Sing ('TLe n)
99+ STLe :: Sing n -> Sing ('TLe n)
51− STGt :: (KnownNat n) => Sing n -> Sing ('TGt n)
100+ STGt :: Sing n -> Sing ('TGt n)
101+ STGe :: Sing n -> Sing ('TGe n)
52− STRegex :: (KnownSymbol s, Known (Sing s)) => Sing s -> Sing ('TRegex s)
102+ STRegex :: Sing s -> Sing ('TRegex s)
53− STEnum :: (All KnownSymbol ss, Known (Sing ss)) => Sing ss -> Sing ('TEnum ss)
103+ STEnum :: Sing ss -> Sing ('TEnum ss)
55−instance (KnownNat n) => Known (Sing ('TEq n)) where known = STEq known
105+instance (KnownNat n) => SingI ('TEq n) where sing = STEq sing
56−instance (KnownNat n) => Known (Sing ('TGt n)) where known = STGt known
106+instance (KnownNat n) => SingI ('TGt n) where sing = STGt sing
57−instance (KnownNat n) => Known (Sing ('TLe n)) where known = STLe known
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
58−instance (KnownSymbol s, Known (Sing s)) => Known (Sing ('TRegex s)) where known = STRegex known
110+instance (KnownSymbol s, SingI s) => SingI ('TRegex s) where sing = STRegex sing
59−instance (All KnownSymbol ss, Known (Sing ss)) => Known (Sing ('TEnum ss)) where known = STEnum known
111+instance (SingI ss) => SingI ('TEnum ss) where sing = STEnum sing
61instance Eq (Sing ('TEq n)) where _ == _ = True
113instance Eq (Sing ('TEq n)) where _ == _ = True
114+instance Eq (Sing ('TLt n)) where _ == _ = True
62instance Eq (Sing ('TLe n)) where _ == _ = True
115instance Eq (Sing ('TLe n)) where _ == _ = True
63instance Eq (Sing ('TGt n)) where _ == _ = True
116instance Eq (Sing ('TGt n)) where _ == _ = True
117+instance Eq (Sing ('TGe n)) where _ == _ = True
64instance Eq (Sing ('TRegex t)) where _ == _ = True
118instance Eq (Sing ('TRegex t)) where _ == _ = True
65instance Eq (Sing ('TEnum ss)) where _ == _ = True
119instance Eq (Sing ('TEnum ss)) where _ == _ = True
67data NumberConstraint
121data NumberConstraint
68 = NLe Nat
122 = NLe Nat
69 | NGt Nat
124 | NGt Nat
70 | NEq Nat
126 | NEq Nat
71 deriving (Generic)
127 deriving (Generic)
73−data instance Sing (nc :: NumberConstraint) where
129+data DemotedNumberConstraint
74− SNEq :: KnownNat n => Sing n -> Sing ('NEq n)
130+ = DNLe Integer
75− SNGt :: KnownNat n => Sing n -> Sing ('NGt n)
131+ | DNLt Integer
76− SNLe :: KnownNat n => Sing n -> Sing ('NLe n)
132+ | DNGt Integer
135+ deriving (Generic, Eq, Show)
78−instance KnownNat n => Known (Sing ('NEq n)) where known = SNEq known
137+data 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)
144+instance KnownNat n => SingI ('NEq n) where sing = SNEq sing
79−instance KnownNat n => Known (Sing ('NGt n)) where known = SNGt known
145+instance KnownNat n => SingI ('NGt n) where sing = SNGt sing
80−instance KnownNat n => Known (Sing ('NLe n)) where known = SNLe known
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
82instance Eq (Sing ('NEq n)) where _ == _ = True
150instance Eq (Sing ('NEq n)) where _ == _ = True
151+instance Eq (Sing ('NLt n)) where _ == _ = True
83instance Eq (Sing ('NLe n)) where _ == _ = True
152instance Eq (Sing ('NLe n)) where _ == _ = True
84instance Eq (Sing ('NGt n)) where _ == _ = True
153instance Eq (Sing ('NGt n)) where _ == _ = True
154+instance Eq (Sing ('NGe n)) where _ == _ = True
156+instance SingKind NumberConstraint where
157+ type Demote NumberConstraint = DemotedNumberConstraint
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)
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"
86data ArrayConstraint
181data ArrayConstraint
87 = AEq Nat
182 = AEq Nat
88 deriving (Generic)
187 deriving (Generic)
90−data instance Sing (ac :: ArrayConstraint) where
189+data DemotedArrayConstraint
91− SAEq :: KnownNat n => Sing n -> Sing ('AEq n)
190+ = DAEq Integer
195+ deriving (Generic, Eq, Show)
93−instance KnownNat n => Known (Sing ('AEq n)) where known = SAEq known
197+data 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)
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
95instance 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
216+instance SingKind ArrayConstraint where
217+ type Demote ArrayConstraint = DemotedArrayConstraint
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)
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"
97data Schema
241data Schema
98 = SchemaText [TextConstraint]
242 = SchemaText [TextConstraint]
99 | SchemaNumber [NumberConstraint]
244 | SchemaNumber [NumberConstraint]
100 | SchemaObject [(Symbol, Schema)]
245 | SchemaObject [(Symbol, Schema)]
101 | SchemaArray [ArrayConstraint] Schema
246 | SchemaArray [ArrayConstraint] Schema
102 | SchemaNull
247 | SchemaNull
103 | SchemaOptional Schema
248 | SchemaOptional Schema
249+ | SchemaUnion [Schema]
104 deriving (Generic)
250 deriving (Generic)
253+ = DSchemaText [DemotedTextConstraint]
254+ | DSchemaNumber [DemotedNumberConstraint]
256+ | DSchemaObject [(Text, DemotedSchema)]
257+ | DSchemaArray [DemotedArrayConstraint] DemotedSchema
259+ | DSchemaOptional DemotedSchema
260+ | DSchemaUnion [DemotedSchema]
261+ deriving (Generic, Eq, Show)
106data instance Sing (schema :: Schema) where
263data instance Sing (schema :: Schema) where
107− SSchemaText :: Known (Sing tcs) => Sing tcs -> Sing ('SchemaText tcs)
264+ SSchemaText :: Sing tcs -> Sing ('SchemaText tcs)
108− SSchemaNumber :: Known (Sing ncs) => Sing ncs -> Sing ('SchemaNumber ncs)
265+ SSchemaNumber :: Sing ncs -> Sing ('SchemaNumber ncs)
266+ SSchemaBoolean :: Sing 'SchemaBoolean
109− SSchemaArray :: (Known (Sing acs), Known (Sing schema)) => Sing acs -> Sing schema -> Sing ('SchemaArray acs schema)
267+ SSchemaArray :: Sing acs -> Sing schema -> Sing ('SchemaArray acs schema)
110− SSchemaObject :: Known (Sing fields) => Sing fields -> Sing ('SchemaObject fields)
268+ SSchemaObject :: Sing fields -> Sing ('SchemaObject fields)
111− SSchemaOptional :: Known (Sing s) => Sing s -> Sing ('SchemaOptional s)
269+ SSchemaOptional :: Sing s -> Sing ('SchemaOptional s)
112 SSchemaNull :: Sing 'SchemaNull
270 SSchemaNull :: Sing 'SchemaNull
113−
271+ SSchemaUnion :: Sing ss -> Sing ('SchemaUnion ss)
114−instance Known (Sing sl) => Known (Sing ('SchemaText sl)) where
273+instance SingI sl => SingI ('SchemaText sl) where
115− known = SSchemaText known
274+ sing = SSchemaText sing
116−instance Known (Sing sl) => Known (Sing ('SchemaNumber sl)) where
275+instance SingI sl => SingI ('SchemaNumber sl) where
117− known = SSchemaNumber known
276+ sing = SSchemaNumber sing
118−instance Known (Sing 'SchemaNull) where
277+instance SingI 'SchemaNull where
119− known = SSchemaNull
278+ sing = SSchemaNull
279+instance SingI 'SchemaBoolean where
280+ sing = SSchemaBoolean
120−instance (Known (Sing ac), Known (Sing s)) => Known (Sing ('SchemaArray ac s)) where
281+instance (SingI ac, SingI s) => SingI ('SchemaArray ac s) where
121− known = SSchemaArray known known
282+ sing = SSchemaArray sing sing
122−instance Known (Sing stl) => Known (Sing ('SchemaObject stl)) where
283+instance SingI stl => SingI ('SchemaObject stl) where
123− known = SSchemaObject known
284+ sing = SSchemaObject sing
124−instance Known (Sing s) => Known (Sing ('SchemaOptional s)) where
285+instance SingI s => SingI ('SchemaOptional s) where
125− known = SSchemaOptional known
286+ sing = SSchemaOptional sing
287+instance SingI s => SingI ('SchemaUnion s) where
288+ sing = SSchemaUnion sing
127instance Eq (Sing ('SchemaText cs)) where _ == _ = True
290instance Eq (Sing ('SchemaText cs)) where _ == _ = True
128instance Eq (Sing ('SchemaNumber cs)) where _ == _ = True
291instance Eq (Sing ('SchemaNumber cs)) where _ == _ = True
129instance Eq (Sing 'SchemaNull) where _ == _ = True
292instance Eq (Sing 'SchemaNull) where _ == _ = True
293+instance Eq (Sing 'SchemaBoolean) where _ == _ = True
130instance Eq (Sing ('SchemaArray as s)) where _ == _ = True
294instance Eq (Sing ('SchemaArray as s)) where _ == _ = True
131instance Eq (Sing ('SchemaObject cs)) where _ == _ = True
295instance Eq (Sing ('SchemaObject cs)) where _ == _ = True
132instance Eq (Sing ('SchemaOptional s)) where _ == _ = True
296instance Eq (Sing ('SchemaOptional s)) where _ == _ = True
297+instance Eq (Sing ('SchemaUnion s)) where _ == _ = True
299+instance SingKind Schema where
300+ type Demote Schema = DemotedSchema
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
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
134data FieldRepr :: (Symbol, Schema) -> Type where
326data FieldRepr :: (Symbol, Schema) -> Type where
135 FieldRepr
327 FieldRepr
136− :: (Known (Sing schema), KnownSymbol name)
328+ :: (SingI schema, KnownSymbol name)
137 => JsonRepr schema
329 => JsonRepr schema
138 -> FieldRepr '(name, schema)
330 -> FieldRepr '(name, schema)
332+-- | Forgetful Functor Ufr
333+toJsonRepr :: FieldRepr '(fn, sch) -> JsonRepr sch
334+toJsonRepr (FieldRepr x) = x
140knownFieldName
336knownFieldName
141 :: forall proxy (fieldName :: Symbol) schema
337 :: forall proxy (fieldName :: Symbol) schema
142 . KnownSymbol fieldName
338 . KnownSymbol fieldName
@@ -146,10 +342,10 @@ knownFieldName _ = T.pack $ symbolVal (Proxy @fieldName)
147knownFieldSchema
343knownFieldSchema
148 :: forall proxy fieldName schema
344 :: forall proxy fieldName schema
149− . Known (Sing schema)
345+ . SingI schema
150 => proxy '(fieldName, schema)
346 => proxy '(fieldName, schema)
151 -> Sing schema
347 -> Sing schema
152−knownFieldSchema _ = known
348+knownFieldSchema _ = sing
154deriving instance Show (JsonRepr schema) => Show (FieldRepr '(name, schema))
350deriving instance Show (JsonRepr schema) => Show (FieldRepr '(name, schema))
@@ -158,50 +354,41 @@ instance Eq (JsonRepr schema) => Eq (FieldRepr '(name, schema)) where
160 ( KnownSymbol name
356 ( KnownSymbol name
161− , Known (Sing schema)
357+ , SingI schema
162 , Serial m (JsonRepr schema) )
358 , Serial m (JsonRepr schema) )
163 => Serial m (FieldRepr '(name, schema)) where
359 => Serial m (FieldRepr '(name, schema)) where
164 series = FieldRepr <$> series
360 series = FieldRepr <$> series
362+type family USubsets (u :: [k]) :: Constraint where
364+ USubsets (h ': tl) = (USubset tl (h ': tl) (V.RImage tl (h ': tl)), USubsets tl)
166data JsonRepr :: Schema -> Type where
366data JsonRepr :: Schema -> Type where
167 ReprText :: Text -> JsonRepr ('SchemaText cs)
367 ReprText :: Text -> JsonRepr ('SchemaText cs)
168 ReprNumber :: Scientific -> JsonRepr ('SchemaNumber cs)
368 ReprNumber :: Scientific -> JsonRepr ('SchemaNumber cs)
369+ ReprBoolean :: Bool -> JsonRepr 'SchemaBoolean
169 ReprNull :: JsonRepr 'SchemaNull
370 ReprNull :: JsonRepr 'SchemaNull
170 ReprArray :: V.Vector (JsonRepr s) -> JsonRepr ('SchemaArray cs s)
371 ReprArray :: V.Vector (JsonRepr s) -> JsonRepr ('SchemaArray cs s)
171 ReprObject :: Rec FieldRepr fs -> JsonRepr ('SchemaObject fs)
372 ReprObject :: Rec FieldRepr fs -> JsonRepr ('SchemaObject fs)
172 ReprOptional :: Maybe (JsonRepr s) -> JsonRepr ('SchemaOptional s)
373 ReprOptional :: Maybe (JsonRepr s) -> JsonRepr ('SchemaOptional s)
374+ ReprUnion :: Union JsonRepr (h ': tl) -> JsonRepr ('SchemaUnion (h ': tl))
174−instance Show (JsonRepr ('SchemaText cs)) where
175− show (ReprText t) = "ReprText " P.++ show t
177−instance Show (JsonRepr ('SchemaNumber cs)) where
178− show (ReprNumber n) = "ReprNumber " P.++ show n
180−instance Show (JsonRepr 'SchemaNull) where show _ = "ReprNull"
182−instance Show (JsonRepr s) => Show (JsonRepr ('SchemaArray acs s)) where
183− show (ReprArray v) = "ReprArray " P.++ show v
185−instance RecAll FieldRepr fs Show => Show (JsonRepr ('SchemaObject fs)) where
186− show (ReprObject fs) = "ReprObject " P.++ show fs
188−instance Show (JsonRepr s) => Show (JsonRepr ('SchemaOptional s)) where
189− show (ReprOptional s) = "ReprOptional " P.++ show s
191−instance (Monad m, Serial m Text)
376+instance (Monad m, Serial m Text, SingI cs)
192 => Serial m (JsonRepr ('SchemaText cs)) where
377 => Serial m (JsonRepr ('SchemaText cs)) where
193− series = cons1 ReprText
378+ series = decDepth $ fmap ReprText $ textSeries $ fromSing (sing :: Sing cs)
195−instance (Monad m, Serial m Scientific)
380+instance (Monad m, Serial m Scientific, SingI cs)
196 => Serial m (JsonRepr ('SchemaNumber cs)) where
381 => Serial m (JsonRepr ('SchemaNumber cs)) where
197− series = cons1 ReprNumber
382+ series = decDepth $ fmap ReprNumber
383+ $ numberSeries $ fromSing (sing :: Sing cs)
199instance Monad m => Serial m (JsonRepr 'SchemaNull) where
385instance Monad m => Serial m (JsonRepr 'SchemaNull) where
200 series = cons0 ReprNull
386 series = cons0 ReprNull
202−instance (Serial m (V.Vector (JsonRepr s)))
388+instance (Serial m (JsonRepr s), Serial m (V.Vector (JsonRepr s)), SingI cs)
203 => Serial m (JsonRepr ('SchemaArray cs s)) where
389 => Serial m (JsonRepr ('SchemaArray cs s)) where
204− series = cons1 ReprArray
390+ series = decDepth $ fmap ReprArray
391+ $ arraySeries $ fromSing (sing :: Sing cs)
206instance (Serial m (JsonRepr s))
393instance (Serial m (JsonRepr s))
207 => Serial m (JsonRepr ('SchemaOptional s)) where
394 => Serial m (JsonRepr ('SchemaOptional s)) where
@@ -211,6 +398,39 @@ instance (Monad m, Serial m (Rec FieldRepr fs))
211 => Serial m (JsonRepr ('SchemaObject fs)) where
398 => Serial m (JsonRepr ('SchemaObject fs)) where
212 series = cons1 ReprObject
399 series = cons1 ReprObject
401+-- | Move to the union package
402+instance Show (JsonRepr ('SchemaText cs)) where
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
430+instance Show (Union JsonRepr (h ': tl))
431+ => Show (JsonRepr ('SchemaUnion (h ': tl))) where
432+ show (ReprUnion s) = "ReprUnion " P.++ show s
214instance Eq (Rec FieldRepr fs) => Eq (JsonRepr ('SchemaObject fs)) where
434instance Eq (Rec FieldRepr fs) => Eq (JsonRepr ('SchemaObject fs)) where
215 ReprObject a == ReprObject b = a == b
435 ReprObject a == ReprObject b = a == b
@@ -220,6 +440,9 @@ instance Eq (JsonRepr ('SchemaText cs)) where
220instance Eq (JsonRepr ('SchemaNumber cs)) where
440instance Eq (JsonRepr ('SchemaNumber cs)) where
221 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
223instance Eq (JsonRepr 'SchemaNull) where
446instance Eq (JsonRepr 'SchemaNull) where
224 ReprNull == ReprNull = True
447 ReprNull == ReprNull = True
@@ -229,56 +452,127 @@ instance Eq (JsonRepr s) => Eq (JsonRepr ('SchemaArray as s)) where
229instance Eq (JsonRepr s) => Eq (JsonRepr ('SchemaOptional s)) where
452instance Eq (JsonRepr s) => Eq (JsonRepr ('SchemaOptional s)) where
230 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
459+instance Ord (Rec FieldRepr fs) => Ord (JsonRepr ('SchemaObject fs)) where
460+ ReprObject a `compare` ReprObject b = a `compare` b
462+instance Ord (JsonRepr ('SchemaText cs)) where
463+ ReprText a `compare` ReprText b = a `compare` b
465+instance Ord (JsonRepr ('SchemaNumber cs)) where
466+ ReprNumber a `compare` ReprNumber b = a `compare` b
468+instance Ord (JsonRepr 'SchemaBoolean) where
469+ ReprBoolean a `compare` ReprBoolean b = a `compare` b
471+instance Ord (JsonRepr 'SchemaNull) where
474+instance Ord (JsonRepr s) => Ord (JsonRepr ('SchemaArray as s)) where
475+ ReprArray a `compare` ReprArray b = a `compare` b
477+instance Ord (JsonRepr s) => Ord (JsonRepr ('SchemaOptional s)) where
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
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
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
497+instance IsString (JsonRepr ('SchemaText cs)) where
498+ fromString = ReprText . fromString
232fromOptional
500fromOptional
233− :: (Known (Sing s))
501+ :: SingI s
234 => Sing ('SchemaOptional s)
502 => Sing ('SchemaOptional s)
235 -> J.Value
503 -> J.Value
236 -> Parser (Maybe (JsonRepr s))
504 -> Parser (Maybe (JsonRepr s))
237fromOptional _ = parseJSON
505fromOptional _ = parseJSON
508+ :: FromJSON (JsonRepr ('SchemaUnion ss))
509+ => sing (ss :: [Schema])
511+ -> Parser (JsonRepr ('SchemaUnion ss))
512+parseUnion _ val = parseJSON val
514+instance FromJSON (Union JsonRepr '[]) where
515+ parseJSON = fail "empty union"
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)))
521+instance ToJSON (Union JsonRepr as) where
522+ toJSON (This fa) = toJSON fa
523+ toJSON (That u) = toJSON u
239−instance Known (Sing schema) => J.FromJSON (JsonRepr schema) where
525+instance SingI schema => J.FromJSON (JsonRepr schema) where
240− parseJSON value = case known :: Sing schema of
526+ parseJSON value = case sing :: Sing schema of
241− SSchemaText _ -> withText "String" (pure . ReprText) value
527+ SSchemaText _ -> withText "String" (pure . ReprText) value
242− SSchemaNumber _ -> withScientific "Number" (pure . ReprNumber) value
528+ SSchemaNumber _ -> withScientific "Number" (pure . ReprNumber) value
529+ SSchemaBoolean -> ReprBoolean <$> parseJSON value
243− SSchemaNull -> case value of
530+ SSchemaNull -> case value of
244 J.Null -> pure ReprNull
531 J.Null -> pure ReprNull
245 _ -> typeMismatch "Null" value
532 _ -> typeMismatch "Null" value
246− so@(SSchemaOptional _) -> ReprOptional <$> fromOptional so value
533+ so@(SSchemaOptional s) -> withSingI s $ ReprOptional <$> fromOptional so value
534+ SSchemaArray sa sb -> withSingI sa $ withSingI sb
247− SSchemaArray _ _ -> withArray "Array" (fmap ReprArray . traverse parseJSON) value
535+ $ withArray "Array" (fmap ReprArray . traverse parseJSON) value
248− SSchemaObject fs -> do
536+ SSchemaObject fs -> do
250 demoteFields :: SList s -> H.HashMap Text J.Value -> Parser (Rec FieldRepr s)
538 demoteFields :: SList s -> H.HashMap Text J.Value -> Parser (Rec FieldRepr s)
251 demoteFields SNil _ = pure RNil
539 demoteFields SNil _ = pure RNil
252 demoteFields (SCons (STuple2 (n :: Sing fn) s) tl) h = withKnownSymbol n $ do
540 demoteFields (SCons (STuple2 (n :: Sing fn) s) tl) h = withKnownSymbol n $ do
253 let fieldName = T.pack $ symbolVal (Proxy @fn)
541 let fieldName = T.pack $ symbolVal (Proxy @fn)
254 fieldRepr <- case s of
542 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
255− SSchemaText _ -> case H.lookup fieldName h of
549+ SSchemaBoolean -> case H.lookup fieldName h of
256 Just v -> FieldRepr <$> parseJSON v
550 Just v -> FieldRepr <$> parseJSON v
257− Nothing -> fail "schematext"
551+ Nothing -> fail $ "No boolean field: " P.++ show fieldName
258− SSchemaNumber _ -> case H.lookup fieldName h of
259− Just v -> FieldRepr <$> parseJSON v
260− Nothing -> fail "schemanumber"
261 SSchemaNull -> case H.lookup fieldName h of
552 SSchemaNull -> case H.lookup fieldName h of
262 Just v -> FieldRepr <$> parseJSON v
553 Just v -> FieldRepr <$> parseJSON v
263− Nothing -> fail "schemanull"
554+ Nothing -> fail $ "No null field: " P.++ show fieldName
264− SSchemaArray _ _ -> case H.lookup fieldName h of
555+ SSchemaArray sa sb -> case H.lookup fieldName h of
265− Just v -> FieldRepr <$> parseJSON v
556+ Just v -> withSingI sa $ withSingI sb $ FieldRepr <$> parseJSON v
266− Nothing -> fail "schemaarray"
557+ Nothing -> fail $ "No array field: " P.++ show fieldName
267− SSchemaObject _ -> case H.lookup fieldName h of
558+ SSchemaObject so -> case H.lookup fieldName h of
268− Just v -> FieldRepr <$> parseJSON v
559+ Just v -> withSingI so $ FieldRepr <$> parseJSON v
269− Nothing -> fail "schemaobject"
560+ Nothing -> fail $ "No object field" P.++ show fieldName
270− SSchemaOptional _ -> case H.lookup fieldName h of
561+ SSchemaOptional so -> case H.lookup fieldName h of
271− Just v -> FieldRepr <$> parseJSON v
562+ Just v -> withSingI so $ FieldRepr <$> parseJSON v
272− Nothing -> fail "schemaoptional"
563+ Nothing -> withSingI so $ pure $ FieldRepr $ ReprOptional Nothing
564+ SSchemaUnion ss -> withSingI ss $ FieldRepr <$> parseUnion ss value
273 (fieldRepr :&) <$> demoteFields tl h
565 (fieldRepr :&) <$> demoteFields tl h
274 ReprObject <$> withObject "Object" (demoteFields fs) value
566 ReprObject <$> withObject "Object" (demoteFields fs) value
567+ SSchemaUnion ss -> parseUnion ss value
276instance J.ToJSON (JsonRepr a) where
569instance J.ToJSON (JsonRepr a) where
277 toJSON ReprNull = J.Null
570 toJSON ReprNull = J.Null
571+ toJSON (ReprBoolean b) = J.Bool b
278 toJSON (ReprText t) = J.String t
572 toJSON (ReprText t) = J.String t
279 toJSON (ReprNumber n) = J.Number n
573 toJSON (ReprNumber n) = J.Number n
280 toJSON (ReprOptional s) = case s of
574 toJSON (ReprOptional s) = case s of
281− Just v -> toJSON v
575+ Just v -> toJSON v
282 Nothing -> J.Null
576 Nothing -> J.Null
283 toJSON (ReprArray v) = J.Array $ toJSON <$> v
577 toJSON (ReprArray v) = J.Array $ toJSON <$> v
284 toJSON (ReprObject r) = J.Object . H.fromList . fold $ r
578 toJSON (ReprObject r) = J.Object . H.fromList . fold $ r
@@ -291,6 +585,7 @@ instance J.ToJSON (JsonRepr a) where
291 fold = \case
585 fold = \case
292 RNil -> []
586 RNil -> []
293 fr@(FieldRepr _) :& tl -> (extract fr) : fold tl
587 fr@(FieldRepr _) :& tl -> (extract fr) : fold tl
588+ toJSON (ReprUnion u) = toJSON u
295class FalseConstraint a
590class FalseConstraint a