Skip to content

Commit

Permalink
Improved support for byte arrays (#14715)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
jhendrixMSFT authored May 26, 2021
1 parent ff5a104 commit a6741c6
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 39 deletions.
20 changes: 9 additions & 11 deletions sdk/azcore/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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{} {
Expand Down
59 changes: 32 additions & 27 deletions sdk/azcore/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion sdk/azcore/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

0 comments on commit a6741c6

Please sign in to comment.