-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeflater.go
62 lines (53 loc) · 1.36 KB
/
deflater.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
package gqldeduplicator
import (
"encoding/json"
"fmt"
)
// Deflate similar object in graphql response.
// Use deep first search (DFS) algorithm to walk over nodes and memoize object.
// If object appeared or memoized before, then it will deflated.
func Deflate(data []byte) ([]byte, error) {
var node interface{}
err := json.Unmarshal(data, &node)
if err != nil {
return nil, err
}
memoize := make(map[string]bool)
result := deflate(node, memoize, "root")
return json.Marshal(result)
}
func deflate(node interface{}, memoize map[string]bool, path string) interface{} {
switch value := node.(type) {
case []interface{}:
for i, v := range value {
switch v.(type) {
case []interface{}, map[string]interface{}:
value[i] = deflate(v, memoize, path)
default:
value[i] = v
}
}
return value
case map[string]interface{}:
if value != nil && value["id"] != nil && value["__typename"] != nil {
key := fmt.Sprintf("%s,%v,%v", path, value["__typename"], value["id"])
if memoize[key] {
return map[string]interface{}{
"id": value["id"],
"__typename": value["__typename"],
}
}
memoize[key] = true
}
for k, v := range value {
switch v.(type) {
case []interface{}, map[string]interface{}:
value[k] = deflate(v, memoize, path+","+k)
default:
value[k] = v
}
}
return value
}
return node
}