-
Notifications
You must be signed in to change notification settings - Fork 1
/
project.pb.name.go
505 lines (434 loc) · 10.7 KB
/
project.pb.name.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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
// Code generated by protoc-gen-goten-resource
// Resource: Project
// DO NOT EDIT!!!
package project
import (
"fmt"
"net/url"
"reflect"
"regexp"
"strings"
"github.com/google/cel-go/common/types"
"github.com/google/cel-go/common/types/ref"
"github.com/google/cel-go/common/types/traits"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/proto"
"github.com/cloudwan/goten-sdk/runtime/goten"
gotenresource "github.com/cloudwan/goten-sdk/runtime/resource"
)
// proto imports
import (
ntt_meta "github.com/cloudwan/edgelq-sdk/common/types/meta"
)
// ensure the imports are used
var (
_ = codes.NotFound
_ = fmt.Stringer(nil)
_ = proto.Message(nil)
_ = status.Status{}
_ = url.URL{}
_ = strings.Builder{}
_ = goten.GotenMessage(nil)
_ = gotenresource.ListQuery(nil)
)
// make sure we're using proto imports
var (
_ = &ntt_meta.Meta{}
)
var project_RegexpId = regexp.MustCompile("^(?P<project_id>[\\w][\\w.-]{0,127})$")
var regexPath = regexp.MustCompile("^projects/(?P<project_id>-|[\\w][\\w.-]{0,127})$")
func (r *Project) MaybePopulateDefaults() error {
projectInterface := interface{}(r)
if defaulter, ok := projectInterface.(goten.Defaulter); ok {
return defaulter.PopulateDefaults()
}
return nil
}
func (r *Project) GetRawName() gotenresource.Name {
return r.GetName()
}
func (r *Project) GetResourceDescriptor() gotenresource.Descriptor {
return descriptor
}
type Name struct {
NamePattern
ProjectId string `firestore:"projectId"`
}
func ParseName(name string) (*Name, error) {
var matches []string
if matches = regexPath.FindStringSubmatch(name); matches != nil {
return NewNameBuilder().
SetId(matches[1]).
Name(), nil
}
return nil, status.Errorf(codes.InvalidArgument, "unable to parse '%s' as Project name", name)
}
func MustParseName(name string) *Name {
result, err := ParseName(name)
if err != nil {
panic(err)
}
return result
}
func ParseNameOrId(nameOrId string) (*Name, error) {
name, err := ParseName(nameOrId)
if err == nil {
return name, err
}
if project_RegexpId.MatchString(nameOrId) {
return NewNameBuilder().SetId(nameOrId).Name(), nil
} else {
return nil, fmt.Errorf("unable to parse '%s' as Project name or id", name)
}
}
func (name *Name) SetFromSegments(segments gotenresource.NameSegments) error {
if len(segments) == 0 {
return status.Errorf(codes.InvalidArgument, "No segments given for Project name")
}
if segments[len(segments)-1].CollectionLowerJson != "projects" {
return status.Errorf(codes.InvalidArgument, "unable to use segments %s to form Project name", segments)
}
name.ProjectId = segments[len(segments)-1].Id
return nil
}
func (name *Name) IsSpecified() bool {
return name != nil && (name.Pattern == NamePattern_Nil)
}
func (name *Name) IsFullyQualified() bool {
if name == nil {
return false
}
if name.ProjectId == "" || name.ProjectId == gotenresource.WildcardId {
return false
}
return true
}
func (name *Name) FullyQualifiedName() (string, error) {
if !name.IsFullyQualified() {
return "", status.Errorf(codes.InvalidArgument, "Name for Project is not fully qualified")
}
return fmt.Sprintf("//devices.edgelq.com/%s", name.String()), nil
}
func (name *Name) String() string {
if name == nil {
return "<nil>"
}
if valueStr, err := name.ProtoString(); err != nil {
panic(err)
} else {
return valueStr
}
}
func (name *Name) AsReference() *Reference {
return &Reference{Name: *name}
}
func (name *Name) AsRawReference() gotenresource.Reference {
return name.AsReference()
}
func (name *Name) GetResourceDescriptor() gotenresource.Descriptor {
return descriptor
}
func (name *Name) GetPattern() gotenresource.NamePattern {
if name == nil {
return ""
}
return name.Pattern
}
func (name *Name) GetIdParts() map[string]string {
if name != nil {
return map[string]string{
"projectId": name.ProjectId,
}
}
return map[string]string{
"projectId": "",
}
}
func (name *Name) GetSegments() gotenresource.NameSegments {
if name == nil || name.Pattern == "" {
return nil
}
return gotenresource.NameSegments{{
CollectionLowerJson: "projects",
Id: name.ProjectId,
}}
}
// implement methods required by protobuf-go library for string-struct conversion
func (name *Name) ProtoString() (string, error) {
if name == nil {
return "", nil
}
result := ""
result += "projects/" + name.ProjectId
return result, nil
}
func (name *Name) ParseProtoString(data string) error {
parsed, err := ParseName(data)
if err != nil {
return err
}
*name = *parsed
return nil
}
// GotenEqual returns true if other is of same type and paths are equal (implements goten.Equaler interface)
func (name *Name) GotenEqual(other interface{}) bool {
if other == nil {
return name == nil
}
other1, ok := other.(*Name)
if !ok {
other2, ok := other.(Name)
if ok {
other1 = &other2
} else {
return false
}
}
if other1 == nil {
return name == nil
} else if name == nil {
return false
}
if name.ProjectId != other1.ProjectId {
return false
}
return true
}
// Matches is same as GotenEqual, but also will accept "other" if name is wildcard.
func (name *Name) Matches(other interface{}) bool {
if other == nil {
return name == nil
}
other1, ok := other.(*Name)
if !ok {
other2, ok := other.(Name)
if ok {
other1 = &other2
} else {
return false
}
}
if other1 == nil {
return name == nil
} else if name == nil {
return false
}
if name.ProjectId != other1.ProjectId {
return name.ProjectId == gotenresource.WildcardId
}
return true
}
// Google CEL integration Type
var celName = types.NewTypeValue("Name", traits.ReceiverType)
func (name *Name) TypeName() string {
return ".ntt.devices.v1alpha.Project.Name"
}
func (name *Name) HasTrait(trait int) bool {
return trait == traits.ReceiverType
}
func (name *Name) Equal(other ref.Val) ref.Val {
return types.Bool(false)
}
func (name *Name) Value() interface{} {
return name
}
func (name *Name) Match(pattern ref.Val) ref.Val {
return types.Bool(true)
}
func (name *Name) Receive(function string, overload string, args []ref.Val) ref.Val {
switch function {
case "getId":
return types.String(name.ProjectId)
case "satisfies":
rhsName, err := ParseName(string(args[0].(types.String)))
if err != nil {
return types.ValOrErr(celName, "function %s name parse error: %s", function, err)
}
return types.Bool(rhsName.Matches(name))
default:
return types.ValOrErr(celName, "no such function - %s", function)
}
}
func (name *Name) ConvertToNative(typeDesc reflect.Type) (interface{}, error) {
panic("not required")
}
func (name *Name) ConvertToType(typeVal ref.Type) ref.Val {
switch typeVal.TypeName() {
case types.StringType.TypeName():
return types.String(name.String())
default:
panic(fmt.Errorf("unable to convert %s to CEL type %s", "Name", typeVal.TypeName()))
}
}
func (name *Name) Type() ref.Type {
return celName
}
// implement CustomTypeCliValue method
func (name *Name) SetFromCliFlag(raw string) error {
parsedName, err := ParseName(raw)
if err != nil {
return err
}
*name = *parsedName
return nil
}
type Reference struct {
Name
project *Project
}
func MakeReference(name *Name, project *Project) (*Reference, error) {
return &Reference{
Name: *name,
project: project,
}, nil
}
func ParseReference(name string) (*Reference, error) {
parsedName, err := ParseName(name)
if err != nil {
return nil, err
}
return MakeReference(parsedName, nil)
}
func MustParseReference(name string) *Reference {
result, err := ParseReference(name)
if err != nil {
panic(err)
}
return result
}
func (ref *Reference) Resolve(resolved *Project) {
ref.project = resolved
}
func (ref *Reference) ResolveRaw(res gotenresource.Resource) error {
if typedRes, ok := res.(*Project); ok && typedRes != nil {
ref.Resolve(typedRes)
return nil
}
return status.Errorf(codes.Internal, "Invalid resource type for Project: %s", reflect.TypeOf(res).Elem().Name())
}
func (ref *Reference) Resolved() bool {
return ref.project != nil
}
func (ref *Reference) ClearCached() {
ref.project = nil
}
func (ref *Reference) GetProject() *Project {
return ref.project
}
func (ref *Reference) GetRawResource() gotenresource.Resource {
return ref.project
}
func (ref *Reference) IsFullyQualified() bool {
if ref == nil {
return false
}
return ref.Name.IsFullyQualified()
}
func (ref *Reference) IsSpecified() bool {
if ref == nil {
return false
}
return ref.Name.IsSpecified()
}
func (ref *Reference) FullyQualifiedName() (string, error) {
if !ref.IsFullyQualified() {
return "", status.Errorf(codes.InvalidArgument, "Name for Project is not fully qualified")
}
return fmt.Sprintf("//devices.edgelq.com/%s", ref.String()), nil
}
func (ref *Reference) GetResourceDescriptor() gotenresource.Descriptor {
return descriptor
}
func (ref *Reference) GetPattern() gotenresource.NamePattern {
if ref == nil {
return ""
}
return ref.Pattern
}
func (ref *Reference) GetIdParts() map[string]string {
if ref != nil {
return ref.Name.GetIdParts()
}
return map[string]string{
"projectId": "",
}
}
func (ref *Reference) GetSegments() gotenresource.NameSegments {
if ref != nil {
return ref.Name.GetSegments()
}
return nil
}
func (ref *Reference) String() string {
if ref == nil {
return "<nil>"
}
return ref.Name.String()
}
// implement methods required by protobuf-go library for string-struct conversion
func (ref *Reference) ProtoString() (string, error) {
if ref == nil {
return "", nil
}
return ref.Name.ProtoString()
}
func (ref *Reference) ParseProtoString(data string) error {
parsed, err := ParseReference(data)
if err != nil {
return err
}
*ref = *parsed
return nil
}
// GotenEqual returns true if other is of same type and paths are equal (implements goten.Equaler interface)
func (ref *Reference) GotenEqual(other interface{}) bool {
if other == nil {
return ref == nil
}
other1, ok := other.(*Reference)
if !ok {
other2, ok := other.(Reference)
if ok {
other1 = &other2
} else {
return false
}
}
if other1 == nil {
return ref == nil
} else if ref == nil {
return false
}
return ref.Name.GotenEqual(other1.Name)
}
// Matches is same as GotenEqual, but also will accept "other" if name is wildcard.
func (name *Reference) Matches(other interface{}) bool {
if other == nil {
return name == nil
}
other1, ok := other.(*Reference)
if !ok {
other2, ok := other.(Reference)
if ok {
other1 = &other2
} else {
return false
}
}
if other1 == nil {
return name == nil
} else if name == nil {
return false
}
return name.Name.Matches(&other1.Name)
}
// implement CustomTypeCliValue method
func (ref *Reference) SetFromCliFlag(raw string) error {
parsedRef, err := ParseReference(raw)
if err != nil {
return err
}
*ref = *parsedRef
return nil
}