-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschema.go
410 lines (338 loc) · 9.59 KB
/
schema.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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
package sajari
import (
"context"
"fmt"
enginev2pb "code.sajari.com/protogen-go/sajari/engine/v2"
)
// Schema returns the schema (list of fields) for the collection.
func (c *Client) Schema() *Schema {
return &Schema{
c: c,
}
}
// Schema provides methods for managing collection schemas. Use Client.Schema to create
// one for a collection.
type Schema struct {
c *Client
}
// Fields returns an iterator which retrieves all the fields in the collection.
func (s *Schema) Fields(ctx context.Context) *FieldIterator {
return &FieldIterator{
ctx: ctx,
c: s.c,
}
}
// FieldIterator iterates through a list of fields.
type FieldIterator struct {
ctx context.Context
c *Client
token string
fields []Field
end bool
lastErr error
}
// Next returns the next field in the iteration. If there are no more fields
// remaining then an error wrapping ErrDone is returned.
func (it *FieldIterator) Next() (Field, error) {
if it.lastErr != nil {
return Field{}, it.lastErr
}
if len(it.fields) == 0 && it.end {
return Field{}, fmt.Errorf("%w", ErrDone)
}
if len(it.fields) == 0 {
if it.fields, it.token, it.lastErr = it.fetch(it.ctx); it.lastErr != nil {
return Field{}, it.lastErr
}
if it.token == "" {
it.end = true
}
}
f := it.fields[0]
it.fields = it.fields[1:]
return f, nil
}
func (it *FieldIterator) fetch(ctx context.Context) ([]Field, string, error) {
resp, err := enginev2pb.NewSchemaClient(it.c.ClientConn).ListFields(it.c.newContext(ctx),
&enginev2pb.ListFieldsRequest{
PageToken: it.token,
})
if err != nil {
return nil, "", err
}
fs := make([]Field, 0, len(resp.GetFields()))
for _, pbField := range resp.GetFields() {
f, err := fieldFromProto(pbField)
if err != nil {
return nil, "", err
}
fs = append(fs, f)
}
return fs, resp.GetNextPageToken(), nil
}
func fieldFromProto(f *enginev2pb.Field) (Field, error) {
t, err := typeFromProto(f.GetType())
if err != nil {
return Field{}, err
}
m, err := modeFromProto(f.GetMode())
if err != nil {
return Field{}, err
}
pbIndexes := f.GetIndexes()
indexes := make([]FieldIndex, 0, len(pbIndexes))
for _, pbIndex := range pbIndexes {
indexes = append(indexes, FieldIndex{
Spec: pbIndex.GetSpec(),
Description: pbIndex.GetDescription(),
})
}
return Field{
Name: f.GetName(),
Description: f.GetDescription(),
Type: t,
Mode: m,
Repeated: f.GetRepeated(),
Indexes: indexes,
}, nil
}
// Field represents a meta field which can be assigned in a collection record.
type Field struct {
// Name used to identify the field.
Name string
// Description of the field.
Description string
// Type of the field.
Type FieldType
// Mode of the field.
Mode FieldMode
// Repeated indicates that this field can hold a list of values.
Repeated bool
// Indexes is a list of the field's indexes.
Indexes []FieldIndex
}
// Index returns the index matching the given identifier/specification.
// If no such index exists then it will return ({}, false).
func (f Field) Index(spec string) (FieldIndex, bool) {
for _, idx := range f.Indexes {
if idx.Spec == spec {
return idx, true
}
}
return FieldIndex{}, false
}
// FieldIndex is a field index.
type FieldIndex struct {
// Spec is the identifier/specification for the creation of the index.
Spec string
// Description is a description of the index.
Description string
}
func (f FieldIndex) proto() *enginev2pb.FieldIndex {
return &enginev2pb.FieldIndex{
Spec: f.Spec,
Description: f.Description,
}
}
func (f Field) proto() (*enginev2pb.Field, error) {
t, err := f.Type.proto()
if err != nil {
return nil, err
}
m, err := f.Mode.proto()
if err != nil {
return nil, err
}
return &enginev2pb.Field{
Name: f.Name,
Description: f.Description,
Type: t,
Mode: m,
Repeated: f.Repeated,
}, nil
}
func typeFromProto(t enginev2pb.Field_Type) (FieldType, error) {
switch t {
case enginev2pb.Field_STRING:
return TypeString, nil
case enginev2pb.Field_INTEGER:
return TypeInteger, nil
case enginev2pb.Field_FLOAT:
return TypeFloat, nil
case enginev2pb.Field_DOUBLE:
return TypeDouble, nil
case enginev2pb.Field_BOOLEAN:
return TypeBoolean, nil
case enginev2pb.Field_TIMESTAMP:
return TypeTimestamp, nil
default:
return TypeString, fmt.Errorf("unknown type: '%v'", t)
}
}
func modeFromProto(m enginev2pb.Field_Mode) (FieldMode, error) {
switch m {
case enginev2pb.Field_NULLABLE:
return ModeNullable, nil
case enginev2pb.Field_REQUIRED:
return ModeRequired, nil
case enginev2pb.Field_UNIQUE:
return ModeUnique, nil
default:
return ModeNullable, fmt.Errorf("unknown mode: '%v'", m)
}
}
// FieldMode defines field modes.
type FieldMode string
// Enumeration of field modes.
const (
ModeNullable FieldMode = "NULLABLE" // Don't require a value.
ModeRequired FieldMode = "REQUIRED" // Field value must be set.
ModeUnique FieldMode = "UNIQUE" // Field value must be unique (and hence also set).
)
func (m FieldMode) proto() (enginev2pb.Field_Mode, error) {
switch m {
case ModeNullable:
return enginev2pb.Field_NULLABLE, nil
case ModeRequired:
return enginev2pb.Field_REQUIRED, nil
case ModeUnique:
return enginev2pb.Field_UNIQUE, nil
default:
return enginev2pb.Field_NULLABLE, fmt.Errorf("unknown mode: %q", string(m))
}
}
// FieldType defines field data types.
type FieldType string
// Enumeration of field types.
const (
TypeString FieldType = "STRING"
TypeInteger FieldType = "INTEGER"
TypeFloat FieldType = "FLOAT"
TypeDouble FieldType = "DOUBLE"
TypeBoolean FieldType = "BOOLEAN"
TypeTimestamp FieldType = "TIMESTAMP"
)
func (t FieldType) proto() (enginev2pb.Field_Type, error) {
switch t {
case TypeString:
return enginev2pb.Field_STRING, nil
case TypeInteger:
return enginev2pb.Field_INTEGER, nil
case TypeFloat:
return enginev2pb.Field_FLOAT, nil
case TypeDouble:
return enginev2pb.Field_DOUBLE, nil
case TypeBoolean:
return enginev2pb.Field_BOOLEAN, nil
case TypeTimestamp:
return enginev2pb.Field_TIMESTAMP, nil
}
return enginev2pb.Field_STRING, fmt.Errorf("unknown type: '%v'", string(t))
}
// CreateField creates a new field in the schema.
func (s *Schema) CreateField(ctx context.Context, f Field) error {
pbf, err := f.proto()
if err != nil {
return err
}
_, err = enginev2pb.NewSchemaClient(s.c.ClientConn).CreateField(s.c.newContext(ctx), &enginev2pb.CreateFieldRequest{
Field: pbf,
})
return err
}
// MutateField mutates the field identified by name.
func (s *Schema) MutateField(ctx context.Context, name string, m FieldMutation) error {
pbm, err := m.proto()
if err != nil {
return err
}
_, err = enginev2pb.NewSchemaClient(s.c.ClientConn).MutateField(ctx, &enginev2pb.MutateFieldRequest{
Name: name,
Mutation: pbm,
})
return err
}
type fieldMutations []FieldMutation
func (ms fieldMutations) proto() ([]*enginev2pb.MutateFieldRequest_Mutation, error) {
out := make([]*enginev2pb.MutateFieldRequest_Mutation, 0, len(ms))
for _, m := range ms {
x, err := m.proto()
if err != nil {
return nil, err
}
out = append(out, x)
}
return out, nil
}
// FieldNameMutation creates a schema field mutation which changes the name of a field.
func FieldNameMutation(name string) FieldMutation {
return fieldNameMutation(name)
}
type fieldNameMutation string
func (n fieldNameMutation) proto() (*enginev2pb.MutateFieldRequest_Mutation, error) {
return &enginev2pb.MutateFieldRequest_Mutation{
Mutation: &enginev2pb.MutateFieldRequest_Mutation_Name{
Name: string(n),
},
}, nil
}
// FieldTypeMutation creates a schema field mutation which changes the type of a field.
func FieldTypeMutation(ty FieldType) FieldMutation {
return fieldTypeMutation(ty)
}
type fieldTypeMutation FieldType
func (t fieldTypeMutation) proto() (*enginev2pb.MutateFieldRequest_Mutation, error) {
ty, err := FieldType(t).proto()
if err != nil {
return nil, err
}
return &enginev2pb.MutateFieldRequest_Mutation{
Mutation: &enginev2pb.MutateFieldRequest_Mutation_Type{
Type: ty,
},
}, nil
}
// FieldModeMutation creates a schema field mutation which changes the unique constraint on a field.
func FieldModeMutation(m FieldMode) FieldMutation {
return fieldModeMutation(m)
}
type fieldModeMutation FieldMode
func (m fieldModeMutation) proto() (*enginev2pb.MutateFieldRequest_Mutation, error) {
pbm, err := FieldMode(m).proto()
if err != nil {
return nil, err
}
return &enginev2pb.MutateFieldRequest_Mutation{
Mutation: &enginev2pb.MutateFieldRequest_Mutation_Mode{
Mode: pbm,
},
}, nil
}
// FieldAddIndexMutation adds a schema field mutation which adds an index to a field.
func FieldAddIndexMutation(x FieldIndex) FieldMutation {
return fieldAddIndexMutation(x)
}
type fieldAddIndexMutation FieldIndex
func (i fieldAddIndexMutation) proto() (*enginev2pb.MutateFieldRequest_Mutation, error) {
return &enginev2pb.MutateFieldRequest_Mutation{
Mutation: &enginev2pb.MutateFieldRequest_Mutation_AddIndex{
AddIndex: FieldIndex(i).proto(),
},
}, nil
}
// FieldRepeatedMutation creates a schema field mutation which changes the repeated property on a field.
func FieldRepeatedMutation(repeated bool) FieldMutation {
return fieldRepeatedMutation(repeated)
}
type fieldRepeatedMutation bool
func (u fieldRepeatedMutation) proto() (*enginev2pb.MutateFieldRequest_Mutation, error) {
return &enginev2pb.MutateFieldRequest_Mutation{
Mutation: &enginev2pb.MutateFieldRequest_Mutation_Repeated{
Repeated: bool(u),
},
}, nil
}
// FieldMutation is an interface which is satisfied by schema field mutations.
type FieldMutation interface {
proto() (*enginev2pb.MutateFieldRequest_Mutation, error)
}