From bdec373c3366fde16f9d270e7a0540e0e67cdf93 Mon Sep 17 00:00:00 2001 From: Leonardo Taglialegne Date: Tue, 12 Mar 2024 00:02:38 +0100 Subject: [PATCH 01/14] Generate bindings from the template app --- codegen/elm.codegen.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/codegen/elm.codegen.json b/codegen/elm.codegen.json index ed18a2666..7f8b0e9a4 100644 --- a/codegen/elm.codegen.json +++ b/codegen/elm.codegen.json @@ -11,7 +11,8 @@ "elm/browser": "1.0.2" }, "local": [ - "src/" + "src/", + "generator/template/app/" ] } } \ No newline at end of file From 541f9c17d5b10829987886ab71386c878788d905 Mon Sep 17 00:00:00 2001 From: Leonardo Taglialegne Date: Tue, 12 Mar 2024 09:05:01 +0100 Subject: [PATCH 02/14] Use local helpers for more compact code --- codegen/GenerateMain.elm | 251 +++++++++------------------------------ 1 file changed, 53 insertions(+), 198 deletions(-) diff --git a/codegen/GenerateMain.elm b/codegen/GenerateMain.elm index d18c0aa62..9227ada68 100644 --- a/codegen/GenerateMain.elm +++ b/codegen/GenerateMain.elm @@ -8,6 +8,7 @@ import Elm.Extra exposing (expose, fnIgnore, topLevelValue) import Elm.Let import Elm.Op import Elm.Pattern +import Gen.Api import Gen.ApiRoute import Gen.BackendTask import Gen.Basics @@ -15,6 +16,8 @@ import Gen.Bytes import Gen.Bytes.Decode import Gen.Bytes.Encode import Gen.Dict +import Gen.Effect +import Gen.ErrorPage import Gen.Head import Gen.Html import Gen.Json.Decode @@ -30,12 +33,16 @@ import Gen.Pages.Internal.RoutePattern import Gen.Pages.Navigation import Gen.Pages.PageUrl import Gen.PagesMsg +import Gen.Platform.Sub import Gen.Server.Request import Gen.Server.Response +import Gen.Shared +import Gen.Site import Gen.String import Gen.Tuple import Gen.Url import Gen.UrlPath +import Gen.View import Pages.Internal.RoutePattern as RoutePattern exposing (RoutePattern) import String.Case @@ -67,7 +74,7 @@ otherFile routes phaseString = , update = update.value [] , subscriptions = subscriptions.value [] , sharedData = - Elm.value { name = "template", importFrom = [ "Shared" ], annotation = Nothing } + Gen.Shared.values_.template |> Elm.get "data" , data = dataForRoute.value [] , action = action.value [] @@ -111,19 +118,13 @@ otherFile routes phaseString = Elm.nothing Cli -> - Elm.just - (Elm.value - { name = "config" - , annotation = Nothing - , importFrom = [ "Site" ] - } - ) + Elm.just Gen.Site.values_.config , toJsPort = Elm.val "toJsPort" , fromJsPort = applyIdentityTo (Elm.val "fromJsPort") , gotBatchSub = case phase of Browser -> - subNone + Gen.Platform.Sub.values_.none Cli -> applyIdentityTo (Elm.val "gotBatchSub") @@ -141,7 +142,7 @@ otherFile routes phaseString = Elm.Op.cons pathsToGenerateHandler.reference (Elm.Op.cons routePatterns.reference (Elm.Op.cons apiPatterns.reference - (Elm.apply (Elm.value { name = "routes", importFrom = [ "Api" ], annotation = Nothing }) + (Elm.apply Gen.Api.values_.routes [ getStaticRoutes.reference , htmlToString ] @@ -169,36 +170,11 @@ otherFile routes phaseString = Cli -> Elm.just (globalHeadTags.value []) - , cmdToEffect = - Elm.value - { annotation = Nothing - , name = "fromCmd" - , importFrom = [ "Effect" ] - } - , perform = - Elm.value - { annotation = Nothing - , name = "perform" - , importFrom = [ "Effect" ] - } - , errorStatusCode = - Elm.value - { annotation = Nothing - , name = "statusCode" - , importFrom = [ "ErrorPage" ] - } - , notFoundPage = - Elm.value - { annotation = Nothing - , name = "notFound" - , importFrom = [ "ErrorPage" ] - } - , internalError = - Elm.value - { annotation = Nothing - , name = "internalError" - , importFrom = [ "ErrorPage" ] - } + , cmdToEffect = Gen.Effect.values_.fromCmd + , perform = Gen.Effect.values_.perform + , errorStatusCode = Gen.ErrorPage.values_.statusCode + , notFoundPage = Gen.ErrorPage.values_.notFound + , internalError = Gen.ErrorPage.values_.internalError , errorPageToData = Elm.val "DataErrorPage____" , notFoundRoute = Elm.nothing } @@ -276,11 +252,7 @@ otherFile routes phaseString = |> Elm.Case.patternToBranch (\subModel -> Elm.apply - (Elm.value - { importFrom = [ "Shared" ] - , name = "template" - , annotation = Nothing - } + (Gen.Shared.values_.template |> Elm.get "view" ) [ globalData @@ -292,19 +264,14 @@ otherFile routes phaseString = (Elm.apply (Elm.val "MsgGlobal") [ myMsg ]) ) , Elm.apply - (Elm.value { importFrom = [ "View" ], name = "map", annotation = Nothing }) + Gen.View.values_.map [ Elm.functionReduced "myMsg" (\myMsg -> Gen.PagesMsg.fromMsg (Elm.apply (Elm.val "MsgErrorPage____") [ myMsg ]) ) , Elm.apply - (Elm.value - { importFrom = [ "ErrorPage" ] - , name = "view" - , annotation = Nothing - } - ) + Gen.ErrorPage.values_.view [ data , subModel ] @@ -342,11 +309,7 @@ otherFile routes phaseString = |> Elm.Case.patternToBranch (\subModel -> Elm.apply - (Elm.value - { importFrom = [ "Shared" ] - , name = "template" - , annotation = Nothing - } + (Gen.Shared.values_.template |> Elm.get "view" ) [ globalData @@ -358,7 +321,7 @@ otherFile routes phaseString = (Elm.apply (Elm.val "MsgGlobal") [ myMsg ]) ) , Elm.apply - (Elm.value { importFrom = [ "View" ], name = "map", annotation = Nothing }) + Gen.View.values_.map [ Elm.functionReduced "innerPageMsg" (Gen.PagesMsg.call_.map (route |> routeVariantExpression Msg)) @@ -518,11 +481,7 @@ otherFile routes phaseString = (\route path model -> subBatch [ Elm.apply - (Elm.value - { importFrom = [ "Shared" ] - , name = "template" - , annotation = Nothing - } + (Gen.Shared.values_.template |> Elm.get "subscriptions" ) [ path @@ -586,7 +545,7 @@ otherFile routes phaseString = ( "model", Type.named [] "Model" |> Just ) (\maybeRoute path model -> Elm.Case.maybe maybeRoute - { nothing = subNone + { nothing = Gen.Platform.Sub.values_.none , just = ( "justRoute" , \justRoute -> @@ -613,7 +572,7 @@ otherFile routes phaseString = ] |> subMap (route |> routeVariantExpression Msg) ) - , Elm.Case.otherwise (\_ -> subNone) + , Elm.Case.otherwise (\_ -> Gen.Platform.Sub.values_.none) ] ) ) @@ -764,21 +723,11 @@ otherFile routes phaseString = ] ) (Elm.apply - (Elm.value - { annotation = Nothing - , name = "batch" - , importFrom = [ "Effect" ] - } - ) + Gen.Effect.values_.batch [ Elm.list [ templateCmd , Elm.apply - (Elm.value - { annotation = Nothing - , importFrom = [ "Effect" ] - , name = "map" - } - ) + Gen.Effect.values_.map [ Elm.val "MsgGlobal" , globalCmd ] @@ -848,12 +797,7 @@ otherFile routes phaseString = ) , ( "submit" , Elm.apply - (Elm.value - { importFrom = [ "Pages", "Fetcher" ] - , name = "submit" - , annotation = Nothing - } - ) + Gen.Pages.Fetcher.values_.submit [ route |> decodeRouteType ActionData ] ) @@ -865,7 +809,7 @@ otherFile routes phaseString = |> Gen.Tuple.call_.mapBoth (route |> routeVariantExpression Model) (Elm.apply - (Elm.value { name = "map", importFrom = [ "Effect" ], annotation = Nothing }) + Gen.Effect.values_.map [ route |> routeVariantExpression Msg ] ) @@ -885,15 +829,11 @@ otherFile routes phaseString = (currentGlobalModel |> Gen.Maybe.map (\m -> - Elm.tuple m (Elm.value { annotation = Nothing, importFrom = [ "Effect" ], name = "none" }) + Elm.tuple m Gen.Effect.values_.none ) |> Gen.Maybe.withDefault (Elm.apply - (Elm.value - { importFrom = [ "Shared" ] - , name = "template" - , annotation = Nothing - } + (Gen.Shared.values_.template |> Elm.get "init" ) [ userFlags, maybePagePath ] @@ -955,12 +895,7 @@ otherFile routes phaseString = |> Elm.Case.patternToBranch (\( pageModel, thisPageData ) -> Elm.apply - (Elm.value - { importFrom = [ "ErrorPage" ] - , name = "update" - , annotation = Nothing - } - ) + Gen.ErrorPage.values_.update [ thisPageData , msg_ , pageModel @@ -971,7 +906,7 @@ otherFile routes phaseString = , Elm.Pattern.ignore |> Elm.Case.patternToBranch (\() -> - Elm.tuple (model |> Elm.get "page") effectNone + Elm.tuple (model |> Elm.get "page") Gen.Effect.values_.none ) ] ) @@ -988,11 +923,7 @@ otherFile routes phaseString = ) |> Elm.Let.destructure (Elm.Pattern.tuple (Elm.Pattern.var "sharedModel") (Elm.Pattern.var "globalCmd")) (Elm.apply - (Elm.value - { importFrom = [ "Shared" ] - , name = "template" - , annotation = Nothing - } + (Gen.Shared.values_.template |> Elm.get "update" ) [ msg_, model |> Elm.get "global" ] @@ -1005,7 +936,7 @@ otherFile routes phaseString = Elm.Let.letIn (\() ( updatedModel, cmd ) -> Elm.Case.maybe - (Elm.value { importFrom = [ "Shared" ], name = "template", annotation = Nothing } + (Gen.Shared.values_.template |> Elm.get "onPageChange" ) { nothing = Elm.tuple updatedModel cmd @@ -1015,7 +946,7 @@ otherFile routes phaseString = Elm.Let.letIn (\( updatedGlobalModel, globalCmd ) -> Elm.tuple (Elm.updateRecord [ ( "global", updatedGlobalModel ) ] updatedModel) - (Elm.apply (Elm.value { importFrom = [ "Effect" ], name = "batch", annotation = Nothing }) + (Elm.apply Gen.Effect.values_.batch [ Elm.list [ cmd , effectMap (Elm.val "MsgGlobal") globalCmd @@ -1025,7 +956,7 @@ otherFile routes phaseString = ) |> Elm.Let.destructure (Elm.Pattern.tuple (Elm.Pattern.var "updatedGlobalModel") (Elm.Pattern.var "globalCmd")) (Elm.apply - (Elm.value { importFrom = [ "Shared" ], name = "template", annotation = Nothing } + (Gen.Shared.values_.template |> Elm.get "update" ) [ Elm.apply thingy @@ -1135,21 +1066,11 @@ otherFile routes phaseString = ] ) (Elm.apply - (Elm.value - { name = "batch" - , importFrom = [ "Effect" ] - , annotation = Nothing - } - ) + Gen.Effect.values_.batch [ Elm.list [ pageCmd , Elm.apply - (Elm.value - { name = "map" - , importFrom = [ "Effect" ] - , annotation = Nothing - } - ) + Gen.Effect.values_.map [ Elm.val "MsgGlobal" , newGlobalCmd ] @@ -1223,7 +1144,7 @@ otherFile routes phaseString = , Elm.Pattern.ignore |> Elm.Case.patternToBranch (\() -> - Elm.tuple model effectNone + Elm.tuple model Gen.Effect.values_.none ) ] ) @@ -1278,23 +1199,19 @@ otherFile routes phaseString = Elm.triple (Elm.apply wrapModel [ a ]) (Elm.apply - (Elm.value { name = "map", importFrom = [ "Effect" ], annotation = Nothing }) + Gen.Effect.values_.map [ wrapMsg, b ] ) (Elm.Case.maybe c { nothing = Elm.tuple (model |> Elm.get "global") - effectNone + Gen.Effect.values_.none , just = ( "sharedMsg" , \sharedMsg -> Elm.apply - (Elm.value - { importFrom = [ "Shared" ] - , name = "template" - , annotation = Nothing - } + (Gen.Shared.values_.template |> Elm.get "update" ) [ sharedMsg @@ -1339,21 +1256,16 @@ otherFile routes phaseString = ( "pageData", Type.named [] "PageData" |> Just ) (\pageData -> Elm.apply - (Elm.value - { importFrom = [ "ErrorPage" ] - , name = "init" - , annotation = Nothing - } - ) + Gen.ErrorPage.values_.init [ Elm.Case.custom pageData Type.unit [ Elm.Case.branch1 "DataErrorPage____" ( "errorPage", Type.unit ) (\errorPage -> errorPage) - , Elm.Case.otherwise (\_ -> Elm.value { importFrom = [ "ErrorPage" ], name = "notFound", annotation = Nothing }) + , Elm.Case.otherwise (\_ -> Gen.ErrorPage.values_.notFound) ] ] - |> Gen.Tuple.call_.mapBoth (Elm.val "ModelErrorPage____") (Elm.apply (Elm.value { name = "map", importFrom = [ "Effect" ], annotation = Nothing }) [ Elm.val "MsgErrorPage____" ]) + |> Gen.Tuple.call_.mapBoth (Elm.val "ModelErrorPage____") (Elm.apply Gen.Effect.values_.map [ Elm.val "MsgErrorPage____" ]) |> Elm.withType (Type.tuple (Type.named [] "PageModel") (Type.namedWith [ "Effect" ] "Effect" [ Type.named [] "Msg" ])) ) @@ -1701,7 +1613,7 @@ otherFile routes phaseString = ) (Elm.Op.cons routePatterns.reference (Elm.Op.cons apiPatterns.reference - (Elm.apply (Elm.value { name = "routes", importFrom = [ "Api" ], annotation = Nothing }) + (Elm.apply Gen.Api.values_.routes [ getStaticRoutes.reference , Elm.fn2 ( "a", Nothing ) ( "b", Nothing ) (\_ _ -> Elm.string "") ] @@ -1727,7 +1639,7 @@ otherFile routes phaseString = (Gen.Json.Encode.call_.list Gen.Basics.values_.identity (Elm.apply - (Elm.value { name = "routes", importFrom = [ "Api" ], annotation = Nothing }) + Gen.Api.values_.routes [ getStaticRoutes.reference , Elm.fn2 ( "a", Nothing ) ( "b", Nothing ) (\_ _ -> Elm.string "") ] @@ -1853,20 +1765,11 @@ otherFile routes phaseString = ) (\htmlToString -> Elm.Op.cons - (Elm.value - { importFrom = [ "Site" ] - , annotation = Nothing - , name = "config" - } + (Gen.Site.values_.config |> Elm.get "head" ) (Elm.apply - (Elm.value - { importFrom = [ "Api" ] - , annotation = Nothing - , name = "routes" - } - ) + Gen.Api.values_.routes [ getStaticRoutes.reference , htmlToString ] @@ -2296,31 +2199,17 @@ routePatternToExpression route = } -effectNone = - Elm.value { annotation = Nothing, importFrom = [ "Effect" ], name = "none" } - - effectMap : Elm.Expression -> Elm.Expression -> Elm.Expression effectMap mapTo value = Elm.apply - (Elm.value - { name = "map" - , importFrom = [ "Effect" ] - , annotation = Nothing - } - ) + Gen.Effect.values_.map [ mapTo, value ] effectMap_ : Elm.Expression -> Elm.Expression effectMap_ mapTo = Elm.apply - (Elm.value - { name = "map" - , importFrom = [ "Effect" ] - , annotation = Nothing - } - ) + Gen.Effect.values_.map [ mapTo ] @@ -2482,30 +2371,10 @@ ignoreBranchIfNeeded info routes = |> List.filterMap identity -subNone : Elm.Expression -subNone = - Elm.value - { importFrom = [ "Platform", "Sub" ] - , name = "none" - , annotation = Just (subType (Type.var "msg")) - } - - subBatch : List Elm.Expression -> Elm.Expression subBatch batchArg = Elm.apply - (Elm.value - { importFrom = [ "Platform", "Sub" ] - , name = "batch" - , annotation = - Just - (Type.function - [ Type.list (subType (Type.var "msg")) - ] - (subType (Type.var "msg")) - ) - } - ) + Gen.Platform.Sub.values_.batch [ Elm.list batchArg ] @@ -2517,19 +2386,5 @@ subType inner = subMap : Elm.Expression -> Elm.Expression -> Elm.Expression subMap mapArg mapArg0 = Elm.apply - (Elm.value - { importFrom = [ "Platform", "Sub" ] - , name = "map" - , annotation = - Just - (Type.function - [ Type.function - [ Type.var "a" ] - (Type.var "msg") - , subType (Type.var "a") - ] - (subType (Type.var "msg")) - ) - } - ) + Gen.Platform.Sub.values_.map [ mapArg, mapArg0 ] From 6a6ec995699af9da6cfeb874cc2677117239ed5e Mon Sep 17 00:00:00 2001 From: Leonardo Taglialegne Date: Tue, 12 Mar 2024 09:17:50 +0100 Subject: [PATCH 03/14] Further simplification using helpers --- codegen/GenerateMain.elm | 113 +++------------------------------------ 1 file changed, 6 insertions(+), 107 deletions(-) diff --git a/codegen/GenerateMain.elm b/codegen/GenerateMain.elm index 9227ada68..eb78be7c7 100644 --- a/codegen/GenerateMain.elm +++ b/codegen/GenerateMain.elm @@ -30,8 +30,8 @@ import Gen.Pages.Internal.NotFoundReason import Gen.Pages.Internal.Platform import Gen.Pages.Internal.Platform.Cli import Gen.Pages.Internal.RoutePattern -import Gen.Pages.Navigation import Gen.Pages.PageUrl +import Gen.Pages.ProgramConfig import Gen.PagesMsg import Gen.Platform.Sub import Gen.Server.Request @@ -178,7 +178,7 @@ otherFile routes phaseString = , errorPageToData = Elm.val "DataErrorPage____" , notFoundRoute = Elm.nothing } - |> make_ + |> Gen.Pages.ProgramConfig.make_.programConfig |> Elm.withType Type.unit --|> Elm.withType @@ -238,7 +238,7 @@ otherFile routes phaseString = [ pageFormState, concurrentSubmissions, navigation, page, maybePageUrl, globalData, pageData, actionData ] -> Elm.Case.custom (Elm.tuple (page |> Elm.get "route") pageData) Type.unit - ([ Elm.Pattern.tuple Elm.Pattern.ignore (Elm.Pattern.variant1 "DataErrorPage____" (Elm.Pattern.var "data")) + ((Elm.Pattern.tuple Elm.Pattern.ignore (Elm.Pattern.variant1 "DataErrorPage____" (Elm.Pattern.var "data")) |> Elm.Case.patternToBranch (\( (), data ) -> Elm.record @@ -289,8 +289,8 @@ otherFile routes phaseString = , ( "head", Elm.list [] ) ] ) - ] - ++ (routes + ) + :: (routes |> List.map (\route -> Elm.Pattern.tuple (Elm.Pattern.variant1 "Just" (routeToSyntaxPattern route)) @@ -2258,108 +2258,7 @@ routeTemplateFunction functionName route = |> Elm.get functionName -make_ : - { init : Elm.Expression - , update : Elm.Expression - , subscriptions : Elm.Expression - , sharedData : Elm.Expression - , data : Elm.Expression - , action : Elm.Expression - , onActionData : Elm.Expression - , view : Elm.Expression - , handleRoute : Elm.Expression - , getStaticRoutes : Elm.Expression - , urlToRoute : Elm.Expression - , routeToPath : Elm.Expression - , site : Elm.Expression - , toJsPort : Elm.Expression - , fromJsPort : Elm.Expression - , gotBatchSub : Elm.Expression - , hotReloadData : Elm.Expression - , onPageChange : Elm.Expression - , apiRoutes : Elm.Expression - , pathPatterns : Elm.Expression - , basePath : Elm.Expression - , sendPageData : Elm.Expression - , byteEncodePageData : Elm.Expression - , byteDecodePageData : Elm.Expression - , encodeResponse : Elm.Expression - , encodeAction : Elm.Expression - , decodeResponse : Elm.Expression - , globalHeadTags : Elm.Expression - , cmdToEffect : Elm.Expression - , perform : Elm.Expression - , errorStatusCode : Elm.Expression - , notFoundPage : Elm.Expression - , internalError : Elm.Expression - , errorPageToData : Elm.Expression - , notFoundRoute : Elm.Expression - } - -> Elm.Expression -make_ programConfig_args = - Elm.record - [ Tuple.pair "init" programConfig_args.init - , Tuple.pair "update" programConfig_args.update - , Tuple.pair - "subscriptions" - programConfig_args.subscriptions - , Tuple.pair "sharedData" programConfig_args.sharedData - , Tuple.pair "data" programConfig_args.data - , Tuple.pair "action" programConfig_args.action - , Tuple.pair "onActionData" programConfig_args.onActionData - , Tuple.pair "view" programConfig_args.view - , Tuple.pair "handleRoute" programConfig_args.handleRoute - , Tuple.pair - "getStaticRoutes" - programConfig_args.getStaticRoutes - , Tuple.pair "urlToRoute" programConfig_args.urlToRoute - , Tuple.pair "routeToPath" programConfig_args.routeToPath - , Tuple.pair "site" programConfig_args.site - , Tuple.pair "toJsPort" programConfig_args.toJsPort - , Tuple.pair "fromJsPort" programConfig_args.fromJsPort - , Tuple.pair "gotBatchSub" programConfig_args.gotBatchSub - , Tuple.pair - "hotReloadData" - programConfig_args.hotReloadData - , Tuple.pair "onPageChange" programConfig_args.onPageChange - , Tuple.pair "apiRoutes" programConfig_args.apiRoutes - , Tuple.pair "pathPatterns" programConfig_args.pathPatterns - , Tuple.pair "basePath" programConfig_args.basePath - , Tuple.pair "sendPageData" programConfig_args.sendPageData - , Tuple.pair - "byteEncodePageData" - programConfig_args.byteEncodePageData - , Tuple.pair - "byteDecodePageData" - programConfig_args.byteDecodePageData - , Tuple.pair - "encodeResponse" - programConfig_args.encodeResponse - , Tuple.pair "encodeAction" programConfig_args.encodeAction - , Tuple.pair - "decodeResponse" - programConfig_args.decodeResponse - , Tuple.pair - "globalHeadTags" - programConfig_args.globalHeadTags - , Tuple.pair "cmdToEffect" programConfig_args.cmdToEffect - , Tuple.pair "perform" programConfig_args.perform - , Tuple.pair - "errorStatusCode" - programConfig_args.errorStatusCode - , Tuple.pair "notFoundPage" programConfig_args.notFoundPage - , Tuple.pair - "internalError" - programConfig_args.internalError - , Tuple.pair - "errorPageToData" - programConfig_args.errorPageToData - , Tuple.pair - "notFoundRoute" - programConfig_args.notFoundRoute - ] - - +ignoreBranchIfNeeded : { primary : Elm.Case.Branch, otherwise : Elm.Expression } -> List b -> List Elm.Case.Branch ignoreBranchIfNeeded info routes = [ info.primary |> Just , if List.length routes > 1 then From 0c4f5ccdda7cc83ef889d8007a064149282476ef Mon Sep 17 00:00:00 2001 From: Leonardo Taglialegne Date: Tue, 12 Mar 2024 09:18:08 +0100 Subject: [PATCH 04/14] Remove unused var --- codegen/GenerateMain.elm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/codegen/GenerateMain.elm b/codegen/GenerateMain.elm index eb78be7c7..2fe5c9794 100644 --- a/codegen/GenerateMain.elm +++ b/codegen/GenerateMain.elm @@ -1335,7 +1335,7 @@ otherFile routes phaseString = ) |> Just - RoutePattern.StaticParam name -> + RoutePattern.StaticParam _ -> Nothing ) |> List.map (\( key, value ) -> Elm.tuple (Elm.string key) value) From 6633a6221a50f5051b5657cad10bd2fc46403cb9 Mon Sep 17 00:00:00 2001 From: Leonardo Taglialegne Date: Tue, 12 Mar 2024 09:49:44 +0100 Subject: [PATCH 05/14] Extract function in let to avoid massive indentation --- codegen/GenerateMain.elm | 508 ++++++++++++++++++++------------------- 1 file changed, 255 insertions(+), 253 deletions(-) diff --git a/codegen/GenerateMain.elm b/codegen/GenerateMain.elm index 2fe5c9794..aa1003b1b 100644 --- a/codegen/GenerateMain.elm +++ b/codegen/GenerateMain.elm @@ -236,6 +236,124 @@ otherFile routes phaseString = (\args -> case args of [ pageFormState, concurrentSubmissions, navigation, page, maybePageUrl, globalData, pageData, actionData ] -> + let + routeToBranch route = + Elm.Pattern.tuple (Elm.Pattern.variant1 "Just" (routeToSyntaxPattern route)) + (Elm.Pattern.variant1 (prefixedRouteType "Data" route) (Elm.Pattern.var "data")) + |> Elm.Case.patternToBranch + (\( maybeRouteParams, data ) -> + Elm.Let.letIn + (\actionDataOrNothing -> + Elm.record + [ ( "view" + , Elm.fn ( "model", Nothing ) + (\model -> + Elm.Case.custom (model |> Elm.get "page") + Type.unit + [ destructureRouteVariant Model "subModel" route + |> Elm.Case.patternToBranch + (\subModel -> + Elm.apply + (Gen.Shared.values_.template + |> Elm.get "view" + ) + [ globalData + , page + , model |> Elm.get "global" + , Elm.fn ( "myMsg", Nothing ) + (\myMsg -> + Gen.PagesMsg.fromMsg + (Elm.apply (Elm.val "MsgGlobal") [ myMsg ]) + ) + , Elm.apply + Gen.View.values_.map + [ Elm.functionReduced + "innerPageMsg" + (Gen.PagesMsg.call_.map (route |> routeVariantExpression Msg)) + , Elm.apply (route |> routeTemplateFunction "view") + [ model |> Elm.get "global" + , subModel + , Elm.record + [ ( "data", data ) + , ( "sharedData", globalData ) + , ( "routeParams", maybeRouteParams |> Maybe.withDefault (Elm.record []) ) + , ( "action", Gen.Maybe.andThen actionDataOrNothing actionData ) + , ( "path", page |> Elm.get "path" ) + , ( "url", maybePageUrl ) + , ( "submit" + , Elm.functionReduced + "fetcherArg" + (Gen.Pages.Fetcher.call_.submit (decodeRouteType ActionData route)) + ) + , ( "navigation", navigation ) + , ( "concurrentSubmissions" + , concurrentSubmissions + |> Gen.Dict.map + (\_ fetcherState -> + fetcherState + |> Gen.Pages.ConcurrentSubmission.map (\ad -> actionDataOrNothing ad) + ) + ) + , ( "pageFormState", pageFormState ) + ] + ] + ] + ] + ) + , Elm.Pattern.ignore + |> Elm.Case.patternToBranch + (\() -> + modelMismatchView.value + ) + ] + ) + ) + , ( "head" + , case phase of + Browser -> + Elm.list [] + + Cli -> + Elm.apply + (route + |> routeTemplateFunction "head" + ) + [ Elm.record + [ ( "data", data ) + , ( "sharedData", globalData ) + , ( "routeParams", maybeRouteParams |> Maybe.withDefault (Elm.record []) ) + , ( "action", Elm.nothing ) + , ( "path", page |> Elm.get "path" ) + , ( "url", Elm.nothing ) + , ( "submit", Elm.functionReduced "value" (Gen.Pages.Fetcher.call_.submit (decodeRouteType ActionData route)) ) + , ( "navigation", Elm.nothing ) + , ( "concurrentSubmissions", Gen.Dict.empty ) + , ( "pageFormState", Gen.Dict.empty ) + ] + ] + ) + ] + ) + |> Elm.Let.fn "actionDataOrNothing" + ( "thisActionData", Nothing ) + (\thisActionData -> + Elm.Case.custom thisActionData + Type.unit + (ignoreBranchIfNeeded + { primary = + destructureRouteVariant ActionData "justActionData" route + |> Elm.Case.patternToBranch + (\justActionData -> + Elm.just justActionData + ) + , otherwise = Elm.nothing + } + routes + ) + ) + |> Elm.Let.toExpression + ) + in Elm.Case.custom (Elm.tuple (page |> Elm.get "route") pageData) Type.unit ((Elm.Pattern.tuple Elm.Pattern.ignore (Elm.Pattern.variant1 "DataErrorPage____" (Elm.Pattern.var "data")) @@ -291,124 +409,7 @@ otherFile routes phaseString = ) ) :: (routes - |> List.map - (\route -> - Elm.Pattern.tuple (Elm.Pattern.variant1 "Just" (routeToSyntaxPattern route)) - (Elm.Pattern.variant1 (prefixedRouteType "Data" route) (Elm.Pattern.var "data")) - |> Elm.Case.patternToBranch - (\( maybeRouteParams, data ) -> - Elm.Let.letIn - (\actionDataOrNothing -> - Elm.record - [ ( "view" - , Elm.fn ( "model", Nothing ) - (\model -> - Elm.Case.custom (model |> Elm.get "page") - Type.unit - [ destructureRouteVariant Model "subModel" route - |> Elm.Case.patternToBranch - (\subModel -> - Elm.apply - (Gen.Shared.values_.template - |> Elm.get "view" - ) - [ globalData - , page - , model |> Elm.get "global" - , Elm.fn ( "myMsg", Nothing ) - (\myMsg -> - Gen.PagesMsg.fromMsg - (Elm.apply (Elm.val "MsgGlobal") [ myMsg ]) - ) - , Elm.apply - Gen.View.values_.map - [ Elm.functionReduced - "innerPageMsg" - (Gen.PagesMsg.call_.map (route |> routeVariantExpression Msg)) - , Elm.apply (route |> routeTemplateFunction "view") - [ model |> Elm.get "global" - , subModel - , Elm.record - [ ( "data", data ) - , ( "sharedData", globalData ) - , ( "routeParams", maybeRouteParams |> Maybe.withDefault (Elm.record []) ) - , ( "action", Gen.Maybe.andThen actionDataOrNothing actionData ) - , ( "path", page |> Elm.get "path" ) - , ( "url", maybePageUrl ) - , ( "submit" - , Elm.functionReduced - "fetcherArg" - (Gen.Pages.Fetcher.call_.submit (decodeRouteType ActionData route)) - ) - , ( "navigation", navigation ) - , ( "concurrentSubmissions" - , concurrentSubmissions - |> Gen.Dict.map - (\_ fetcherState -> - fetcherState - |> Gen.Pages.ConcurrentSubmission.map (\ad -> actionDataOrNothing ad) - ) - ) - , ( "pageFormState", pageFormState ) - ] - ] - ] - ] - ) - , Elm.Pattern.ignore - |> Elm.Case.patternToBranch - (\() -> - modelMismatchView.value - ) - ] - ) - ) - , ( "head" - , case phase of - Browser -> - Elm.list [] - - Cli -> - Elm.apply - (route - |> routeTemplateFunction "head" - ) - [ Elm.record - [ ( "data", data ) - , ( "sharedData", globalData ) - , ( "routeParams", maybeRouteParams |> Maybe.withDefault (Elm.record []) ) - , ( "action", Elm.nothing ) - , ( "path", page |> Elm.get "path" ) - , ( "url", Elm.nothing ) - , ( "submit", Elm.functionReduced "value" (Gen.Pages.Fetcher.call_.submit (decodeRouteType ActionData route)) ) - , ( "navigation", Elm.nothing ) - , ( "concurrentSubmissions", Gen.Dict.empty ) - , ( "pageFormState", Gen.Dict.empty ) - ] - ] - ) - ] - ) - |> Elm.Let.fn "actionDataOrNothing" - ( "thisActionData", Nothing ) - (\thisActionData -> - Elm.Case.custom thisActionData - Type.unit - (ignoreBranchIfNeeded - { primary = - destructureRouteVariant ActionData "justActionData" route - |> Elm.Case.patternToBranch - (\justActionData -> - Elm.just justActionData - ) - , otherwise = Elm.nothing - } - routes - ) - ) - |> Elm.Let.toExpression - ) - ) + |> List.map routeToBranch ) ++ [ Elm.Case.otherwise (\_ -> @@ -872,6 +873,141 @@ otherFile routes phaseString = (\args -> case args of [ pageFormState, concurrentSubmissions, navigation, sharedData, pageData, navigationKey, msg, model ] -> + let + routeToBranch route = + (route |> destructureRouteVariant Msg "msg_") + |> Elm.Case.patternToBranch + (\msg_ -> + Elm.Case.custom + (Elm.triple + (model |> Elm.get "page") + pageData + (Gen.Maybe.call_.map3 + (toTriple.value []) + (model + |> Elm.get "current" + |> Gen.Maybe.andThen + (Elm.get "metadata") + ) + (model + |> Elm.get "current" + |> Gen.Maybe.andThen + (Elm.get "pageUrl") + ) + (model + |> Elm.get "current" + |> Gen.Maybe.map + (Elm.get "path") + ) + ) + ) + Type.unit + [ Elm.Pattern.triple + (route |> destructureRouteVariant Model "pageModel") + (route |> destructureRouteVariant Data "thisPageData") + (Elm.Pattern.variant1 + "Just" + (Elm.Pattern.triple + (routeToSyntaxPattern route) + (Elm.Pattern.var "pageUrl") + (Elm.Pattern.var "justPage") + ) + ) + |> Elm.Case.patternToBranch + (\( pageModel, thisPageData, ( maybeRouteParams, pageUrl, justPage ) ) -> + Elm.Let.letIn + (\( updatedPageModel, pageCmd, ( newGlobalModel, newGlobalCmd ) ) -> + Elm.tuple + (model + |> Elm.updateRecord + [ ( "page", updatedPageModel ) + , ( "global", newGlobalModel ) + ] + ) + (Elm.apply + Gen.Effect.values_.batch + [ Elm.list + [ pageCmd + , Elm.apply + Gen.Effect.values_.map + [ Elm.val "MsgGlobal" + , newGlobalCmd + ] + ] + ] + ) + ) + |> Elm.Let.destructure + (Elm.Pattern.triple + (Elm.Pattern.var "updatedPageModel") + (Elm.Pattern.var "pageCmd") + (Elm.Pattern.tuple (Elm.Pattern.var "newGlobalModel") + (Elm.Pattern.var "newGlobalCmd") + ) + ) + (fooFn.call + (route |> routeVariantExpression Model) + (route |> routeVariantExpression Msg) + model + (Elm.apply + (Elm.value + { annotation = Nothing + , importFrom = "Route" :: RoutePattern.toModuleName route + , name = "route" + } + |> Elm.get "update" + ) + [ Elm.record + [ ( "data", thisPageData ) + , ( "sharedData", sharedData ) + , ( "action", Elm.nothing ) + , ( "routeParams", maybeRouteParams |> Maybe.withDefault (Elm.record []) ) + , ( "path", justPage |> Elm.get "path" ) + , ( "url", Elm.just pageUrl ) + , ( "submit", Elm.fn ( "options", Nothing ) (Gen.Pages.Fetcher.call_.submit (decodeRouteType ActionData route)) ) + , ( "navigation", navigation ) + , ( "concurrentSubmissions" + , concurrentSubmissions + |> Gen.Dict.map + (\_ fetcherState -> + fetcherState + |> Gen.Pages.ConcurrentSubmission.map + (\ad -> + Elm.Case.custom ad + Type.unit + (ignoreBranchIfNeeded + { primary = + route + |> destructureRouteVariant ActionData "justActionData" + |> Elm.Case.patternToBranch + (\justActionData -> + Elm.just justActionData + ) + , otherwise = Elm.nothing + } + routes + ) + ) + ) + ) + , ( "pageFormState", pageFormState ) + ] + , msg_ + , pageModel + , model |> Elm.get "global" + ] + ) + ) + |> Elm.Let.toExpression + ) + , Elm.Pattern.ignore + |> Elm.Case.patternToBranch + (\() -> + Elm.tuple model Gen.Effect.values_.none + ) + ] + ) + in Elm.Case.custom msg Type.unit ([ Elm.Pattern.variant1 "MsgErrorPage____" (Elm.Pattern.var "msg_") @@ -1014,141 +1150,7 @@ otherFile routes phaseString = ) ] ++ (routes - |> List.map - (\route -> - (route |> destructureRouteVariant Msg "msg_") - |> Elm.Case.patternToBranch - (\msg_ -> - Elm.Case.custom - (Elm.triple - (model |> Elm.get "page") - pageData - (Gen.Maybe.call_.map3 - (toTriple.value []) - (model - |> Elm.get "current" - |> Gen.Maybe.andThen - (Elm.get "metadata") - ) - (model - |> Elm.get "current" - |> Gen.Maybe.andThen - (Elm.get "pageUrl") - ) - (model - |> Elm.get "current" - |> Gen.Maybe.map - (Elm.get "path") - ) - ) - ) - Type.unit - [ Elm.Pattern.triple - (route |> destructureRouteVariant Model "pageModel") - (route |> destructureRouteVariant Data "thisPageData") - (Elm.Pattern.variant1 - "Just" - (Elm.Pattern.triple - (routeToSyntaxPattern route) - (Elm.Pattern.var "pageUrl") - (Elm.Pattern.var "justPage") - ) - ) - |> Elm.Case.patternToBranch - (\( pageModel, thisPageData, ( maybeRouteParams, pageUrl, justPage ) ) -> - Elm.Let.letIn - (\( updatedPageModel, pageCmd, ( newGlobalModel, newGlobalCmd ) ) -> - Elm.tuple - (model - |> Elm.updateRecord - [ ( "page", updatedPageModel ) - , ( "global", newGlobalModel ) - ] - ) - (Elm.apply - Gen.Effect.values_.batch - [ Elm.list - [ pageCmd - , Elm.apply - Gen.Effect.values_.map - [ Elm.val "MsgGlobal" - , newGlobalCmd - ] - ] - ] - ) - ) - |> Elm.Let.destructure - (Elm.Pattern.triple - (Elm.Pattern.var "updatedPageModel") - (Elm.Pattern.var "pageCmd") - (Elm.Pattern.tuple (Elm.Pattern.var "newGlobalModel") - (Elm.Pattern.var "newGlobalCmd") - ) - ) - (fooFn.call - (route |> routeVariantExpression Model) - (route |> routeVariantExpression Msg) - model - (Elm.apply - (Elm.value - { annotation = Nothing - , importFrom = "Route" :: RoutePattern.toModuleName route - , name = "route" - } - |> Elm.get "update" - ) - [ Elm.record - [ ( "data", thisPageData ) - , ( "sharedData", sharedData ) - , ( "action", Elm.nothing ) - , ( "routeParams", maybeRouteParams |> Maybe.withDefault (Elm.record []) ) - , ( "path", justPage |> Elm.get "path" ) - , ( "url", Elm.just pageUrl ) - , ( "submit", Elm.fn ( "options", Nothing ) (Gen.Pages.Fetcher.call_.submit (decodeRouteType ActionData route)) ) - , ( "navigation", navigation ) - , ( "concurrentSubmissions" - , concurrentSubmissions - |> Gen.Dict.map - (\_ fetcherState -> - fetcherState - |> Gen.Pages.ConcurrentSubmission.map - (\ad -> - Elm.Case.custom ad - Type.unit - (ignoreBranchIfNeeded - { primary = - route - |> destructureRouteVariant ActionData "justActionData" - |> Elm.Case.patternToBranch - (\justActionData -> - Elm.just justActionData - ) - , otherwise = Elm.nothing - } - routes - ) - ) - ) - ) - , ( "pageFormState", pageFormState ) - ] - , msg_ - , pageModel - , model |> Elm.get "global" - ] - ) - ) - |> Elm.Let.toExpression - ) - , Elm.Pattern.ignore - |> Elm.Case.patternToBranch - (\() -> - Elm.tuple model Gen.Effect.values_.none - ) - ] - ) - ) + |> List.map routeToBranch ) ) |> Elm.withType From fd29089bc9db59e4dbcb1d763ce4ac14087cda9d Mon Sep 17 00:00:00 2001 From: Leonardo Taglialegne Date: Tue, 12 Mar 2024 09:55:30 +0100 Subject: [PATCH 06/14] Use `call_` instead of `apply` --- codegen/GenerateMain.elm | 132 +++++++++++++++++---------------------- 1 file changed, 57 insertions(+), 75 deletions(-) diff --git a/codegen/GenerateMain.elm b/codegen/GenerateMain.elm index aa1003b1b..571edcb9c 100644 --- a/codegen/GenerateMain.elm +++ b/codegen/GenerateMain.elm @@ -142,10 +142,9 @@ otherFile routes phaseString = Elm.Op.cons pathsToGenerateHandler.reference (Elm.Op.cons routePatterns.reference (Elm.Op.cons apiPatterns.reference - (Elm.apply Gen.Api.values_.routes - [ getStaticRoutes.reference - , htmlToString - ] + (Gen.Api.call_.routes + getStaticRoutes.reference + htmlToString ) ) ) @@ -265,12 +264,12 @@ otherFile routes phaseString = Gen.PagesMsg.fromMsg (Elm.apply (Elm.val "MsgGlobal") [ myMsg ]) ) - , Elm.apply - Gen.View.values_.map - [ Elm.functionReduced + , Gen.View.call_.map + (Elm.functionReduced "innerPageMsg" (Gen.PagesMsg.call_.map (route |> routeVariantExpression Msg)) - , Elm.apply (route |> routeTemplateFunction "view") + ) + (Elm.apply (route |> routeTemplateFunction "view") [ model |> Elm.get "global" , subModel , Elm.record @@ -297,7 +296,7 @@ otherFile routes phaseString = , ( "pageFormState", pageFormState ) ] ] - ] + ) ] ) , Elm.Pattern.ignore @@ -381,19 +380,17 @@ otherFile routes phaseString = Gen.PagesMsg.fromMsg (Elm.apply (Elm.val "MsgGlobal") [ myMsg ]) ) - , Elm.apply - Gen.View.values_.map - [ Elm.functionReduced "myMsg" + , Gen.View.call_.map + (Elm.functionReduced "myMsg" (\myMsg -> Gen.PagesMsg.fromMsg (Elm.apply (Elm.val "MsgErrorPage____") [ myMsg ]) ) - , Elm.apply - Gen.ErrorPage.values_.view - [ data - , subModel - ] - ] + ) + (Gen.ErrorPage.call_.view + data + subModel + ) ] ) , Elm.Pattern.ignore @@ -723,17 +720,14 @@ otherFile routes phaseString = , ( "current", maybePagePath ) ] ) - (Elm.apply - Gen.Effect.values_.batch - [ Elm.list + (Gen.Effect.call_.batch + (Elm.list [ templateCmd - , Elm.apply - Gen.Effect.values_.map - [ Elm.val "MsgGlobal" - , globalCmd - ] + , Gen.Effect.call_.map + (Elm.val "MsgGlobal") + globalCmd ] - ] + ) ) ) |> Elm.Let.tuple "templateModel" @@ -924,17 +918,14 @@ otherFile routes phaseString = , ( "global", newGlobalModel ) ] ) - (Elm.apply - Gen.Effect.values_.batch - [ Elm.list + (Gen.Effect.call_.batch + (Elm.list [ pageCmd - , Elm.apply - Gen.Effect.values_.map - [ Elm.val "MsgGlobal" - , newGlobalCmd - ] + , Gen.Effect.call_.map + (Elm.val "MsgGlobal") + newGlobalCmd ] - ] + ) ) ) |> Elm.Let.destructure @@ -1030,12 +1021,10 @@ otherFile routes phaseString = (Elm.Pattern.variant1 "DataErrorPage____" (Elm.Pattern.var "thisPageData")) |> Elm.Case.patternToBranch (\( pageModel, thisPageData ) -> - Elm.apply - Gen.ErrorPage.values_.update - [ thisPageData - , msg_ - , pageModel - ] + Gen.ErrorPage.call_.update + thisPageData + msg_ + pageModel |> Gen.Tuple.call_.mapBoth (Elm.val "ModelErrorPage____") (effectMap_ (Elm.val "MsgErrorPage____")) ) @@ -1082,12 +1071,12 @@ otherFile routes phaseString = Elm.Let.letIn (\( updatedGlobalModel, globalCmd ) -> Elm.tuple (Elm.updateRecord [ ( "global", updatedGlobalModel ) ] updatedModel) - (Elm.apply Gen.Effect.values_.batch - [ Elm.list + (Gen.Effect.call_.batch + (Elm.list [ cmd , effectMap (Elm.val "MsgGlobal") globalCmd ] - ] + ) ) ) |> Elm.Let.destructure (Elm.Pattern.tuple (Elm.Pattern.var "updatedGlobalModel") (Elm.Pattern.var "globalCmd")) @@ -1200,9 +1189,9 @@ otherFile routes phaseString = (\( a, b, c ) -> Elm.triple (Elm.apply wrapModel [ a ]) - (Elm.apply - Gen.Effect.values_.map - [ wrapMsg, b ] + (Gen.Effect.call_.map + wrapMsg + b ) (Elm.Case.maybe c { nothing = @@ -1257,16 +1246,15 @@ otherFile routes phaseString = Elm.Declare.fn "initErrorPage" ( "pageData", Type.named [] "PageData" |> Just ) (\pageData -> - Elm.apply - Gen.ErrorPage.values_.init - [ Elm.Case.custom pageData + Gen.ErrorPage.call_.init + (Elm.Case.custom pageData Type.unit [ Elm.Case.branch1 "DataErrorPage____" ( "errorPage", Type.unit ) (\errorPage -> errorPage) , Elm.Case.otherwise (\_ -> Gen.ErrorPage.values_.notFound) ] - ] + ) |> Gen.Tuple.call_.mapBoth (Elm.val "ModelErrorPage____") (Elm.apply Gen.Effect.values_.map [ Elm.val "MsgErrorPage____" ]) |> Elm.withType (Type.tuple (Type.named [] "PageModel") (Type.namedWith [ "Effect" ] "Effect" [ Type.named [] "Msg" ])) ) @@ -1615,10 +1603,9 @@ otherFile routes phaseString = ) (Elm.Op.cons routePatterns.reference (Elm.Op.cons apiPatterns.reference - (Elm.apply Gen.Api.values_.routes - [ getStaticRoutes.reference - , Elm.fn2 ( "a", Nothing ) ( "b", Nothing ) (\_ _ -> Elm.string "") - ] + (Gen.Api.call_.routes + getStaticRoutes.reference + (Elm.fn2 ( "a", Nothing ) ( "b", Nothing ) (\_ _ -> Elm.string "")) ) ) |> Gen.List.call_.map Gen.ApiRoute.values_.getBuildTimeRoutes @@ -1640,11 +1627,9 @@ otherFile routes phaseString = (Gen.ApiRoute.succeed (Gen.Json.Encode.call_.list Gen.Basics.values_.identity - (Elm.apply - Gen.Api.values_.routes - [ getStaticRoutes.reference - , Elm.fn2 ( "a", Nothing ) ( "b", Nothing ) (\_ _ -> Elm.string "") - ] + (Gen.Api.call_.routes + getStaticRoutes.reference + (Elm.fn2 ( "a", Nothing ) ( "b", Nothing ) (\_ _ -> Elm.string "")) |> Gen.List.call_.map Gen.ApiRoute.values_.toJson ) |> Gen.Json.Encode.encode 0 @@ -1770,11 +1755,9 @@ otherFile routes phaseString = (Gen.Site.values_.config |> Elm.get "head" ) - (Elm.apply - Gen.Api.values_.routes - [ getStaticRoutes.reference - , htmlToString - ] + (Gen.Api.call_.routes + getStaticRoutes.reference + htmlToString |> Gen.List.call_.filterMap Gen.ApiRoute.values_.getGlobalHeadTagsBackendTask ) |> Gen.BackendTask.call_.combine @@ -2203,9 +2186,9 @@ routePatternToExpression route = effectMap : Elm.Expression -> Elm.Expression -> Elm.Expression effectMap mapTo value = - Elm.apply - Gen.Effect.values_.map - [ mapTo, value ] + Gen.Effect.call_.map + mapTo + value effectMap_ : Elm.Expression -> Elm.Expression @@ -2274,9 +2257,8 @@ ignoreBranchIfNeeded info routes = subBatch : List Elm.Expression -> Elm.Expression subBatch batchArg = - Elm.apply - Gen.Platform.Sub.values_.batch - [ Elm.list batchArg ] + Gen.Platform.Sub.call_.batch + (Elm.list batchArg) subType : Type.Annotation -> Type.Annotation @@ -2286,6 +2268,6 @@ subType inner = subMap : Elm.Expression -> Elm.Expression -> Elm.Expression subMap mapArg mapArg0 = - Elm.apply - Gen.Platform.Sub.values_.map - [ mapArg, mapArg0 ] + Gen.Platform.Sub.call_.map + mapArg + mapArg0 From f2c6bd5a929dab8629d2bf03f5f88463a7f2b7ec Mon Sep 17 00:00:00 2001 From: Leonardo Taglialegne Date: Tue, 12 Mar 2024 10:10:20 +0100 Subject: [PATCH 07/14] Remove redundant uses of `call_` --- codegen/GenerateMain.elm | 108 ++++++++++++--------------------------- 1 file changed, 33 insertions(+), 75 deletions(-) diff --git a/codegen/GenerateMain.elm b/codegen/GenerateMain.elm index 571edcb9c..b3752f494 100644 --- a/codegen/GenerateMain.elm +++ b/codegen/GenerateMain.elm @@ -477,7 +477,7 @@ otherFile routes phaseString = ( "path", pathType |> Just ) ( "model", Type.named [] "Model" |> Just ) (\route path model -> - subBatch + Gen.Platform.Sub.batch [ Elm.apply (Gen.Shared.values_.template |> Elm.get "subscriptions" @@ -486,10 +486,10 @@ otherFile routes phaseString = , model |> Elm.get "global" ] - |> subMap (Elm.val "MsgGlobal") + |> Gen.Platform.Sub.call_.map (Elm.val "MsgGlobal") , templateSubscriptions.call route path model ] - |> Elm.withType (subType (Type.named [] "Msg")) + |> Elm.withType (Gen.Platform.Sub.annotation_.sub (Type.named [] "Msg")) ) onActionData : @@ -568,14 +568,14 @@ otherFile routes phaseString = , templateModel , model |> Elm.get "global" ] - |> subMap (route |> routeVariantExpression Msg) + |> Gen.Platform.Sub.call_.map (route |> routeVariantExpression Msg) ) , Elm.Case.otherwise (\_ -> Gen.Platform.Sub.values_.none) ] ) ) } - |> Elm.withType (subType (Type.named [] "Msg")) + |> Elm.withType (Gen.Platform.Sub.annotation_.sub (Type.named [] "Msg")) ) dataForRoute : @@ -918,14 +918,12 @@ otherFile routes phaseString = , ( "global", newGlobalModel ) ] ) - (Gen.Effect.call_.batch - (Elm.list - [ pageCmd - , Gen.Effect.call_.map - (Elm.val "MsgGlobal") - newGlobalCmd - ] - ) + (Gen.Effect.batch + [ pageCmd + , Gen.Effect.call_.map + (Elm.val "MsgGlobal") + newGlobalCmd + ] ) ) |> Elm.Let.destructure @@ -1021,12 +1019,12 @@ otherFile routes phaseString = (Elm.Pattern.variant1 "DataErrorPage____" (Elm.Pattern.var "thisPageData")) |> Elm.Case.patternToBranch (\( pageModel, thisPageData ) -> - Gen.ErrorPage.call_.update + Gen.ErrorPage.update thisPageData msg_ pageModel |> Gen.Tuple.call_.mapBoth (Elm.val "ModelErrorPage____") - (effectMap_ (Elm.val "MsgErrorPage____")) + (Elm.apply Gen.Effect.values_.map [ Elm.val "MsgErrorPage____" ]) ) , Elm.Pattern.ignore |> Elm.Case.patternToBranch @@ -1044,7 +1042,7 @@ otherFile routes phaseString = (\( sharedModel, globalCmd ) -> Elm.tuple (Elm.updateRecord [ ( "global", sharedModel ) ] model) - (effectMap (Elm.val "MsgGlobal") globalCmd) + (Gen.Effect.call_.map (Elm.val "MsgGlobal") globalCmd) ) |> Elm.Let.destructure (Elm.Pattern.tuple (Elm.Pattern.var "sharedModel") (Elm.Pattern.var "globalCmd")) (Elm.apply @@ -1071,12 +1069,10 @@ otherFile routes phaseString = Elm.Let.letIn (\( updatedGlobalModel, globalCmd ) -> Elm.tuple (Elm.updateRecord [ ( "global", updatedGlobalModel ) ] updatedModel) - (Gen.Effect.call_.batch - (Elm.list - [ cmd - , effectMap (Elm.val "MsgGlobal") globalCmd - ] - ) + (Gen.Effect.batch + [ cmd + , Gen.Effect.call_.map (Elm.val "MsgGlobal") globalCmd + ] ) ) |> Elm.Let.destructure (Elm.Pattern.tuple (Elm.Pattern.var "updatedGlobalModel") (Elm.Pattern.var "globalCmd")) @@ -1189,10 +1185,7 @@ otherFile routes phaseString = (\( a, b, c ) -> Elm.triple (Elm.apply wrapModel [ a ]) - (Gen.Effect.call_.map - wrapMsg - b - ) + (Gen.Effect.call_.map wrapMsg b) (Elm.Case.maybe c { nothing = Elm.tuple @@ -1246,7 +1239,7 @@ otherFile routes phaseString = Elm.Declare.fn "initErrorPage" ( "pageData", Type.named [] "PageData" |> Just ) (\pageData -> - Gen.ErrorPage.call_.init + Gen.ErrorPage.init (Elm.Case.custom pageData Type.unit [ Elm.Case.branch1 "DataErrorPage____" @@ -1603,14 +1596,14 @@ otherFile routes phaseString = ) (Elm.Op.cons routePatterns.reference (Elm.Op.cons apiPatterns.reference - (Gen.Api.call_.routes + (Gen.Api.routes getStaticRoutes.reference - (Elm.fn2 ( "a", Nothing ) ( "b", Nothing ) (\_ _ -> Elm.string "")) + (\_ _ -> Elm.string "") ) ) |> Gen.List.call_.map Gen.ApiRoute.values_.getBuildTimeRoutes |> Gen.BackendTask.call_.combine - |> Gen.BackendTask.call_.map Gen.List.values_.concat + |> Gen.BackendTask.map Gen.List.call_.concat ) ) |> Gen.ApiRoute.literal "all-paths.json" @@ -1627,9 +1620,9 @@ otherFile routes phaseString = (Gen.ApiRoute.succeed (Gen.Json.Encode.call_.list Gen.Basics.values_.identity - (Gen.Api.call_.routes + (Gen.Api.routes getStaticRoutes.reference - (Elm.fn2 ( "a", Nothing ) ( "b", Nothing ) (\_ _ -> Elm.string "")) + (\_ _ -> Elm.string "") |> Gen.List.call_.map Gen.ApiRoute.values_.toJson ) |> Gen.Json.Encode.encode 0 @@ -1651,14 +1644,12 @@ otherFile routes phaseString = routePatterns = topLevelValue "routePatterns" (Gen.ApiRoute.succeed - (Gen.Json.Encode.call_.list - (Elm.fn ( "info", Nothing ) - (\info -> - Gen.Json.Encode.object - [ Elm.tuple (Elm.string "kind") (Gen.Json.Encode.call_.string (info |> Elm.get "kind")) - , Elm.tuple (Elm.string "pathPattern") (Gen.Json.Encode.call_.string (info |> Elm.get "pathPattern")) - ] - ) + (Gen.Json.Encode.list + (\info -> + Gen.Json.Encode.object + [ Elm.tuple (Elm.string "kind") (Gen.Json.Encode.call_.string (info |> Elm.get "kind")) + , Elm.tuple (Elm.string "pathPattern") (Gen.Json.Encode.call_.string (info |> Elm.get "pathPattern")) + ] ) (routes |> List.concatMap @@ -1716,7 +1707,6 @@ otherFile routes phaseString = ) ] ) - |> Elm.list ) |> Gen.Json.Encode.encode 0 |> Gen.BackendTask.succeed @@ -1761,7 +1751,7 @@ otherFile routes phaseString = |> Gen.List.call_.filterMap Gen.ApiRoute.values_.getGlobalHeadTagsBackendTask ) |> Gen.BackendTask.call_.combine - |> Gen.BackendTask.call_.map Gen.List.values_.concat + |> Gen.BackendTask.map Gen.List.call_.concat |> Elm.withType (Gen.BackendTask.annotation_.backendTask (Type.named [ "FatalError" ] "FatalError") @@ -1883,7 +1873,7 @@ otherFile routes phaseString = ) ) ) - |> Gen.BackendTask.call_.map Gen.List.values_.concat + |> Gen.BackendTask.map Gen.List.call_.concat |> Elm.withType (Gen.BackendTask.annotation_.backendTask (Type.named [ "FatalError" ] "FatalError") @@ -2184,20 +2174,6 @@ routePatternToExpression route = } -effectMap : Elm.Expression -> Elm.Expression -> Elm.Expression -effectMap mapTo value = - Gen.Effect.call_.map - mapTo - value - - -effectMap_ : Elm.Expression -> Elm.Expression -effectMap_ mapTo = - Elm.apply - Gen.Effect.values_.map - [ mapTo ] - - append : List Elm.Expression -> Elm.Expression append expressions = case expressions |> List.reverse of @@ -2253,21 +2229,3 @@ ignoreBranchIfNeeded info routes = Nothing ] |> List.filterMap identity - - -subBatch : List Elm.Expression -> Elm.Expression -subBatch batchArg = - Gen.Platform.Sub.call_.batch - (Elm.list batchArg) - - -subType : Type.Annotation -> Type.Annotation -subType inner = - Type.namedWith [ "Platform", "Sub" ] "Sub" [ inner ] - - -subMap : Elm.Expression -> Elm.Expression -> Elm.Expression -subMap mapArg mapArg0 = - Gen.Platform.Sub.call_.map - mapArg - mapArg0 From 6b9d8149e4ac3358ced5f6c47fb1b4e3abd36200 Mon Sep 17 00:00:00 2001 From: Leonardo Taglialegne Date: Thu, 14 Mar 2024 16:26:48 +0100 Subject: [PATCH 08/14] Switch to released elm-codegen --- .gitmodules | 3 --- codegen/elm-codegen | 1 - codegen/elm.json | 4 ++-- examples/routing/package-lock.json | 2 +- package-lock.json | 8 ++++---- package.json | 2 +- 6 files changed, 8 insertions(+), 12 deletions(-) delete mode 160000 codegen/elm-codegen diff --git a/.gitmodules b/.gitmodules index f475dfa80..0936d130f 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,6 +1,3 @@ [submodule "examples/todos/send-grid"] path = examples/todos/send-grid url = https://github.com/MartinSStewart/send-grid -[submodule "codegen/elm-codegen"] - path = codegen/elm-codegen - url = https://github.com/dillonkearns/elm-codegen diff --git a/codegen/elm-codegen b/codegen/elm-codegen deleted file mode 160000 index 5576e9d15..000000000 --- a/codegen/elm-codegen +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 5576e9d159bed8fb689bf368be6465d6fcee7474 diff --git a/codegen/elm.json b/codegen/elm.json index adb8ac269..28f29f0be 100644 --- a/codegen/elm.json +++ b/codegen/elm.json @@ -2,8 +2,7 @@ "type": "application", "source-directories": [ ".", - "../src/", - "elm-codegen/src" + "../src/" ], "elm-version": "0.19.1", "dependencies": { @@ -13,6 +12,7 @@ "elm/html": "1.0.0", "elm/json": "1.1.3", "elm/regex": "1.0.0", + "mdgriffith/elm-codegen": "4.2.0", "rtfeldman/elm-hex": "1.0.0", "stil4m/elm-syntax": "7.2.9", "the-sett/elm-pretty-printer": "3.0.0", diff --git a/examples/routing/package-lock.json b/examples/routing/package-lock.json index c80015981..a136c907c 100644 --- a/examples/routing/package-lock.json +++ b/examples/routing/package-lock.json @@ -56,7 +56,7 @@ "@types/node": "^20.10.7", "@types/serve-static": "^1.15.5", "cypress": "^13.6.2", - "elm-codegen": "0.3.0", + "elm-codegen": "^0.5.2", "elm-optimize-level-2": "^0.3.5", "elm-review": "^2.10.3", "elm-test": "^0.19.1-revision12", diff --git a/package-lock.json b/package-lock.json index 0058d9c00..b76634653 100644 --- a/package-lock.json +++ b/package-lock.json @@ -44,7 +44,7 @@ "@types/node": "^20.10.7", "@types/serve-static": "^1.15.5", "cypress": "^13.6.2", - "elm-codegen": "0.3.0", + "elm-codegen": "^0.5.2", "elm-optimize-level-2": "^0.3.5", "elm-review": "^2.10.3", "elm-test": "^0.19.1-revision12", @@ -2836,9 +2836,9 @@ "integrity": "sha512-WMwm9LhRUo+WUaRN+vRuETqG89IgZphVSNkdFgeb6sS/E4OrDIN7t48CAewSHXc6C8lefD8KKfr5vY61brQlow==" }, "node_modules/elm-codegen": { - "version": "0.3.0", - "resolved": "https://registry.npmjs.org/elm-codegen/-/elm-codegen-0.3.0.tgz", - "integrity": "sha512-IsgEL7HkXA1vuTIoxEKVz04t64u5BAsLYtCwXFvCzo0KHE9xFaM1gOsSxJbO5E92Kp58QFR4C/Km+z4K+RBMMg==", + "version": "0.5.2", + "resolved": "https://registry.npmjs.org/elm-codegen/-/elm-codegen-0.5.2.tgz", + "integrity": "sha512-U9Plxwu85TSdod+1KGJuFufJnoDBlNhbObfRC5UWF+KIEcwAlA0bdUXom60R+ZzXx2TKUXt0Htfe3Dcji6PriQ==", "dev": true, "dependencies": { "chalk": "^4.1.1", diff --git a/package.json b/package.json index 81a2aecbe..fefe7f9ee 100644 --- a/package.json +++ b/package.json @@ -57,7 +57,7 @@ "@types/node": "^20.10.7", "@types/serve-static": "^1.15.5", "cypress": "^13.6.2", - "elm-codegen": "0.3.0", + "elm-codegen": "^0.5.2", "elm-optimize-level-2": "^0.3.5", "elm-review": "^2.10.3", "elm-test": "^0.19.1-revision12", From a0b19f13274d133166060e69732eeb631ff6ccbb Mon Sep 17 00:00:00 2001 From: Leonardo Taglialegne Date: Thu, 14 Mar 2024 16:36:41 +0100 Subject: [PATCH 09/14] Build the generator when testing --- examples/escaping/package-lock.json | 2 +- test.sh | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/examples/escaping/package-lock.json b/examples/escaping/package-lock.json index 2e4b6133b..e5f9e83f0 100644 --- a/examples/escaping/package-lock.json +++ b/examples/escaping/package-lock.json @@ -56,7 +56,7 @@ "@types/node": "^20.10.7", "@types/serve-static": "^1.15.5", "cypress": "^13.6.2", - "elm-codegen": "0.3.0", + "elm-codegen": "^0.5.2", "elm-optimize-level-2": "^0.3.5", "elm-review": "^2.10.3", "elm-test": "^0.19.1-revision12", diff --git a/test.sh b/test.sh index ed7a8d899..2234fbe58 100755 --- a/test.sh +++ b/test.sh @@ -1,5 +1,6 @@ set -ex; yes | lamdera reset || true +npm run build:generator npx elm-test-rs (cd examples/routing && yes | lamdera reset || true && npm i && npm run build && npx elm-test-rs) (cd generator/dead-code-review && npx elm-test-rs) From 7436696033c7a9e00dc3b9a3afc8d30d6c79a7b3 Mon Sep 17 00:00:00 2001 From: Leonardo Taglialegne Date: Thu, 14 Mar 2024 16:48:28 +0100 Subject: [PATCH 10/14] Update to the new API --- codegen/GenerateMain.elm | 443 +++++++++++++++++++-------------------- 1 file changed, 216 insertions(+), 227 deletions(-) diff --git a/codegen/GenerateMain.elm b/codegen/GenerateMain.elm index b3752f494..0169f002d 100644 --- a/codegen/GenerateMain.elm +++ b/codegen/GenerateMain.elm @@ -3,11 +3,11 @@ module GenerateMain exposing (..) import Elm exposing (File) import Elm.Annotation as Type import Elm.Case +import Elm.Case.Branch import Elm.Declare import Elm.Extra exposing (expose, fnIgnore, topLevelValue) import Elm.Let import Elm.Op -import Elm.Pattern import Gen.Api import Gen.ApiRoute import Gen.BackendTask @@ -237,9 +237,10 @@ otherFile routes phaseString = [ pageFormState, concurrentSubmissions, navigation, page, maybePageUrl, globalData, pageData, actionData ] -> let routeToBranch route = - Elm.Pattern.tuple (Elm.Pattern.variant1 "Just" (routeToSyntaxPattern route)) - (Elm.Pattern.variant1 (prefixedRouteType "Data" route) (Elm.Pattern.var "data")) - |> Elm.Case.patternToBranch + Elm.Case.Branch.tuple + (Elm.Case.Branch.variant1 "Just" (routeToSyntaxPattern route) identity) + (Elm.Case.Branch.variant1 (prefixedRouteType "Data" route) (Elm.Case.Branch.var "data") identity) + |> Elm.Case.Branch.map (\( maybeRouteParams, data ) -> Elm.Let.letIn (\actionDataOrNothing -> @@ -250,7 +251,7 @@ otherFile routes phaseString = Elm.Case.custom (model |> Elm.get "page") Type.unit [ destructureRouteVariant Model "subModel" route - |> Elm.Case.patternToBranch + |> Elm.Case.Branch.map (\subModel -> Elm.apply (Gen.Shared.values_.template @@ -299,11 +300,7 @@ otherFile routes phaseString = ) ] ) - , Elm.Pattern.ignore - |> Elm.Case.patternToBranch - (\() -> - modelMismatchView.value - ) + , Elm.Case.Branch.ignore modelMismatchView.value ] ) ) @@ -341,7 +338,7 @@ otherFile routes phaseString = (ignoreBranchIfNeeded { primary = destructureRouteVariant ActionData "justActionData" route - |> Elm.Case.patternToBranch + |> Elm.Case.Branch.map (\justActionData -> Elm.just justActionData ) @@ -355,8 +352,8 @@ otherFile routes phaseString = in Elm.Case.custom (Elm.tuple (page |> Elm.get "route") pageData) Type.unit - ((Elm.Pattern.tuple Elm.Pattern.ignore (Elm.Pattern.variant1 "DataErrorPage____" (Elm.Pattern.var "data")) - |> Elm.Case.patternToBranch + ((Elm.Case.Branch.tuple (Elm.Case.Branch.ignore ()) (Elm.Case.Branch.variant1 "DataErrorPage____" (Elm.Case.Branch.var "data") identity) + |> Elm.Case.Branch.map (\( (), data ) -> Elm.record [ ( "view" @@ -364,40 +361,33 @@ otherFile routes phaseString = (\model -> Elm.Case.custom (model |> Elm.get "page") Type.unit - [ Elm.Pattern.variant1 "ModelErrorPage____" - (Elm.Pattern.var "subModel") - |> Elm.Case.patternToBranch - (\subModel -> - Elm.apply - (Gen.Shared.values_.template - |> Elm.get "view" + [ Elm.Case.Branch.variant1 "ModelErrorPage____" (Elm.Case.Branch.var "subModel") <| + \subModel -> + Elm.apply + (Gen.Shared.values_.template + |> Elm.get "view" + ) + [ globalData + , page + , model |> Elm.get "global" + , Elm.fn ( "myMsg", Nothing ) + (\myMsg -> + Gen.PagesMsg.fromMsg + (Elm.apply (Elm.val "MsgGlobal") [ myMsg ]) ) - [ globalData - , page - , model |> Elm.get "global" - , Elm.fn ( "myMsg", Nothing ) + , Gen.View.call_.map + (Elm.functionReduced "myMsg" (\myMsg -> Gen.PagesMsg.fromMsg - (Elm.apply (Elm.val "MsgGlobal") [ myMsg ]) - ) - , Gen.View.call_.map - (Elm.functionReduced "myMsg" - (\myMsg -> - Gen.PagesMsg.fromMsg - (Elm.apply (Elm.val "MsgErrorPage____") [ myMsg ]) - ) + (Elm.apply (Elm.val "MsgErrorPage____") [ myMsg ]) ) - (Gen.ErrorPage.call_.view - data - subModel - ) - ] - ) - , Elm.Pattern.ignore - |> Elm.Case.patternToBranch - (\() -> - modelMismatchView.value - ) + ) + (Gen.ErrorPage.call_.view + data + subModel + ) + ] + , Elm.Case.Branch.ignore modelMismatchView.value ] ) ) @@ -458,7 +448,7 @@ otherFile routes phaseString = , valueFrom : List String -> Elm.Expression } modelMismatchView = - Elm.Declare.topLevelValue "modelMismatchView" + Elm.Declare.value "modelMismatchView" (Elm.record [ ( "title", Elm.string "Model mismatch" ) , ( "body", [ Gen.Html.text "Model mismatch" ] |> Elm.list ) @@ -509,7 +499,7 @@ otherFile routes phaseString = (\route -> route |> destructureRouteVariant ActionData "thisActionData" - |> Elm.Case.patternToBranch + |> Elm.Case.Branch.map (\thisActionData -> (Elm.value { annotation = Nothing @@ -553,7 +543,7 @@ otherFile routes phaseString = Type.unit [ route |> destructureRouteVariant Model "templateModel" - |> Elm.Case.patternToBranch + |> Elm.Case.Branch.map (\templateModel -> Elm.apply (Elm.value @@ -746,10 +736,10 @@ otherFile routes phaseString = ((routes |> List.map (\route -> - Elm.Pattern.tuple + Elm.Case.Branch.tuple (routeToSyntaxPattern route) (route |> destructureRouteVariant Data "thisPageData") - |> Elm.Case.patternToBranch + |> Elm.Case.Branch.map (\( maybeRouteParams, thisPageData ) -> Elm.apply (Elm.value @@ -773,7 +763,7 @@ otherFile routes phaseString = { primary = route |> destructureRouteVariant ActionData "thisActionData" - |> Elm.Case.patternToBranch + |> Elm.Case.Branch.map (\thisActionData -> Elm.just thisActionData ) @@ -811,7 +801,7 @@ otherFile routes phaseString = ) ) ) - ++ [ Elm.Pattern.ignore |> Elm.Case.patternToBranch (\() -> initErrorPage.call pageData) + ++ [ Elm.Case.Branch.ignore (initErrorPage.call pageData) ] ) ) @@ -870,7 +860,7 @@ otherFile routes phaseString = let routeToBranch route = (route |> destructureRouteVariant Msg "msg_") - |> Elm.Case.patternToBranch + |> Elm.Case.Branch.map (\msg_ -> Elm.Case.custom (Elm.triple @@ -896,44 +886,46 @@ otherFile routes phaseString = ) ) Type.unit - [ Elm.Pattern.triple + [ Elm.Case.Branch.triple (route |> destructureRouteVariant Model "pageModel") (route |> destructureRouteVariant Data "thisPageData") - (Elm.Pattern.variant1 + (Elm.Case.Branch.variant1 "Just" - (Elm.Pattern.triple + (Elm.Case.Branch.triple (routeToSyntaxPattern route) - (Elm.Pattern.var "pageUrl") - (Elm.Pattern.var "justPage") + (Elm.Case.Branch.var "pageUrl") + (Elm.Case.Branch.var "justPage") ) + identity ) - |> Elm.Case.patternToBranch + |> Elm.Case.Branch.map (\( pageModel, thisPageData, ( maybeRouteParams, pageUrl, justPage ) ) -> Elm.Let.letIn - (\( updatedPageModel, pageCmd, ( newGlobalModel, newGlobalCmd ) ) -> - Elm.tuple - (model - |> Elm.updateRecord - [ ( "page", updatedPageModel ) - , ( "global", newGlobalModel ) - ] - ) - (Gen.Effect.batch - [ pageCmd - , Gen.Effect.call_.map - (Elm.val "MsgGlobal") - newGlobalCmd - ] + (\( updatedPageModel, pageCmd, newGLobalModelAndCmd ) -> + Elm.Let.letIn + (\( newGlobalModel, newGlobalCmd ) -> + Elm.tuple + (model + |> Elm.updateRecord + [ ( "page", updatedPageModel ) + , ( "global", newGlobalModel ) + ] + ) + (Gen.Effect.batch + [ pageCmd + , Gen.Effect.call_.map + (Elm.val "MsgGlobal") + newGlobalCmd + ] + ) ) + |> Elm.Let.tuple "newGlobalModel" "newGlobalCmd" newGLobalModelAndCmd + |> Elm.Let.toExpression ) - |> Elm.Let.destructure - (Elm.Pattern.triple - (Elm.Pattern.var "updatedPageModel") - (Elm.Pattern.var "pageCmd") - (Elm.Pattern.tuple (Elm.Pattern.var "newGlobalModel") - (Elm.Pattern.var "newGlobalCmd") - ) - ) + |> Elm.Let.triple + "updatedPageModel" + "pageCmd" + "globalModelAndCmd" (fooFn.call (route |> routeVariantExpression Model) (route |> routeVariantExpression Msg) @@ -968,7 +960,7 @@ otherFile routes phaseString = { primary = route |> destructureRouteVariant ActionData "justActionData" - |> Elm.Case.patternToBranch + |> Elm.Case.Branch.map (\justActionData -> Elm.just justActionData ) @@ -989,150 +981,146 @@ otherFile routes phaseString = ) |> Elm.Let.toExpression ) - , Elm.Pattern.ignore - |> Elm.Case.patternToBranch - (\() -> - Elm.tuple model Gen.Effect.values_.none - ) + , Elm.Case.Branch.ignore + (Elm.tuple model Gen.Effect.values_.none) ] ) in Elm.Case.custom msg Type.unit - ([ Elm.Pattern.variant1 "MsgErrorPage____" (Elm.Pattern.var "msg_") - |> Elm.Case.patternToBranch - (\msg_ -> - Elm.Let.letIn - (\( updatedPageModel, pageCmd ) -> - Elm.tuple - (Elm.updateRecord - [ ( "page", updatedPageModel ) - ] - model - ) - pageCmd - ) - |> Elm.Let.destructure (Elm.Pattern.tuple (Elm.Pattern.var "updatedPageModel") (Elm.Pattern.var "pageCmd")) - (Elm.Case.custom (Elm.tuple (model |> Elm.get "page") pageData) - Type.unit - [ Elm.Pattern.tuple (Elm.Pattern.variant1 "ModelErrorPage____" (Elm.Pattern.var "pageModel")) - (Elm.Pattern.variant1 "DataErrorPage____" (Elm.Pattern.var "thisPageData")) - |> Elm.Case.patternToBranch - (\( pageModel, thisPageData ) -> - Gen.ErrorPage.update - thisPageData - msg_ - pageModel - |> Gen.Tuple.call_.mapBoth (Elm.val "ModelErrorPage____") - (Elm.apply Gen.Effect.values_.map [ Elm.val "MsgErrorPage____" ]) - ) - , Elm.Pattern.ignore - |> Elm.Case.patternToBranch - (\() -> - Elm.tuple (model |> Elm.get "page") Gen.Effect.values_.none - ) + ([ Elm.Case.Branch.variant1 "MsgErrorPage____" (Elm.Case.Branch.var "msg_") <| + \msg_ -> + Elm.Let.letIn + (\( updatedPageModel, pageCmd ) -> + Elm.tuple + (Elm.updateRecord + [ ( "page", updatedPageModel ) ] + model ) - |> Elm.Let.toExpression - ) - , Elm.Pattern.variant1 "MsgGlobal" (Elm.Pattern.var "msg_") - |> Elm.Case.patternToBranch - (\msg_ -> - Elm.Let.letIn - (\( sharedModel, globalCmd ) -> - Elm.tuple - (Elm.updateRecord [ ( "global", sharedModel ) ] model) - (Gen.Effect.call_.map (Elm.val "MsgGlobal") globalCmd) + pageCmd + ) + |> Elm.Let.tuple + "updatedPageModel" + "pageCmd" + (Elm.Case.custom (Elm.tuple (model |> Elm.get "page") pageData) + Type.unit + [ Elm.Case.Branch.tuple + (Elm.Case.Branch.variant1 "ModelErrorPage____" (Elm.Case.Branch.var "pageModel") identity) + (Elm.Case.Branch.variant1 "DataErrorPage____" (Elm.Case.Branch.var "thisPageData") identity) + |> Elm.Case.Branch.map + (\( pageModel, thisPageData ) -> + Gen.ErrorPage.update + thisPageData + msg_ + pageModel + |> Gen.Tuple.call_.mapBoth (Elm.val "ModelErrorPage____") + (Elm.apply Gen.Effect.values_.map [ Elm.val "MsgErrorPage____" ]) + ) + , Elm.Case.Branch.ignore (Elm.tuple (model |> Elm.get "page") Gen.Effect.values_.none) + ] ) - |> Elm.Let.destructure (Elm.Pattern.tuple (Elm.Pattern.var "sharedModel") (Elm.Pattern.var "globalCmd")) - (Elm.apply - (Gen.Shared.values_.template - |> Elm.get "update" - ) - [ msg_, model |> Elm.get "global" ] + |> Elm.Let.toExpression + , Elm.Case.Branch.variant1 "MsgGlobal" (Elm.Case.Branch.var "msg_") <| + \msg_ -> + Elm.Let.letIn + (\( sharedModel, globalCmd ) -> + Elm.tuple + (Elm.updateRecord [ ( "global", sharedModel ) ] model) + (Gen.Effect.call_.map (Elm.val "MsgGlobal") globalCmd) + ) + |> Elm.Let.tuple + "sharedModel" + "globalCmd" + (Elm.apply + (Gen.Shared.values_.template + |> Elm.get "update" ) - |> Elm.Let.toExpression - ) - , Elm.Pattern.variant1 "OnPageChange" (Elm.Pattern.var "record") - |> Elm.Case.patternToBranch - (\record -> - Elm.Let.letIn - (\() ( updatedModel, cmd ) -> - Elm.Case.maybe - (Gen.Shared.values_.template - |> Elm.get "onPageChange" - ) - { nothing = Elm.tuple updatedModel cmd - , just = - ( "thingy" - , \thingy -> - Elm.Let.letIn - (\( updatedGlobalModel, globalCmd ) -> - Elm.tuple (Elm.updateRecord [ ( "global", updatedGlobalModel ) ] updatedModel) - (Gen.Effect.batch - [ cmd - , Gen.Effect.call_.map (Elm.val "MsgGlobal") globalCmd - ] - ) - ) - |> Elm.Let.destructure (Elm.Pattern.tuple (Elm.Pattern.var "updatedGlobalModel") (Elm.Pattern.var "globalCmd")) - (Elm.apply - (Gen.Shared.values_.template - |> Elm.get "update" - ) - [ Elm.apply thingy - [ Elm.record - [ ( "path", record |> Elm.get "path" ) - , ( "query", record |> Elm.get "query" ) - , ( "fragment", record |> Elm.get "fragment" ) - ] - ] - , model |> Elm.get "global" + [ msg_, model |> Elm.get "global" ] + ) + |> Elm.Let.toExpression + , Elm.Case.Branch.variant1 "OnPageChange" (Elm.Case.Branch.var "record") <| + \record -> + Elm.Let.letIn + (\( updatedModel, cmd ) -> + Elm.Case.maybe + (Gen.Shared.values_.template + |> Elm.get "onPageChange" + ) + { nothing = Elm.tuple updatedModel cmd + , just = + ( "thingy" + , \thingy -> + Elm.Let.letIn + (\( updatedGlobalModel, globalCmd ) -> + Elm.tuple (Elm.updateRecord [ ( "global", updatedGlobalModel ) ] updatedModel) + (Gen.Effect.batch + [ cmd + , Gen.Effect.call_.map (Elm.val "MsgGlobal") globalCmd ] ) - |> Elm.Let.toExpression - ) - } - ) - |> Elm.Let.destructure - -- TODO there is a bug where the Browser.Navigation.Key type wasn't imported because the argument wasn't referenced. - -- Remove this hack when that bug is fixed - Elm.Pattern.ignore - navigationKey - |> Elm.Let.destructure (Elm.Pattern.tuple (Elm.Pattern.var "updatedModel") (Elm.Pattern.var "cmd")) - (init.call - (Elm.just (model |> Elm.get "global")) - (Elm.value { importFrom = [ "Pages", "Flags" ], name = "PreRenderFlags", annotation = Nothing }) - sharedData - pageData - Elm.nothing - (Elm.just - (Elm.record - [ ( "path" - , Elm.record - [ ( "path", record |> Elm.get "path" ) - , ( "query", record |> Elm.get "query" ) - , ( "fragment", record |> Elm.get "fragment" ) - ] - ) - , ( "metadata", record |> Elm.get "metadata" ) - , ( "pageUrl" - , Elm.record - [ ( "protocol", record |> Elm.get "protocol" ) - , ( "host", record |> Elm.get "host" ) - , ( "port_", record |> Elm.get "port_" ) - , ( "path", record |> Elm.get "path" ) - , ( "query", record |> Elm.get "query" |> Gen.Maybe.map Gen.Pages.PageUrl.call_.parseQueryParams |> Gen.Maybe.withDefault Gen.Dict.empty ) - , ( "fragment", record |> Elm.get "fragment" ) + ) + |> Elm.Let.tuple + "updatedGlobalModel" + "globalCmd" + (Elm.apply + (Gen.Shared.values_.template + |> Elm.get "update" + ) + [ Elm.apply thingy + [ Elm.record + [ ( "path", record |> Elm.get "path" ) + , ( "query", record |> Elm.get "query" ) + , ( "fragment", record |> Elm.get "fragment" ) + ] + ] + , model |> Elm.get "global" ] - |> Elm.just - ) - ] - ) + ) + |> Elm.Let.toExpression + ) + } + ) + -- |> Elm.Let.destructure + -- -- TODO there is a bug where the Browser.Navigation.Key type wasn't imported because the argument wasn't referenced. + -- -- Remove this hack when that bug is fixed + -- Elm.Case.Branch.ignore + -- navigationKey + |> Elm.Let.tuple + "updatedModel" + "cmd" + (init.call + (Elm.just (model |> Elm.get "global")) + (Elm.value { importFrom = [ "Pages", "Flags" ], name = "PreRenderFlags", annotation = Nothing }) + sharedData + pageData + Elm.nothing + (Elm.just + (Elm.record + [ ( "path" + , Elm.record + [ ( "path", record |> Elm.get "path" ) + , ( "query", record |> Elm.get "query" ) + , ( "fragment", record |> Elm.get "fragment" ) + ] + ) + , ( "metadata", record |> Elm.get "metadata" ) + , ( "pageUrl" + , Elm.record + [ ( "protocol", record |> Elm.get "protocol" ) + , ( "host", record |> Elm.get "host" ) + , ( "port_", record |> Elm.get "port_" ) + , ( "path", record |> Elm.get "path" ) + , ( "query", record |> Elm.get "query" |> Gen.Maybe.map Gen.Pages.PageUrl.call_.parseQueryParams |> Gen.Maybe.withDefault Gen.Dict.empty ) + , ( "fragment", record |> Elm.get "fragment" ) + ] + |> Elm.just + ) + ] ) ) - |> Elm.Let.toExpression - ) + ) + |> Elm.Let.toExpression ] ++ (routes |> List.map routeToBranch @@ -1177,11 +1165,11 @@ otherFile routes phaseString = (\wrapModel wrapMsg model triple -> Elm.Case.custom triple Type.unit - [ Elm.Pattern.triple - (Elm.Pattern.var "a") - (Elm.Pattern.var "b") - (Elm.Pattern.var "c") - |> Elm.Case.patternToBranch + [ Elm.Case.Branch.triple + (Elm.Case.Branch.var "a") + (Elm.Case.Branch.var "b") + (Elm.Case.Branch.var "c") + |> Elm.Case.Branch.map (\( a, b, c ) -> Elm.triple (Elm.apply wrapModel [ a ]) @@ -1379,8 +1367,8 @@ otherFile routes phaseString = Elm.Case.custom nonEmpty Type.unit - [ Elm.Pattern.tuple (Elm.Pattern.var "first") (Elm.Pattern.var "rest") - |> Elm.Case.patternToBranch + [ Elm.Case.Branch.tuple (Elm.Case.Branch.var "first") (Elm.Case.Branch.var "rest") + |> Elm.Case.Branch.map (\( first, rest ) -> append [ Elm.string "( " @@ -1456,7 +1444,7 @@ otherFile routes phaseString = (\route -> route |> destructureRouteVariant ActionData "thisActionData" - |> Elm.Case.patternToBranch + |> Elm.Case.Branch.map (\thisActionData -> Elm.apply (route |> encodeRouteType ActionData) @@ -2066,7 +2054,7 @@ otherFile routes phaseString = ] -routeToSyntaxPattern : RoutePattern -> Elm.Pattern.Pattern (Maybe Elm.Expression) +routeToSyntaxPattern : RoutePattern -> Elm.Case.Branch.Pattern (Maybe Elm.Expression) routeToSyntaxPattern route = let moduleName : String @@ -2074,12 +2062,12 @@ routeToSyntaxPattern route = "Route." ++ (RoutePattern.toModuleName route |> String.join "__") in if RoutePattern.hasRouteParams route then - Elm.Pattern.variant1 moduleName - (Elm.Pattern.var "routeParams" |> Elm.Pattern.map Just) + Elm.Case.Branch.variant1 moduleName + (Elm.Case.Branch.var "routeParams" |> Elm.Case.Branch.map Just) + identity else - Elm.Pattern.variant0 moduleName - |> Elm.Pattern.map (\() -> Nothing) + Elm.Case.Branch.variant0 moduleName Nothing type RouteVariant @@ -2105,15 +2093,16 @@ routeVariantToString variant = "Msg" -destructureRouteVariant : RouteVariant -> String -> RoutePattern -> Elm.Pattern.Pattern Elm.Expression +destructureRouteVariant : RouteVariant -> String -> RoutePattern -> Elm.Case.Branch.Pattern Elm.Expression destructureRouteVariant variant varName route = let moduleName : String moduleName = routeVariantToString variant ++ (RoutePattern.toModuleName route |> String.join "__") in - Elm.Pattern.variant1 moduleName - (Elm.Pattern.var varName) + Elm.Case.Branch.variant1 moduleName + (Elm.Case.Branch.var varName) + identity routeVariantExpression : RouteVariant -> RoutePattern -> Elm.Expression @@ -2223,7 +2212,7 @@ ignoreBranchIfNeeded : { primary : Elm.Case.Branch, otherwise : Elm.Expression } ignoreBranchIfNeeded info routes = [ info.primary |> Just , if List.length routes > 1 then - Elm.Pattern.ignore |> Elm.Case.patternToBranch (\() -> info.otherwise) |> Just + Elm.Case.Branch.ignore info.otherwise |> Just else Nothing From 22b84457bea3d98cce2037e5cf3aff9decefe4c8 Mon Sep 17 00:00:00 2001 From: Leonardo Taglialegne Date: Sun, 24 Mar 2024 11:35:47 +0100 Subject: [PATCH 11/14] Simplify further workarounds --- codegen/GenerateMain.elm | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/codegen/GenerateMain.elm b/codegen/GenerateMain.elm index 0169f002d..2c44e8122 100644 --- a/codegen/GenerateMain.elm +++ b/codegen/GenerateMain.elm @@ -15,6 +15,7 @@ import Gen.Basics import Gen.Bytes import Gen.Bytes.Decode import Gen.Bytes.Encode +import Gen.Debug import Gen.Dict import Gen.Effect import Gen.ErrorPage @@ -88,7 +89,7 @@ otherFile routes phaseString = Cli -> getStaticRoutes.reference - |> Gen.BackendTask.map (Gen.List.call_.map (Elm.val "Just")) + |> Gen.BackendTask.map (Gen.List.call_.map (Elm.functionReduced "x" Gen.Maybe.make_.just)) , urlToRoute = Elm.value { annotation = Nothing @@ -775,7 +776,9 @@ otherFile routes phaseString = ) , ( "routeParams", maybeRouteParams |> Maybe.withDefault (Elm.record []) ) , ( "path" - , Elm.apply (Elm.val ".path") [ justRouteAndPath |> Gen.Tuple.second ] + , justRouteAndPath + |> Gen.Tuple.second + |> Elm.get "path" ) , ( "url" , Gen.Maybe.andThen (Elm.get "pageUrl") maybePagePath @@ -2122,7 +2125,7 @@ applyIdentityTo to = todo : Elm.Expression todo = - Elm.apply (Elm.val "Debug.todo") [ Elm.string "" ] + Gen.Debug.todo "" pathType : Type.Annotation From 1e10b040b19ac7950194bb00f827a285b523fa26 Mon Sep 17 00:00:00 2001 From: Leonardo Taglialegne Date: Sun, 24 Mar 2024 11:42:31 +0100 Subject: [PATCH 12/14] Always use `lamdera` when running tests --- examples/base-path/package-lock.json | 2 +- test.sh | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/examples/base-path/package-lock.json b/examples/base-path/package-lock.json index 26370a194..32b6bbbf5 100644 --- a/examples/base-path/package-lock.json +++ b/examples/base-path/package-lock.json @@ -56,7 +56,7 @@ "@types/node": "^20.10.7", "@types/serve-static": "^1.15.5", "cypress": "^13.6.2", - "elm-codegen": "0.3.0", + "elm-codegen": "^0.5.2", "elm-optimize-level-2": "^0.3.5", "elm-review": "^2.10.3", "elm-test": "^0.19.1-revision12", diff --git a/test.sh b/test.sh index 2234fbe58..bb6e73ee4 100755 --- a/test.sh +++ b/test.sh @@ -1,10 +1,10 @@ set -ex; yes | lamdera reset || true npm run build:generator -npx elm-test-rs -(cd examples/routing && yes | lamdera reset || true && npm i && npm run build && npx elm-test-rs) -(cd generator/dead-code-review && npx elm-test-rs) -(cd generator/review && npx elm-test-rs) +npx elm-test-rs --compiler lamdera +(cd examples/routing && yes | lamdera reset || true && npm i && npm run build && npx elm-test-rs --compiler lamdera) +(cd generator/dead-code-review && npx elm-test-rs --compiler lamdera) +(cd generator/review && npx elm-test-rs --compiler lamdera) npm run test:snapshot npx elmi-to-json --version elm-verify-examples --run-tests --elm-test-args '--compiler=lamdera' From 485253c4d96607db7a69eb8abcbbd2b171da8637 Mon Sep 17 00:00:00 2001 From: Leonardo Taglialegne Date: Sun, 24 Mar 2024 11:43:23 +0100 Subject: [PATCH 13/14] Update elm-codegen for the latest fixes --- codegen/elm.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/codegen/elm.json b/codegen/elm.json index 28f29f0be..5bfb2338a 100644 --- a/codegen/elm.json +++ b/codegen/elm.json @@ -12,7 +12,7 @@ "elm/html": "1.0.0", "elm/json": "1.1.3", "elm/regex": "1.0.0", - "mdgriffith/elm-codegen": "4.2.0", + "mdgriffith/elm-codegen": "4.2.1", "rtfeldman/elm-hex": "1.0.0", "stil4m/elm-syntax": "7.2.9", "the-sett/elm-pretty-printer": "3.0.0", From dde3ad6f4a58936de132521c085d97c817dd07a5 Mon Sep 17 00:00:00 2001 From: Leonardo Taglialegne Date: Sun, 24 Mar 2024 11:43:35 +0100 Subject: [PATCH 14/14] Add `lamdera/*` packages to the example --- examples/routing/elm.json | 2 ++ 1 file changed, 2 insertions(+) diff --git a/examples/routing/elm.json b/examples/routing/elm.json index 3fe1fdc28..8fc65cc17 100644 --- a/examples/routing/elm.json +++ b/examples/routing/elm.json @@ -31,6 +31,8 @@ "elm-community/list-extra": "8.7.0", "jluckyiv/elm-utc-date-strings": "1.0.0", "justinmimbs/date": "4.0.1", + "lamdera/codecs": "1.0.0", + "lamdera/core": "1.0.0", "matheus23/elm-default-tailwind-modules": "4.0.1", "mdgriffith/elm-codegen": "4.1.1", "miniBill/elm-codec": "2.1.0",