/
config_map.go
57 lines (48 loc) · 1.64 KB
/
config_map.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
package modconfig
import (
"fmt"
"reflect"
"strings"
"github.com/turbot/go-kit/helpers"
"github.com/turbot/pipe-fittings/options"
)
type ConfigMap map[string]interface{}
// SetStringItem checks is string pointer is non-nil and if so, add to map with given key
func (m ConfigMap) SetStringItem(argValue *string, argName string) {
if argValue != nil {
m[argName] = *argValue
}
}
// SetStringSliceItem checks is string slice pointer is non-nil and if so, add to map with given key
func (m ConfigMap) SetStringSliceItem(argValue []string, argName string) {
if argValue != nil {
m[argName] = argValue
}
}
// SetIntItem checks is int pointer is non-nil and if so, add to map with given key
func (m ConfigMap) SetIntItem(argValue *int, argName string) {
if argValue != nil {
m[argName] = *argValue
}
}
// SetBoolItem checks is bool pointer is non-nil and if so, add to map with given key
func (m ConfigMap) SetBoolItem(argValue *bool, argName string) {
if argValue != nil {
m[argName] = *argValue
}
}
// PopulateConfigMapForOptions populates the config map for a given options object
// NOTE: this mutates configMap
func (m ConfigMap) PopulateConfigMapForOptions(o options.Options) {
for k, v := range o.ConfigMap() {
m[k] = v
// also store a scoped version of the config property
m[getScopedKey(o, k)] = v
}
}
// generated a scoped key for the config property. For example if o is a database options object and k is 'search-path'
// the scoped key will be 'database.search-path'
func getScopedKey(o options.Options, k string) string {
t := reflect.TypeOf(helpers.DereferencePointer(o)).Name()
return fmt.Sprintf("%s.%s", strings.ToLower(t), k)
}