-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
51 lines (49 loc) · 1.17 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
package mo
import (
xerr "github.com/goclub/error"
"go.mongodb.org/mongo-driver/bson"
"reflect"
)
func ProjectByStructTag(v interface{}) bson.M {
data := bson.M{}
fields := scanProject(v)
for _, field := range fields {
data[field] = 1
}
return data
}
func autoScanProject(projection *interface{}, v interface{}) {
if _, ok := v.(Document); ok == false {
if *projection == nil {
*projection = ProjectByStructTag(v)
}
}
}
func scanProject(v interface{}) (fields []string) {
rValue := reflect.ValueOf(v)
rValue = reflect.Indirect(rValue)
rType := rValue.Type()
tier := 0
coreScanProject(rValue, rType, &fields, &tier)
return
}
func coreScanProject(rValue reflect.Value, rType reflect.Type, fields *[]string, tier *int) {
if *tier > 10 {
panic(xerr.New("goclub/sql: Too many structures are nested"))
}
for i:=0;i<rType.NumField();i++ {
structField := rType.Field(i)
tag, has := structField.Tag.Lookup("bson")
if !has {
if structField.Type.Kind() == reflect.Struct {
fieldValue := rValue.Field(i)
coreScanProject(fieldValue, structField.Type, fields, tier)
// fieldValue
}
continue
}
if tag != "" {
*fields = append(*fields, tag)
}
}
}