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

automatically infer namespaces #1744

Merged
merged 8 commits into from
Mar 27, 2025
Merged

automatically infer namespaces #1744

merged 8 commits into from
Mar 27, 2025

Conversation

tgummerer
Copy link
Contributor

@tgummerer tgummerer commented Mar 26, 2025

Allow Java components to automatically infer the namespace based on the package name of the component.

@tgummerer tgummerer force-pushed the tg/auto-infer-namespaces branch from f6978a9 to 5df9dd6 Compare March 26, 2025 09:35
@@ -32,7 +32,8 @@ void testGenerateSchemaWithoutMetadata() {
var expected = new PackageSpec()
.setName("infer")
.setDisplayName("infer")
.setVersion(null);
.setVersion(null)
.setNamespace("pulumi");
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Before this PR there would not have been any namespace set, so this will test it.

throw new IllegalArgumentException("At least one component class must be provided");
}

String namespace = classes[0].getPackage().getName().split("\\.")[1];
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure if we should error out if not all classes have the same namespace?

Copy link
Member

Choose a reason for hiding this comment

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

Probably a safer way to go

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done, added an exception.

@tgummerer tgummerer marked this pull request as ready for review March 26, 2025 09:47
@tgummerer tgummerer requested a review from a team as a code owner March 26, 2025 09:47
throw new IllegalArgumentException("At least one component class must be provided");
}

String namespace = classes[0].getPackage().getName().split("\\.")[1];
Copy link
Member

Choose a reason for hiding this comment

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

Probably a safer way to go

throw new IllegalArgumentException("At least one component class must be provided");
}

String namespace = classes[0].getPackage().getName().split("\\.")[1];
Copy link
Member

Choose a reason for hiding this comment

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

Can there be no . at all?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There can be. I think in that case we just leave the namespace empty. I made it so now.

@@ -58,6 +66,10 @@ public String getName() {
return name;
}

public String getNamespace() {
return namespace;
Copy link
Member

Choose a reason for hiding this comment

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

Thank you for cleaning up all whitespaces. We probably need a linter. But - nit: this line doesn't look right?

Copy link
Member

Choose a reason for hiding this comment

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

In another view it's fine but here it's shown as
image

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Whoops, that's tabs vs.spaces, sorry about that, should be fixed now.

if (split.length == 0) {
return "";
}
return split[1];
Copy link
Member

Choose a reason for hiding this comment

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

This will fail for length == 1. Unit tests, please.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

if (ComponentResource.class.isAssignableFrom(clazz) && !clazz.isInterface() && !java.lang.reflect.Modifier.isAbstract(clazz.getModifiers())) {
components.put(clazz.getSimpleName(), analyzer.analyzeComponent(clazz));
}
}

return analyzer.generateSchema(metadata, components, analyzer.typeDefinitions);
// Java package names are not allowed to contain underscores, but namespaces expect dashes instead.
namespace = namespace.replaceAll("_", "-");
Copy link
Member

Choose a reason for hiding this comment

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

Move to getNamespace?

for (Class<?> clazz : classes) {
if (!getNamespace(clazz).equals(namespace)) {
throw new IllegalArgumentException("All classes must be in the same top level package. Expected: " + namespace + " Found: " + getNamespace(clazz));
Copy link
Member

Choose a reason for hiding this comment

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

Test this condition?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

tgummerer and others added 2 commits March 27, 2025 08:47
@@ -0,0 +1,13 @@
package com;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure if there's a better way to add these test classes other than checking these in? At least it's only in src/test so won't end up in the real package, so maybe it's okay?

Copy link
Member

@mikhailshilkov mikhailshilkov left a comment

Choose a reason for hiding this comment

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

LGTM

@tgummerer tgummerer merged commit 0aa6dbe into main Mar 27, 2025
20 checks passed
@tgummerer tgummerer deleted the tg/auto-infer-namespaces branch March 27, 2025 09:41
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.

3 participants