From a6741c6305304bc74f8d29521d51461726751d61 Mon Sep 17 00:00:00 2001 From: Joel Hendrix Date: Wed, 26 May 2021 12:43:19 -0700 Subject: [PATCH] Improved support for byte arrays (#14715) * Improved support for byte arrays Moved guts of MarshalAsByteArray() and UnmarshalAsByteArray() into stand-alone funcs to be used in code generation. * Update EncodeByteArray to not return an error --- sdk/azcore/request.go | 20 +++++++------- sdk/azcore/response.go | 59 +++++++++++++++++++++++------------------- sdk/azcore/version.go | 2 +- 3 files changed, 42 insertions(+), 39 deletions(-) diff --git a/sdk/azcore/request.go b/sdk/azcore/request.go index f5636f733ceb..f7e1bb5af143 100644 --- a/sdk/azcore/request.go +++ b/sdk/azcore/request.go @@ -118,18 +118,8 @@ func (req *Request) Next() (*Response, error) { // MarshalAsByteArray will base-64 encode the byte slice v, then calls SetBody. // The encoded value is treated as a JSON string. func (req *Request) MarshalAsByteArray(v []byte, format Base64Encoding) error { - var encode string - switch format { - case Base64StdFormat: - encode = base64.StdEncoding.EncodeToString(v) - case Base64URLFormat: - // use raw encoding so that '=' characters are omitted as they have special meaning in URLs - encode = base64.RawURLEncoding.EncodeToString(v) - default: - return fmt.Errorf("unrecognized byte array format: %d", format) - } // send as a JSON string - encode = fmt.Sprintf("\"%s\"", encode) + encode := fmt.Sprintf("\"%s\"", EncodeByteArray(v, format)) return req.SetBody(NopCloser(strings.NewReader(encode)), contentTypeAppJSON) } @@ -309,6 +299,14 @@ func (req *Request) writeBody(b *bytes.Buffer) error { return nil } +// EncodeByteArray will base-64 encode the byte slice v. +func EncodeByteArray(v []byte, format Base64Encoding) string { + if format == Base64URLFormat { + return base64.RawURLEncoding.EncodeToString(v) + } + return base64.StdEncoding.EncodeToString(v) +} + // returns a clone of the object graph pointed to by v, omitting values of all read-only // fields. if there are no read-only fields in the object graph, no clone is created. func cloneWithoutReadOnlyFields(v interface{}) interface{} { diff --git a/sdk/azcore/response.go b/sdk/azcore/response.go index bf0161b2542f..d46229acb6af 100644 --- a/sdk/azcore/response.go +++ b/sdk/azcore/response.go @@ -60,33 +60,7 @@ func (r *Response) UnmarshalAsByteArray(v *[]byte, format Base64Encoding) error if err != nil { return err } - if len(p) == 0 { - return nil - } - payload := string(p) - if payload[0] == '"' { - // remove surrounding quotes - payload = payload[1 : len(payload)-1] - } - switch format { - case Base64StdFormat: - decoded, err := base64.StdEncoding.DecodeString(payload) - if err == nil { - *v = decoded - return nil - } - return err - case Base64URLFormat: - // use raw encoding as URL format should not contain any '=' characters - decoded, err := base64.RawURLEncoding.DecodeString(payload) - if err == nil { - *v = decoded - return nil - } - return err - default: - return fmt.Errorf("unrecognized byte array format: %d", format) - } + return DecodeByteArray(string(p), v, format) } // UnmarshalAsJSON calls json.Unmarshal() to unmarshal the received payload into the value pointed to by v. @@ -198,6 +172,37 @@ func RetryAfter(resp *http.Response) time.Duration { return 0 } +// DecodeByteArray will base-64 decode the provided string into v. +func DecodeByteArray(s string, v *[]byte, format Base64Encoding) error { + if len(s) == 0 { + return nil + } + payload := string(s) + if payload[0] == '"' { + // remove surrounding quotes + payload = payload[1 : len(payload)-1] + } + switch format { + case Base64StdFormat: + decoded, err := base64.StdEncoding.DecodeString(payload) + if err == nil { + *v = decoded + return nil + } + return err + case Base64URLFormat: + // use raw encoding as URL format should not contain any '=' characters + decoded, err := base64.RawURLEncoding.DecodeString(payload) + if err == nil { + *v = decoded + return nil + } + return err + default: + return fmt.Errorf("unrecognized byte array format: %d", format) + } +} + // writeRequestWithResponse appends a formatted HTTP request into a Buffer. If request and/or err are // not nil, then these are also written into the Buffer. func writeRequestWithResponse(b *bytes.Buffer, request *Request, response *Response, err error) { diff --git a/sdk/azcore/version.go b/sdk/azcore/version.go index 3cfa4e3967fd..bf278053a215 100644 --- a/sdk/azcore/version.go +++ b/sdk/azcore/version.go @@ -10,5 +10,5 @@ const ( UserAgent = "azcore/" + Version // Version is the semantic version (see http://semver.org) of this module. - Version = "v0.16.1" + Version = "v0.16.2" )