-
Notifications
You must be signed in to change notification settings - Fork 21
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
Conversation
f6978a9
to
5df9dd6
Compare
@@ -32,7 +32,8 @@ void testGenerateSchemaWithoutMetadata() { | |||
var expected = new PackageSpec() | |||
.setName("infer") | |||
.setDisplayName("infer") | |||
.setVersion(null); | |||
.setVersion(null) | |||
.setNamespace("pulumi"); |
There was a problem hiding this comment.
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]; |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, added an exception.
throw new IllegalArgumentException("At least one component class must be provided"); | ||
} | ||
|
||
String namespace = classes[0].getPackage().getName().split("\\.")[1]; |
There was a problem hiding this comment.
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]; |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
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]; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
sdk/java/pulumi/src/main/java/com/pulumi/provider/internal/infer/ComponentAnalyzer.java
Outdated
Show resolved
Hide resolved
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("_", "-"); |
There was a problem hiding this comment.
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)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Test this condition?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
…er/ComponentAnalyzer.java Co-authored-by: Mikhail Shilkov <[email protected]>
@@ -0,0 +1,13 @@ | |||
package com; |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Allow Java components to automatically infer the namespace based on the package name of the component.