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

fix: value type enums #674

Merged
merged 3 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,6 @@
// TODO: Remove anonymous function in each of the shape visitor and test if it will work
public class DafnyToAwsSdkShapeVisitor extends ShapeVisitor.Default<String> {

private static final List<String> shapeName = Arrays.asList(
"IndexSizeBytes",
"ItemCount",
"ProcessedSizeBytes",
"TableSizeBytes"
);
private final AwsSdkGoPointableIndex awsSdkGoPointableIndex;
private final GenerationContext context;
private final String dataSource;
Expand Down Expand Up @@ -213,7 +207,6 @@ public String structureShape(final StructureShape shape) {
assertionRequired,
writer,
memberShape.isOptional(),
shapeName.contains(memberName) ||
rishav-karanjit marked this conversation as resolved.
Show resolved Hide resolved
awsSdkGoPointableIndex.isPointable(targetShape)
)
)
Expand Down Expand Up @@ -377,86 +370,54 @@ public String booleanShape(final BooleanShape shape) {
public String stringShape(final StringShape shape) {
writer.addImportFromModule(DAFNY_RUNTIME_GO_LIBRARY_MODULE, "dafny");
if (shape.hasTrait(EnumTrait.class)) {
var nilCheck = "";
if (this.isOptional) {
final String unAssertedDataSource = dataSource.startsWith("input.(")
final var unAssertedDataSource = dataSource.startsWith("input.(")
? "input"
: dataSource;
return """
func () %s.%s {
var u %s.%s
//TODO: What to do if nil
if %s == nil {
nilCheck =
"""
if %s == nil {
return u
}
inputEnum := %s.(%s)
index := -1;
for allEnums := dafny.Iterate(%s{}.AllSingletonConstructors()); ; {
enum, ok := allEnums()
if ok {
index++
if enum.(%s).Equals(inputEnum) {
break;
}
}
}
return u.Values()[index]
}()""".formatted(
SmithyNameResolver.smithyTypesNamespaceAws(serviceTrait, true),
context.symbolProvider().toSymbol(shape).getName(),
SmithyNameResolver.smithyTypesNamespaceAws(serviceTrait, true),
context.symbolProvider().toSymbol(shape).getName(),
unAssertedDataSource,
dataSource,
DafnyNameResolver.getDafnyType(
shape,
context.symbolProvider().toSymbol(shape)
),
DafnyNameResolver.getDafnyCompanionStructType(
shape,
context.symbolProvider().toSymbol(shape)
),
DafnyNameResolver.getDafnyType(
shape,
context.symbolProvider().toSymbol(shape)
)
);
} else {
return """
func () %s.%s {
var u %s.%s

inputEnum := %s
index := -1;
for allEnums := dafny.Iterate(%s{}.AllSingletonConstructors()); ; {
enum, ok := allEnums()
if ok {
index++
if enum.(%s).Equals(inputEnum.(%s)) {
break;
}
}
}
return u.Values()[index]
}()""".formatted(
SmithyNameResolver.smithyTypesNamespaceAws(serviceTrait, true),
context.symbolProvider().toSymbol(shape).getName(),
SmithyNameResolver.smithyTypesNamespaceAws(serviceTrait, true),
context.symbolProvider().toSymbol(shape).getName(),
dataSource,
DafnyNameResolver.getDafnyCompanionStructType(
shape,
context.symbolProvider().toSymbol(shape)
),
DafnyNameResolver.getDafnyType(
shape,
context.symbolProvider().toSymbol(shape)
),
DafnyNameResolver.getDafnyType(
shape,
context.symbolProvider().toSymbol(shape)
)
);
""".formatted(unAssertedDataSource);
}
return """
func () %s.%s {
var u %s.%s
%s
inputEnum := %s.(%s)
index := -1;
for allEnums := dafny.Iterate(%s{}.AllSingletonConstructors()); ; {
enum, ok := allEnums()
if ok {
index++
if enum.(%s).Equals(inputEnum) {
break;
}
}
}
return u.Values()[index]
}()""".formatted(
SmithyNameResolver.smithyTypesNamespaceAws(serviceTrait, true),
context.symbolProvider().toSymbol(shape).getName(),
SmithyNameResolver.smithyTypesNamespaceAws(serviceTrait, true),
context.symbolProvider().toSymbol(shape).getName(),
nilCheck,
dataSource,
DafnyNameResolver.getDafnyType(
shape,
context.symbolProvider().toSymbol(shape)
),
DafnyNameResolver.getDafnyCompanionStructType(
shape,
context.symbolProvider().toSymbol(shape)
),
DafnyNameResolver.getDafnyType(
shape,
context.symbolProvider().toSymbol(shape)
)
);
}

final var underlyingType = shape.hasTrait(DafnyUtf8BytesTrait.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -416,45 +416,79 @@ public String booleanShape(final BooleanShape shape) {
public String stringShape(final StringShape shape) {
writer.addImportFromModule(DAFNY_RUNTIME_GO_LIBRARY_MODULE, "dafny");
if (shape.hasTrait(EnumTrait.class)) {
return """
return func () *%s.%s {
var u %s.%s
if %s == nil {
return nil
}
inputEnum := %s.(%s)
index := -1;
for allEnums := dafny.Iterate(%s{}.AllSingletonConstructors()); ; {
enum, ok := allEnums()
if ok {
index++
if enum.(%s).Equals(inputEnum) {
break;
}
}
}
if (this.isOptional) {
return """
return func () *%s.%s {
var u %s.%s
if %s == nil {
return nil
}
inputEnum := %s.(%s)
index := -1;
for allEnums := dafny.Iterate(%s{}.AllSingletonConstructors()); ; {
enum, ok := allEnums()
if ok {
index++
if enum.(%s).Equals(inputEnum) {
break;
}
}
}

return &u.Values()[index]
}()""".formatted(
SmithyNameResolver.smithyTypesNamespace(shape),
context.symbolProvider().toSymbol(shape).getName(),
SmithyNameResolver.smithyTypesNamespace(shape),
context.symbolProvider().toSymbol(shape).getName(),
dataSource,
dataSource,
DafnyNameResolver.getDafnyType(
shape,
context.symbolProvider().toSymbol(shape)
),
DafnyNameResolver.getDafnyCompanionStructType(
shape,
context.symbolProvider().toSymbol(shape)
),
DafnyNameResolver.getDafnyType(
shape,
context.symbolProvider().toSymbol(shape)
)
);
return &u.Values()[index]
}()""".formatted(
SmithyNameResolver.smithyTypesNamespace(shape),
context.symbolProvider().toSymbol(shape).getName(),
SmithyNameResolver.smithyTypesNamespace(shape),
context.symbolProvider().toSymbol(shape).getName(),
dataSource,
dataSource,
DafnyNameResolver.getDafnyType(
shape,
context.symbolProvider().toSymbol(shape)
),
DafnyNameResolver.getDafnyCompanionStructType(
shape,
context.symbolProvider().toSymbol(shape)
),
DafnyNameResolver.getDafnyType(
shape,
context.symbolProvider().toSymbol(shape)
)
);
} else {
return """
return func () %s.%s {
var u %s.%s
inputEnum := %s
index := -1;
for allEnums := dafny.Iterate(%s{}.AllSingletonConstructors()); ; {
enum, ok := allEnums()
if ok {
index++
if enum.(%s).Equals(inputEnum) {
break;
}
}
}

return u.Values()[index]
}()""".formatted(
SmithyNameResolver.smithyTypesNamespace(shape),
context.symbolProvider().toSymbol(shape).getName(),
SmithyNameResolver.smithyTypesNamespace(shape),
context.symbolProvider().toSymbol(shape).getName(),
dataSource,
DafnyNameResolver.getDafnyCompanionStructType(
shape,
context.symbolProvider().toSymbol(shape)
),
DafnyNameResolver.getDafnyType(
shape,
context.symbolProvider().toSymbol(shape)
)
);
}
}

final var underlyingType = shape.hasTrait(DafnyUtf8BytesTrait.class)
Expand Down
Loading