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.
Apologies if this has been discussed before, but I'd like to ask about changing the generated
fieldLensfrom usingEntity:To just be in terms of
record: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 eWhile 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
editfunction, which takes arecord -> recordfor setting up test data. SofieldLens 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 therecordand not anEntity recordto work on.