-
Notifications
You must be signed in to change notification settings - Fork 551
/
formatter.go
181 lines (165 loc) · 4.28 KB
/
formatter.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
package proxy
import "strings"
// EntityFormatter formats the response data
type EntityFormatter interface {
Format(entity Response) Response
}
// EntityFormatterFunc holds the formatter function
type EntityFormatterFunc struct {
Func func(Response) Response
}
// Format implements the EntityFormatter interface
func (e EntityFormatterFunc) Format(entity Response) Response {
return e.Func(entity)
}
type propertyFilter func(entity *Response)
type entityFormatter struct {
Target string
Prefix string
PropertyFilter propertyFilter
Mapping map[string]string
}
// NewEntityFormatter creates an entity formatter with the received params
func NewEntityFormatter(target string, whitelist, blacklist []string, group string, mappings map[string]string) EntityFormatter {
var propertyFilter propertyFilter
if len(whitelist) > 0 {
propertyFilter = newWhitelistingFilter(whitelist)
} else {
propertyFilter = newBlacklistingFilter(blacklist)
}
sanitizedMappings := make(map[string]string, len(mappings))
for i, m := range mappings {
v := strings.Split(m, ".")
sanitizedMappings[i] = v[0]
}
return entityFormatter{
Target: target,
Prefix: group,
PropertyFilter: propertyFilter,
Mapping: sanitizedMappings,
}
}
// Format implements the EntityFormatter interface
func (e entityFormatter) Format(entity Response) Response {
if e.Target != "" {
extractTarget(e.Target, &entity)
}
if len(entity.Data) > 0 {
e.PropertyFilter(&entity)
}
if len(entity.Data) > 0 {
for formerKey, newKey := range e.Mapping {
if v, ok := entity.Data[formerKey]; ok {
entity.Data[newKey] = v
delete(entity.Data, formerKey)
}
}
}
if e.Prefix != "" {
entity.Data = map[string]interface{}{e.Prefix: entity.Data}
}
return entity
}
func extractTarget(target string, entity *Response) {
if tmp, ok := entity.Data[target]; ok {
entity.Data, ok = tmp.(map[string]interface{})
if !ok {
entity.Data = map[string]interface{}{}
}
} else {
entity.Data = map[string]interface{}{}
}
}
func whitelistPrune(wlDict map[string]interface{}, inDict map[string]interface{}) bool {
canDelete := true
var deleteSibling bool
for k, v := range inDict {
deleteSibling = true
if subWl, ok := wlDict[k]; ok {
if subWlDict, okk := subWl.(map[string]interface{}); okk {
if subInDict, isDict := v.(map[string]interface{}); isDict && !whitelistPrune(subWlDict, subInDict) {
deleteSibling = false
}
} else {
// whitelist leaf, maintain this branch
deleteSibling = false
}
}
if deleteSibling {
delete(inDict, k)
} else {
canDelete = false
}
}
return canDelete
}
func newWhitelistingFilter(whitelist []string) propertyFilter {
wlDict := make(map[string]interface{})
for _, k := range whitelist {
wlFields := strings.Split(k, ".")
d := buildDictPath(wlDict, wlFields[:len(wlFields)-1])
d[wlFields[len(wlFields)-1]] = true
}
return func(entity *Response) {
if whitelistPrune(wlDict, entity.Data) {
for k := range entity.Data {
delete(entity.Data, k)
}
}
}
}
func buildDictPath(accumulator map[string]interface{}, fields []string) map[string]interface{} {
var ok bool = true
var c map[string]interface{}
var fIdx int
fEnd := len(fields)
p := accumulator
for fIdx = 0; fIdx < fEnd; fIdx++ {
if c, ok = p[fields[fIdx]].(map[string]interface{}); !ok {
break
}
p = c
}
for ; fIdx < fEnd; fIdx++ {
c = make(map[string]interface{})
p[fields[fIdx]] = c
p = c
}
return p
}
func newBlacklistingFilter(blacklist []string) propertyFilter {
bl := make(map[string][]string, len(blacklist))
for _, key := range blacklist {
keys := strings.Split(key, ".")
if len(keys) > 1 {
if sub, ok := bl[keys[0]]; ok {
bl[keys[0]] = append(sub, keys[1])
} else {
bl[keys[0]] = []string{keys[1]}
}
} else {
bl[keys[0]] = []string{}
}
}
return func(entity *Response) {
for k, sub := range bl {
if len(sub) == 0 {
delete(entity.Data, k)
} else {
if tmp := blacklistFilterSub(entity.Data[k], sub); len(tmp) > 0 {
entity.Data[k] = tmp
}
}
}
}
}
func blacklistFilterSub(v interface{}, blacklist []string) map[string]interface{} {
tmp, ok := v.(map[string]interface{})
if !ok {
return map[string]interface{}{}
}
for _, key := range blacklist {
delete(tmp, key)
}
return tmp
}