/
convert.go
99 lines (92 loc) · 2.2 KB
/
convert.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
package mongo
import (
"github.com/bmeg/grip/gdbi"
"go.mongodb.org/mongo-driver/bson/primitive"
"google.golang.org/protobuf/types/known/structpb"
)
// PackVertex take a GRIP vertex and convert it to a mongo doc
func PackVertex(v *gdbi.Vertex) map[string]interface{} {
p := map[string]interface{}{}
if v.Data != nil {
p = v.Data
}
return map[string]interface{}{
"_id": v.ID,
"label": v.Label,
"data": p,
}
}
// PackEdge takes a GRIP edge and converts it to a mongo doc
func PackEdge(e *gdbi.Edge) map[string]interface{} {
p := map[string]interface{}{}
if e.Data != nil {
p = e.Data
}
return map[string]interface{}{
"_id": e.ID,
"from": e.From,
"to": e.To,
"label": e.Label,
"data": p,
}
}
type pair struct {
key string
valueMap interface{}
valueStruct *structpb.Struct
}
// UnpackVertex takes a mongo doc and converts it into an gripql.Vertex
func UnpackVertex(i map[string]interface{}) *gdbi.Vertex {
o := &gdbi.Vertex{}
o.ID = i["_id"].(string)
o.Label = i["label"].(string)
if d, ok := i["data"]; ok {
d = removePrimatives(d)
o.Data = d.(map[string]interface{})
o.Loaded = true
} else {
o.Loaded = false
o.Data = map[string]interface{}{}
}
return o
}
// UnpackEdge takes a mongo doc and convertes it into an gripql.Edge
func UnpackEdge(i map[string]interface{}) *gdbi.Edge {
o := &gdbi.Edge{}
id := i["_id"]
o.ID = id.(string)
o.Label = i["label"].(string)
o.From = i["from"].(string)
o.To = i["to"].(string)
if d, ok := i["data"]; ok {
o.Data = d.(map[string]interface{})
o.Loaded = true
} else {
o.Loaded = false
o.Data = map[string]interface{}{}
}
return o
}
// this is needed because protobuf doesn't recognize primitive.A
// may want to find another solution, rather then copying the
// entire data structure
func removePrimatives(i interface{}) interface{} {
if x, ok := i.(primitive.A); ok {
out := make([]interface{}, len(x))
for i := range x {
out[i] = removePrimatives(x[i])
}
return out
}
if x, ok := i.(primitive.ObjectID); ok {
return x.String()
}
if x, ok := i.(map[string]interface{}); ok {
out := make(map[string]interface{})
for i := range x {
out[i] = removePrimatives(x[i])
}
return out
}
return i
}