Skip to content

Commit

Permalink
fix: handle error message field in structure deserializer
Browse files Browse the repository at this point in the history
  • Loading branch information
RanVaknin committed Nov 5, 2024
1 parent 061540b commit 7cc4a3a
Show file tree
Hide file tree
Showing 3 changed files with 255 additions and 0 deletions.
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)");
writer.write("if keyLower == \"message\" {");
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

Loading

0 comments on commit 7cc4a3a

Please sign in to comment.