-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
59 lines (54 loc) · 1.49 KB
/
utils.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
package config
import (
"fmt"
)
const (
errMissingConfigValue = "missing config value for %s"
)
// GetConfigValueAsString getting a value as a string, if possible
func GetConfigValueAsString(properties map[string]any, key string) (string, error) {
if _, ok := properties[key]; !ok {
return "", fmt.Errorf(errMissingConfigValue, key)
}
if properties[key] == nil {
return "", nil
}
value, ok := properties[key].(string)
if !ok {
return "", fmt.Errorf("config value for %s is not a string", key)
}
return value, nil
}
// GetConfigValueAsBool getting a value as a bool, if possible
func GetConfigValueAsBool(properties map[string]any, key string) (bool, error) {
if _, ok := properties[key]; !ok {
return false, fmt.Errorf(errMissingConfigValue, key)
}
if properties[key] == nil {
return false, nil
}
value, ok := properties[key].(bool)
if !ok {
return false, fmt.Errorf("config value for %s is not a string", key)
}
return value, nil
}
// GetConfigValueAsInt getting a value as a int64, if possible
func GetConfigValueAsInt(properties map[string]any, key string) (int64, error) {
if _, ok := properties[key]; !ok {
return 0, fmt.Errorf(errMissingConfigValue, key)
}
if properties[key] == nil {
return 0, nil
}
var value int64
switch v := properties[key].(type) {
case int:
value = int64(v)
case int64:
value = v
default:
return 0, fmt.Errorf("config value for %s is not a integer", key)
}
return value, nil
}