Extract out hardcoded names for framework types into helper.

This commit is contained in:
Dillon Kearns 2022-09-09 11:09:25 -07:00
parent 8711b7185a
commit 8e8ab404db
2 changed files with 26 additions and 11 deletions

View File

@ -16,7 +16,7 @@ import Gen.Platform.Sub
import Gen.Server.Request
import Gen.Server.Response
import Gen.View
import Pages.Generate
import Pages.Generate exposing (Type(..))
type alias CliOptions =
@ -109,13 +109,13 @@ createFile moduleName =
Gen.Platform.Sub.none
, types =
{ data =
Elm.alias "Data" (Elm.Annotation.record [])
Alias (Elm.Annotation.record [])
, actionData =
Elm.alias "ActionData" (Elm.Annotation.record [])
Alias (Elm.Annotation.record [])
, model =
Elm.alias "Model" (Elm.Annotation.record [])
Alias (Elm.Annotation.record [])
, msg =
Elm.customType "Msg" [ Elm.variant "NoOp" ]
Custom [ Elm.variant "NoOp" ]
}
}

View File

@ -1,4 +1,4 @@
module Pages.Generate exposing (..)
module Pages.Generate exposing (Type(..), userFunction)
{-| -}
@ -7,6 +7,21 @@ import Elm.Annotation
import Elm.Declare
type Type
= Alias Elm.Annotation.Annotation
| Custom (List Elm.Variant)
typeToDeclaration : String -> Type -> Elm.Declaration
typeToDeclaration name type_ =
case type_ of
Alias annotation ->
Elm.alias name annotation
Custom variants ->
Elm.customType name variants
userFunction :
List String
->
@ -17,7 +32,7 @@ userFunction :
, data : Elm.Expression -> Elm.Expression
, action : Elm.Expression -> Elm.Expression
, head : Elm.Expression -> Elm.Expression
, types : { model : Elm.Declaration, msg : Elm.Declaration, data : Elm.Declaration, actionData : Elm.Declaration }
, types : { model : Type, msg : Type, data : Type, actionData : Type }
}
-> Elm.File
userFunction moduleName definitions =
@ -106,8 +121,8 @@ userFunction moduleName definitions =
)
in
Elm.file ("Route" :: moduleName)
[ definitions.types.model
, definitions.types.msg
[ definitions.types.model |> typeToDeclaration "Model"
, definitions.types.msg |> typeToDeclaration "Msg"
, Elm.alias "RouteParams"
(Elm.Annotation.record
[-- TODO generate params based on input for module name
@ -145,8 +160,8 @@ userFunction moduleName definitions =
, initFn.declaration
, updateFn.declaration
, subscriptionsFn.declaration
, definitions.types.data
, definitions.types.actionData
, definitions.types.data |> typeToDeclaration "Data"
, definitions.types.actionData |> typeToDeclaration "ActionData"
, dataFn.declaration
, actionFn.declaration
, headFn.declaration