@@ -83,35 +83,30 @@ parseAndValidateTopVersionJson _ v =
8383
8484-- [("top version", schemaTopVersion), ("n-1 version", schemaNminusOneVersion), ...]
8585
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)
92+
93+class Migratable (revisions :: [(Revision, Schema)]) where
94+ mparse :: Sing revisions -> J.Value -> Either String (JsonRepr (Snd (Head revisions)))
95+
96+instance {-# OVERLAPPING #-} (Migratable (Tail revisions), MigrateSchema (Snd (Head (Tail revisions))) (Snd (Head revisions)), SingI (Snd (Head revisions))) => Migratable revisions where
97+ mparse s v = case parseEither parseJSON v of
98+ Left _ -> migrate <$> mparse (sTail s) v
99+ Right x -> Right x
100+
101+instance {-# OVERLAPPABLE #-} (TopLevel (Snd rev), SingI (Snd rev)) => Migratable '[rev] where
102+ mparse _ = parseEither parseJSON
103+
86104parseAndValidateVersionedJson
87− :: forall proxy rav av (v :: Versioned) rs tv
88− . ( SingI (AllVersions v)
89− , Migratable (AllVersions v)
90− , SingI (TopVersion (AllVersions v))
91− , SingI (AllVersions v))
105+ :: forall proxy v. (SingI (AllVersions v), Migratable (AllVersions v))
92106 => proxy v
93107 -> J.Value
94− -> ParseResult (JsonRepr (TopVersion (AllVersions v)))
95−parseAndValidateVersionedJson _ v =
96− let
97− rss :: Sing (AllVersions v)
98− rss = sing
99− stv :: Sing (TopVersion (AllVersions v))
100− stv = sing
101− in parseAndValidateVersionedJsonByVersions stv rss v
102−
103−parseAndValidateVersionedJsonByVersions
104− :: forall (rs :: [(Revision, Schema)]) tv proxy
105− . (Migratable rs)
106− => proxy (tv :: Schema)
107− -> Sing rs
108− -> J.Value
109− -> ParseResult (JsonRepr tv)
110−parseAndValidateVersionedJsonByVersions stv srs value = case srs of
111− SNil -> DecodingError "blabla"
112− SCons (STuple2 r s) tl -> case parseAndValidateJsonBySing s value of
113− Valid a -> Valid $ migrate a
114− _ -> DecodingError "dummy plug"
108+ -> Either String (JsonRepr (Snd (Head (AllVersions v))))
109+parseAndValidateVersionedJson s v = mparse (sing :: Sing (AllVersions v)) v
115110
116111decodeAndValidateJson
117112 :: forall schema