-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgo-utils.go
185 lines (147 loc) · 3.93 KB
/
go-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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
utils are currently just a wrapper on top of github/segmentio's extremely fast
Camelcase and Snakecase functions, with an added PascalCase.
Thank you @tj for switching to Go just before we did! ;)
*/
package utils
import (
"errors"
"log"
"reflect"
"regexp"
"runtime"
"strconv"
"strings"
"github.com/segmentio/go-camelcase"
"github.com/segmentio/go-snakecase"
)
func Slug(str string) string {
return strings.Replace(snakecase.Snakecase(str), "_", "-", -1)
}
func UnCase(str string) string {
str = strings.Replace(snakecase.Snakecase(str), "_", " ", -1)
str = strings.ToUpper(str[0:1]) + str[1:]
return str
}
func SnakeCase(str string) string {
return snakecase.Snakecase(str)
}
func KebabCase(str string) string {
return strings.Replace(snakecase.Snakecase(str), "_", "-", -1)
}
func CamelCase(str string) string {
return camelcase.Camelcase(str)
}
func PascalCase(str string) string {
out := camelcase.Camelcase(str)
if len(out) > 0 {
out = strings.ToUpper(out[0:1]) + out[1:]
}
return out
}
func StringInSlice(searchStr string, strs []string) bool {
for _, str := range strs {
if searchStr == str {
return true
}
}
return false
}
func UniqueInts(arr []int) (unique []int) {
tmpMap := map[int]bool{}
for i := 0; i < len(arr); i++ {
tmpMap[arr[i]] = true
}
for val := range tmpMap {
unique = append(unique, val)
}
return
}
func UniqueStrings(arr []string) (unique []string) {
tmpMap := map[string]bool{}
for i := 0; i < len(arr); i++ {
tmpMap[arr[i]] = true
}
for val := range tmpMap {
unique = append(unique, val)
}
return
}
func Unique(arr interface{}) (unique interface{}, err error) {
arrType := reflect.TypeOf(arr)
arrValue := reflect.ValueOf(arr)
if arrType.Kind().String() != "slice" {
return nil, errors.New("Not a slice")
}
tmpMap := map[interface{}]bool{}
for i := 0; i < arrValue.Len(); i++ {
tmpMap[arrValue.Index(i).Interface()] = true
}
newArr := reflect.MakeSlice(arrType, 0, arrValue.Len())
for val := range tmpMap {
newArr = reflect.Append(newArr, reflect.ValueOf(val))
}
unique = newArr.Interface()
return
}
// InterfaceToReflect helps ensure the reflect value is in an editable state
// It will check the type and get the correct reference if possible
// TODO(morphar) Make some tests
func InterfaceToReflect(val interface{}) (reflectValue reflect.Value, err error) {
typ := reflect.TypeOf(val)
// TODO(morphar) Is this correct?
if typ.String() == "reflect.Value" {
reflectValue = val.(reflect.Value)
} else if typ.String()[0:1] != "*" {
err = errors.New("Please provide a reference to the value")
return
} else {
reflectValue = reflect.ValueOf(val).Elem()
}
return
}
var callerRE *regexp.Regexp
func init() {
// Matching e.g. (*ServiceName).ServiceMethod
callerRE = regexp.MustCompile("(?:\\(\\*{0,1}([^\\)]*?)\\)|([^\\.]+))\\.([^\\.]+)$")
}
func GetCallerName(skip int) (callerName string) {
pc, _, _, ok := runtime.Caller(skip)
if !ok {
return
}
pcFunc := runtime.FuncForPC(pc)
matches := callerRE.FindStringSubmatch(pcFunc.Name())
if matches == nil || len(matches) != 4 {
return
}
return matches[1] + matches[2] + "." + matches[3]
}
func GetCallerNames(skip int) (typeName, callerName string) {
pc, _, _, ok := runtime.Caller(skip)
if !ok {
return
}
pcFunc := runtime.FuncForPC(pc)
matches := callerRE.FindStringSubmatch(pcFunc.Name())
if matches == nil || len(matches) != 4 {
return
}
return matches[1] + matches[2], matches[3]
}
func GetCallStack() (stack []string) {
pcs := make([]uintptr, 50)
pcCount := runtime.Callers(2, pcs)
pathRE := regexp.MustCompile("^.*/")
for i := 0; i < pcCount; i++ {
pcFunc := runtime.FuncForPC(pcs[i])
file, line := pcFunc.FileLine(pcs[i])
fileName := pathRE.ReplaceAllString(file, "")
stack = append(stack, "["+fileName+":"+strconv.Itoa(line)+"]: "+pcFunc.Name())
}
return
}
func PrintCallStack() {
stack := GetCallStack()
log.Println("Call stack:\n", strings.Join(stack[1:len(stack)-1], "\n "))
}