forked from infobloxopen/atlas-app-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utilities.go
156 lines (138 loc) · 3.92 KB
/
utilities.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
144
145
146
147
148
149
150
151
152
153
154
155
156
package gorm
import (
"context"
"database/sql/driver"
"fmt"
"reflect"
"strings"
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/protoc-gen-go/generator"
jgorm "github.com/jinzhu/gorm"
"github.com/jinzhu/inflection"
"time"
"github.com/infobloxopen/atlas-app-toolkit/rpc/resource"
)
// HandleFieldPath converts fieldPath to appropriate db string for use in where/order by clauses
// according to obj GORM model. If fieldPath cannot be found in obj then original fieldPath is returned
// to allow tables joined by a third party.
// If association join is required to resolve the field path then it's name is returned as a second return value.
func HandleFieldPath(ctx context.Context, fieldPath []string, obj interface{}) (string, string, error) {
if len(fieldPath) > 2 {
return "", "", fmt.Errorf("Field path longer than 2 is not supported")
}
dbPath, err := fieldPathToDBName(fieldPath, obj)
if err != nil {
switch err.(type) {
case *EmptyFieldPathError:
return "", "", err
default:
return strings.Join(fieldPath, "."), "", nil
}
}
if len(fieldPath) == 2 {
return dbPath, generator.CamelCase(fieldPath[0]), nil
}
return dbPath, "", nil
}
func fieldPathToDBName(fieldPath []string, obj interface{}) (string, error) {
objType := indirectType(reflect.TypeOf(obj))
pathLength := len(fieldPath)
assocAlias := ""
for i, part := range fieldPath {
if !isModel(objType) {
return "", fmt.Errorf("%s: non-last field of %s field path should be a model", objType, fieldPath)
}
sf, ok := objType.FieldByName(generator.CamelCase(part))
if !ok {
return "", fmt.Errorf("Cannot find field %s in %s", part, objType)
}
if i < pathLength-1 {
objType = indirectType(sf.Type)
assocAlias = part
} else {
if isModel(indirectType(sf.Type)) {
return "", fmt.Errorf("%s: last field of %s field path should be a model", objType, fieldPath)
}
var dbPrefix string
if assocAlias != "" {
dbPrefix = assocAlias
} else {
dbPrefix = tableName(objType)
}
return dbPrefix + "." + columnName(&sf), nil
}
}
return "", &EmptyFieldPathError{}
}
func tableName(t reflect.Type) string {
table := reflect.Zero(t).Interface()
if tn, ok := table.(tableNamer); ok {
return tn.TableName()
}
return inflection.Plural(jgorm.ToDBName(t.Name()))
}
func columnName(sf *reflect.StructField) string {
ex, tagCol := gormTag(sf, "column")
if ex {
return tagCol
}
return jgorm.ToDBName(sf.Name)
}
func gormTag(sf *reflect.StructField, tag string) (bool, string) {
return extractTag(sf, "gorm", tag)
}
func atlasTag(sf *reflect.StructField, tag string) (bool, string) {
return extractTag(sf, "atlas", tag)
}
func extractTag(sf *reflect.StructField, tag string, subTag string) (bool, string) {
gormTags := strings.Split(sf.Tag.Get(tag), ";")
for _, t := range gormTags {
var key, value string
keyValue := strings.Split(t, ":")
switch len(keyValue) {
case 2:
value = keyValue[1]
fallthrough
case 1:
key = keyValue[0]
}
if strings.ToLower(key) == strings.ToLower(subTag) {
return true, value
}
}
return false, ""
}
type tableNamer interface {
TableName() string
}
func indirectType(t reflect.Type) reflect.Type {
for {
switch t.Kind() {
case reflect.Ptr, reflect.Slice, reflect.Array:
t = t.Elem()
default:
return t
}
}
}
func isModel(t reflect.Type) bool {
kind := t.Kind()
_, isValuer := reflect.Zero(t).Interface().(driver.Valuer)
if (kind == reflect.Struct || kind == reflect.Slice) && !isValuer && t != reflect.TypeOf(time.Time{}) {
return true
}
return false
}
func isProtoMessage(t reflect.Type) bool {
_, isProtoMessage := reflect.Zero(t).Interface().(proto.Message)
return isProtoMessage
}
func isIdentifier(t reflect.Type) bool {
_, isIdentifier := reflect.Zero(t).Interface().(resource.Identifier)
return isIdentifier
}
type EmptyFieldPathError struct {
}
func (e *EmptyFieldPathError) Error() string {
return fmt.Sprintf("Empty field path is not allowed")
}