1{-# OPTIONS -fprint-explicit-kinds #-}
2{-# LANGUAGE AllowAmbiguousTypes #-}
5 ( module Data.Schematic.JsonSchema
6 , module Data.Schematic.Helpers
7 , module Data.Schematic.Lens
8 , module Data.Schematic.Migration
9 , module Data.Schematic.Schema
10 , module Data.Schematic.Utils
11 , decodeAndValidateJson
12 , parseAndValidateJson
13 , parseAndValidateJsonBy
14 , parseAndValidateTopVersionJson
15 , parseAndValidateWithMList
16 , decodeAndValidateVersionedWithMList
17 , decodeAndValidateVersionedWithPureMList
27import Control.Applicative
28import Control.Monad.Validation
30import Data.Aeson.Types as J
31import Data.ByteString.Lazy as BL
32import Data.Functor.Identity
34import Data.Schematic.Helpers
35import Data.Schematic.JsonSchema
36import Data.Schematic.Lens
37import Data.Schematic.Migration
38import Data.Schematic.Schema
39import Data.Schematic.Utils
40import Data.Schematic.Validation
42import Data.Singletons.Prelude
43import Data.Singletons.TypeLits
47import qualified Data.Vector as V
53 . (J.FromJSON (JsonRepr schema), TopLevel schema, SingI schema)
55 -> ParseResult (JsonRepr schema)
56parseAndValidateJson v =
57 case parseEither parseJSON v of
58 Left s -> DecodingError $ T.pack s
61 validate = validateJsonRepr (sing :: Sing schema) [] jsonRepr
62 res = runIdentity . runValidationTEither $ validate
64 Left em -> ValidationError em
65 Right () -> Valid jsonRepr
68 :: (J.FromJSON (JsonRepr schema), TopLevel schema, SingI schema)
71 -> ParseResult (JsonRepr schema)
72parseAndValidateJsonBy _ = parseAndValidateJson
74parseAndValidateTopVersionJson
75 :: forall proxy (v :: Versioned)
76 . (SingI (TopVersion (AllVersions v)))
79 -> ParseResult (JsonRepr (TopVersion (AllVersions v)))
80parseAndValidateTopVersionJson _ v =
81 case parseEither parseJSON v of
82 Left s -> DecodingError $ T.pack s
86 validateJsonRepr (sing :: Sing (TopVersion (AllVersions v))) [] jsonRepr
87 res = runIdentity . runValidationTEither $ validate
89 Left em -> ValidationError em
90 Right () -> Valid jsonRepr
92parseAndValidateWithMList
96 -> m (ParseResult (JsonRepr (Head revisions)))
97parseAndValidateWithMList MNil v = pure $ parseAndValidateJson v
98parseAndValidateWithMList ((:&&) p f tl) v = case parseAndValidateJsonBy p v of
99 Valid a -> pure $ Valid a
100 DecodingError _ -> do
101 pr <- parseAndValidateWithMList tl v
104 ValidationError _ -> do
105 pr <- parseAndValidateWithMList tl v
111 . (J.FromJSON (JsonRepr schema), TopLevel schema, SingI schema)
113 -> ParseResult (JsonRepr schema)
114decodeAndValidateJson bs = case decode bs of
115 Nothing -> DecodingError "malformed json"
116 Just x -> parseAndValidateJson x
118type family MapSnd (l :: [(a,k)]) = (r :: [k]) where
120 MapSnd ( '(a, b) ': tl) = b ': MapSnd tl
122decodeAndValidateVersionedWithMList
125 -> MList m (MapSnd (AllVersions versioned))
127 -> m (ParseResult (JsonRepr (Head (MapSnd (AllVersions versioned)))))
128decodeAndValidateVersionedWithMList _ mlist bs = case decode bs of
129 Nothing -> pure $ DecodingError "malformed json"
130 Just x -> parseAndValidateWithMList mlist x
132decodeAndValidateVersionedWithPureMList
134 -> MList Identity (MapSnd (AllVersions versioned))
136 -> ParseResult (JsonRepr (Head (MapSnd (AllVersions versioned))))
137decodeAndValidateVersionedWithPureMList a b c =
138 runIdentity $ decodeAndValidateVersionedWithMList a b c
140-- convenience helpers
143 = forall b. FSubset (FieldsOf a) b (FImage (FieldsOf a) b)
144 => Tagged (FieldsOf a) (Rec FieldRepr b)
145 -> JsonRepr ('SchemaObject (FieldsOf a))
147withRepr :: Constructor a
148withRepr = ReprObject . unTagged . fmap fcast
153 -> m (Rec FieldRepr b)
154 -> m (Rec FieldRepr (fns ': b))
159class Representable s where
161 construct :: Sing fn -> Proxy s -> Repr s -> FieldRepr '(fn, s)
163instance SingI so => Representable ('SchemaObject so) where
164 type Repr ('SchemaObject so) = Rec FieldRepr so
165 construct sfn _ o = withKnownSymbol sfn $ FieldRepr $ ReprObject o
167instance (SingI cs, SingI sa) => Representable ('SchemaArray cs sa) where
168 type Repr ('SchemaArray cs sa) = V.Vector (JsonRepr sa)
169 construct sfn _ a = withKnownSymbol sfn $ FieldRepr $ ReprArray a
171instance SingI cs => Representable ('SchemaText cs) where
172 type Repr ('SchemaText cs) = Text
173 construct sfn _ t = withKnownSymbol sfn $ FieldRepr $ ReprText t
175instance SingI cs => Representable ('SchemaNumber cs) where
176 type Repr ('SchemaNumber cs) = Scientific
177 construct sfn _ n = withKnownSymbol sfn $ FieldRepr $ ReprNumber n
179instance Representable 'SchemaBoolean where
180 type Repr 'SchemaBoolean = Bool
181 construct sfn _ b = withKnownSymbol sfn $ FieldRepr $ ReprBoolean b
183instance SingI so => Representable ('SchemaOptional so) where
184 type Repr ('SchemaOptional so) = Maybe (JsonRepr so)
185 construct sfn _ o = withKnownSymbol sfn $ FieldRepr $ ReprOptional o
187instance SingI (h ': tl) => Representable ('SchemaUnion (h ': tl)) where
188 type Repr ('SchemaUnion (h ': tl)) = Union JsonRepr (h ': tl)
189 construct sfn _ u = withKnownSymbol sfn $ FieldRepr $ ReprUnion u
191type family FieldsOf (s :: Schema) :: [(Symbol, Schema)] where
192 FieldsOf ('SchemaObject fs) = fs
194type FieldConstructor fn =
195 forall fs. (Representable (ByField fn fs (FIndex fn fs)))
196 => Repr (ByField fn fs (FIndex fn fs))
197 -> Tagged fs (FieldRepr '(fn, (ByField fn fs (FIndex fn fs))))
199field :: forall fn. KnownSymbol fn => FieldConstructor fn
200field = Tagged . construct (sing :: Sing fn) Proxy
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list