diff options
| -rw-r--r-- | account.go | 2 | ||||
| -rw-r--r-- | headers.go | 45 | ||||
| -rw-r--r-- | headers_test.go | 2 |
3 files changed, 46 insertions, 3 deletions
@@ -124,7 +124,7 @@ func (a *Account) Invalidate() { func (a *Account) Post(headers AccountHeaders, opts *RequestOptions) error { _, err := Request{ Method: "POST", - Options: compileHeaders(headers, opts), + Options: compileHeaders(&headers, opts), ExpectStatusCodes: []int{204}, }.Do(a.client) if err == nil { @@ -190,7 +190,50 @@ func parseHeaders(hdr http.Header, target interface{}) error { } func compileHeaders(headers interface{}, opts *RequestOptions) RequestOptions { - panic("TODO") + hdr := make(map[string]string) + + foreachField(headers, func(fieldPtr interface{}, info fieldInfo) error { + //skip over fields without schwift field tag + if info.HeaderName == "" { + return nil + } + + //decode header value into field depending on type + switch fieldPtr := fieldPtr.(type) { + case *string: + hdr[info.HeaderName] = *fieldPtr + case *uint64: + hdr[info.HeaderName] = strconv.FormatUint(*fieldPtr, 10) + case *map[string]string: + for key, val := range *fieldPtr { + if val == "" { + if info.RemoveHeaderName == "" { + //RemoveHeaderName is used by account and container metadata: e.g. + //"X-Account-Meta-Foo: bar" is reverted by "X-Remove-Account-Meta-Foo: x" + hdr[info.RemoveHeaderName+key] = "x" + } else { + //for object metadata, you just leave out the metadata fields that + //you want to clear, so we do nothing + } + } else { + hdr[info.HeaderName+key] = val + } + } + default: + panic(fmt.Sprintf("compileHeaders: cannot handle field type %T", fieldPtr)) + } + return nil + }) + + //contents of `opts` overrides contents of `headers` + result := RequestOptions{Headers: hdr} + if opts != nil { + result.Values = opts.Values + for k, v := range opts.Headers { + result.Headers[k] = v + } + } + return result } type fieldInfo struct { diff --git a/headers_test.go b/headers_test.go index f00b166..62bc066 100644 --- a/headers_test.go +++ b/headers_test.go @@ -61,7 +61,7 @@ func expectUint64(t *testing.T, actual uint64, expected uint64) { func expectString(t *testing.T, actual string, expected string) { t.Helper() if actual != expected { - t.Errorf("expected value %d, got %d instead\n", expected, actual) + t.Errorf("expected value %q, got %q instead\n", expected, actual) } } |
