-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.go
More file actions
93 lines (85 loc) · 1.64 KB
/
utils.go
File metadata and controls
93 lines (85 loc) · 1.64 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
package gottings
import (
"encoding/json"
"errors"
)
func LoadConfiguration(data []byte, v any) error {
var err error
if len(data) > 0 {
err = json.Unmarshal(data, v)
if err != nil {
return err
}
}
err = LoadEnv(v)
if err != nil {
return err
}
return nil
}
func IsInteger(v any) bool {
switch v.(type) {
case int, int8, int16, int32, int64, *int, *int8, *int16, *int32, *int64:
return true
default:
return false
}
}
func IsFloat(v any) bool {
switch v.(type) {
case float32, float64, *float32, *float64:
return true
default:
return false
}
}
func ToInt64(v any) (int64, error) {
isInt := IsInteger(v)
if !isInt {
return 0, errors.New("expected v to satisfy IsInteger(v) == true")
}
switch n := v.(type) {
case int:
return int64(n), nil
case int8:
return int64(n), nil
case int16:
return int64(n), nil
case int32:
return int64(n), nil
case int64:
return n, nil
case *int:
return int64(*n), nil
case *int8:
return int64(*n), nil
case *int16:
return int64(*n), nil
case *int32:
return int64(*n), nil
case *int64:
return int64(*n), nil
default:
// This should never be reached due to the IsInteger check
return 0, errors.New("unexpected type encountered")
}
}
func ToFloat64(v any) (float64, error) {
isF := IsFloat(v)
if !isF {
return 0, errors.New("expected v to satisfy isFloat(v) == true")
}
switch n := v.(type) {
case float32:
return float64(n), nil
case float64:
return float64(n), nil
case *float32:
return float64(*n), nil
case *float64:
return float64(*n), nil
default:
// This should never be reached
return 0., errors.New("unexpected type encountered")
}
}