-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget.go
More file actions
119 lines (98 loc) · 3.19 KB
/
Copy pathget.go
File metadata and controls
119 lines (98 loc) · 3.19 KB
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
package ecp
import (
"fmt"
"reflect"
)
// getValue looks up a field by the environment key it is bound to. The
// optional prefix must match the one Parse was called with, since that is
// what the key names are built from.
func (e *ECP) getValue(config interface{}, keyName string, prefix ...string) (reflect.Value, error) {
if len(prefix) == 0 {
prefix = []string{""}
}
v, err := e.rangeOver(roOption{target: config, find: keyName, prefix: prefix[0]})
if err != nil {
return reflect.Value{}, err
}
if !v.IsValid() {
return reflect.Value{}, fmt.Errorf("key %s not found", keyName)
}
if !v.CanInterface() {
return reflect.Value{}, fmt.Errorf("bad structure field %s", keyName)
}
return v, nil
}
// Get the value of the keyName in that struct
func (e *ECP) Get(config interface{}, keyName string, prefix ...string) (interface{}, error) {
v, err := e.getValue(config, keyName, prefix...)
if err != nil {
return nil, err
}
return v.Interface(), nil
}
// GetBool returns bool
func (e *ECP) GetBool(config interface{}, keyName string, prefix ...string) (bool, error) {
v, err := e.getValue(config, keyName, prefix...)
if err != nil {
return false, err
}
if v.Kind() == reflect.Bool {
return v.Bool(), nil
}
return false, fmt.Errorf("value is not bool, it's %s", v.Kind())
}
// GetInt64 returns int64
func (e *ECP) GetInt64(config interface{}, keyName string, prefix ...string) (int64, error) {
v, err := e.getValue(config, keyName, prefix...)
if err != nil {
return -1, err
}
switch v.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return v.Int(), nil
}
return -1, fmt.Errorf("value is %s", v.Kind())
}
// GetString returns string
func (e *ECP) GetString(config interface{}, keyName string, prefix ...string) (string, error) {
v, err := e.getValue(config, keyName, prefix...)
if err != nil {
return "", err
}
if v.Kind() == reflect.String {
return v.String(), nil
}
return "", fmt.Errorf("value is not string, it's %s", v.Kind())
}
// GetFloat64 returns float64
func (e *ECP) GetFloat64(config interface{}, keyName string, prefix ...string) (float64, error) {
v, err := e.getValue(config, keyName, prefix...)
if err != nil {
return -1, err
}
switch v.Kind() {
case reflect.Float32, reflect.Float64:
return v.Float(), nil
}
return -1, fmt.Errorf("value is %s", v.Kind())
}
// Get the value of the keyName in that struct
func Get(config interface{}, keyName string, prefix ...string) (interface{}, error) {
return globalEcp.Get(config, keyName, prefix...)
}
// GetBool returns bool
func GetBool(config interface{}, keyName string, prefix ...string) (bool, error) {
return globalEcp.GetBool(config, keyName, prefix...)
}
// GetInt64 returns int64
func GetInt64(config interface{}, keyName string, prefix ...string) (int64, error) {
return globalEcp.GetInt64(config, keyName, prefix...)
}
// GetString returns string
func GetString(config interface{}, keyName string, prefix ...string) (string, error) {
return globalEcp.GetString(config, keyName, prefix...)
}
// GetFloat64 returns float64
func GetFloat64(config interface{}, keyName string, prefix ...string) (float64, error) {
return globalEcp.GetFloat64(config, keyName, prefix...)
}