-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathannotation.go
202 lines (166 loc) · 6.04 KB
/
annotation.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
package entx
import "encoding/json"
// CascadeAnnotationName is a name for our cascading delete annotation
var CascadeAnnotationName = "OPENLANE_CASCADE"
// CascadeThroughAnnotationName is a name for our cascading through edge delete annotation
var CascadeThroughAnnotationName = "OPENLANE_CASCADE_THROUGH"
// SchemaGenAnnotationName is a name for our graphql schema generation annotation
var SchemaGenAnnotationName = "OPENLANE_SCHEMAGEN"
// QueryGenAnnotationName is a name for our graphql query generation annotation
var QueryGenAnnotationName = "OPENLANE_QUERYGEN"
// SearchFieldAnnotationName is a name for the search field annotation
var SearchFieldAnnotationName = "OPENLANE_SEARCH"
// CascadeAnnotation is an annotation used to indicate that an edge should be cascaded
type CascadeAnnotation struct {
Field string
}
// CascadeThroughAnnotation is an annotation used to indicate that an edge should be cascaded through
type CascadeThroughAnnotation struct {
Schemas []ThroughCleanup
}
// ThroughCleanup is a struct used to indicate the field and through edge to cascade through
type ThroughCleanup struct {
Field string
Through string
}
// SchemaGenAnnotation is an annotation used to indicate that schema generation should be skipped for this type
// When Skip is true, the search schema generation is always skipped
// SkipSearch allow for schemas to be be opt out of search schema generation
type SchemaGenAnnotation struct {
// Skip indicates that the schema generation should be skipped for this type
Skip bool
// SkipSearch indicates that the schema should not be searchable
// Schemas are also not searchable if not fields are marked as searchable
SkipSearch bool
}
// QueryGenAnnotation is an annotation used to indicate that query generation should be skipped for this type
type QueryGenAnnotation struct {
Skip bool
}
// SearchFieldAnnotation is an annotation used to indicate that the field should be searchable
type SearchFieldAnnotation struct {
// Searchable indicates that the field should be searchable
Searchable bool
// ExcludeAdmin indicates that the field will be excluded from the admin search which includes all fields by default
ExcludeAdmin bool
// JSONPath is the path to the field in the JSON object
JSONPath string
// JSONDotPath is the path to the field in the JSON object using dot notation
JSONDotPath string
}
// Name returns the name of the CascadeAnnotation
func (a CascadeAnnotation) Name() string {
return CascadeAnnotationName
}
// Name returns the name of the CascadeThroughAnnotation
func (a CascadeThroughAnnotation) Name() string {
return CascadeThroughAnnotationName
}
// Name returns the name of the SchemaGenAnnotation
func (a SchemaGenAnnotation) Name() string {
return SchemaGenAnnotationName
}
// Name returns the name of the QueryGenAnnotation
func (a QueryGenAnnotation) Name() string {
return QueryGenAnnotationName
}
// Name returns the name of the SearchFieldAnnotation
func (a SearchFieldAnnotation) Name() string {
return SearchFieldAnnotationName
}
// CascadeAnnotationField sets the field name of the edge containing the ID of a record from the current schema
func CascadeAnnotationField(fieldname string) *CascadeAnnotation {
return &CascadeAnnotation{
Field: fieldname,
}
}
// CascadeThroughAnnotationField sets the field name of the edge containing the ID of a record from the current schema
func CascadeThroughAnnotationField(schemas []ThroughCleanup) *CascadeThroughAnnotation {
return &CascadeThroughAnnotation{
Schemas: schemas,
}
}
// SchemaGenSkip sets whether the schema generation should be skipped for this type
func SchemaGenSkip(skip bool) *SchemaGenAnnotation {
return &SchemaGenAnnotation{
Skip: skip,
}
}
// SchemaSearchable sets if the schema should be searchable and generated in the search schema template
func SchemaSearchable(s bool) *SchemaGenAnnotation {
return &SchemaGenAnnotation{
SkipSearch: !s,
}
}
// QueryGenSkip sets whether the query generation should be skipped for this type
func QueryGenSkip(skip bool) *QueryGenAnnotation {
return &QueryGenAnnotation{
Skip: skip,
}
}
// FieldJSONPathSearchable returns a new SearchFieldAnnotation with the searchable flag set and the JSONPath set
func FieldJSONPathSearchable(path string) *SearchFieldAnnotation {
return &SearchFieldAnnotation{
JSONPath: path,
Searchable: true,
}
}
// FieldJSONDotPathSearchable returns a new SearchFieldAnnotation with the searchable flag set and the JSONDotPath set
func FieldJSONDotPathSearchable(path string) *SearchFieldAnnotation {
return &SearchFieldAnnotation{
JSONDotPath: path,
Searchable: true,
}
}
// FieldSearchable returns a new SearchFieldAnnotation with the searchable flag set
func FieldSearchable() *SearchFieldAnnotation {
return &SearchFieldAnnotation{
Searchable: true,
}
}
// FieldAdminSearchable returns a new SearchFieldAnnotation with the exclude admin searchable flag set
func FieldAdminSearchable(s bool) *SearchFieldAnnotation {
return &SearchFieldAnnotation{
ExcludeAdmin: !s,
}
}
// Decode unmarshalls the CascadeAnnotation
func (a *CascadeAnnotation) Decode(annotation interface{}) error {
buf, err := json.Marshal(annotation)
if err != nil {
return err
}
return json.Unmarshal(buf, a)
}
// Decode unmarshalls the CascadeThroughAnnotation
func (a *CascadeThroughAnnotation) Decode(annotation interface{}) error {
buf, err := json.Marshal(annotation)
if err != nil {
return err
}
return json.Unmarshal(buf, a)
}
// Decode unmarshalls the SchemaGenAnnotation
func (a *SchemaGenAnnotation) Decode(annotation interface{}) error {
buf, err := json.Marshal(annotation)
if err != nil {
return err
}
return json.Unmarshal(buf, a)
}
// Decode unmarshalls the QueryGenAnnotation
func (a *QueryGenAnnotation) Decode(annotation interface{}) error {
buf, err := json.Marshal(annotation)
if err != nil {
return err
}
return json.Unmarshal(buf, a)
}
// Decode unmarshalls the SearchFieldAnnotation
func (a *SearchFieldAnnotation) Decode(annotation interface{}) error {
buf, err := json.Marshal(annotation)
if err != nil {
return err
}
return json.Unmarshal(buf, a)
}