Skip to content

Commit

Permalink
refactor logic to use switch case
Browse files Browse the repository at this point in the history
  • Loading branch information
RanVaknin committed Nov 6, 2024
1 parent 7c4bde0 commit 2ae3246
Show file tree
Hide file tree
Showing 372 changed files with 4,059 additions and 142,110 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -181,20 +181,8 @@ 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());
Expand All @@ -204,10 +192,16 @@ protected void deserializeStructure(GenerationContext context, StructureShape sh
}
String memberName = symbolProvider.toMemberName(member);
String serializedMemberName = getSerializedMemberName(member);
writer.openBlock("case $S:", "", serializedMemberName, () -> {
String dest = "sv." + memberName;
context.getModel().expectShape(member.getTarget()).accept(getMemberDeserVisitor(member, dest));
});

boolean isErrorStructure = shape.hasTrait(ErrorTrait.class);

if(isErrorStructure && memberName.equals("Message")){
writer.write("case \"message\", \"Message\":");
} else {
writer.write("case $S:",serializedMemberName);
}
String dest = "sv." + memberName;
context.getModel().expectShape(member.getTarget()).accept(getMemberDeserVisitor(member, dest));
}

writer.openBlock("default:", "", () -> {
Expand All @@ -216,11 +210,6 @@ 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
Loading

0 comments on commit 2ae3246

Please sign in to comment.