2016-03-23 20:53:09 +01:00
|
|
|
package fastly
|
|
|
|
|
|
|
|
import (
|
2016-04-20 20:43:54 +02:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"reflect"
|
|
|
|
"time"
|
2016-03-23 20:53:09 +01:00
|
|
|
|
2016-04-20 20:43:54 +02:00
|
|
|
"github.com/mitchellh/mapstructure"
|
2016-03-23 20:53:09 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// mapToHTTPHeaderHookFunc returns a function that converts maps into an
|
|
|
|
// http.Header value.
|
|
|
|
func mapToHTTPHeaderHookFunc() mapstructure.DecodeHookFunc {
|
2016-04-20 20:43:54 +02:00
|
|
|
return func(
|
|
|
|
f reflect.Type,
|
|
|
|
t reflect.Type,
|
|
|
|
data interface{}) (interface{}, error) {
|
|
|
|
if f.Kind() != reflect.Map {
|
|
|
|
return data, nil
|
|
|
|
}
|
|
|
|
if t != reflect.TypeOf(new(http.Header)) {
|
|
|
|
return data, nil
|
|
|
|
}
|
2016-03-23 20:53:09 +01:00
|
|
|
|
2016-04-20 20:43:54 +02:00
|
|
|
typed, ok := data.(map[string]interface{})
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("cannot convert %T to http.Header", data)
|
|
|
|
}
|
2016-03-23 20:53:09 +01:00
|
|
|
|
2016-04-20 20:43:54 +02:00
|
|
|
n := map[string][]string{}
|
|
|
|
for k, v := range typed {
|
|
|
|
switch v.(type) {
|
|
|
|
case string:
|
|
|
|
n[k] = []string{v.(string)}
|
|
|
|
case []string:
|
|
|
|
n[k] = v.([]string)
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("cannot convert %T to http.Header", v)
|
|
|
|
}
|
|
|
|
}
|
2016-03-23 20:53:09 +01:00
|
|
|
|
2016-04-20 20:43:54 +02:00
|
|
|
return n, nil
|
|
|
|
}
|
2016-03-23 20:53:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// stringToTimeHookFunc returns a function that converts strings to a time.Time
|
|
|
|
// value.
|
|
|
|
func stringToTimeHookFunc() mapstructure.DecodeHookFunc {
|
2016-04-20 20:43:54 +02:00
|
|
|
return func(
|
|
|
|
f reflect.Type,
|
|
|
|
t reflect.Type,
|
|
|
|
data interface{}) (interface{}, error) {
|
|
|
|
if f.Kind() != reflect.String {
|
|
|
|
return data, nil
|
|
|
|
}
|
|
|
|
if t != reflect.TypeOf(time.Now()) {
|
|
|
|
return data, nil
|
|
|
|
}
|
2016-03-23 20:53:09 +01:00
|
|
|
|
2016-04-20 20:43:54 +02:00
|
|
|
// Convert it by parsing
|
|
|
|
return time.Parse(time.RFC3339, data.(string))
|
|
|
|
}
|
2016-03-23 20:53:09 +01:00
|
|
|
}
|