-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproperties.go
64 lines (54 loc) · 1.58 KB
/
properties.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
package proper
import "strconv"
// NewProperties creates a new Properties object
func NewProperties(m map[string]string) *Properties {
return &Properties{propertyMap: m}
}
// Properties is a string map decorator
type Properties struct {
propertyMap map[string]string
}
// StringParam attempts to look up a string value by key. If not found, return the default string value
func (p *Properties) StringParam(key string, defaultValue string) string {
value, ok := p.propertyMap[key]
if !ok {
return defaultValue
}
return value
}
// BooleanParam attempts to look up a boolean value by key. If not found, return the default boolean value
func (p *Properties) BooleanParam(key string, defaultValue bool) bool {
value, ok := p.propertyMap[key]
if !ok {
return defaultValue
}
integerValue, err := strconv.ParseBool(value)
if err != nil {
return defaultValue
}
return integerValue
}
// IntegerParam attempts to look up a integer value by key. If not found, return the default integer value
func (p *Properties) IntegerParam(key string, defaultValue int) int {
value, ok := p.propertyMap[key]
if !ok {
return defaultValue
}
integerValue, err := strconv.Atoi(value)
if err != nil {
return defaultValue
}
return integerValue
}
// FloatParam attempts to look up a float value by key. If not found, return the default float value
func (p *Properties) FloatParam(key string, defaultValue float64) float64 {
value, ok := p.propertyMap[key]
if !ok {
return defaultValue
}
integerValue, err := strconv.ParseFloat(value, 64)
if err != nil {
return defaultValue
}
return integerValue
}