Skip to content

Refactor setEmbedField to use do notation#1242

Merged
parsonsmatt merged 5 commits intoyesodweb:masterfrom
danbroooks:set-embed-do-notation
Apr 27, 2021
Merged

Refactor setEmbedField to use do notation#1242
parsonsmatt merged 5 commits intoyesodweb:masterfrom
danbroooks:set-embed-do-notation

Conversation

@danbroooks
Copy link
Contributor

Before submitting your PR, check that you've:

  • Documented new APIs with Haddock markup
  • Added @since declarations to the Haddock
  • Ran stylish-haskell on any changed files.
  • Adhered to the code style (see the .editorconfig file for details)

After submitting your PR:

  • Update the Changelog.md file with a link to your PR
  • Bumped the version number if there isn't an (unreleased) on the Changelog
  • Check that CI passes (or if it fails, for reasons unrelated to your change, like CI timeouts)

A step towards taming the laddering in the setEmbedField function. The main part being consolidating this portion to using do notation instead:

case stripId $ fieldType field of
Nothing ->
NoReference
Just name ->
case M.lookup (EntityNameHS name) allEntities of
Nothing ->
NoReference
Just _ ->
ForeignRef
(EntityNameHS name)
-- This can get corrected in mkEntityDefSqlTypeExp
(FTTypeCon (Just "Data.Int") "Int64")

I've extracted that out into this top level function here:

lookupEmbedEntity :: EmbedEntityMap -> FieldDef -> Maybe EntityNameHS
lookupEmbedEntity allEntities field = do
    entName <- EntityNameHS <$> stripId (fieldType field)
    guard (M.member entName allEntities) -- check entity name exists in embed map
    pure entName

@danbroooks danbroooks changed the title Set embed do notation Refactor setEmbedField to use do notation Apr 22, 2021
then SelfReference
else case fieldType field of
FTList _ -> SelfReference
_ -> error $ unpack $ unEntityNameHS entName <> ": a self reference must be a Maybe"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah this is great 😄

@parsonsmatt parsonsmatt merged commit cebec38 into yesodweb:master Apr 27, 2021
@danbroooks danbroooks deleted the set-embed-do-notation branch April 27, 2021 16:17
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

Successfully merging this pull request may close these issues.

2 participants