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

Proof-of-concept to allow initialisms to be set through flags. #89

Merged
merged 6 commits into from
Feb 27, 2025

Conversation

cory-miller
Copy link
Contributor

@cory-miller cory-miller commented Feb 20, 2025

Showcasing what I see in #90

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Most relevant part of the change is here and the strcase.go file below.

Copy link
Owner

@cludden cludden left a comment

Choose a reason for hiding this comment

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

Hey @cory-miller, this is great! Thanks for the submission, I like where this is going.

I have a few small changes/nits that I think would ago a long ways in improving the overall UX of this new plugin option:

  1. Rename InitialismsStrCase []string to IgnoreAcronyms []string and the ignore-strcase-initialisms to ignore-acronyms or even just Acronyms/acronyms and sort alphabetically for consistency with the other Config fields
  2. move the actual registration logic (i.e. strcase.ConfigureAcronym() out of toCamel/toLowerCamel and into the plugin's Run method
  3. remove the .idea directory and consider adding an entry to .gitignore
  4. bonus points for updating the docs and changelog

Let me know your thoughts!

Comment on lines 17 to 26

s := fmt.Sprintf(format, args...)

for _, initialism := range svc.cfg.InitialismsStrCase {
if strings.Contains(s, initialism) {
strcase.ConfigureAcronym(s, s)
}
}

return strcase.ToCamel(s)
Copy link
Owner

Choose a reason for hiding this comment

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

instead of calling ConfigureAcronym everytime toCamel and toLowerCamel are called, consolidate and move this logic into the plugin's Run method

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So unfortunately I don't think strcase works in the way you'd expect. Instead of letting you mask particular parts of the input, you have to mask everything. It might be easier to illustrate with an example:

https://go.dev/play/p/8UUJHeOxQSo

Maybe that should be a bug on the strcase part. I'm not sure what the intended behavior is.

Copy link
Owner

Choose a reason for hiding this comment

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

I think there's a miscommunication, I'm suggesting that we keep the same logic you've added to both toCamel and toLowerCamel, but relocate it from those methods into plugin.go's Run method.

strcase.ConfigureAcronym inserts a key/value pair into the module's uppercaseAcronyms global variable of type sync.Map, so inserting the values passed via plugin options once at plugin initialization should be sufficient, as opposed to reinserting the same values every time toCamel or toLowerCamel is called:

https://go.dev/play/p/A6MpqUrsJMK

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The difficulty there is that I do not know what the full strcase.ConfigureAcronym should be ahead of time, unless I understand the implementation details of this tool.

Let me explain:

You have calls to strcase that look like New%sClient where we replace %s with whatever is in my protobuf. If what I want is to be able to mask what's in my protobuf (AWS can be the example for the service and ManageAWS the RPC). Then I have to put as flags:

This would be cumbersome to find all of these. That's why I am creating new entries with strcase.ConfigureAcronym by doing partial string match in the call.

Copy link
Owner

Choose a reason for hiding this comment

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

👍 I see now, thank you for clarifying. Let me noodle on this a bit this week. It might be worth ripping out the third-party dependency and replacing it with something (first or third party) that fits this use-case better. I don't think it would be too difficult.

@cory-miller
Copy link
Contributor Author

@cludden I took care of 1, 3, and 4 from #89 (review) and left a comment in #89 (comment).

Another option I thought of - when calling out to the protogen library and the GoName field is provided back. I'm wondering why these are changed at all. I am not sure but think that for max compatibility you would want to make sure that the protogen output is not altered. Since the input to strcase is New%sClient and similar where the unformatted part comes from the protogen part, perhaps removing strcase is an option. I'm not sure the history on where this came in, or if strcase is used by this tool outside of the protogen parts.

@cory-miller cory-miller requested a review from cludden February 21, 2025 13:30
Copy link
Owner

@cludden cludden left a comment

Choose a reason for hiding this comment

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

@cory-miller huge thank you for adding this! While I noodle on replacement of iancoleman/strcase, I'm going to go ahead and this merged in 👍

@cludden cludden merged commit b70ae5b into cludden:main Feb 27, 2025
1 check failed
cludden added a commit that referenced this pull request Feb 28, 2025
This builds on top of #89 by replacing the existing usage of the
`iancoleman/strcase` third-party library with a local strcase package
that better supports the new ignore-acronyms plugin option and also
changes the flags behavior to use semicolon delimiters for better
ergonomics.

In order to prevent backwards incompatible changes to generated code,
the protobuf options have been expanded to support custom overrides to
various cli command and field names, aliases, and usage.
cludden added a commit that referenced this pull request Feb 28, 2025
This builds on top of #89 by replacing the existing usage of the
`iancoleman/strcase` third-party library with a local strcase package
that better supports the new ignore-acronyms plugin option and also
changes the flags behavior to use semicolon delimiters for better
ergonomics.

In order to prevent backwards incompatible changes to generated code,
the protobuf options have been expanded to support custom overrides to
various cli command and field names, aliases, and usage.
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.

2 participants