This repository has been archived by the owner on Jun 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
util.go
93 lines (85 loc) · 1.75 KB
/
util.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
package loggable
import (
"encoding/json"
"reflect"
"strings"
"unicode"
)
func isEqual(item1, item2 interface{}, except ...string) bool {
except = StringMap(except, ToSnakeCase)
m1, m2 := somethingToMapStringInterface(item1), somethingToMapStringInterface(item2)
if len(m1) != len(m2) {
return false
}
for k, v := range m1 {
if isInStringSlice(ToSnakeCase(k), except) {
continue
}
v2, ok := m2[k]
if !ok || !reflect.DeepEqual(v, v2) {
return false
}
}
return true
}
func somethingToMapStringInterface(item interface{}) map[string]interface{} {
if item == nil {
return nil
}
switch raw := item.(type) {
case JSONB:
return somethingToMapStringInterface([]byte(raw))
case string:
return somethingToMapStringInterface([]byte(raw))
case []byte:
var m map[string]interface{}
err := json.Unmarshal(raw, &m)
if err != nil {
return nil
}
return m
default:
data, err := json.Marshal(item)
if err != nil {
return nil
}
return somethingToMapStringInterface(data)
}
return nil
}
var ToSnakeCase = toSomeCase("_")
func toSomeCase(sep string) func(string) string {
return func(s string) string {
for i := range s {
if unicode.IsUpper(rune(s[i])) {
if i != 0 {
s = strings.Join([]string{s[:i], ToLowerFirst(s[i:])}, sep)
} else {
s = ToLowerFirst(s)
}
}
}
return s
}
}
func ToLowerFirst(s string) string {
if len(s) == 0 {
return ""
}
return strings.ToLower(string(s[0])) + s[1:]
}
func StringMap(strs []string, fn func(string) string) []string {
res := make([]string, len(strs))
for i := range strs {
res[i] = fn(strs[i])
}
return res
}
func isInStringSlice(what string, where []string) bool {
for i := range where {
if what == where[i] {
return true
}
}
return false
}