-
Notifications
You must be signed in to change notification settings - Fork 0
/
markers.go
372 lines (296 loc) · 11.8 KB
/
markers.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
package shelf
import (
"errors"
"fmt"
"strings"
)
const (
MarkerEntity = "shelf:entity"
MarkerTable = "shelf:table"
MarkerId = "shelf:id"
MarkerGeneratedValue = "shelf:generated-value"
MarkerColumn = "shelf:column"
MarkerTransient = "shelf:transient"
MarkerLob = "shelf:lob"
MarkerEnumerated = "shelf:enumerated"
MarkerRepository = "shelf:repository"
MarkerQuery = "shelf:query"
MarkerEmbeddable = "shelf:embeddable"
MarkerEmbedded = "shelf:embedded"
MarkerAttributeOverride = "shelf:attribute-override"
MarkerMapsId = "shelf:maps-id"
MarkerOneToOne = "shelf:one-to-one"
MarkerOneToMany = "shelf:one-to-many"
MarkerManyToOne = "shelf:many-to-one"
MarkerManyToMany = "shelf:many-to-many"
MarkerTemporal = "shelf:temporal"
)
// +marker="shelf:entity", UseValueSyntax=true, Description="Specifies that the class is an entity."
type EntityMarker struct {
// +marker:argument="Value", Optional=true, Description="The entity name."
Name string `marker:"Value,useValueSyntax,optional"`
}
// +marker="shelf:table", UseValueSyntax=true, Description="Specifies the primary table for the marked entity."
type TableMarker struct {
// +marker:argument="Value", Optional=true, Description="The name of the table."
Name string `marker:"Value,useValueSyntax,optional"`
}
// +marker="shelf:id", Description="Specifies the primary key of an entity."
type IdMarker struct{}
// +marker="shelf:generated-value", Description="Provides for the specification of generation strategies \
// for the values of primary keys."
type GeneratedValueMarker struct{}
// +marker="shelf:column", UseValueSyntax=true, Description="Specifies the mapped column for a persistent field."
type ColumnMarker struct {
// +marker:argument="Name", Optional=true, Description="The name of the column."
Name string `marker:"Value,useValueSyntax,optional"`
// +marker:argument="Unique", Optional=true, Description="Whether the column is a unique key."
Unique bool `marker:"Unique,optional"`
// +marker:argument="Length", Optional=true, Description="The column length."
Length int `marker:"Length,optional"`
}
// +marker="shelf:id", Description="Specifies the primary key of an entity."
type TransientMarker struct{}
// +marker="shelf-lob", Description="Specifies that a persistent field should be persisted as a large object \
// to a database-supported large object type.
type LobMarker struct{}
// +marker="shelf:enumerated", UseValueSyntax=true, \
// Description="Specifies that a persistent field should be persisted as a enumerated type."
type EnumeratedMarker struct {
// +marker:argument="Value", \
// Options={STRING, ORDINAL}, \
// Description="The type used in mapping an enum type."
Value string `marker:"Value,useValueSyntax"`
}
func (e EnumeratedMarker) Validate() error {
matched := false
enumeratedOptions := []string{"STRING", "ORDINAL"}
for _, option := range enumeratedOptions {
if strings.TrimSpace(e.Value) == option {
matched = true
}
}
if !matched {
return fmt.Errorf("invalid Enumerated option. Here is the list of valid options %s", strings.Join(enumeratedOptions, ", "))
}
return nil
}
// +marker="shelf:repository", UseValueSyntax=true, Description="Specifies a repository."
type RepositoryMarker struct {
// +marker:argument="Value", Description="The repository name."
Name string `marker:"Value,useValueSyntax"`
// +marker:argument="EntityStruct", Description="The entity name"
Entity string `marker:"Entity"`
}
func (r RepositoryMarker) Validate() error {
if strings.TrimSpace(r.Name) == "" {
return errors.New("'Value' cannot be empty or nil")
}
if strings.TrimSpace(r.Entity) == "" {
return errors.New("'Entity' cannot be empty or nil")
}
return nil
}
// +marker="shelf:query", UseValueSyntax=true, Description="Specifies a query."
type QueryMarker struct {
// +marker:argument="Value", Description="The query string."
Value string `marker:"Value,useValueSyntax"`
// +marker:argument="Unique", Optional=true, Description="Whether the query is a native queery."
NativeQuery bool `marker:"NativeQuery,optional"`
}
func (q QueryMarker) Validate() error {
if strings.TrimSpace(q.Value) == "" {
return errors.New("'Value' cannot be empty or nil")
}
return nil
}
// +marker="shelf:embeddable", Description="Specifies that a struct will be embedded by other entities."
type EmbeddableMarker struct{}
// +marker="shelf:embedded", Description="Specifies that an entity embed a struct"
type EmbeddedMarker struct{}
// +marker="shelf:attribute-override", Description="Specifies that the column property of embedded type will be overridden."
type AttributeOverrideMarker struct {
// +marker:argument="Value", Description=" The association override mappings that are to be applied to the relationship field."
Name string `marker:"Value,useValueSyntax"`
// +marker:argument="ColumnName", Optional=true, Description="The name of the column."
ColumnName string `marker:"ColumnName"`
// +marker:argument="ColumnUnique", Optional=true, Description="Whether the column is a unique key."
ColumnUnique bool `marker:"ColumnUnique,optional"`
// +marker:argument="ColumnLength", Optional=true, Description="The column length."
ColumnLength int `marker:"ColumnLength,optional"`
}
func (a AttributeOverrideMarker) Validate() error {
if strings.TrimSpace(a.Name) == "" {
return errors.New("'Value' cannot be empty or nil")
}
if strings.TrimSpace(a.ColumnName) == "" {
return errors.New("'ColumnName' cannot be empty or nil")
}
return nil
}
// +marker="shelf:maps-id", Description=""
type MapsIdMarker struct{}
// +marker="shelf:one-to-one", Description="Specifies a single-valued association to another entity."
type OneToOneMarker struct {
// +marker:argument="Cascade", Description="The operations that must be cascaded to the target of the association."
Cascade []string `marker:"Cascade,optional"`
// +marker:argument="FetchType", Description="Whether the association should be lazily loaded or must be eagerly fetched."
FetchType string `marker:"FetchType,optional"`
// +marker:argument="MappedBy", Description="The field that owns the relationship."
MappedBy string `marker:"MappedBy,optional"`
}
func (o OneToOneMarker) Validate() error {
if o.FetchType != "" {
matched := false
fetchTypeOptions := []string{"LAZY", "EAGER"}
for _, option := range fetchTypeOptions {
if strings.TrimSpace(o.FetchType) == option {
matched = true
}
}
if !matched {
return fmt.Errorf("invalid FetchType option. Here is the list of valid options %s", strings.Join(fetchTypeOptions, ", "))
}
}
if o.Cascade != nil && len(o.Cascade) != 0 {
matched := false
cascadeOptions := []string{"ALL", "PERSIST", "SAVE_UPDATE", "REMOVE"}
for _, option := range cascadeOptions {
if strings.TrimSpace(o.FetchType) == option {
matched = true
}
}
if !matched {
return fmt.Errorf("invalid FetchType option. Here is the list of valid options %s", strings.Join(cascadeOptions, ", "))
}
}
return nil
}
// +marker="shelf:one-to-many", Description="Specifies a many-valued association."
type OneToManyMarker struct {
// +marker:argument="Cascade", Description="The operations that must be cascaded to the target of the association."
Cascade []string `marker:"Cascade,optional"`
// +marker:argument="FetchType", Description="Whether the association should be lazily loaded or must be eagerly fetched."
FetchType string `marker:"FetchType,optional"`
// +marker:argument="MappedBy", Description="The field that owns the relationship."
MappedBy string `marker:"MappedBy,optional"`
}
func (o OneToManyMarker) Validate() error {
if o.FetchType != "" {
matched := false
fetchTypeOptions := []string{"LAZY", "EAGER"}
for _, option := range fetchTypeOptions {
if strings.TrimSpace(o.FetchType) == option {
matched = true
}
}
if !matched {
return fmt.Errorf("invalid FetchType option. Here is the list of valid options %s", strings.Join(fetchTypeOptions, ", "))
}
}
if o.Cascade != nil && len(o.Cascade) != 0 {
matched := false
cascadeOptions := []string{"ALL", "PERSIST", "SAVE_UPDATE", "REMOVE"}
for _, option := range cascadeOptions {
if strings.TrimSpace(o.FetchType) == option {
matched = true
}
}
if !matched {
return fmt.Errorf("invalid FetchType option. Here is the list of valid options %s", strings.Join(cascadeOptions, ", "))
}
}
return nil
}
// +marker="shelf:many-to-one", Description="Specifies a single-valued association to another entity class that has many-to-one multiplicity"
type ManyToOneMarker struct {
// +marker:argument="Cascade", Description="The operations that must be cascaded to the target of the association."
Cascade []string `marker:"Cascade,optional"`
// +marker:argument="FetchType", Description="Whether the association should be lazily loaded or must be eagerly fetched."
FetchType string `marker:"FetchType,optional"`
// +marker:argument="MappedBy", Description="The field that owns the relationship."
MappedBy string `marker:"MappedBy,optional"`
}
func (o ManyToOneMarker) Validate() error {
if o.FetchType != "" {
matched := false
fetchTypeOptions := []string{"LAZY", "EAGER"}
for _, option := range fetchTypeOptions {
if strings.TrimSpace(o.FetchType) == option {
matched = true
}
}
if !matched {
return fmt.Errorf("invalid FetchType option. Here is the list of valid options %s", strings.Join(fetchTypeOptions, ", "))
}
}
if o.Cascade != nil && len(o.Cascade) != 0 {
matched := false
cascadeOptions := []string{"ALL", "PERSIST", "SAVE_UPDATE", "REMOVE"}
for _, option := range cascadeOptions {
if strings.TrimSpace(o.FetchType) == option {
matched = true
}
}
if !matched {
return fmt.Errorf("invalid FetchType option. Here is the list of valid options %s", strings.Join(cascadeOptions, ", "))
}
}
return nil
}
// +marker="shelf:many-to-many", Description="Specifies a many-valued association with many-to-many multiplicity."
type ManyToManyMarker struct {
// +marker:argument="Cascade", Description="The operations that must be cascaded to the target of the association."
Cascade []string `marker:"Cascade,optional"`
// +marker:argument="FetchType", Description="Whether the association should be lazily loaded or must be eagerly fetched."
FetchType string `marker:"FetchType,optional"`
// +marker:argument="MappedBy", Description="The field that owns the relationship."
MappedBy string `marker:"MappedBy,optional"`
}
func (o ManyToManyMarker) Validate() error {
if o.FetchType != "" {
matched := false
fetchTypeOptions := []string{"LAZY", "EAGER"}
for _, option := range fetchTypeOptions {
if strings.TrimSpace(o.FetchType) == option {
matched = true
}
}
if !matched {
return fmt.Errorf("invalid FetchType option. Here is the list of valid options %s", strings.Join(fetchTypeOptions, ", "))
}
}
if o.Cascade != nil && len(o.Cascade) != 0 {
matched := false
cascadeOptions := []string{"ALL", "PERSIST", "SAVE_UPDATE", "REMOVE"}
for _, option := range cascadeOptions {
if strings.TrimSpace(o.FetchType) == option {
matched = true
}
}
if !matched {
return fmt.Errorf("invalid FetchType option. Here is the list of valid options %s", strings.Join(cascadeOptions, ", "))
}
}
return nil
}
// +marker="shelf:temporal", UseValueSyntax=true
type TemporalMarker struct {
// +marker:argument="Value", \
// Options={DATE, TIME, TIMESTAMP}, \
// Description="The type used in mapping an enum type."
Value string `marker:"Value,useValueSyntax"`
}
func (t TemporalMarker) Validate() error {
matched := false
temporalOptions := []string{"DATE", "TIME", "TIMESTAMP"}
for _, option := range temporalOptions {
if strings.TrimSpace(t.Value) == option {
matched = true
}
}
if !matched {
return fmt.Errorf("invalid Temporal option. Here is the list of valid options %s", strings.Join(temporalOptions, ", "))
}
return nil
}