Skip to content

Change fieldLens to not require Entity #1332

@pbrisbin

Description

@pbrisbin

Apologies if this has been discussed before, but I'd like to ask about changing the generated fieldLens from using Entity:

fieldLens :: EntityField record field -> Lens' (Entity record) field

To just be in terms of record:

fieldLens :: EntityField record field -> Lens' record field

That latter cannot be recovered through the former without unsafe (and perhaps unlawful) shenanigans:

proposedFieldLens
    :: PersistEntity record => EntityField record field -> Lens' record field
proposedFieldLens field = unsafeEntityLens . fieldLens field

unsafeEntityLens :: Lens' record (Entity record)
unsafeEntityLens = lens (Entity undefined) $ \_ e -> entityVal e

While the former can be recovered from the latter trivially and safely:

recoveredFieldLens
    :: PersistEntity record => EntityField record field -> Lens' (Entity record) field
recoveredFieldLens field = entityValLens . proposedFieldLens field

entityValLens :: Lens' (Entity record) record
entityValLens = lens entityVal $ \x y -> x { entityVal = y }

As a concrete use case, we use Graphula, and its edit function, which takes a record -> record for setting up test data. So fieldLens SomeField .~ x (at the proposed type) is a really nice way to do it. This usage is before we insert the data, so we have only the record and not an Entity record to work on.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions