From 533e57b03a5148acb054ceeaab01689bed94a580 Mon Sep 17 00:00:00 2001 From: Stefan Majewsky Date: Tue, 30 Jan 2018 16:58:35 +0100 Subject: implement compileHeaders Test is still failing though... --- account.go | 2 +- headers.go | 45 ++++++++++++++++++++++++++++++++++++++++++++- headers_test.go | 2 +- 3 files changed, 46 insertions(+), 3 deletions(-) diff --git a/account.go b/account.go index af7698b..e38704a 100644 --- a/account.go +++ b/account.go @@ -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 { diff --git a/headers.go b/headers.go index 2153d9d..da11ab0 100644 --- a/headers.go +++ b/headers.go @@ -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) } } -- cgit v1.2.3