-
Notifications
You must be signed in to change notification settings - Fork 1
/
avoSchemaParser.go
81 lines (74 loc) · 1.88 KB
/
avoSchemaParser.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
package avoinspector
import (
"strconv"
)
// Property represents a schema of a single event property
type Property struct {
PropertyName string `json:"propertyName"`
PropertyType string `json:"propertyType"`
Children []Property `json:"children,omitempty"`
}
// extractSchema extracts the schema of event properties
func extractSchema(eventProperties map[string]interface{}) []Property {
var result []Property
for key, value := range eventProperties {
result = append(result, struct {
PropertyName string `json:"propertyName"`
PropertyType string `json:"propertyType"`
Children []Property `json:"children,omitempty"`
}{
PropertyName: key,
PropertyType: getType(value),
Children: getChildren(value),
})
}
return result
}
// getType returns the type of a value
func getType(value interface{}) string {
switch value.(type) {
case string:
return "string"
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
return "int"
case float32, float64:
return "float"
case bool:
return "bool"
case nil:
return "null"
case []interface{}:
return "list"
case map[string]interface{}:
return "object"
default:
return "unknown"
}
}
// getChildren returns the children of a value if it's an object or a list
func getChildren(value interface{}) []Property {
switch v := value.(type) {
case []interface{}:
var children []Property
for index, element := range v {
children = append(children, Property{
PropertyName: strconv.Itoa(index),
PropertyType: getType(element),
Children: getChildren(element),
})
}
return children
case map[string]interface{}:
var children []Property
for key, element := range v {
children = append(children, Property{
PropertyName: key,
PropertyType: getType(element),
Children: getChildren(element),
})
}
return children
default:
return nil
}
}