-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathTransformer.go
87 lines (78 loc) · 2.38 KB
/
Transformer.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
package main
import (
"fmt"
)
//AppDetails stores the application details, for every userId
type AppDetails map[string]UserDetails
//UserDetails stores the device details of this user, for every deviceId
type UserDetails map[string]DeviceDetails
//DeviceDetails stores the timeUsed on this device, for every location
type DeviceDetails map[string]int
func main() {
var ad AppDetails = AppDetails{}
var ud UserDetails = UserDetails{}
var androidDetails DeviceDetails = DeviceDetails{}
androidDetails["Hyderabad"] = 20
androidDetails["Mumbai"] = 12
androidDetails["Chennai"] = 35
var macDetails DeviceDetails = DeviceDetails{}
macDetails["Delhi"] = 30
macDetails["Mumbai"] = 10
macDetails["Hyderabad"] = 17
ud["android"] = androidDetails
ud["mac"] = macDetails
ad["gaurav.sen"] = ud
fmt.Println(Transform(&ad))
unknownDeviceDetails := DeviceDetails{}
unknownDeviceDetails["Hyderabad"] = 90
unknownDeviceDetails[""] = 12
unknownUserDetails := UserDetails{}
unknownUserDetails[""] = unknownDeviceDetails
ad[""] = unknownUserDetails
param := make(map[string]interface{})
for k, v := range ad {
param[k] = v
}
fmt.Println(GraphSearch(param, 0))
}
//Transform populates a map iteratively
func Transform(co *AppDetails) map[string]map[string]map[string]int {
result := make(map[string]map[string]map[string]int)
for username, userDetails := range *co {
if username == "" {
username = "John Doe"
}
result[username] = make(map[string]map[string]int)
for device, deviceDetails := range userDetails {
if device == "" {
device = "Unknown"
}
result[username][device] = make(map[string]int)
for location, timeUsed := range deviceDetails {
if location == "" {
location = "Unknown"
}
if timeUsed > 10 {
result[username][device][location] = timeUsed
}
}
}
}
return result
}
var keys = []string{"username", "device", "location"}
// GraphSearch populates a map recursively
func GraphSearch(dataSlice map[string]interface{}, level int) map[string]interface{} {
result := make(map[string]interface{})
if level == len(keys)-1 {
for baseAttributeKey, baseAttributeValue := range dataSlice {
result[baseAttributeKey], _ = baseAttributeValue.(int)
}
} else {
for attributeKey, details := range dataSlice {
attributeDetails := details.(map[string]interface{})
result[attributeKey] = GraphSearch(attributeDetails, level+1)
}
}
return result
}