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: handle error message field in structure deserializer #2889

Merged
merged 5 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -39,6 +39,7 @@
import software.amazon.smithy.model.shapes.Shape;
import software.amazon.smithy.model.shapes.StructureShape;
import software.amazon.smithy.model.shapes.UnionShape;
import software.amazon.smithy.model.traits.ErrorTrait;
import software.amazon.smithy.model.traits.JsonNameTrait;
import software.amazon.smithy.model.traits.TimestampFormatTrait;
import software.amazon.smithy.model.traits.TimestampFormatTrait.Format;
Expand Down Expand Up @@ -180,8 +181,21 @@ protected void deserializeStructure(GenerationContext context, StructureShape sh
});
});

boolean isErrorStructure = shape.hasTrait(ErrorTrait.class);
if (isErrorStructure) {
writer.write("var errorMessage string");
}

// Iterate through the decoder. The member visitor will handle popping tokens.
writer.openBlock("for key, value := range shape {", "}", () -> {
if (isErrorStructure) {
writer.write("keyLower := strings.ToLower(key)");
RanVaknin marked this conversation as resolved.
Show resolved Hide resolved
writer.write("if keyLower == \"message\" {");
RanVaknin marked this conversation as resolved.
Show resolved Hide resolved
writer.write(" errorMessage = value.(string)");
writer.write(" continue");
writer.write("}");
}

writer.openBlock("switch key {", "}", () -> {
Set<MemberShape> members = new TreeSet<>(shape.members());
for (MemberShape member : members) {
Expand All @@ -202,6 +216,12 @@ protected void deserializeStructure(GenerationContext context, StructureShape sh
});
});

if (isErrorStructure) {
writer.write("if errorMessage != \"\" {");
writer.write(" sv.Message = &errorMessage");
writer.write("}");
}

writer.write("*v = sv");
writer.write("return nil");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,4 @@ software.amazon.smithy.aws.go.codegen.customization.s3.ExpressUserAgent
software.amazon.smithy.aws.go.codegen.customization.BackfillRequiredTrait
software.amazon.smithy.aws.go.codegen.customization.DeprecateService
software.amazon.smithy.aws.go.codegen.customization.BasicUserAgentFeatures

RanVaknin marked this conversation as resolved.
Show resolved Hide resolved
Loading