| 1 |
package fastly |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"net/http" |
| 6 |
"reflect" |
| 7 |
"time" |
| 8 |
|
| 9 |
"github.com/mitchellh/mapstructure" |
| 10 |
) |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
func mapToHTTPHeaderHookFunc() mapstructure.DecodeHookFunc { |
| 15 |
return func( |
| 16 |
f reflect.Type, |
| 17 |
t reflect.Type, |
| 18 |
data interface{}) (interface{}, error) { |
| 19 |
if f.Kind() != reflect.Map { |
| 20 |
return data, nil |
| 21 |
} |
| 22 |
if t != reflect.TypeOf(new(http.Header)) { |
| 23 |
return data, nil |
| 24 |
} |
| 25 |
|
| 26 |
typed, ok := data.(map[string]interface{}) |
| 27 |
if !ok { |
| 28 |
return nil, fmt.Errorf("cannot convert %T to http.Header", data) |
| 29 |
} |
| 30 |
|
| 31 |
n := map[string][]string{} |
| 32 |
for k, v := range typed { |
| 33 |
switch v.(type) { |
| 34 |
case string: |
| 35 |
n[k] = []string{v.(string)} |
| 36 |
case []string: |
| 37 |
n[k] = v.([]string) |
| 38 |
case int, int8, int16, int32, int64: |
| 39 |
n[k] = []string{fmt.Sprintf("%d", v.(int))} |
| 40 |
case float32, float64: |
| 41 |
n[k] = []string{fmt.Sprintf("%f", v.(float64))} |
| 42 |
default: |
| 43 |
return nil, fmt.Errorf("cannot convert %T to http.Header", v) |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
return n, nil |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
func stringToTimeHookFunc() mapstructure.DecodeHookFunc { |
| 54 |
return func( |
| 55 |
f reflect.Type, |
| 56 |
t reflect.Type, |
| 57 |
data interface{}) (interface{}, error) { |
| 58 |
if f.Kind() != reflect.String { |
| 59 |
return data, nil |
| 60 |
} |
| 61 |
if t != reflect.TypeOf(time.Now()) { |
| 62 |
return data, nil |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
return time.Parse(time.RFC3339, data.(string)) |
| 67 |
} |
| 68 |
} |