time formats for json unmarshaling
diff --git a/results.go b/results.go
index 21c66e3..e11a492 100644
--- a/results.go
+++ b/results.go
@@ -5,10 +5,8 @@
"encoding/json"
"io"
"net/http"
- "reflect"
+ "strconv"
"time"
-
- "github.com/mitchellh/mapstructure"
)
/*
@@ -109,40 +107,28 @@
// ExtractHeader will return the http.Header and error from the HeaderResult.
//
// header, err := objects.Create(client, "my_container", objects.CreateOpts{}).ExtractHeader()
-func (hr HeaderResult) ExtractHeader() (http.Header, error) {
- 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,
+func (r HeaderResult) ExtractInto(to interface{}) error {
+ if r.Err != nil {
+ return r.Err
}
- decoder, err := mapstructure.NewDecoder(config)
+
+ tmpHeaderMap := map[string]string{}
+ for k, v := range r.Header {
+ tmpHeaderMap[k] = v[0]
+ }
+
+ b, err := json.Marshal(tmpHeaderMap)
if err != nil {
return err
}
- if err := decoder.Decode(from); err != nil {
- return err
- }
- return nil
+ err = json.Unmarshal(b, to)
+
+ return err
}
// RFC3339Milli describes a common time format used by some API responses.
const RFC3339Milli = "2006-01-02T15:04:05.999999Z"
-const RFC3339MilliNoZ = "2006-01-02T03:04:05.999999"
-
type JSONRFC3339Milli time.Time
func (jt *JSONRFC3339Milli) UnmarshalJSON(data []byte) error {
@@ -160,6 +146,8 @@
return nil
}
+const RFC3339MilliNoZ = "2006-01-02T03:04:05.999999"
+
type JSONRFC3339MilliNoZ time.Time
func (jt *JSONRFC3339MilliNoZ) UnmarshalJSON(data []byte) error {
@@ -177,8 +165,64 @@
return nil
}
+type JSONRFC1123 time.Time
+
+func (jt *JSONRFC1123) UnmarshalJSON(data []byte) error {
+ b := bytes.NewBuffer(data)
+ dec := json.NewDecoder(b)
+ var s string
+ if err := dec.Decode(&s); err != nil {
+ return err
+ }
+ t, err := time.Parse(time.RFC1123, s)
+ if err != nil {
+ return err
+ }
+ *jt = JSONRFC1123(t)
+ return nil
+}
+
+type JSONUnix time.Time
+
+func (jt *JSONUnix) UnmarshalJSON(data []byte) error {
+ b := bytes.NewBuffer(data)
+ dec := json.NewDecoder(b)
+ var s string
+ if err := dec.Decode(&s); err != nil {
+ return err
+ }
+
+ unix, err := strconv.ParseInt(s, 10, 64)
+ if err != nil {
+ return err
+ }
+ t = time.Unix(unix, 0)
+ *jt = JSONUnix(t)
+ return nil
+}
+
// StackFmtTime is the time format used in Heat (Orchestration).
-const StackFmtTime = "2006-01-02T15:04:05"
+const RFC3339NoZ = "2006-01-02T15:04:05"
+
+type JSONRFC3339NoZ time.Time
+
+func (jt *JSONRFC3339NoZ) UnmarshalJSON(data []byte) error {
+ b := bytes.NewBuffer(data)
+ dec := json.NewDecoder(b)
+ var s string
+ if err := dec.Decode(&s); err != nil {
+ return err
+ }
+ if s == "" {
+ return nil
+ }
+ t, err := time.Parse(RFC3339NoZ, s)
+ if err != nil {
+ return err
+ }
+ *jt = JSONRFC3339NoZ(t)
+ return nil
+}
/*
Link is an internal type to be used in packages of collection resources that are