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(go): enum and map in type conversion #782

Open
wants to merge 11 commits into
base: main-1.x
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -239,14 +239,12 @@ public String mapShape(final MapShape shape) {
var nilWrapIfRequired = "nil";
var someWrapIfRequired = "%s";
var returnType = "dafny.Map";
var nilCheck = "";

if (this.isOptional) {
nilWrapIfRequired = "Wrappers.Companion_Option_.Create_None_()";
someWrapIfRequired = "Wrappers.Companion_Option_.Create_Some_(%s)";
returnType = "Wrappers.Option";
}
var nilCheck = "";
if (isPointerType) {
nilCheck =
"if %s == nil {return %s}".formatted(dataSource, nilWrapIfRequired);
}
Expand Down Expand Up @@ -374,10 +372,18 @@ public String stringShape(final StringShape shape) {
shape,
context.symbolProvider().toSymbol(shape)
);

var noEnumMatchedCheck = "";
if (this.isOptional) {
someWrapIfRequired = "Wrappers.Companion_Option_.Create_Some_(%s)";
returnType = "Wrappers.Option";
// In AWS SDK, some shapes don't have required trait and also don't have pointers in it.
// This will result the default value of the string be "" if not provided.
noEnumMatchedCheck =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's be defensive and error out in case of it being required and not matching an enum value.

"""
if index == len(%s.Values()) {
return Wrappers.Companion_Option_.Create_None_()
}
""".formatted(dataSource);
}

return """
Expand All @@ -388,6 +394,7 @@ public String stringShape(final StringShape shape) {
if enumVal == %s{
break;
}
%s
}
var enum interface{}
for allEnums, i := dafny.Iterate(%s{}.AllSingletonConstructors()), 0; i < index; i++ {
Expand All @@ -402,6 +409,7 @@ var enum interface{}
returnType,
dataSource,
dataSource,
noEnumMatchedCheck,
DafnyNameResolver.getDafnyCompanionStructType(
shape,
context.symbolProvider().toSymbol(shape)
Expand Down
Loading