First attempt at customising prefixes #776#1044
Conversation
parsonsmatt
left a comment
There was a problem hiding this comment.
Great first pass! We can tidy up the implementation to give a nicer migration path for users currently using mpsPrefixFields = False, though.
Thanks for the PR 😄
| -- liftIO $ fmap _otherCustomFieldName mcp2 @?= Just 5 | ||
|
|
||
| -- insert_ $ CustomPrefixedLeftSum 5 | ||
| -- insert_ $ CustomPrefixedRightSum "Hello" |
There was a problem hiding this comment.
This is a great integration test for the feature! Thanks for writing it up.
| , mpsPrefixFields :: Bool | ||
| -- ^ Prefix field names with the model name. Default: True. | ||
| , mpsFieldLabelModifier :: Text -> Text -> Text | ||
| -- ^ Customise the field accessors and lens names using the entity and field name. Default: appends both |
There was a problem hiding this comment.
I'd prefer for things to just keep working as expected as much as possible. The corner case that you mention is that someone has mpsPrefixFields = False, and they go to use the mpsFieldLabelModifier. Obviously, they'd expect the entity name passed in with mpsFieldLabelModifier function, and not just empty string.
I'm tempted to say that if they have specified mpsPrefixFields = False, then we just pass both things in to the function. The user is free to have a function like:
mpsFieldLabelModifier = \entity field -> "_schema_x_" <> fieldWith this design, we don't know if the user has provided an implementation or is using the default. So perhaps we should set the type here to mpsFieldLabelModifier ::Maybe (Text -> Text -> Text) and a default of Nothing.
Then the logic looks like:
case mpsFieldLabelModifier mps of
Nothing -> do the logic as currently written
Just fn -> provide both entity name and field to the functionThis basically makes mpsPrefixFields redundant, in the case that the user specifies mpsPrefixFields = False, mpsFieldLabelModifier = Just ....
Another thing we'll want to consider: if they have mpsPrefixFields = False, do we want to uppercase the first character of the field name for the user? Whatever choice we make should be documented.
There was a problem hiding this comment.
I've thought about this some more. I make two observations:
- We want the change to be backwards compatible for users using
mpsPrefixFields = Falsewho don't want to use thempsFieldLabelModifier - Other than for this backward compatibility case,
mpsPrefixFieldsis redundant since you can discardentitywhen specifying thempsFieldLabelModifier.
I propose that we deprecate mpsPrefixFields and document that mpsFieldLabelModifier will be ignored if mpsPrefixFields is set to False.
This way, we avoid complicating the type of the fieldLabelModifier.
WDYT?
There was a problem hiding this comment.
That sounds good to me!
There was a problem hiding this comment.
Observation 2 is not completely correct. mpsPrefixFields is also used for disabling the entity prefix in the constraint name, which should be uppercase and not start with an underscore.
One solution is to reuse the modifier function, drop any leading underscore and uppercase the result, but maybe an additional modifier function for the constraints would make more sense?
There was a problem hiding this comment.
I went ahead and implemented a second modifier function (mpsConstraintLabelModifier) for the constraints which need to be upper case. Let me know what you think
parsonsmatt
left a comment
There was a problem hiding this comment.
Looks great to me! Please commit the suggestions for the version numbers and we'll be set to roll.
| , mpsPrefixFields :: Bool | ||
| -- ^ Prefix field names with the model name. Default: True. | ||
| -- | ||
| -- Note: this field is deprecated. Use the mpsFieldLabelModifier and mpsConstraintLabelModifier instead. |
There was a problem hiding this comment.
We can use a DEPRECATED pragma to make this official.
persistent/ChangeLog.md
Outdated
| @@ -1,5 +1,11 @@ | |||
| # Changelog for persistent | |||
|
|
|||
| ## 2.10.5.3 | |||
There was a problem hiding this comment.
| ## 2.10.5.3 | |
| ## 2.11.0.0 |
persistent/persistent.cabal
Outdated
| @@ -1,5 +1,5 @@ | |||
| name: persistent | |||
| version: 2.10.5.2 | |||
| version: 2.10.5.3 | |||
There was a problem hiding this comment.
| version: 2.10.5.3 | |
| version: 2.11.0.0 |
I gave #776 a try, and wanted to get some initial feedback.
This implementation doesn't interact very nicely with setting mpsPrefixFields to False. An empty entity name will then be passed to the function, which is surprising and limiting.
Maybe I could instead make the default implementation smarter to get around this?
Before submitting your PR, check that you've:
@sincedeclarations to the HaddockAfter submitting your PR: