-
Notifications
You must be signed in to change notification settings - Fork 0
/
writer.go
70 lines (65 loc) · 1.6 KB
/
writer.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
package writer
import (
"context"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"reflect"
)
type Writer[T any] struct {
collection *mongo.Collection
idIndex int
Map func(T)
isPointer bool
}
func NewWriter[T any](database *mongo.Database, collectionName string, options ...func(T)) *Writer[T] {
var mp func(T)
if len(options) > 0 {
mp = options[0]
}
var t T
modelType := reflect.TypeOf(t)
isPointer := false
if modelType.Kind() == reflect.Ptr {
modelType = modelType.Elem()
isPointer = true
}
index := FindIdField(modelType)
collection := database.Collection(collectionName)
return &Writer[T]{collection: collection, idIndex: index, Map: mp, isPointer: isPointer}
}
func (w *Writer[T]) Write(ctx context.Context, model T) error {
if w.Map != nil {
w.Map(model)
}
vo := reflect.ValueOf(model)
if w.isPointer {
vo = reflect.Indirect(vo)
}
id := vo.Field(w.idIndex).Interface()
sid, ok := id.(string)
if ok && len(sid) == 0 || isNil(id) {
_, err := w.collection.InsertOne(ctx, model)
return err
}
return Upsert(ctx, w.collection, id, model)
}
func Upsert(ctx context.Context, collection *mongo.Collection, id interface{}, model interface{}) error {
filter := bson.M{"_id": id}
updateQuery := bson.M{
"$set": model,
}
opts := options.Update().SetUpsert(true)
_, err := collection.UpdateOne(ctx, filter, updateQuery, opts)
return err
}
func isNil(i interface{}) bool {
if i == nil {
return true
}
switch reflect.TypeOf(i).Kind() {
case reflect.Ptr:
return reflect.ValueOf(i).IsNil()
}
return false
}