Skip to content

Commit

Permalink
Use @Export.refs in SDK
Browse files Browse the repository at this point in the history
part of #629
  • Loading branch information
pawelprazak committed Aug 29, 2022
1 parent 868c8db commit 934c421
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 27 deletions.
5 changes: 2 additions & 3 deletions sdk/java/pulumi/src/main/java/com/pulumi/core/TypeShape.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ public <U> boolean isAssignableFrom(TypeShape<U> other) {

/**
* Create a string representations of this {@link TypeShape}.
* The reverse operation is {@link #fromString(String)}.
*
* @return a string representation of this {@link TypeShape}
*/
Expand Down Expand Up @@ -178,7 +177,7 @@ private static TypeShape<?> extract(Class<?> type, TypeToken<?> resolverToken) {
/**
* Parse a tree representation of a Java generic type.
*
* @param refs the class references for the generic type
* @param refs the class references for the generic type
* @param treeJson the generic type tree shape as JSON
* @return a {@link TypeShape} described by the tree representation
*/
Expand Down Expand Up @@ -211,7 +210,7 @@ private static TypeShape<?> fromTreeInner(Class<?>[] refs, String treeJson, @Sup
TypeShape.Builder<?> builder = TypeShape.builder(refs[rootIndex]);
for (int i = 1; i < tree.size(); i++) {
var obj = tree.get(i);
if (obj instanceof List){
if (obj instanceof List) {
//noinspection rawtypes
builder.addParameter(fromTreeInner(refs, treeJson, (List) obj));
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
* Requirements:
* <ul>
* <li>annotate a field of type @see {@link com.pulumi.core.Output}</li>
* <li>the type {@code T} of the @see {@link com.pulumi.core.Output} needs to be given explicitly using {@code type} parameter</li>
* <li>if the type {@code T} is also generic, the list of its generic parameter types mus be given using {@code parameters} parameter</li>
* <li>the type {@code T} of the @see {@link com.pulumi.core.Output} needs to be given explicitly using {@link #tree()} and {@link #refs()} parameters</li>
* <li>if the type {@code T} is also generic, the tree shape of its generic parameter types must be given using {@link #tree()} parameter otherwise it can be omitted</li>
* </ul>
*/
@Retention(RetentionPolicy.RUNTIME)
Expand All @@ -29,12 +29,27 @@

/**
* @return the generic type parameter of the annotated @see {@link com.pulumi.core.Output}
* @deprecated use {@link #tree()} and {@link #refs()}
*/
Class<?> type();
@Deprecated
Class<?> type() default Object.class;

/**
* @return the generic type parameters of the @see {@link #type()}
* If not set, the assumption is that the @see {@link #type()} is not a generic type.
* @deprecated use {@link #tree()} and {@link #refs()}
*/
@Deprecated
Class<?>[] parameters() default {};

/**
* @return the generic type parameter tree shape of the annotated @see {@link com.pulumi.core.Output}
*/
String tree() default "";

/**
* @return the generic type parameters of the @see {@link #tree()}
* If not set, the assumption is that this is not a generic type.
*/
Class<?>[] refs() default {};
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,17 +94,31 @@ public static ImmutableMap<String, ExportMetadata<?>> of(Class<?> extractionType
)));

var fieldType = field.getType();
var exportType = export.type();
var parameters = export.parameters();

return of(field, export, fieldType, exportType, parameters);
if (export.refs().length == 0) {
var exportType = export.type();
var parameters = export.parameters();
return of(field, export, fieldType, exportType, parameters);
} else {
var exportTypeNew = TypeShape.fromTree(export.refs(), export.tree());
return of(field, export, fieldType, exportTypeNew);
}
})
.collect(toImmutableMap(
ImportExportMetadata::getName,
Function.identity()
));
}

private static <T> ExportMetadata<T> of(
Field field,
Export exportAnnotation,
Class<?> fieldType,
TypeShape<T> dataType
) {
//noinspection unchecked
return new ExportMetadata<>(field, exportAnnotation, (Class<Output<T>>) fieldType, dataType);
}

private static <T> ExportMetadata<T> of(
Field field,
Export exportAnnotation,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class CustomResource extends Resource {

private CompletableFuture<Output<String>> idFuture; // effectively final, lazy init

@Export(name = Constants.IdPropertyName, type = String.class)
@Export(name = Constants.IdPropertyName, refs = {String.class})
private Output<String> id; // effectively final, lazy init

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public abstract class Resource {

private final CompletableFuture<Output<String>> urnFuture = new CompletableFuture<>();

@Export(name = Constants.UrnPropertyName, type = String.class)
@Export(name = Constants.UrnPropertyName, refs = {String.class})
private final Output<String> urn = Output.of(urnFuture).apply(urn -> urn);

private final String type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@
*/
public class StackReference extends CustomResource {

@Export(name = "name", type = String.class)
@Export(name = "name", refs = {String.class})
private Output<String> name;

@Export(name = "outputs", type = Map.class, parameters = {String.class, Object.class})
@Export(name = "outputs", refs = {Map.class, String.class, Object.class}, tree = "[0,1,2]")
private Output<Map<String, Object>> outputs;

@Export(name = "secretOutputNames", type = List.class, parameters = String.class)
@Export(name = "secretOutputNames", refs = {List.class, String.class}, tree = "[0,1]")
private Output<List<String>> secretOutputNames;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ void testTypeShapeToGSONConversion() {
public static Stream<Arguments> testTypeShapeFromTree() {
return Stream.of(
arguments("", new Class<?>[]{}, "java.lang.Void"),
arguments("[]", new Class<?>[]{}, "java.lang.Void"),
arguments("", new Class<?>[]{Integer.class}, "java.lang.Integer"),
arguments("[0,1]", new Class<?>[]{List.class, Integer.class}, "java.util.List<java.lang.Integer>"),
arguments(
Expand All @@ -41,9 +42,9 @@ public static Stream<Arguments> testTypeShapeFromTree() {
"java.util.List<java.util.List<java.util.List<java.lang.String>>>"
),
arguments(
"[0,[1,2,[3,2]],[1,2,[3,2]]]",
"[0,[1,2,[3,2]],[1,[3,2],2]]",
new Class<?>[]{Either.class, Map.class, String.class, List.class},
"com.pulumi.core.Either<java.util.Map<java.lang.String,java.util.List<java.lang.String>>,java.util.Map<java.lang.String,java.util.List<java.lang.String>>>"
"com.pulumi.core.Either<java.util.Map<java.lang.String,java.util.List<java.lang.String>>,java.util.Map<java.util.List<java.lang.String>,java.lang.String>>"
)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,22 @@ class ImportExportMetadataTest {
@SuppressWarnings("unused")
public static class Tester {

@Export(name = "complex1", type = Either.class, parameters = {Integer.class, String.class})
@Export(name = "complex1", refs = {Either.class, Integer.class, String.class}, tree = "[0,1,2]")
public final Output<Either<Integer, String>> complex1 = Output.of(Either.ofLeft(1));

@Export(name = "complex2", type = Either.class, parameters = {Integer.class, String.class})
@Export(name = "complex2", refs = {Either.class, Integer.class, String.class}, tree = "[0,1,2]")
public final Output<Either<Integer, String>> complex2 = Output.of(Either.ofRight("1"));

@Export(name = "foo", type = String.class)
@Export(name = "foo", refs = String.class)
final Output<String> explicitFoo = Output.of("");

@Export(type = String.class)
@Export(refs = String.class)
private final Output<String> implicitFoo = Output.of("");

@Export(type = Map.class, parameters = {String.class, Integer.class})
@Export(refs = {Map.class, String.class, Integer.class}, tree = "[0,1,2]")
public final Output<Map<String, Integer>> implicitBaz = Output.of(Map.of());

@Export(type = Double.class)
@Export(refs = Double.class)
private Output<Double> incomplete;

@Import(name = "bar")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ class OutputCompletionSourceTest {

@SuppressWarnings("unused")
private static class Tester {
@Export(type = String.class)
@Export(refs = {String.class})
Output<String> foo;

@Export(type = Either.class, parameters = {Integer.class, String.class})
@Export(refs = {Either.class, Integer.class, String.class}, tree = "[0,1,2]")
private Output<Either<Integer, String>> complex1;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ void testStackWithInvalidSchema() {

@ResourceType(type = "aws:ec2/instance:Instance")
public static class Instance extends CustomResource {
@Export(type = String.class)
@Export(refs = String.class)
public Output<String> publicIp;

public Instance(String name, InstanceArgs args, @Nullable CustomResourceOptions options) {
Expand All @@ -222,7 +222,7 @@ public static final class InstanceArgs extends ResourceArgs {
}

public static class MyCustom extends CustomResource {
@Export(type = Instance.class)
@Export(refs = Instance.class)
public Output<Instance> instance;

public MyCustom(String name, MyCustomArgs args, @Nullable CustomResourceOptions options) {
Expand Down

0 comments on commit 934c421

Please sign in to comment.