forked from infobloxopen/atlas-app-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fields.go
143 lines (134 loc) · 3.84 KB
/
fields.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
140
141
142
143
package gorm
import (
"context"
"fmt"
"reflect"
"sort"
"strings"
"github.com/golang/protobuf/protoc-gen-go/generator"
"github.com/jinzhu/gorm"
"github.com/infobloxopen/atlas-app-toolkit/query"
)
// FieldSelectionStringToGorm is a shortcut to parse a string into FieldSelection struct and
// receive a list of associations to preload.
func FieldSelectionStringToGorm(ctx context.Context, fs string, obj interface{}) ([]string, error) {
return FieldSelectionToGorm(ctx, query.ParseFieldSelection(fs), obj)
}
// FieldSelectionToGorm receives FieldSelection struct and returns a list of associations to preload.
func FieldSelectionToGorm(ctx context.Context, fs *query.FieldSelection, obj interface{}) ([]string, error) {
objType := indirectType(reflect.TypeOf(obj))
if fs.GetFields() == nil {
return preloadEverything(objType, nil)
}
var toPreload []string
fieldNames := getSortedFieldNames(fs.GetFields())
for _, fieldName := range fieldNames {
f := fs.GetFields()[fieldName]
subPreload, err := handlePreloads(f, objType)
if err != nil {
return nil, err
}
toPreload = append(toPreload, subPreload...)
}
return toPreload, nil
}
func preloadEverything(objType reflect.Type, path []reflect.Type) ([]string, error) {
if !isModel(objType) {
return nil, fmt.Errorf("%s is not a model", objType)
}
numField := objType.NumField()
var toPreload []string
fields:
for i := 0; i < numField; i++ {
sf := objType.Field(i)
fType := indirectType(sf.Type)
for _, e := range path {
if fType == e {
continue fields
}
}
if isModel(fType) {
if ok, flag := gormTag(&sf, "preload"); ok && flag == "false" {
continue
}
subPreload, err := preloadEverything(fType, append(path, objType))
if err != nil {
return nil, err
}
for i, e := range subPreload {
subPreload[i] = sf.Name + "." + e
}
toPreload = append(toPreload, subPreload...)
toPreload = append(toPreload, sf.Name)
}
}
return toPreload, nil
}
func handlePreloads(f *query.Field, objType reflect.Type) ([]string, error) {
sf, ok := objType.FieldByName(generator.CamelCase(f.GetName()))
if !ok {
return nil, nil
}
fType := indirectType(sf.Type)
if f.GetSubs() == nil {
if isModel(fType) {
return []string{generator.CamelCase(f.GetName())}, nil
} else {
return nil, nil
}
}
if !isModel(fType) {
return nil, fmt.Errorf("%s is expected to be a model, but got %s ", f.GetName(), fType)
}
var toPreload []string
fieldNames := getSortedFieldNames(f.GetSubs())
for _, fieldName := range fieldNames {
subField := f.GetSubs()[fieldName]
subPreload, err := handlePreloads(subField, fType)
if err != nil {
return nil, err
}
for i, e := range subPreload {
subPreload[i] = generator.CamelCase(f.GetName()) + "." + e
}
toPreload = append(toPreload, subPreload...)
}
return append(toPreload, generator.CamelCase(f.GetName())), nil
}
func getSortedFieldNames(fields map[string]*query.Field) []string {
var keys []string
for k := range fields {
keys = append(keys, k)
}
sort.Strings(keys)
return keys
}
func preload(db *gorm.DB, obj interface{}, assoc string) (*gorm.DB, error) {
objType := indirectType(reflect.TypeOf(obj))
if !isModel(objType) {
return nil, fmt.Errorf("%s is not a model", objType)
}
assocPath := strings.Split(assoc, ".")
pathLength := len(assocPath)
for i, part := range assocPath {
sf, ok := objType.FieldByName(part)
if !ok {
return nil, fmt.Errorf("Cannot find %s in %s", part, objType)
}
objType = indirectType(sf.Type)
if !isModel(objType) {
return nil, fmt.Errorf("%s is not a model", objType)
}
if i == pathLength-1 {
ok, pos := atlasTag(&sf, "position")
if !ok {
return db.Preload(assoc), nil
} else {
return db.Preload(assoc, func(db *gorm.DB) *gorm.DB {
return db.Order(gorm.ToDBName(pos))
}), nil
}
}
}
return nil, fmt.Errorf("Cannot preload empty association")
}