Skip to content

Commit f962452

Browse files
wip
1 parent 0f7bbfd commit f962452

File tree

3 files changed

+58
-13
lines changed

3 files changed

+58
-13
lines changed

extra/Lamdera/Evergreen/MigrationGenerator.hs

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -253,12 +253,17 @@ migrateUnionDefinition_ author pkg oldUnion newUnion tvarMapOld tvarMapNew oldVe
253253
migration :: Text
254254
migration = migrationName <> " " <> tvarMigrationTextsCombined
255255

256-
paramMigrationPairs = zip tvarsOld tvarsNew
256+
paramMigrationPairs :: [(N.Name, N.Name)]
257+
paramMigrationPairs = filter (\(_, newTVar) -> isTVarInUseUnion newTVar) (zip tvarsOld tvarsNew)
257258

258259
paramMigrationFnsTypeSig :: [Text]
259260
paramMigrationFnsTypeSig =
260261
paramMigrationPairs
261-
& fmap (\(oldT, newT) -> T.concat [ "(", N.toText oldT, "_old -> ", N.toText newT, "_new)" ] )
262+
& fmap (\(oldT, newT) -> T.concat [ "(", N.toText oldT, "_old -> ", N.toText newT, "_new)" ])
263+
264+
isTVarInUseUnion :: N.Name -> Bool
265+
isTVarInUseUnion newTVar =
266+
any (\(Can.Ctor _ _ _ params) -> any (isTVarInUse newTVar) params) (Can._u_alts newUnion)
262267

