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

Provide HasField instances for Entity for OverloadedRecordDot #1381

Merged
merged 9 commits into from
Apr 12, 2022

Conversation

parsonsmatt
Copy link
Collaborator

OK, so this is a fun PR!

We can provide:

instance (SymbolToField sym rec typ, PersistEntity rec) => HasField sym (Entity rec) typ where
  getField =
    view (fieldLens (symbolToField @sym @rec @typ))

which uses similar machinery to this esqueleto PR.

It uses the non-prefixed versions of the fields, which means that we're also polymorphic over what this means.

mkPersist sqlSettings [persistLowerCase|
User
    name Text
    age Int
|]

main = do
    let user = Entity (toSqlKey 1) User { userName = "Matt", userAge = 33 }
    print user.name
    print user.age

This is a polymorphic entity use:

wow :: (PersistEntity rec, SymbolToField "hello" (Entity rec) a, Show a) => Entity rec -> IO ()
wow rec = print rec.hello 

Which does mimic the polymorphism in HasField generally.


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)

@parsonsmatt parsonsmatt added this to the 2.14 milestone Apr 8, 2022
@parsonsmatt parsonsmatt merged commit 6c8afab into master Apr 12, 2022
@hw202207
Copy link
Contributor

Notes:
This implementation had been removed at later version: #1401

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.

None yet

2 participants