-
Notifications
You must be signed in to change notification settings - Fork 8
/
util.go
155 lines (140 loc) · 3.54 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
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
package eventmaster
import (
"fmt"
"strings"
"time"
)
// Pair represents a single string key to empty interface value mapping.
type Pair struct {
a string
b interface{}
}
// stringify* used for mapping to cassandra inputs
func stringify(str string) string {
if str == "" {
return "null"
}
return fmt.Sprintf("'%s'", str)
}
func stringifyArr(arr []string) string {
if len(arr) == 0 {
return "null"
}
var newArr []string
for _, str := range arr {
newArr = append(newArr, stringify(str))
}
return fmt.Sprintf("{%s}", strings.Join(newArr, ","))
}
func stringifyUUID(str string) string {
if str == "" {
return "null"
}
return str
}
func getDataQueries(data map[string]interface{}) []Pair {
var pairs []Pair
for k, v := range data {
m, ok := v.(map[string]interface{})
if ok {
nextPairs := getDataQueries(m)
for _, pair := range nextPairs {
pairs = append(pairs, Pair{fmt.Sprintf("%s.%s", k, pair.a), pair.b})
}
} else {
pairs = append(pairs, Pair{k, v})
}
}
return pairs
}
func insertDefaults(schema map[string]interface{}, m map[string]interface{}) {
for k, v := range schema {
s, ok := v.(map[string]interface{})
if !ok {
continue
}
if d, ok := s["default"]; ok {
if _, ok = m[k]; !ok {
m[k] = d
}
} else if property, ok := m[k]; ok {
// property exists, check inner objects
if innerM, ok := property.(map[string]interface{}); ok {
innerProperties := s["properties"]
if innerSchema, ok := innerProperties.(map[string]interface{}); ok {
insertDefaults(innerSchema, innerM)
}
}
} else if properties, ok := schema["properties"]; ok {
// property doesn't exist, create it and check inner levels
innerProperties := properties.(map[string]interface{})
innerMap := make(map[string]interface{})
insertDefaults(innerProperties, innerMap)
if len(innerMap) != 0 {
m[k] = innerMap
}
}
}
}
func checkBackwardsCompatible(oldSchema map[string]interface{}, newSchema map[string]interface{}) bool {
oldProperties := oldSchema["properties"]
oldP, _ := oldProperties.(map[string]interface{})
oldRequired := oldSchema["required"]
oldR, _ := oldRequired.([]interface{})
newProperties := newSchema["properties"]
newP, _ := newProperties.(map[string]interface{})
newRequired := newSchema["required"]
newR, _ := newRequired.([]interface{})
// get diff of new required properties, check if those have defaults
for _, p := range newR {
exists := false
for _, oldP := range oldR {
if oldP == p {
exists = true
}
}
if !exists {
prop := p.(string)
property, ok := newP[prop]
if !ok {
return false
}
if typedProperty, ok := property.(map[string]interface{}); ok {
if _, ok := typedProperty["default"]; !ok {
return false
}
} else {
return false
}
}
}
// check backwards compatibility for all nested objects
for k, v := range newP {
if innerP, ok := v.(map[string]interface{}); ok {
oldInnerP, ok := oldP[k]
if !ok {
continue
}
typedOldInnerP := oldInnerP.(map[string]interface{})
if !checkBackwardsCompatible(typedOldInnerP, innerP) {
return false
}
}
}
return true
}
func parseKeyValuePair(content string) map[string]interface{} {
data := make(map[string]interface{})
pairs := strings.Split(content, " ")
for _, pair := range pairs {
parts := strings.Split(pair, "=")
if len(parts) > 1 {
data[parts[0]] = parts[1]
}
}
return data
}
func getDate(t int64) string {
eventTime := time.Unix(t, 0).UTC()
return fmt.Sprintf("%04d-%02d-%02d", eventTime.Year(), eventTime.Month(), eventTime.Day())
}