Skip to content

Commit

Permalink
debug union
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasmcdonald3 committed Aug 6, 2024
1 parent 02c87be commit ac52df4
Showing 1 changed file with 16 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import software.amazon.smithy.model.traits.DocumentationTrait;
import software.amazon.smithy.utils.StringUtils;

import static java.lang.String.format;

/**
* Renders unions.
*/
Expand Down Expand Up @@ -56,13 +58,10 @@ protected void writeInitMethodConstraintsChecksForMember(MemberShape member, Str
}

protected void writeInitMethodForMember(MemberShape member, Symbol memberSymbol, Shape targetShape, Symbol targetSymbol) {
writer.openBlock("def __init__(self, value: '$L'):".formatted(
targetShape.isStructureShape() || targetShape.isUnionShape()
? "'$T'"
: "$T"
),
String formatString = format("def __init__(self, value: %s):", getTargetFormat(member));
writer.openBlock(formatString,
"",
targetSymbol.getNamespace() + "." + targetSymbol.getName(),
targetSymbol,
() -> {
writeInitMethodConstraintsChecksForMember(member, memberSymbol.getName());
writer.write("self.value = value");
Expand Down Expand Up @@ -173,6 +172,17 @@ def __repr__(self) -> str:
writeGlobalFromDict();
}

private String getTargetFormat(MemberShape member) {
Shape target = model.expectShape(member.getTarget());
// Recursive shapes may be referenced before they're defined even with
// a topological sort. So forward references need to be used when
// referencing them.
if (recursiveShapes.contains(target)) {
return "'$T'";
}
return "$T";
}

private void writeGlobalFromDict() {
var parentSymbol = symbolProvider.toSymbol(shape);
var fromDictSymbol = parentSymbol.expectProperty("fromDict", Symbol.class);
Expand Down

0 comments on commit ac52df4

Please sign in to comment.