@@ -10,6 +10,8 @@ import Data.Schematic.Utils
10import Data.Singletons.Prelude hiding (All)
10import Data.Singletons.Prelude hiding (All)
11import Data.Singletons.TypeLits
11import Data.Singletons.TypeLits
13+class MigrateSchema (a :: Schema) (b :: Schema) where
14+ migrate :: JsonRepr a -> JsonRepr b
14data Action = AddKey Symbol Schema | Update Schema | DeleteKey
16data Action = AddKey Symbol Schema | Update Schema | DeleteKey
@@ -42,13 +44,24 @@ type family TypedDiffList (s :: Schema) (ds :: [Diff]) :: Constraint where
42 TypedDiffList s '[] = ()
44 TypedDiffList s '[] = ()
43 TypedDiffList s (d ': tl) = (TypedDiff s d, TypedDiffList s tl)
45 TypedDiffList s (d ': tl) = (TypedDiff s d, TypedDiffList s tl)
47+type family TypedDiffListSchema (s :: Schema) (ds :: [Diff]) :: Schema where
48+ TypedDiffListSchema s '[] = s
49+ TypedDiffListSchema s (d ': tl) = TypedDiffListSchema (TypedDiffSchema s d) tl
45type family TypedDiff (s :: Schema) (d :: Diff) :: Constraint where
51type family TypedDiff (s :: Schema) (d :: Diff) :: Constraint where
46 TypedDiff s ('Diff ps a) = TypedSubSchema s ps
52 TypedDiff s ('Diff ps a) = TypedSubSchema s ps
54+type family TypedDiffSchema (s :: Schema) (d :: Diff) :: Schema where
55+ TypedDiffSchema s ('Diff ps a) = TypedSubSchemaSchema s ps
48type family TypedSubSchema (s :: Schema) (p :: [PathSegment]) :: Constraint where
57type family TypedSubSchema (s :: Schema) (p :: [PathSegment]) :: Constraint where
49 TypedSubSchema s '[] = ()
58 TypedSubSchema s '[] = ()
50 TypedSubSchema s (h ': tl) = TypedSubSchema (TraverseStep s h) tl
59 TypedSubSchema s (h ': tl) = TypedSubSchema (TraverseStep s h) tl
61+type family TypedSubSchemaSchema (s :: Schema) (p :: [PathSegment]) :: Schema where
62+ TypedSubSchemaSchema s '[] = s
63+ TypedSubSchemaSchema s (h ': tl) = TypedSubSchemaSchema (TraverseStep s h) tl
52type family TraverseStep (s :: Schema) (ps :: PathSegment) :: Schema where
65type family TraverseStep (s :: Schema) (ps :: PathSegment) :: Schema where
53 TraverseStep ('SchemaArray acs s) ('Ix n) = s
66 TraverseStep ('SchemaArray acs s) ('Ix n) = s
54 TraverseStep ('SchemaObject ( '(fn, s) ': tl)) ('Key fn) = s
67 TraverseStep ('SchemaObject ( '(fn, s) ': tl)) ('Key fn) = s
@@ -67,6 +80,9 @@ data Migration = Migration Revision [Diff]
67type family TypedMigration (s :: Schema) (m :: Migration) :: Constraint where
80type family TypedMigration (s :: Schema) (m :: Migration) :: Constraint where
68 TypedMigration s ('Migration r ds) = (TypedDiffList s ds)
81 TypedMigration s ('Migration r ds) = (TypedDiffList s ds)
83+type family TypedMigrationSchema (s :: Schema) (m :: Migration) :: (Symbol, Schema) where
84+ TypedMigrationSchema s ('Migration r ds) = '(r, TypedDiffListSchema s ds)
70data instance Sing (m :: Migration) where
86data instance Sing (m :: Migration) where
71 SMigration
87 SMigration
72 :: (KnownSymbol r, Known (Sing ds))
88 :: (KnownSymbol r, Known (Sing ds))
@@ -76,9 +92,23 @@ data instance Sing (m :: Migration) where
77data Versioned = Versioned Schema [Migration]
93data Versioned = Versioned Schema [Migration]
95+type family TypedVersioned (s :: Schema) (ms :: [Migration]) :: Constraint where
96+ TypedVersioned s '[] = ()
97+ TypedVersioned s (h ': tl) =
99+ , MigrateSchema s (Snd (TypedMigrationSchema s h))
100+ , TypedVersioned (Snd (TypedMigrationSchema s h)) tl )
102+type family TypedVersionedSchema
79data instance Sing (v :: Versioned) where
104data instance Sing (v :: Versioned) where
80 SVersioned
105 SVersioned
81 :: (Known (Sing s), Known (Sing ms), TypedMigration s m)
106 :: (Known (Sing s), Known (Sing ms), TypedMigration s m)
82 => Sing (s :: Schema) -- base version
107 => Sing (s :: Schema) -- base version
83 -> Sing (ms :: [Migration]) -- a bunch of migrations
108 -> Sing (ms :: [Migration]) -- a bunch of migrations
84 -> Sing ('Versioned s ms)
109 -> Sing ('Versioned s ms)
111+type family HeadVersion (vd :: Versioned) :: Schema where
112+ HeadVersion ('Versioned s '[]) = s
113+ HeadVersion ('Versioned s (m ': tl)) =
114+ HeadVersion ('Versioned (Snd (TypedMigrationSchema s m)) tl)