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

First attempt at customising prefixes #776 #1044

Merged

Conversation

olivierdeckers
Copy link
Contributor

@olivierdeckers olivierdeckers commented Feb 15, 2020

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:

After submitting your PR:

  • Update the Changelog.md file with a link to your PR
  • Check that CI passes (or if it fails, for reasons unrelated to your change, like CI timeouts)

Copy link
Collaborator

@parsonsmatt parsonsmatt left a comment

Choose a reason for hiding this comment

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

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"
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is a great integration test for the feature! Thanks for writing it up.

persistent-test/src/PersistentTestModels.hs Outdated Show resolved Hide resolved
persistent-template/Database/Persist/TH.hs Outdated Show resolved Hide resolved
@@ -437,6 +438,8 @@ data MkPersistSettings = MkPersistSettings
-- False.
, 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
Copy link
Collaborator

Choose a reason for hiding this comment

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

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_" <> field

With 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 function

This 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've thought about this some more. I make two observations:

  1. We want the change to be backwards compatible for users using mpsPrefixFields = False who don't want to use the mpsFieldLabelModifier
  2. Other than for this backward compatibility case, mpsPrefixFields is redundant since you can discard entity when specifying the mpsFieldLabelModifier.

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?

Copy link
Collaborator

Choose a reason for hiding this comment

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

That sounds good to me!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

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

Copy link
Collaborator

@parsonsmatt parsonsmatt left a comment

Choose a reason for hiding this comment

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

Looks great to me! Please commit the suggestions for the version numbers and we'll be set to roll.

@@ -437,6 +439,24 @@ data MkPersistSettings = MkPersistSettings
-- False.
, mpsPrefixFields :: Bool
-- ^ Prefix field names with the model name. Default: True.
--
-- Note: this field is deprecated. Use the mpsFieldLabelModifier and mpsConstraintLabelModifier instead.
Copy link
Collaborator

Choose a reason for hiding this comment

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

We can use a DEPRECATED pragma to make this official.

persistent-template/Database/Persist/TH.hs Outdated Show resolved Hide resolved
persistent-template/Database/Persist/TH.hs Outdated Show resolved Hide resolved
@@ -1,5 +1,11 @@
# Changelog for persistent

## 2.10.5.3
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
## 2.10.5.3
## 2.11.0.0

@@ -1,5 +1,5 @@
name: persistent
version: 2.10.5.2
version: 2.10.5.3
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
version: 2.10.5.3
version: 2.11.0.0

@parsonsmatt parsonsmatt added this to the 2.11 milestone Mar 30, 2020
@parsonsmatt parsonsmatt merged commit b117188 into yesodweb:master Mar 31, 2020
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