-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdater.go
61 lines (55 loc) · 1.51 KB
/
updater.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
package dynamodb
import (
"context"
"github.com/aws/aws-sdk-go/service/dynamodb"
"reflect"
"strings"
)
type Updater struct {
DB *dynamodb.DynamoDB
tableName string
keys []string
Map func(ctx context.Context, model interface{}) (interface{}, error)
}
func NewUpdater(database *dynamodb.DynamoDB, tableName string, keys []string, options ...func(context.Context, interface{}) (interface{}, error)) *Updater {
var mp func(context.Context, interface{}) (interface{}, error)
if len(options) >= 1 {
mp = options[0]
}
return &Updater{DB: database, tableName: tableName, keys: keys, Map: mp}
}
func (w *Updater) Write(ctx context.Context, model interface{}) error {
var modelNew interface{}
var err error
if w.Map != nil {
modelNew, err = w.Map(ctx, model)
if err != nil {
return err
}
} else {
modelNew = model
}
_ ,err = UpdateOne(ctx, w.DB, w.tableName, w.keys, modelNew)
return err
}
func FindIdField(modelType reflect.Type) (int, string, string) {
return findBsonField(modelType, "_id")
}
func findBsonField(modelType reflect.Type, bsonName string) (int, string, string) {
numField := modelType.NumField()
for i := 0; i < numField; i++ {
field := modelType.Field(i)
bsonTag := field.Tag.Get("bson")
tags := strings.Split(bsonTag, ",")
json := field.Name
if tag1, ok1 := field.Tag.Lookup("json"); ok1 {
json = strings.Split(tag1, ",")[0]
}
for _, tag := range tags {
if strings.TrimSpace(tag) == bsonName {
return i, field.Name, json
}
}
}
return -1, "", ""
}