Skip to content

Commit

Permalink
Migrate discard / collect unknown to smithyjson
Browse files Browse the repository at this point in the history
  • Loading branch information
JordonPhillips committed Aug 27, 2020
1 parent 2aaa276 commit 7db4a0c
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 32 deletions.
30 changes: 2 additions & 28 deletions aws/protocol/restjson/decoder_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"

"github.com/awslabs/smithy-go"
smithyjson "github.com/awslabs/smithy-go/json"
)

// GetErrorInfo util looks for code, __type, and message members in the
Expand Down Expand Up @@ -41,7 +42,7 @@ func GetErrorInfo(decoder *json.Decoder) (errorType string, message string, err
case strings.EqualFold(st, "message"):
target = &message
default:
DiscardUnknownField(decoder)
smithyjson.DiscardUnknownField(decoder)
continue
}

Expand Down Expand Up @@ -83,34 +84,7 @@ func SanitizeErrorCode(errorCode string) string {
return errorCode
}

// DiscardUnknownField discards unknown fields from decoder body.
// This function is useful while deserializing json body with additional
// unknown information that should be discarded.
func DiscardUnknownField(decoder *json.Decoder) error {
v, err := decoder.Token()
if err == io.EOF {
return nil
}
if err != nil {
return err
}

if _, ok := v.(json.Delim); ok {
for decoder.More() {
err = DiscardUnknownField(decoder)
}
endToken, err := decoder.Token()
if err != nil {
return err
}
if _, ok := endToken.(json.Delim); !ok {
return fmt.Errorf("invalid JSON : expected json delimiter, found %T %v",
endToken, endToken)
}
}

return nil
}

// GetSmithyGenericAPIError returns smithy generic api error and an error interface.
// Takes in json decoder, and error Code string as args. The function retrieves error message
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,8 @@ protected void deserializeStructure(GenerationContext context, StructureShape sh
}

writer.openBlock("default:", "", () -> {
writer.addUseImports(AwsGoDependency.AWS_REST_JSON_PROTOCOL);
writer.write("err := restjson.DiscardUnknownField(decoder)");
writer.addUseImports(SmithyGoDependency.SMITHY_JSON);
writer.write("err := smithyjson.DiscardUnknownField(decoder)");
writer.write("if err != nil {return err}");
});
});
Expand Down Expand Up @@ -262,10 +262,10 @@ protected void deserializeUnion(GenerationContext context, UnionShape shape) {
UnionGenerator.UNKNOWN_MEMBER_NAME,
symbol.getNamespace()
).build();
writer.addUseImports(AwsGoDependency.AWS_REST_JSON_PROTOCOL);
writer.addUseImports(SmithyGoDependency.SMITHY_JSON);
writer.write("tagString, ok := t.(string)");
writer.write("if !ok { return fmt.Errorf(\"expected string key, found %T\", t) }");
writer.write("value, err := restjson.CollectUnknownField(decoder)");
writer.write("value, err := smithyjson.CollectUnknownField(decoder)");
writer.write("if err != nil { return err }");
writer.write("uv = &$T{Tag: tagString, Value: value}", unknownMemberSymbol);
});
Expand Down

0 comments on commit 7db4a0c

Please sign in to comment.