Skip to content

Improved support for byte arrays #14715

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 26, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions sdk/azcore/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,9 @@ 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)
encode, err := EncodeByteArray(v, format)
if err != nil {
return err
}
// send as a JSON string
encode = fmt.Sprintf("\"%s\"", encode)
Expand Down Expand Up @@ -309,6 +303,19 @@ 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, error) {
switch format {
case Base64StdFormat:
return base64.StdEncoding.EncodeToString(v), nil
case Base64URLFormat:
// use raw encoding so that '=' characters are omitted as they have special meaning in URLs
return base64.RawURLEncoding.EncodeToString(v), nil
default:
return "", fmt.Errorf("unrecognized byte array format: %d", format)
}
}

// 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"
)