@@ -7,10 +7,12 @@ module Data.Schematic
7 , decodeAndValidateJson
7 , decodeAndValidateJson
8 , parseAndValidateJson
8 , parseAndValidateJson
9 , parseAndValidateVersionedJson
9 , parseAndValidateVersionedJson
10+ , parseAndValidateTopVersionJson
11 , isDecodingError
12 , isDecodingError
12 , isValidationError
13 , isValidationError
13 , ParseResult(..)
14 , ParseResult(..)
16import Control.Monad.Validation
18import Control.Monad.Validation
@@ -42,27 +44,6 @@ parseAndValidateJson v =
42 Left em -> ValidationError em
44 Left em -> ValidationError em
43 Right () -> Valid jsonRepr
45 Right () -> Valid jsonRepr
45−parseAndValidateJsonBySing
46− :: forall schema proxy
47− . (J.FromJSON (JsonRepr schema), TopLevel schema)
50− -> ParseResult (JsonRepr schema)
51−parseAndValidateJsonBySing sschema v = case parseEither parseJSON v of
52− Left s -> DecodingError $ T.pack s
55− validate = validateJsonRepr sschema [] jsonRepr
56− res = runIdentity . runValidationTEither $ validate
58− Left em -> ValidationError em
59− Right () -> Valid jsonRepr
61−-- parseAndValidateVersionedJson
64−-- -> ParseResult (JsonRepr)
66parseAndValidateTopVersionJson
47parseAndValidateTopVersionJson
67 :: forall proxy (v :: Versioned)
48 :: forall proxy (v :: Versioned)
68 . (SingI (TopVersion (AllVersions v)))
49 . (SingI (TopVersion (AllVersions v)))
@@ -81,32 +62,30 @@ parseAndValidateTopVersionJson _ v =
81 Left em -> ValidationError em
62 Left em -> ValidationError em
82 Right () -> Valid jsonRepr
63 Right () -> Valid jsonRepr
84−-- [("top version", schemaTopVersion), ("n-1 version", schemaNminusOneVersion), ...]
86−-- type family Migratable (rs :: [(Revision, Schema)]) :: Constraint where
87−-- -- constraint duplication
88−-- Migratable ('(r,s) ': '(r', s') ': tl) =
89−-- (SingI s, MigrateSchema s s', TopLevel s, Migratable ('(r',s') ': tl))
90−-- Migratable ('(r,s) ': '[]) = (TopLevel s, SingI s)
91−-- -- Migratable '[] = ('True ~ 'False)
93class Migratable (revisions :: [(Revision, Schema)]) where
65class Migratable (revisions :: [(Revision, Schema)]) where
94− mparse :: Sing revisions -> J.Value -> Either String (JsonRepr (Snd (Head revisions)))
69+ -> ParseResult (JsonRepr (Snd (Head revisions)))
71+instance {-# OVERLAPPING #-}
72+ ( Migratable (Tail revisions)
96−instance {-# OVERLAPPING #-} (Migratable (Tail revisions), MigrateSchema (Snd (Head (Tail revisions))) (Snd (Head revisions)), SingI (Snd (Head revisions))) => Migratable revisions where
73+ , MigrateSchema (Snd (Head (Tail revisions))) (Snd (Head revisions))
74+ , SingI (Snd (Head revisions)))
75+ => Migratable revisions where
97 mparse s v = case parseEither parseJSON v of
76 mparse s v = case parseEither parseJSON v of
98 Left _ -> migrate <$> mparse (sTail s) v
77 Left _ -> migrate <$> mparse (sTail s) v
99− Right x -> Right x
78+ Right x -> Valid x
101instance {-# OVERLAPPABLE #-} (TopLevel (Snd rev), SingI (Snd rev)) => Migratable '[rev] where
80instance {-# OVERLAPPABLE #-} (TopLevel (Snd rev), SingI (Snd rev)) => Migratable '[rev] where
102− mparse _ = parseEither parseJSON
81+ mparse _ = parseAndValidateJson
104parseAndValidateVersionedJson
83parseAndValidateVersionedJson
105 :: forall proxy v. (SingI (AllVersions v), Migratable (AllVersions v))
84 :: forall proxy v. (SingI (AllVersions v), Migratable (AllVersions v))
106 => proxy v
85 => proxy v
107 -> J.Value
86 -> J.Value
108− -> Either String (JsonRepr (Snd (Head (AllVersions v))))
87+ -> ParseResult (JsonRepr (Snd (Head (AllVersions v))))
109−parseAndValidateVersionedJson s v = mparse (sing :: Sing (AllVersions v)) v
88+parseAndValidateVersionedJson _ v = mparse (sing :: Sing (AllVersions v)) v
111decodeAndValidateJson
90decodeAndValidateJson
112 :: forall schema
91 :: forall schema