refactored DecodeHeader function to gophercloud package; updated tests
diff --git a/results.go b/results.go
index 3fd5029..7c86ce4 100644
--- a/results.go
+++ b/results.go
@@ -3,6 +3,9 @@
import (
"encoding/json"
"net/http"
+ "reflect"
+
+ "github.com/mitchellh/mapstructure"
)
/*
@@ -82,6 +85,31 @@
return hr.Header, hr.Err
}
+// DecodeHeader is a function that decodes a header (usually of type map[string]interface{}) to
+// another type (usually a struct). This function is used by the objectstorage package to give
+// users access to response headers without having to query a map. A DecodeHookFunction is used,
+// because OpenStack-based clients return header values as arrays (Go slices).
+func DecodeHeader(from, to interface{}) error {
+ config := &mapstructure.DecoderConfig{
+ DecodeHook: func(from, to reflect.Kind, data interface{}) (interface{}, error) {
+ if from == reflect.Slice {
+ return data.([]string)[0], nil
+ }
+ return data, nil
+ },
+ Result: to,
+ WeaklyTypedInput: true,
+ }
+ decoder, err := mapstructure.NewDecoder(config)
+ if err != nil {
+ return err
+ }
+ if err := decoder.Decode(from); err != nil {
+ return err
+ }
+ return nil
+}
+
// RFC3339Milli describes a common time format used by some API responses.
const RFC3339Milli = "2006-01-02T15:04:05.999999Z"