263268
paramMigrationVars :: Text
264269
paramMigrationVars =
@@ -660,11 +665,11 @@ canAliasToMigration oldVersion newVersion scope interfaces recursionSet (typeNew
660665
usageParamMigrationFns :: [Text]
661666
usageParamMigrationFns = usageParamMigrations & fmap migrationFn
662667

663-
tvarsNew :: [Text]
664-
tvarsNew = tvarMapNew & fmap (N.toText . fst)
668+
tvarsNew :: [N.Name]
669+
tvarsNew = tvarMapNew & fmap fst
665670

666-
tvarsOld :: [Text]
667-
tvarsOld = tvarMapOldReplaced & fmap (N.toText . fst)
671+
tvarsOld :: [N.Name]
672+
tvarsOld = tvarMapOldReplaced & fmap fst
668673

669674
(MigrationNested migrationAliasedType imps subDefs) =
670675
let
@@ -680,17 +685,28 @@ canAliasToMigration oldVersion newVersion scope interfaces recursionSet (typeNew
680685
migrationName :: Text
681686
migrationName = migrationNameUnderscored newModule oldVersion newVersion typeNameNew
682687

683-
paramMigrationPairs = zip tvarsOld tvarsNew
688+
paramMigrationPairs :: [(N.Name, N.Name)]
689+
paramMigrationPairs =
690+
filter
691+
(\(_, newTVar) ->
692+
isTVarInUse
693+
newTVar
694+
(case aliasTypeNew of
695+
Can.Holey a -> debugHaskellPass "123123123123" newTVar a
696+
Can.Filled a -> debugHaskellPass "123123123123" newTVar a
697+
)
698+
)
699+
(zip tvarsOld tvarsNew)
684700

685701
paramMigrationFnsTypeSig :: [Text]
686702
paramMigrationFnsTypeSig =
687703
paramMigrationPairs
688-
& fmap (\(oldT, newT) -> T.concat [ "(", oldT, "_old -> ", newT, "_new)" ] )
704+
& fmap (\(oldT, newT) -> T.concat [ "(", N.toText oldT, "_old -> ", N.toText newT, "_new)" ] )
689705

690706
paramMigrationVars :: Text
691707
paramMigrationVars =
692708
paramMigrationPairs
693-
& fmap (\(oldT, newT) -> T.concat [ "migrate_", oldT ] )
709+
& fmap (\(oldT, newT) -> T.concat [ "migrate_", N.toText oldT ] )
694710
& T.intercalate " "
695711

696712
oldType = T.concat [moduleNameOld & dropCan & N.toText, ".", typeNameOld & N.toText]
@@ -700,9 +716,9 @@ canAliasToMigration oldVersion newVersion scope interfaces recursionSet (typeNew
700716
migrationTypeSignature = T.concat
701717
[ paramMigrationFnsTypeSig & T.intercalate " -> " & suffixIfNonempty " -> "
702718
, " " , oldType , " "
703-
, tvarsOld & fmap (\tvar -> T.concat [tvar, "_old"]) & T.intercalate " "
719+
, tvarsOld & fmap (\tvar -> T.concat [N.toText tvar, "_old"]) & T.intercalate " "
704720
, " -> " , newType , " "
705-
, tvarsNew & fmap (\tvar -> T.concat [tvar, "_new"]) & T.intercalate " "
721+
, tvarsNew & fmap (\tvar -> T.concat [N.toText tvar, "_new"]) & T.intercalate " "
706722
]
707723

708724
applyOldValueIfNotRecord m =

extra/Lamdera/Evergreen/MigrationGeneratorHelpers.hs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,35 @@ loadTvar tvarMap name =
644644
-- This would be the alternative:
645645
-- Can.Tvar name
646646

647+
isTVarInUse :: N.Name -> Can.Type -> Bool
648+
isTVarInUse tVar typeValue =
649+
case typeValue of
650+
Can.TLambda a b -> isTVarInUse tVar a || isTVarInUse tVar b
651+
652+
Can.TVar name -> tVar == name
653+
654+
Can.TType _ _ typeVars -> any (isTVarInUse tVar) typeVars
655+
656+
Can.TRecord fields maybeExtension ->
657+
any (\(Can.FieldType _ fieldType) -> isTVarInUse tVar fieldType) fields
658+
|| case maybeExtension of
659+
Just extension -> tVar == extension
660+
Nothing -> False
661+
662+
Can.TUnit -> False
663+
664+
Can.TTuple t0 t1 maybeT2 ->
665+
isTVarInUse tVar t0
666+
|| isTVarInUse tVar t1
667+
|| case maybeT2 of
668+
Just t2 -> isTVarInUse tVar t2
669+
Nothing -> False
670+
671+
Can.TAlias moduleNameCanonical name fields aliasType ->
672+
any (\(_, fieldType) -> isTVarInUse tVar fieldType) fields
673+
|| case aliasType of
674+
Can.Holey a -> isTVarInUse tVar a
675+
Can.Filled a -> isTVarInUse tVar a
647676

648677
tvarResolveParams :: [Can.Type] -> [(N.Name, Can.Type)] -> [Can.Type]
649678
tvarResolveParams params tvarMap =

test/scenario-migration-generate/src/Migrate_External_Paramed/Actual.elm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ migrate_Migrate_External_Paramed_New_Point2d migrate_units migrate_coordinates o
106106
old |> migrate_Migrate_External_Paramed_New_Point2d_ migrate_units migrate_coordinates
107107

108108

109-
migrate_Migrate_External_Paramed_New_Point2d_ : (units_old -> units_new) -> (coordinates_old -> coordinates_new) -> Migrate_External_Paramed.Old.Point2d_ units_old coordinates_old -> Migrate_External_Paramed.New.Point2d_ units_new coordinates_new
110-
migrate_Migrate_External_Paramed_New_Point2d_ migrate_units migrate_coordinates old =
109+
migrate_Migrate_External_Paramed_New_Point2d_ : Migrate_External_Paramed.Old.Point2d_ units_old coordinates_old -> Migrate_External_Paramed.New.Point2d_ units_new coordinates_new
110+
migrate_Migrate_External_Paramed_New_Point2d_ old =
111111
case old of
112112
Migrate_External_Paramed.Old.Point2d_ p0 ->
113113
Migrate_External_Paramed.New.Point2d_ p0

0 commit comments

Comments
 (0)