-
Notifications
You must be signed in to change notification settings - Fork 1
/
fields.go
332 lines (288 loc) · 8.43 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
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
package directus
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"github.com/perimeterx/marshmallow"
)
type Field struct {
Collection string `json:"collection"`
Field string `json:"field"`
Type FieldType `json:"type"`
Meta FieldMeta `json:"meta"`
Schema *FieldSchema `json:"schema,omitempty"`
}
type FieldType string
const (
FieldTypeString FieldType = "string"
FieldTypeAlias FieldType = "alias"
FieldTypeDate FieldType = "date"
FieldTypeTimestamp FieldType = "timestamp"
)
type FieldMeta struct {
ID int64 `json:"id,omitempty"`
Hidden bool `json:"hidden"`
Width FieldWidth `json:"width,omitempty"`
ReadOnly bool `json:"read_only"`
Required bool `json:"required"`
Sort int64 `json:"sort,omitempty"`
System bool `json:"system,omitempty"`
Special []FieldSpecial `json:"special,omitempty"`
Translations []*FieldTranslation `json:"translations,omitempty"`
Options *FieldOptions `json:"options,omitempty"`
Unknown map[string]any `json:"-"`
}
func (meta *FieldMeta) UnmarshalJSON(data []byte) error {
values, err := marshmallow.Unmarshal(data, meta, marshmallow.WithExcludeKnownFieldsFromMap(true))
if err != nil {
return err
}
meta.Unknown = values
return nil
}
func (meta *FieldMeta) MarshalJSON() ([]byte, error) {
type alias FieldMeta
base, err := json.Marshal((*alias)(meta))
if err != nil {
return nil, err
}
m := make(map[string]any)
for k, v := range meta.Unknown {
m[k] = v
}
if err := json.Unmarshal(base, &m); err != nil {
return nil, err
}
return json.Marshal(m)
}
func (meta *FieldMeta) HasSpecial(special FieldSpecial) bool {
for _, s := range meta.Special {
if s == special {
return true
}
}
return false
}
func (meta *FieldMeta) Translation(language string) *FieldTranslation {
for _, t := range meta.Translations {
if t.Language == language {
return t
}
}
return nil
}
type FieldWidth string
const (
FieldWidthFull FieldWidth = "full"
FieldWidthHalf FieldWidth = "half"
)
func (width *FieldWidth) UnmarshalJSON(data []byte) error {
var str string
if err := json.Unmarshal(data, &str); err != nil {
return err
}
*width = FieldWidth(str)
return nil
}
type FieldSpecial string
const (
FieldSpecialManyToOne FieldSpecial = "m2o"
FieldSpecialDateCreated FieldSpecial = "date-created"
FieldSpecialDateUpdated FieldSpecial = "date-updated"
FieldSpecialUUID FieldSpecial = "uuid"
FieldSpecialUserCreated FieldSpecial = "user-created"
FieldSpecialUserUpdated FieldSpecial = "user-updated"
FieldSpecialFile FieldSpecial = "file"
FieldSpecialAlias FieldSpecial = "alias"
FieldSpecialNoData FieldSpecial = "no-data"
FieldSpecialCastBoolean FieldSpecial = "cast-boolean"
FieldSpecialGroup FieldSpecial = "group"
)
func (special *FieldSpecial) UnmarshalJSON(data []byte) error {
var value string
if err := json.Unmarshal(data, &value); err != nil {
return err
}
*special = FieldSpecial(value)
return nil
}
type FieldTranslation struct {
Language string `json:"language"`
Translation string `json:"translation"`
}
type FieldOptions struct {
Choices FieldChoices `json:"choices,omitempty"`
unknown map[string]any
}
func (options *FieldOptions) UnmarshalJSON(data []byte) error {
values, err := marshmallow.Unmarshal(data, options, marshmallow.WithExcludeKnownFieldsFromMap(true))
if err != nil {
return err
}
options.unknown = values
return nil
}
func (options *FieldOptions) MarshalJSON() ([]byte, error) {
type alias FieldOptions
base, err := json.Marshal((*alias)(options))
if err != nil {
return nil, err
}
m := make(map[string]any)
for k, v := range options.unknown {
m[k] = v
}
if err := json.Unmarshal(base, &m); err != nil {
return nil, err
}
return json.Marshal(m)
}
type FieldChoices struct {
Choices []*FieldChoice
Values []any
}
func (choices *FieldChoices) UnmarshalJSON(data []byte) error {
var raw []json.RawMessage
if err := json.Unmarshal(data, &raw); err != nil {
return err
}
for _, rawchoice := range raw {
var c string
if err := json.Unmarshal(rawchoice, &c); err == nil {
choices.Values = append(choices.Values, c)
continue
}
var choice FieldChoice
if err := json.Unmarshal(rawchoice, &choice); err != nil {
return err
}
choices.Choices = append(choices.Choices, &choice)
}
return nil
}
type FieldChoice struct {
Text string `json:"text"`
Value any `json:"value"`
}
type FieldSchema struct {
Name string `json:"name,omitempty"`
Table string `json:"table,omitempty"`
DataType string `json:"data_type"`
MaxLength int64 `json:"max_length,omitempty"`
IsNullable bool `json:"is_nullable"`
IsUnique bool `json:"is_unique,omitempty"`
IsPrimaryKey bool `json:"is_primary_key,omitempty"`
HasAutoIncrement bool `json:"has_auto_increment,omitempty"`
Unknown map[string]any `json:"-"`
}
func (schema *FieldSchema) UnmarshalJSON(data []byte) error {
values, err := marshmallow.Unmarshal(data, schema, marshmallow.WithExcludeKnownFieldsFromMap(true))
if err != nil {
return err
}
schema.Unknown = values
return nil
}
func (schema *FieldSchema) MarshalJSON() ([]byte, error) {
type alias FieldSchema
base, err := json.Marshal((*alias)(schema))
if err != nil {
return nil, err
}
m := make(map[string]any)
for k, v := range schema.Unknown {
m[k] = v
}
if err := json.Unmarshal(base, &m); err != nil {
return nil, err
}
return json.Marshal(m)
}
type clientFields struct {
client *Client
}
func (cr *clientFields) List(ctx context.Context) ([]*Field, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, cr.client.urlf("/fields"), nil)
if err != nil {
return nil, fmt.Errorf("directus: cannot prepare request: %v", err)
}
reply := struct {
Data []*Field `json:"data"`
}{}
if err := cr.client.sendRequest(req, &reply); err != nil {
return nil, err
}
return reply.Data, nil
}
func (cr *clientFields) ListCollection(ctx context.Context, collection string) ([]*Field, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, cr.client.urlf("/fields/%s", collection), nil)
if err != nil {
return nil, fmt.Errorf("directus: cannot prepare request: %v", err)
}
reply := struct {
Data []*Field `json:"data"`
}{}
if err := cr.client.sendRequest(req, &reply); err != nil {
return nil, err
}
return reply.Data, nil
}
func (cr *clientFields) Get(ctx context.Context, collection, field string) (*Field, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, cr.client.urlf("/fields/%s/%s", collection, field), nil)
if err != nil {
return nil, fmt.Errorf("directus: cannot prepare request: %v", err)
}
var reply Field
if err := cr.client.sendRequest(req, &reply); err != nil {
return nil, err
}
return &reply, nil
}
func (cr *clientFields) Create(ctx context.Context, field *Field) (*Field, error) {
if field.Collection == "" {
return nil, fmt.Errorf("directus: field collection is required")
}
var buf bytes.Buffer
if err := json.NewEncoder(&buf).Encode(field); err != nil {
return nil, fmt.Errorf("directus: cannot encode request: %v", err)
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, cr.client.urlf("/fields/%s", field.Collection), &buf)
if err != nil {
return nil, fmt.Errorf("directus: cannot prepare request: %v", err)
}
var reply Field
if err := cr.client.sendRequest(req, &reply); err != nil {
return nil, err
}
return &reply, nil
}
func (cr *clientFields) Delete(ctx context.Context, collection, field string) error {
req, err := http.NewRequestWithContext(ctx, http.MethodDelete, cr.client.urlf("/fields/%s/%s", collection, field), nil)
if err != nil {
return fmt.Errorf("directus: cannot prepare request: %v", err)
}
return cr.client.sendRequest(req, nil)
}
func (cr *clientFields) Patch(ctx context.Context, f *Field) (*Field, error) {
if f.Collection == "" {
return nil, fmt.Errorf("directus: field collection is required")
}
if f.Field == "" {
return nil, fmt.Errorf("directus: field name is required")
}
var buf bytes.Buffer
if err := json.NewEncoder(&buf).Encode(f); err != nil {
return nil, fmt.Errorf("directus: cannot encode request: %v", err)
}
req, err := http.NewRequestWithContext(ctx, http.MethodPatch, cr.client.urlf("/fields/%s/%s", f.Collection, f.Field), &buf)
if err != nil {
return nil, fmt.Errorf("directus: cannot prepare request: %v", err)
}
var reply Field
if err := cr.client.sendRequest(req, &reply); err != nil {
return nil, err
}
return &reply, nil
}