Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate BookId instead of (Key Book) for persistent's type aliases #50

Open
k-bx opened this issue May 28, 2019 · 2 comments
Open

Generate BookId instead of (Key Book) for persistent's type aliases #50

k-bx opened this issue May 28, 2019 · 2 comments

Comments

@k-bx
Copy link
Collaborator

k-bx commented May 28, 2019

Currently if you have a type alias type BookId = Key Book that you Capture "book-id" BookId it'll be rendered as (Key Book), and you have to replace it manually, like at https://gitlab.com/k-bx/meetup/blob/master/backend/meetup/src/Meetup/GenerateElm.hs. Having a BookId would be nicer.

@tkuriyama
Copy link

I've run into this issue as well. Looks like the URL referenced above has moved to https://gitlab.com/k-bx/meetup/-/blob/master/backend/meetup/src/Le/GenerateElm.hs

And the behavior from the persistent side is described for example at https://www.yesodweb.com/book/persistent#persistent_closer_look_at_types

@tkuriyama
Copy link

tkuriyama commented Nov 11, 2020

It seems this is already possible by defining something like myAlteration from elm-bridge and then call it from servant-elm's defElmOptions like so:

generateElmModuleWith
    defElmOptions { urlPrefix = Static $ baseURL
                  , elmAlterations = myAlterations }
    [...]
   ... 

... where the alteration looks like:

import           Elm.TyRep as ET
import qualified Elm.Module as Elm


myAlterations :: ET.ETypeDef -> ET.ETypeDef
myAlterations = Elm.recAlterType myTypeAlterations

myTypeAlterations :: ET.EType -> ET.EType
myTypeAlterations t = case t of
  ET.ETyApp (ET.ETyCon (ET.ETCon "Key")) (ET.ETyCon (ET.ETCon "Book")) ->
    ET.ETyCon (ET.ETCon "BookId")
  _ ->
    Elm.defaultTypeAlterations t

But then you still need to specify the type of BookId that matches the database backend, such that elm-bridge can generate the type aliases and serialization functions...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants