Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion codegen/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@

allprojects {
group = "software.amazon.smithy.python"
version = "0.3.1"
version = "0.3.2"
}
Original file line number Diff line number Diff line change
Expand Up @@ -351,8 +351,16 @@ public Symbol intEnumShape(IntEnumShape shape) {
}

private Symbol genericEnum(Shape shape) {
Symbol symbol = createGeneratedSymbolBuilder(shape, getDefaultShapeName(shape), SHAPES_FILE).build();
return escaper.escapeSymbol(shape, symbol);
var enumSymbol = createGeneratedSymbolBuilder(shape, getDefaultShapeName(shape), SHAPES_FILE).build();

// We add this enum symbol as a property on a generic string/int symbol
// rather than returning the enum symbol directly because we only
// generate the enum constants for convenience. We actually want
// to pass around plain types rather than what is effectively
// a namespace class.
return createSymbolBuilder(shape, shape.isEnumShape() ? "str" : "int")
.putProperty(SymbolProperties.ENUM_SYMBOL, escaper.escapeSymbol(shape, enumSymbol))
.build();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ public final class SymbolProperties {
*/
public static final Property<Boolean> STDLIB = Property.named("stdlib");

/**
* Contains a symbol representing the class containing known enum values for the symbol.
*
* <p>In type signatures, the base int or str is used instead for forwards compatibility.
*/
public static final Property<Symbol> ENUM_SYMBOL = Property.named("enumSymbol");

/**
* Contains a symbol representing the unknown variant of a union.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import software.amazon.smithy.python.codegen.GenerationContext;
import software.amazon.smithy.python.codegen.RuntimeTypes;
import software.amazon.smithy.python.codegen.SmithyPythonDependency;
import software.amazon.smithy.python.codegen.SymbolProperties;
import software.amazon.smithy.utils.SmithyInternalApi;

/**
Expand All @@ -36,7 +37,7 @@ public EnumGenerator(GenerationContext context, EnumShape enumShape) {

@Override
public void run() {
var enumSymbol = context.symbolProvider().toSymbol(shape);
var enumSymbol = context.symbolProvider().toSymbol(shape).expectProperty(SymbolProperties.ENUM_SYMBOL);
context.writerDelegator().useShapeWriter(shape, writer -> {
writer.addStdlibImport("enum", "StrEnum");
writer.addDependency(SmithyPythonDependency.SMITHY_CORE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import software.amazon.smithy.python.codegen.PythonSettings;
import software.amazon.smithy.python.codegen.RuntimeTypes;
import software.amazon.smithy.python.codegen.SmithyPythonDependency;
import software.amazon.smithy.python.codegen.SymbolProperties;
import software.amazon.smithy.utils.SmithyInternalApi;

/**
Expand All @@ -36,7 +37,7 @@ public IntEnumGenerator(GenerateIntEnumDirective<GenerationContext, PythonSettin

@Override
public void run() {
var enumSymbol = directive.symbol();
var enumSymbol = directive.symbol().expectProperty(SymbolProperties.ENUM_SYMBOL);
directive.context().writerDelegator().useShapeWriter(directive.shape(), writer -> {
writer.addStdlibImport("enum", "IntEnum");
writer.addDependency(SmithyPythonDependency.SMITHY_CORE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,9 @@ public Void integerShape(IntegerShape shape) {
@Override
public Void intEnumShape(IntEnumShape shape) {
pushMemberState();
var enumSymbol = context.symbolProvider().toSymbol(shape);
var enumSymbol = context.symbolProvider()
.toSymbol(shape)
.expectProperty(SymbolProperties.ENUM_SYMBOL);
writer.write("$T(${deserializer:L}.read_integer(${C|}))",
enumSymbol,
writer.consumer(w -> writeSchema()));
Expand Down Expand Up @@ -188,7 +190,9 @@ public Void stringShape(StringShape shape) {
@Override
public Void enumShape(EnumShape shape) {
pushMemberState();
var enumSymbol = context.symbolProvider().toSymbol(shape);
var enumSymbol = context.symbolProvider()
.toSymbol(shape)
.expectProperty(SymbolProperties.ENUM_SYMBOL);
writer.write("$T(${deserializer:L}.read_string(${C|}))",
enumSymbol,
writer.consumer(w -> writeSchema()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,17 @@ public Consumer<PythonWriter> mapShape(MapShape shape) {

@Override
public Consumer<PythonWriter> enumShape(EnumShape shape) {
var enumSymbol = context.symbolProvider().toSymbol(shape);
var enumSymbol = context.symbolProvider()
.toSymbol(shape)
.expectProperty(SymbolProperties.ENUM_SYMBOL);
return writer -> writer.writeInline("$T._corrected(\"\")", enumSymbol);
}

@Override
public Consumer<PythonWriter> intEnumShape(IntEnumShape shape) {
var enumSymbol = context.symbolProvider().toSymbol(shape);
var enumSymbol = context.symbolProvider()
.toSymbol(shape)
.expectProperty(SymbolProperties.ENUM_SYMBOL);
// -1 is used (rather than 0) as the placeholder because it is least likely to
// collide with a real member value.
return writer -> writer.writeInline("$T._corrected(-1)", enumSymbol);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,12 +295,14 @@ private String getDefaultValue(PythonWriter writer, MemberShape member) {
return String.format("b'%s'", defaultNode.expectStringNode().getValue());
} else if (target.isEnumShape()) {
// Wrap rather than emit a bare string so the value matches the field type.
var enumSymbol = symbolProvider.toSymbol(target);
var enumSymbol = symbolProvider.toSymbol(target)
.expectProperty(SymbolProperties.ENUM_SYMBOL);
return String.format("%s(\"%s\")",
writer.format("$T", enumSymbol),
defaultNode.expectStringNode().getValue());
} else if (target.isIntEnumShape()) {
var enumSymbol = symbolProvider.toSymbol(target);
var enumSymbol = symbolProvider.toSymbol(target)
.expectProperty(SymbolProperties.ENUM_SYMBOL);
return String.format("%s(%s)",
writer.format("$T", enumSymbol),
defaultNode.expectNumberNode().getValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@
package software.amazon.smithy.python.codegen.writer;

import software.amazon.smithy.build.FileManifest;
import software.amazon.smithy.codegen.core.Symbol;
import software.amazon.smithy.codegen.core.SymbolProvider;
import software.amazon.smithy.codegen.core.WriterDelegator;
import software.amazon.smithy.model.shapes.MemberShape;
import software.amazon.smithy.model.shapes.Shape;
import software.amazon.smithy.python.codegen.PythonSettings;
import software.amazon.smithy.python.codegen.SymbolProperties;
import software.amazon.smithy.utils.SmithyInternalApi;

/**
Expand All @@ -23,7 +27,30 @@ public PythonDelegator(
) {
super(
fileManifest,
symbolProvider,
new EnumSymbolProviderWrapper(symbolProvider),
new PythonWriter.PythonWriterFactory(settings));
}

private static final class EnumSymbolProviderWrapper implements SymbolProvider {

private final SymbolProvider wrapped;

EnumSymbolProviderWrapper(SymbolProvider wrapped) {
this.wrapped = wrapped;
}

@Override
public Symbol toSymbol(Shape shape) {
Symbol symbol = wrapped.toSymbol(shape);
if (shape.isEnumShape() || shape.isIntEnumShape()) {
symbol = symbol.expectProperty(SymbolProperties.ENUM_SYMBOL);
}
return symbol;
}

@Override
public String toMemberName(MemberShape shape) {
return wrapped.toMemberName(shape);
}
}
}
Loading