-
Notifications
You must be signed in to change notification settings - Fork 154
/
field.go
139 lines (127 loc) · 3.67 KB
/
field.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
/*
Copyright 2020 The Qmgo Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package field
import (
"context"
"reflect"
"time"
"github.com/qiniu/qmgo/operator"
)
var nilTime time.Time
// filedHandler defines the relations between field type and handler
var fieldHandler = map[operator.OpType]func(doc interface{}) error{
operator.BeforeInsert: beforeInsert,
operator.BeforeUpdate: beforeUpdate,
operator.BeforeReplace: beforeUpdate,
operator.BeforeUpsert: beforeUpsert,
}
//func init() {
// middleware.Register(Do)
//}
// Do call the specific method to handle field based on fType
// Don't use opts here
func Do(ctx context.Context, doc interface{}, opType operator.OpType, opts ...interface{}) error {
to := reflect.TypeOf(doc)
if to == nil {
return nil
}
switch reflect.TypeOf(doc).Kind() {
case reflect.Slice:
return sliceHandle(doc, opType)
case reflect.Ptr:
v := reflect.ValueOf(doc).Elem()
switch v.Kind() {
case reflect.Slice:
return sliceHandle(v.Interface(), opType)
default:
return do(doc, opType)
}
}
//fmt.Println("not support type")
return nil
}
// sliceHandle handles the slice docs
func sliceHandle(docs interface{}, opType operator.OpType) error {
// []interface{}{UserType{}...}
if h, ok := docs.([]interface{}); ok {
for _, v := range h {
if err := do(v, opType); err != nil {
return err
}
}
return nil
}
// []UserType{}
s := reflect.ValueOf(docs)
for i := 0; i < s.Len(); i++ {
if err := do(s.Index(i).Interface(), opType); err != nil {
return err
}
}
return nil
}
// beforeInsert handles field before insert
// If value of field createAt is valid in doc, upsert doesn't change it
// If value of field id is valid in doc, upsert doesn't change it
// Change the value of field updateAt anyway
func beforeInsert(doc interface{}) error {
if ih, ok := doc.(DefaultFieldHook); ok {
ih.DefaultId()
ih.DefaultCreateAt()
ih.DefaultUpdateAt()
}
if ih, ok := doc.(CustomFieldsHook); ok {
fields := ih.CustomFields()
fields.(*CustomFields).CustomId(doc)
fields.(*CustomFields).CustomCreateTime(doc)
fields.(*CustomFields).CustomUpdateTime(doc)
}
return nil
}
// beforeUpdate handles field before update
func beforeUpdate(doc interface{}) error {
if ih, ok := doc.(DefaultFieldHook); ok {
ih.DefaultUpdateAt()
}
if ih, ok := doc.(CustomFieldsHook); ok {
fields := ih.CustomFields()
fields.(*CustomFields).CustomUpdateTime(doc)
}
return nil
}
// beforeUpsert handles field before upsert
// If value of field createAt is valid in doc, upsert doesn't change it
// If value of field id is valid in doc, upsert doesn't change it
// Change the value of field updateAt anyway
func beforeUpsert(doc interface{}) error {
if ih, ok := doc.(DefaultFieldHook); ok {
ih.DefaultId()
ih.DefaultCreateAt()
ih.DefaultUpdateAt()
}
if ih, ok := doc.(CustomFieldsHook); ok {
fields := ih.CustomFields()
fields.(*CustomFields).CustomId(doc)
fields.(*CustomFields).CustomCreateTime(doc)
fields.(*CustomFields).CustomUpdateTime(doc)
}
return nil
}
// do check if opType is supported and call fieldHandler
func do(doc interface{}, opType operator.OpType) error {
if f, ok := fieldHandler[opType]; !ok {
return nil
} else {
return f(doc)
}
}