-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
145 lines (131 loc) · 2.72 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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package toolkit
import (
"encoding/json"
"fmt"
"os"
"reflect"
"time"
"unicode"
)
func Match(o1, o2 interface{}) bool {
switch t := o1.(type) { //type switch
case Equaler:
return t.Equals(o2)
}
switch t := o2.(type) { //type switch
case Equaler:
return t.Equals(o1)
}
return o1 == o2 // even if both are null
}
func SliceContains(list interface{}, elem interface{}) bool {
v := reflect.ValueOf(list)
if v.Kind() == reflect.Slice || v.Kind() == reflect.Array {
for i := 0; i < v.Len(); i++ {
instance := v.Index(i).Interface()
switch t := instance.(type) {
case Equaler:
if t.Equals(elem) {
return true
}
default:
if instance == elem {
return true
}
}
}
}
return false
}
func Set(instance interface{}, value interface{}) {
v := reflect.ValueOf(instance)
if v.Kind() == reflect.Ptr {
v = v.Elem()
}
r := reflect.ValueOf(value)
if r.Kind() == reflect.Ptr {
r = r.Elem()
}
v.Set(r)
}
func Milliseconds() int64 {
return time.Now().UnixNano() / int64(1e6)
}
// UncapFirst returns the input string with the first letter in lower case
func UncapFirst(str string) string {
var s string
if len(str) > 0 {
s = string(unicode.ToLower(rune(str[0])))
}
if len(str) > 1 {
s += str[1:]
}
return s
}
// CapFirst returns the input string with the first letter in Upper case
func CapFirst(str string) string {
var s string
if len(str) > 0 {
s = string(unicode.ToUpper(rune(str[0])))
}
if len(str) > 1 {
s += str[1:]
}
return s
}
func ToString(v interface{}) string {
if t, isT := v.(string); isT {
return t
} else if t, isT := v.(fmt.Stringer); isT {
return t.String()
} else {
var isNil bool
var val reflect.Value
if v == nil {
isNil = true
} else {
val = reflect.ValueOf(v)
if val.Kind() == reflect.Ptr && val.IsNil() {
isNil = true
}
}
if isNil {
return "<nil>"
} else {
x := val.Interface()
if val.Kind() == reflect.Ptr {
x = val.Elem().Interface()
}
return fmt.Sprint(x)
}
}
}
// LazyString enable us to use a function in fmt.(S)Printf
// eg:
// fmt.Printf(
// "Hello %s",
// LazyString(func() string {
// return "world!"
// }),
// )
type LazyString func() string
func (ls LazyString) String() string {
return ls()
}
// LoadConfiguration configuration from a json file
// config is a pointer to a configuration variable
// file is the json file location
func LoadConfiguration(config interface{}, file string, mandatory bool) error {
if mandatory {
if _, err := os.Stat(file); os.IsNotExist(err) {
panic(err)
}
}
configFile, err := os.Open(file)
defer configFile.Close()
if err != nil {
fmt.Println(err.Error())
}
jsonParser := json.NewDecoder(configFile)
return jsonParser.Decode(config)
}