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

Implemented package mapping for Java/Kotlin code generation #499

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

Conversation

netvl
Copy link

@netvl netvl commented May 17, 2024

See #456 for motivation.

  • Implement package mapping in Java codegen, add tests
  • Implement package mapping in Kotlin codegen, add tests
  • Propagate configuration to the Gradle plugin

Closes #456

@netvl
Copy link
Author

netvl commented May 17, 2024

@bioball - I've started the package mapping implementation, just wanted to show the overall direction before I start implementing it for Kotlin. What do you think?

Copy link
Contributor

@bioball bioball left a comment

Choose a reason for hiding this comment

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

Generally looks on the right track!

* When this option is set, the mapping will be used to translate package names derived from
* module names to the specified package names.
*/
val packageMapping: String? = null
Copy link
Contributor

Choose a reason for hiding this comment

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

This should accept a structured Map<String, String>; trace how externalProperties and environmentVariables get populated from the CLI args parser.

Copy link
Author

Choose a reason for hiding this comment

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

Thanks for the pointer, I hoped that there is something like that!


private fun mapModuleName(name: String): kotlin.Pair<String, String> {
val packageName = mapPackageName(name.substringBeforeLast('.', ""))
val className = name.substringAfterLast('.').replaceFirstChar { it.titlecaseChar() }
Copy link
Contributor

Choose a reason for hiding this comment

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

Should maybe accept the name as-is?

Suggested change
val className = name.substringAfterLast('.').replaceFirstChar { it.titlecaseChar() }
val className = name.substringAfterLast('.')

Copy link
Author

@netvl netvl May 21, 2024

Choose a reason for hiding this comment

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

This change would result in differences from the current behavior:

// current:
module foo.bar.baz -> package foo.bar; class Baz {}

// proposed:
module foo.bar.baz -> package foo.bar; class baz {}

That is, the class name would be lowercase, which is against Java naming conventions. This, in turn, would require users to always use a module name like foo.bar.Baz if they want to get conventional Java names.

Ultimately, I think that even if we decide to do this, this should happen as a separate change, because it is only tangentially related to the original purpose of this PR.

Copy link
Contributor

Choose a reason for hiding this comment

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

Canonically, such Pkl code should also have module foo.bar.Baz if it's meant to be a class.

At the very least, though, if a package mapping is provided, we should probably not alter it.

Copy link
Author

Choose a reason for hiding this comment

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

Canonically, such Pkl code should also have module foo.bar.Baz if it's meant to be a class.

This is the first time I hear about it :( I don't believe it is documented anywhere, and it is definitely not followed by many Pkl modules in the wild.

Different behavior when mapping is specified or not would be very confusing, IMO. Also, the mapping is specifically about packages, not module names.

I would prefer to limit the scope of the change and avoid doing unexpected changes in the existing behavior. But if you insist I can change it here.

Copy link
Contributor

Choose a reason for hiding this comment

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

It's part of our style guide actually; see filename and module name

@netvl netvl marked this pull request as ready for review May 30, 2024 20:23
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.

Allow overriding Java/Kotlin package name in codegen
2 participants