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

Add --no-field-prefix option #228

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

ccycle
Copy link

@ccycle ccycle commented Feb 3, 2023

Added option to remove field name prefixes from records generated by compile-proto-file. This change is intended for use with NoFieldSelectors for auto-generated records.

@ccycle ccycle force-pushed the no-field-prefix branch 2 times, most recently from a7810c5 to aebd273 Compare February 8, 2023 05:48
@ccycle ccycle force-pushed the no-field-prefix branch 3 times, most recently from d89c30d to 04e93ac Compare February 20, 2023 09:12
@ccycle
Copy link
Author

ccycle commented Feb 27, 2023

@riz0id Could you review this PR?

@fumieval
Copy link
Contributor

@j6carey @ixmatus @riz0id Does anyone have a chance to take a look at this?

Copy link
Collaborator

@j6carey j6carey left a comment

Choose a reason for hiding this comment

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

Thanks for this feature. Mostly this looks good, though I do have some requests.

src/Proto3/Suite/DotProto/Internal.hs Outdated Show resolved Hide resolved
tests/TestCodeGen.hs Show resolved Hide resolved
Comment on lines 515 to 521
prefixedMethodNameWithFlag :: MonadError CompileError m => IsPrefixed -> String -> String -> m String
prefixedMethodNameWithFlag _ _ "" = invalidTypeNameError "<empty name>"
prefixedMethodNameWithFlag (IsPrefixed flag) serviceName (x : xs)
| flag = prefixedMethodName serviceName (x : xs)
| isLower x = return (fieldLikeName (x : xs))
| otherwise = fieldLikeName <$> typeLikeName (x : xs)

Copy link
Collaborator

Choose a reason for hiding this comment

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

Do you need a keyword check in this function too?

Copy link
Author

Choose a reason for hiding this comment

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

This is needed for the same reason in this comment (#228 (comment)), since the function is used in generating service field name and needed to avoid parse error.

Copy link
Collaborator

Choose a reason for hiding this comment

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

OK, so if it is needed, then could you add it?

Copy link
Author

Choose a reason for hiding this comment

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

Thank you for your comment, I added a commit including changes you requested. Could you please review for that?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Does prefixedMethodNameWithFlag also need a keyword check?

Copy link
Author

Choose a reason for hiding this comment

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

@j6carey Sorry I forgot including the commit, I'll add it.

@riz0id Thank you for the review. I'm not sure I follow your opinion correctly, but I also agree adding hs_name option and removing the part of my changes to avoid colliding with keywords.

I strongly suggest not trying to do any case convention changes and just use the name of service methods and message fields that are provided in the protobuf source. I can't think of any situations where there is a benefit to modifying the lexical content names. Also, theres no function that exists that can convert snake case to camel case without opening up the potential for unintended name shadowing in the code generator.

I have a question about this part. In this PR, should the use of toCamelCase be avoided, or allowed temporally to consider that it will be removed in the future?

Copy link
Collaborator

Choose a reason for hiding this comment

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

In this PR and until we have a hs_name option, we should stick to the naming convention rules that the rest of proto3-suite uses for record selector names which is what you do here with toCamelCase. In the future though, when we have facilities to override names in generated Haskell code, we will probably make breaking changes to instead use snake case for records. Currently the number of edge cases that need to be considered in order to rename protobuf names to camel case Haskell identifiers is getting out of hand. Using snake case record selectors makes a lot more sense (imo) since:

  1. Protobuf suggests snake case for message field names by convention.
  2. Snake case would not require specific rules to avoid collisions with keywords or double underscores (i.e. "__").
  3. Many people (myself included) already use snake case by convention to distinguish record selectors from ordinary functions.

tldr; Yes use toCamelCase for now, but once proto3-suite supports a hs_name option expect breaking changes that use snake case for message field names.

Copy link
Author

Choose a reason for hiding this comment

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

@riz0id OK, thanks. I also think that it is nice to control field selector names with the hs_name option and use snake case record selectors.

@j6carey I added some changes, could you take a look at this?

Copy link
Collaborator

Choose a reason for hiding this comment

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

My concerns have been addressed, but @riz0id , did you want the keyword checks eliminated, or are they OK until hs_name is available?

Copy link
Contributor

Choose a reason for hiding this comment

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

@riz0id Do you have any remaining concerns?

Copy link
Collaborator

@j6carey j6carey left a comment

Choose a reason for hiding this comment

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

My concerns have been addressed, but @riz0id , did you want the keyword checks eliminated, or are they OK until hs_name is available?

@@ -47,5 +47,10 @@ parseArgs = info (helper <*> parser) (fullDesc <> progDesc "Compiles a .proto fi
$ long "largeRecords"
<> help "Use large-records library to optimize the core code size of generated records"

isPrefixed = IsPrefixed . not <$> switch (
long "no-field-prefix"
<> help "Remove prefix for record field names"
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a nitpicking, but perhaps more natural explanation of the behaviour would be "do not prefix record field names by the type names", since it does not remove anything when this is enabled.

Copy link
Author

Choose a reason for hiding this comment

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

Thank you, I fixed this part of the message.

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.

4 participants