Handle required splats in generated route module.

This commit is contained in:
Dillon Kearns 2022-09-13 06:51:21 -07:00
parent 27e58f1027
commit c81649d93b
2 changed files with 67 additions and 19 deletions

View File

@ -188,7 +188,13 @@ routeToBranch route =
nonEmpty ->
nonEmpty |> Elm.CodeGen.record |> Just
in
[ ( Elm.CodeGen.listPattern
[ ( (case ending of
Optional _ ->
Elm.CodeGen.listPattern
_ ->
unconsPattern
)
((route.segments
|> List.map
(\segment ->
@ -200,17 +206,18 @@ routeToBranch route =
Elm.CodeGen.varPattern (decapitalize name)
)
)
++ [ case ending of
++ (case ending of
Optional name ->
Elm.CodeGen.varPattern (decapitalize name)
[ Elm.CodeGen.varPattern (decapitalize name) ]
RequiredSplat ->
-- TODO splatRest
Elm.CodeGen.varPattern "splatFirst"
[ Elm.CodeGen.varPattern "splatFirst"
, Elm.CodeGen.varPattern "splatRest"
]
OptionalSplat ->
Elm.CodeGen.varPattern "splat"
]
[ Elm.CodeGen.varPattern "splat" ]
)
)
, toRecordVariant innerType somethingNew
)
@ -369,7 +376,10 @@ endingToVariantNameFields ending =
( "SPLAT_"
, Just
( "splat"
, Elm.CodeGen.val "( requiredSplat, splat )"
, Elm.CodeGen.tuple
[ Elm.CodeGen.val "splatFirst"
, Elm.CodeGen.val "splatRest"
]
)
)
@ -524,3 +534,19 @@ type Param
| OptionalParam
| RequiredSplatParam
| OptionalSplatParam
unconsPattern : List Elm.CodeGen.Pattern -> Elm.CodeGen.Pattern
unconsPattern list =
case list of
[] ->
Debug.todo ""
listFirst :: listRest ->
List.foldl
(\soFar item ->
soFar
|> Elm.CodeGen.unConsPattern item
)
listFirst
listRest

View File

@ -173,21 +173,43 @@ suite =
, Elm.CodeGen.val "Docs__Section__ { section = Nothing }"
)
]
--, test "splat" <|
-- \() ->
-- [ "Docs", "Section__" ]
-- |> testCaseGenerator
-- ( Elm.CodeGen.listPattern
-- [ Elm.CodeGen.stringPattern "docs"
-- , Elm.CodeGen.varPattern "section"
-- ]
-- , Elm.CodeGen.val "Docs__Section__ { section = section }"
-- )
, test "required splat" <|
\() ->
[ "Username_", "Repo_", "Blob", "SPLAT_" ]
|> testCaseGenerator
[ ( --Elm. """username :: repo :: "blob" :: splatFirst :: splatRest"""
--( Elm.CodeGen.unConsPattern
unconsPattern
[ Elm.CodeGen.varPattern "username"
, Elm.CodeGen.varPattern "repo"
, Elm.CodeGen.stringPattern "blob"
, Elm.CodeGen.varPattern "splatFirst"
, Elm.CodeGen.varPattern "splatRest"
]
, Elm.CodeGen.val
"Username___Repo___Blob__SPLAT_ { username = username, repo = repo, splat = ( splatFirst, splatRest ) }"
)
]
]
]
unconsPattern : List Elm.CodeGen.Pattern -> Elm.CodeGen.Pattern
unconsPattern list =
case list of
[] ->
Debug.todo ""
listFirst :: listRest ->
List.foldl
(\soFar item ->
soFar
|> Elm.CodeGen.unConsPattern item
)
listFirst
listRest
testCaseGenerator : List ( Elm.CodeGen.Pattern, Elm.CodeGen.Expression ) -> List String -> Expectation
testCaseGenerator expected moduleName =
RoutePattern.fromModuleName moduleName