-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.go
271 lines (242 loc) · 6.17 KB
/
functions.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package restify
import (
"bytes"
"encoding/json"
"fmt"
"github.com/getevo/evo/v2/lib/db"
"path/filepath"
"reflect"
"strings"
)
func SetPrefix(prefix string) {
Prefix = prefix
}
func SetDefaultPermissionHandler(handler func(permissions Permissions, context *Context) bool) {
permissionHandler = handler
}
func UseModel(model any) *Resource {
var features = GetFeatures(model)
ref := reflect.ValueOf(model)
for ref.Kind() == reflect.Ptr {
ref = ref.Elem()
}
model = ref.Interface()
typ := reflect.TypeOf(model)
stmt := db.Model(model).Statement
_ = stmt.Parse(model)
var resource = Resource{
PrimaryFieldDBNames: stmt.Schema.PrimaryFieldDBNames,
Feature: features,
Schema: stmt.Schema,
Table: stmt.Table,
Ref: ref,
Instance: model,
Type: typ,
Name: filepath.Base(ref.Type().PkgPath()) + "." + typ.Name(),
}
if !features.API {
return &resource
}
resource.PostmanGroup = collection.CreateFolder(stmt.Schema.Name, stmt.Schema.Name+" API List")
var handler = Handler{}
resource.SetAction(&Endpoint{
Name: "MODEL INFO",
Method: MethodGET,
URL: "/",
Handler: handler.ModelInfo,
Description: "return information of the model",
})
if !features.DisableSet {
resource.SetAction(&Endpoint{
Name: "SET",
Method: MethodPOST,
URL: "/set",
PKUrl: false,
Handler: handler.Set,
AcceptData: true,
Batch: true,
Filterable: true,
Description: "set objects in database",
})
}
if !features.DisableAggregate {
resource.SetAction(&Endpoint{
Name: "AGGREGATE",
Method: MethodGET,
URL: "/aggregate",
PKUrl: false,
Handler: handler.Aggregate,
AcceptData: false,
Batch: false,
Filterable: true,
Description: "generate aggregate data",
})
}
if !features.DisableList {
resource.SetAction(&Endpoint{
Name: "ALL",
Method: MethodGET,
URL: "/all",
Handler: handler.All,
Filterable: true,
Description: "return all objects in one call",
})
resource.SetAction(&Endpoint{
Name: "PAGINATE",
Method: MethodGET,
URL: "/paginate",
Handler: handler.Paginate,
Filterable: true,
Pagination: true,
Description: "paginate objects",
})
resource.SetAction(&Endpoint{
Name: "GET",
Method: MethodGET,
URL: "/",
PKUrl: true,
Handler: handler.Get,
Filterable: true,
Description: "get single object using primary key",
})
}
if !features.DisableCreate {
resource.SetAction(&Endpoint{
Name: "CREATE",
Method: MethodPUT,
URL: "/",
Handler: handler.Create,
AcceptData: true,
Description: "create an object using given values",
})
resource.SetAction(&Endpoint{
Name: "BATCH.CREATE",
Method: MethodPUT,
URL: "/batch",
PKUrl: false,
Handler: handler.BatchCreate,
AcceptData: true,
Batch: true,
Description: "create a batch of objects",
})
}
if !features.DisableUpdate {
resource.SetAction(&Endpoint{
Name: "BATCH.UPDATE",
Method: MethodPatch,
URL: "/batch",
PKUrl: false,
Handler: handler.BatchUpdate,
Filterable: true,
Batch: true,
Description: "update batch objects",
})
resource.SetAction(&Endpoint{
Name: "UPDATE",
Method: MethodPatch,
URL: "/",
PKUrl: true,
Handler: handler.Update,
AcceptData: true,
Description: "update single object select using primary key",
})
}
if !features.DisableDelete {
resource.SetAction(&Endpoint{
Name: "BATCH.DELETE",
Method: MethodDELETE,
URL: "/batch",
PKUrl: false,
Handler: handler.BatchDelete,
Filterable: true,
Description: "batch delete objects",
})
resource.SetAction(&Endpoint{
Name: "DELETE",
Method: MethodDELETE,
URL: "/",
PKUrl: true,
Handler: handler.Delete,
Description: "delete existing object using primary key",
})
}
resources[resource.Table] = &resource
return &resource
}
var _true = reflect.ValueOf(true)
func GetFeatures(v interface{}) Feature {
var features = Feature{}
var inputType = reflect.ValueOf(v)
for inputType.Kind() == reflect.Ptr {
inputType = inputType.Elem()
}
var featureValue = reflect.ValueOf(&features).Elem()
var featureType = reflect.TypeOf(features)
for i := 0; i < featureValue.NumField(); i++ {
for j := 0; j < inputType.NumField(); j++ {
t := inputType.Field(j).Type().String()
var chunks = strings.Split(t, ".")
if len(chunks) == 2 && chunks[1] == featureType.Field(i).Name {
featureValue.Field(i).Set(_true)
break
}
}
}
return features
}
func equal(val1, val2 reflect.Value) bool {
// Ensure both values are structs or pointers to structs
for val1.Kind() == reflect.Ptr {
val1 = val1.Elem()
}
for val2.Kind() == reflect.Ptr {
val2 = val2.Elem()
}
// Both values must be structs
if val1.Kind() != reflect.Struct || val2.Kind() != reflect.Struct {
return false
}
// Iterate through fields of the first struct
for i := 0; i < val1.NumField(); i++ {
field1 := val1.Field(i)
field2 := val2.Field(i)
// Skip zero fields
if field2.IsZero() || field1.IsZero() {
continue
}
// Dereference pointers if applicable
if field1.Kind() == reflect.Ptr {
if field1.IsNil() || field2.IsNil() {
if field1.IsNil() != field2.IsNil() {
return false
}
continue // Both are nil, consider them equal
}
field1 = field1.Elem()
}
for field2.Kind() == reflect.Ptr {
field2 = field2.Elem()
}
for field1.Kind() == reflect.Ptr {
field1 = field1.Elem()
}
// Skip struct fields
if field1.Kind() == reflect.Struct {
continue
}
// Compare non-struct fields
if fmt.Sprint(field2.Interface()) != fmt.Sprint(field1.Interface()) {
return false
}
}
return true
}
func PrettyJson(v interface{}) string {
var out bytes.Buffer
enc := json.NewEncoder(&out)
enc.SetIndent("", " ")
if err := enc.Encode(v); err != nil {
return "{}"
}
return out.String()
}