-
Notifications
You must be signed in to change notification settings - Fork 12
/
key_prefixed_data_provider.go
145 lines (116 loc) · 5.72 KB
/
key_prefixed_data_provider.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
Copyright © 2024 Acronis International GmbH.
Released under MIT license.
*/
package config
import (
"io"
"strings"
"time"
)
// KeyPrefixedDataProvider is a DataProvider implementation that uses a specified key prefix for parsing configuration parameters.
type KeyPrefixedDataProvider struct {
delegate DataProvider
keyPrefix string
}
var _ DataProvider = (*KeyPrefixedDataProvider)(nil)
// NewKeyPrefixedDataProvider creates a new KeyPrefixedDataProvider.
func NewKeyPrefixedDataProvider(delegate DataProvider, keyPrefix string) *KeyPrefixedDataProvider {
return &KeyPrefixedDataProvider{delegate: delegate, keyPrefix: keyPrefix}
}
func (kp *KeyPrefixedDataProvider) makeKey(key string) string {
return strings.Trim(kp.keyPrefix+"."+key, ".")
}
// UseEnvVars enables the ability to use environment variables for configuration parameters.
// Prefix defines what environment variables will be looked.
// E.g., if your prefix is "spf", the env registry will look for env
// variables that start with "SPF_".
func (kp *KeyPrefixedDataProvider) UseEnvVars(prefix string) {
kp.delegate.UseEnvVars(prefix)
}
// Set sets the value for the key in the override register.
func (kp *KeyPrefixedDataProvider) Set(key string, value interface{}) {
kp.delegate.Set(kp.makeKey(key), value)
}
// SetDefault sets the default value for this key.
// Default only used when no value is provided by the user via config or ENV.
func (kp *KeyPrefixedDataProvider) SetDefault(key string, value interface{}) {
kp.delegate.SetDefault(kp.makeKey(key), value)
}
// IsSet checks to see if the key has been set in any of the data locations.
// IsSet is case-insensitive for a key.
func (kp *KeyPrefixedDataProvider) IsSet(key string) bool {
return kp.delegate.IsSet(kp.makeKey(key))
}
// Get retrieves any value given the key to use.
func (kp *KeyPrefixedDataProvider) Get(key string) interface{} {
return kp.delegate.Get(kp.makeKey(key))
}
// SetFromFile specifies that discovering and loading configuration data will be performed from file.
func (kp *KeyPrefixedDataProvider) SetFromFile(path string, dataType DataType) error {
return kp.delegate.SetFromFile(path, dataType)
}
// SetFromReader specifies that discovering and loading configuration data will be performed from reader.
func (kp *KeyPrefixedDataProvider) SetFromReader(reader io.Reader, dataType DataType) error {
return kp.delegate.SetFromReader(reader, dataType)
}
// GetInt tries to retrieve the value associated with the key as an integer.
func (kp *KeyPrefixedDataProvider) GetInt(key string) (res int, err error) {
return kp.delegate.GetInt(kp.makeKey(key))
}
// GetIntSlice tries to retrieve the value associated with the key as a slice of integers.
func (kp *KeyPrefixedDataProvider) GetIntSlice(key string) (res []int, err error) {
return kp.delegate.GetIntSlice(kp.makeKey(key))
}
// GetFloat32 tries to retrieve the value associated with the key as an float32.
func (kp *KeyPrefixedDataProvider) GetFloat32(key string) (res float32, err error) {
return kp.delegate.GetFloat32(kp.makeKey(key))
}
// GetFloat64 tries to retrieve the value associated with the key as an float64.
func (kp *KeyPrefixedDataProvider) GetFloat64(key string) (res float64, err error) {
return kp.delegate.GetFloat64(kp.makeKey(key))
}
// GetString tries to retrieve the value associated with the key as a string.
func (kp *KeyPrefixedDataProvider) GetString(key string) (res string, err error) {
return kp.delegate.GetString(kp.makeKey(key))
}
// GetBool tries to retrieve the value associated with the key as a bool.
func (kp *KeyPrefixedDataProvider) GetBool(key string) (res bool, err error) {
return kp.delegate.GetBool(kp.makeKey(key))
}
// GetStringSlice tries to retrieve the value associated with the key as an slice of strings.
func (kp *KeyPrefixedDataProvider) GetStringSlice(key string) (res []string, err error) {
return kp.delegate.GetStringSlice(kp.makeKey(key))
}
// GetSizeInBytes tries to retrieve the value associated with the key as a size in bytes.
func (kp *KeyPrefixedDataProvider) GetSizeInBytes(key string) (uint64, error) {
return kp.delegate.GetSizeInBytes(kp.makeKey(key))
}
// GetStringFromSet tries to retrieve the value associated with the key as a string from the specified set.
func (kp *KeyPrefixedDataProvider) GetStringFromSet(key string, set []string, ignoreCase bool) (string, error) {
return kp.delegate.GetStringFromSet(kp.makeKey(key), set, ignoreCase)
}
// GetDuration tries to retrieve the value associated with the key as a duration.
func (kp *KeyPrefixedDataProvider) GetDuration(key string) (res time.Duration, err error) {
return kp.delegate.GetDuration(kp.makeKey(key))
}
// GetStringMapString tries to retrieve the value associated with the key as an map where key and value are strings.
func (kp *KeyPrefixedDataProvider) GetStringMapString(key string) (res map[string]string, err error) {
return kp.delegate.GetStringMapString(kp.makeKey(key))
}
// Unmarshal unmarshals the config into a Struct.
func (kp *KeyPrefixedDataProvider) Unmarshal(rawVal interface{}, opts ...DecoderConfigOption) (err error) {
return kp.delegate.UnmarshalKey(kp.makeKey(""), rawVal, opts...)
}
// UnmarshalKey takes a single key and unmarshals it into a Struct.
func (kp *KeyPrefixedDataProvider) UnmarshalKey(key string, rawVal interface{}, opts ...DecoderConfigOption) (err error) {
return kp.delegate.UnmarshalKey(kp.makeKey(key), rawVal, opts...)
}
// WrapKeyErr wraps error adding information about a key where this error occurs.
func (kp *KeyPrefixedDataProvider) WrapKeyErr(key string, err error) error {
return WrapKeyErr(kp.makeKey(key), err)
}
// SaveToFile writes config into file according data type.
func (kp *KeyPrefixedDataProvider) SaveToFile(path string, dataType DataType) error {
return kp.delegate.SaveToFile(path, dataType)
}