forked from hashicorp/consul-template
/
mapstructure.go
75 lines (67 loc) · 1.91 KB
/
mapstructure.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package config
import (
"log"
"os"
"reflect"
"strconv"
"github.com/mitchellh/mapstructure"
)
// StringToFileModeFunc returns a function that converts strings to os.FileMode
// value. This is designed to be used with mapstructure for parsing out a
// filemode value.
func StringToFileModeFunc() mapstructure.DecodeHookFunc {
return func(
f reflect.Type,
t reflect.Type,
data interface{}) (interface{}, error) {
if f.Kind() != reflect.String {
return data, nil
}
if t != reflect.TypeOf(os.FileMode(0)) {
return data, nil
}
// Convert it by parsing
v, err := strconv.ParseUint(data.(string), 8, 12)
if err != nil {
return data, err
}
return os.FileMode(v), nil
}
}
// StringToWaitDurationHookFunc returns a function that converts strings to wait
// value. This is designed to be used with mapstructure for parsing out a wait
// value.
func StringToWaitDurationHookFunc() mapstructure.DecodeHookFunc {
return func(
f reflect.Type,
t reflect.Type,
data interface{}) (interface{}, error) {
if f.Kind() != reflect.String {
return data, nil
}
if t != reflect.TypeOf(WaitConfig{}) {
return data, nil
}
// Convert it by parsing
return ParseWaitConfig(data.(string))
}
}
// ConsulStringToStructFunc checks if the value set for the key should actually
// be a struct and sets the appropriate value in the struct. This is for
// backwards-compatability with older versions of Consul Template.
func ConsulStringToStructFunc() mapstructure.DecodeHookFunc {
return func(
f reflect.Type,
t reflect.Type,
data interface{}) (interface{}, error) {
if t == reflect.TypeOf(ConsulConfig{}) && f.Kind() == reflect.String {
log.Println("[WARN] consul now accepts a stanza instead of a string. " +
"Update your configuration files and change consul = \"\" to " +
"consul { } instead.")
return &ConsulConfig{
Address: String(data.(string)),
}, nil
}
return data, nil
}
}