forked from uber-go/config
-
Notifications
You must be signed in to change notification settings - Fork 0
/
value.go
139 lines (118 loc) · 3.86 KB
/
value.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
// Copyright (c) 2017 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package config
import (
"fmt"
"reflect"
"time"
)
const _separator = "."
var _typeOfString = reflect.TypeOf("string")
// A Value holds the value of a configuration
type Value struct {
root Provider
provider Provider
key string
value interface{}
found bool
}
// NewValue creates a configuration value from a provider and a set
// of parameters describing the key.
func NewValue(
provider Provider,
key string,
value interface{},
found bool,
) Value {
return Value{
provider: provider,
key: key,
value: value,
found: found,
}
}
// Source returns a configuration provider's name.
func (cv Value) Source() string {
if cv.provider == nil {
return ""
}
return cv.provider.Name()
}
// WithDefault sets the default value that can be overridden
// by providers with a highger priority.
func (cv Value) WithDefault(value interface{}) (Value, error) {
p, err := NewStaticProvider(map[string]interface{}{cv.key: value})
if err != nil {
return cv, err
}
g, err := NewProviderGroup("withDefault", p, cv.provider)
if err != nil {
return cv, err
}
return g.Get(cv.key), nil
}
// String prints out underlying value in Value with fmt.Sprint.
func (cv Value) String() string {
return fmt.Sprint(cv.Value())
}
// HasValue returns whether the configuration has a value that can be used.
func (cv Value) HasValue() bool {
return cv.found
}
// Value returns the underlying configuration's value.
func (cv Value) Value() interface{} {
return cv.value
}
// Get returns a value scoped in the current value.
func (cv Value) Get(key string) Value {
return NewScopedProvider(cv.key, cv.provider).Get(key)
}
// convertValue is a quick-and-dirty conversion method that only handles
// a couple of cases and complains if it finds one it doesn't like.
// TODO: This needs a lot more cases.
func convertValue(value interface{}, targetType reflect.Type) (interface{}, error) {
valueType := reflect.TypeOf(value)
if valueType.AssignableTo(targetType) {
return value, nil
} else if targetType == _typeOfString {
return fmt.Sprint(value), nil
}
switch v := value.(type) {
case string:
target := reflect.New(targetType).Interface()
switch target.(type) {
case *time.Duration:
return time.ParseDuration(v)
}
}
return nil, fmt.Errorf("can't convert %v to %v", reflect.TypeOf(value).String(), targetType)
}
// Populate fills in an object from configuration.
func (cv Value) Populate(target interface{}) error {
if reflect.TypeOf(target).Kind() != reflect.Ptr {
return fmt.Errorf("can't populate non pointer type %T", target)
}
ptr := reflect.Indirect(reflect.ValueOf(target))
if !ptr.IsValid() {
return fmt.Errorf("can't populate nil %T", target)
}
d := decoder{Value: &cv, m: make(map[interface{}]struct{})}
return d.unmarshal(cv.key, ptr, "")
}