/
util.go
140 lines (112 loc) · 3.36 KB
/
util.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
package mongodb
import (
"context"
"fmt"
"strings"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"github.com/opensourceways/app-cla-server/util"
)
func genCorpID(email string) string {
return util.EmailSuffix(email)
}
func filterOfCorpID(email string) bson.M {
return bson.M{fieldCorpID: genCorpID(email)}
}
func isErrNoDocuments(err error) bool {
return err.Error() == mongo.ErrNoDocuments.Error()
}
func (this *client) isArrayElemNotExists(ctx context.Context, collection, array string, filterOfDoc, filterOfArray bson.M) (bool, error) {
query := bson.M{array: bson.M{"$elemMatch": filterOfArray}}
for k, v := range filterOfDoc {
query[k] = v
}
var v []struct {
ID primitive.ObjectID `bson:"_id"`
}
err := this.getDocs(ctx, collection, query, bson.M{"_id": 1}, &v)
if err != nil {
return false, err
}
return len(v) <= 0, nil
}
func (this *client) getArrayElem(ctx context.Context, collection, array string, filterOfDoc, filterOfArray, project bson.M, result interface{}) error {
ma := map[string]bson.M{}
if len(filterOfArray) > 0 {
ma[array] = filterOfArray
}
return this.getMultiArrays(ctx, collection, filterOfDoc, ma, project, result)
}
func (this *client) getMultiArrays(ctx context.Context, collection string, filterOfDoc bson.M, filterOfArrays map[string]bson.M, project bson.M, result interface{}) error {
pipeline := bson.A{bson.M{"$match": filterOfDoc}}
if len(filterOfArrays) > 0 {
project1 := bson.M{}
for array, filterOfArray := range filterOfArrays {
project1[array] = bson.M{"$filter": bson.M{
"input": fmt.Sprintf("$%s", array),
"cond": conditionTofilterArray(filterOfArray),
}}
}
for k, v := range project {
s := k
if i := strings.Index(k, "."); i >= 0 {
s = k[:i]
}
if _, ok := filterOfArrays[s]; !ok {
project1[k] = v
}
}
pipeline = append(pipeline, bson.M{"$project": project1})
}
if len(project) > 0 {
pipeline = append(pipeline, bson.M{"$project": project})
}
col := this.collection(collection)
cursor, err := col.Aggregate(ctx, pipeline)
if err != nil {
return err
}
return cursor.All(ctx, result)
}
func conditionTofilterArray(filterOfArray bson.M) bson.M {
cond := make(bson.A, 0, len(filterOfArray))
for k, v := range filterOfArray {
cond = append(cond, bson.M{"$eq": bson.A{"$$this." + k, v}})
}
if len(filterOfArray) == 1 {
return cond[0].(bson.M)
}
return bson.M{"$and": cond}
}
func arrayElemFilter(array string, filterOfArray bson.M) bson.M {
return bson.M{"$filter": bson.M{
"input": fmt.Sprintf("$%s", array),
"cond": conditionTofilterArray(filterOfArray),
}}
}
func (this *client) getDocs(ctx context.Context, collection string, filterOfDoc, project bson.M, result interface{}) error {
col := this.collection(collection)
var cursor *mongo.Cursor
var err error
if len(project) > 0 {
cursor, err = col.Find(ctx, filterOfDoc, &options.FindOptions{
Projection: project,
})
} else {
cursor, err = col.Find(ctx, filterOfDoc)
}
if err != nil {
return err
}
return cursor.All(ctx, result)
}
func (this *client) insertDoc(ctx context.Context, collection string, docInfo bson.M) (string, error) {
col := this.collection(collection)
r, err := col.InsertOne(ctx, docInfo)
if err != nil {
return "", err
}
return toUID(r.InsertedID)
}