-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcombined.go
41 lines (36 loc) · 1.17 KB
/
combined.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
// (c) 2022 Rick Arnold. Licensed under the BSD license (see LICENSE).
package props
// Combined provides property value lookups across multiple sources.
type Combined struct {
// The property sources to use for lookup in priority order. The first
// source to have a value for a property will be used.
Sources []PropertyGetter
}
// Ensure that Combined implements PropertyGetter
var _ PropertyGetter = &Combined{}
// Get retrieves the value of a property from the source list. If the source
// list is empty or none of the sources has the property, an empty string will
// be returned. The bool return value indicates whether the property was found.
func (c *Combined) Get(key string) (string, bool) {
if c.Sources == nil {
return "", false
}
for _, l := range c.Sources {
val, ok := l.Get(key)
if ok {
return val, true
}
}
return "", false
}
// GetDefault retrieves the value of a property from the source list. If the
// source list is empty or none of the sources has the property, then the
// default value will be returned.
func (c *Combined) GetDefault(key string, defVal string) string {
val, ok := c.Get(key)
if ok {
return val
} else {
return defVal
}
}