digitallyinduced / ihp

🔥 The fastest way to build type safe web apps. IHP is a new batteries-included web framework optimized for longterm productivity and programmer happiness
https://ihp.digitallyinduced.com/
MIT License
4.94k stars 196 forks source link

Refactor Code Generators to be more nice-looking again #1171

Open mpscholten opened 3 years ago

mpscholten commented 3 years ago

If you compare the old PHP-based code generators with the newer Haskell-based one's , the PHP-based code gen's have much more readable code.

One low hanging fruit is to replace the normal haskell strings with https://hackage.haskell.org/package/neat-interpolation-0.5.1.2/docs/NeatInterpolation.html

Basically replacing code like this:

indexAction =
            ""
            <> "    action " <> pluralName <> "Action = do\n"
            <> (if paginationEnabled
                then   "        (" <> modelVariablePlural <> "Q, pagination) <- query @" <> model <> " |> paginate\n"
                    <> "        " <> modelVariablePlural <> " <- " <> modelVariablePlural <> "Q |> fetch\n"
                else "        " <> modelVariablePlural <> " <- query @" <> model <> " |> fetch\n"
            )
            <> "        render IndexView { .. }\n"

With this:

indexAction = [text|
    action #{pluralName}Action = do
        ...
|]
amitaibu commented 3 years ago

Some initial work has been done in https://github.com/digitallyinduced/ihp/pull/1174

bereketgodebo commented 2 years ago

would it be in any way, like this:

indexAction = [text|
                action #{pluralName}Action = do
                    #{(if paginationEnabled } #{then} ( #{modelVariablePlural}Q, pagination) <- query @#{model}  |>  paginate
                                #{modelVariablePlural}  <-  #{modelVariablePlural}Q |> fetch
                    #{else}  #{modelVariablePlural}  <-  query @#{model} |> fetch
                #{)}  render IndexView { .. }

        |]
mpscholten commented 2 years ago

Yes, like that 👍 (The if else syntax would be a bit different, like #{if paginationEnabled then ... else ..})

bereketgodebo commented 2 years ago

thanks, I am on it then 👍

mpscholten commented 2 years ago

Cool! Let us know in case you need any help

bereketgodebo commented 2 years ago

Sure 👍

bereketgodebo commented 2 years ago

I went as far as using the local copy of the development server to check the generated code but i had no luck embedding Haskell code inside. As to the following snippet, I had to import NeatInterpolation and it worked; is there any doc that's available other than the one provided above which i could read to better understand what to do?

newAction = [text| 
             action New${singularName}Action = do 
                 let $modelVariablePlural = newRecord
                 render NewView { .. }
        |]