-
Notifications
You must be signed in to change notification settings - Fork 242
/
Copy pathdecode_hooks.go
50 lines (40 loc) · 1.2 KB
/
decode_hooks.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
package config
import (
"os"
"reflect"
"regexp"
"github.com/go-errors/errors"
)
var envPattern = regexp.MustCompile(`^env\((.*)\)$`)
// LoadEnvHook is a mapstructure decode hook that loads environment variables
// from strings formatted as env(VAR_NAME).
func LoadEnvHook(f reflect.Kind, t reflect.Kind, data interface{}) (interface{}, error) {
if f != reflect.String {
return data, nil
}
value := data.(string)
if matches := envPattern.FindStringSubmatch(value); len(matches) > 1 {
if env := os.Getenv(matches[1]); len(env) > 0 {
value = env
}
}
return value, nil
}
const invalidFunctionsConfigFormat = `Invalid functions config format. Functions should be configured as:
[functions.<function-name>]
field = value
Example:
[functions.hello]
verify_jwt = true`
// ValidateFunctionsHook is a mapstructure decode hook that validates the functions config format.
func ValidateFunctionsHook(f reflect.Type, t reflect.Type, data interface{}) (interface{}, error) {
// Only handle FunctionConfig type
if t != reflect.TypeOf(FunctionConfig{}) {
return data, nil
}
// Check if source is not a map
if f.Kind() != reflect.Map {
return nil, errors.New(invalidFunctionsConfigFormat)
}
return data, nil
}