-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepositoryutils.go
94 lines (82 loc) · 2.11 KB
/
repositoryutils.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
package mongoutils
import (
"encoding/json"
"errors"
"fmt"
"reflect"
"go.mongodb.org/mongo-driver/mongo"
)
type countResult struct {
Count int64 `bson:"count" json:"count"`
}
type MongoOption struct {
IgnoreHooks bool
DebugPipe bool
DebugResult bool
Pipeline string
Params []any
Database *mongo.Database
}
// optionOf get option of dynamic params or return empty option
func optionOf(opts ...MongoOption) MongoOption {
opt := MongoOption{}
if len(opts) > 0 {
opt = opts[0]
}
if opt.Pipeline == "" {
opt.Pipeline = "Pipeline"
}
return opt
}
// parsePipeline get pipeline from CallMethod result or return nil
func parsePipeline(res []reflect.Value) MongoPipeline {
if len(res) > 0 {
if v, ok := res[0].Interface().(MongoPipeline); ok {
return v
}
}
return nil
}
// callMethod call object method dynamically
func callMethod(obj any, method string, params ...any) ([]reflect.Value, error) {
_type := reflect.TypeOf(obj)
for i := 0; i < _type.NumMethod(); i++ {
_method := _type.Method(i)
if method == _method.Name {
vals := make([]reflect.Value, 0)
vals = append(vals, reflect.ValueOf(obj))
for _, p := range params {
vals = append(vals, reflect.ValueOf(p))
}
return _method.Func.Call(vals), nil
}
}
return nil, errors.New("method " + method + " not defined!")
}
// prettyLog log data to output using json indent format
func prettyLog(data any) {
_bytes, _ := json.MarshalIndent(data, "", " ")
fmt.Println(string(_bytes))
}
func modelChecksum(v any) (string, Backup) {
if model, ok := parseAsInterface[Backup](v); ok && model != nil {
return NewChecksum(model.ToMap()).MD5(), model
}
return "", nil
}
// modelSafe convert v to github.com/gomig/mongoutils.Model or panic
func modelSafe[T any](v T) Model {
if _v, ok := any(v).(Model); !ok {
panic("T must implements github.com/gomig/mongoutils.Model")
} else {
return _v
}
}
// Get new instance of github.com/gomig/mongoutils.Model or panic if T not implement model
func typeModelSafe[T any]() Model {
return modelSafe(new(T))
}
func parseAsInterface[T any](v any) (T, bool) {
i, ok := v.(T)
return i, ok
}