github.com/cloudwan/edgelq-sdk@v1.15.4/devices/resources/v1/device_distribution_counter/device_distribution_counter.pb.parentname.go (about) 1 // Code generated by protoc-gen-goten-resource 2 // Resource: DeviceDistributionCounter 3 // DO NOT EDIT!!! 4 5 package device_distribution_counter 6 7 import ( 8 "fmt" 9 "net/url" 10 "reflect" 11 "regexp" 12 "strings" 13 14 "google.golang.org/grpc/codes" 15 "google.golang.org/grpc/status" 16 "google.golang.org/protobuf/proto" 17 18 "github.com/cloudwan/goten-sdk/runtime/goten" 19 gotenresource "github.com/cloudwan/goten-sdk/runtime/resource" 20 ) 21 22 // proto imports 23 import ( 24 project "github.com/cloudwan/edgelq-sdk/devices/resources/v1/project" 25 meta "github.com/cloudwan/goten-sdk/types/meta" 26 ) 27 28 // ensure the imports are used 29 var ( 30 _ = codes.NotFound 31 _ = new(fmt.Stringer) 32 _ = new(proto.Message) 33 _ = status.Status{} 34 _ = url.URL{} 35 _ = strings.Builder{} 36 37 _ = new(goten.GotenMessage) 38 _ = new(gotenresource.ListQuery) 39 ) 40 41 // make sure we're using proto imports 42 var ( 43 _ = &project.Project{} 44 _ = &meta.Meta{} 45 ) 46 47 var parentRegexPath_Project_Region = regexp.MustCompile("^projects/(?P<project_id>-|[\\w][\\w.-]{0,127})/regions/(?P<region_id>-|[a-z][a-z0-9\\-]{0,28}[a-z0-9])$") 48 49 type ParentName struct { 50 NamePattern 51 ProjectId string `firestore:"projectId"` 52 RegionId string `firestore:"regionId"` 53 } 54 55 func ParseParentName(name string) (*ParentName, error) { 56 var matches []string 57 if matches = parentRegexPath_Project_Region.FindStringSubmatch(name); matches != nil { 58 return NewNameBuilder(). 59 SetProjectId(matches[1]). 60 SetRegionId(matches[2]). 61 Parent(), nil 62 } 63 64 return nil, status.Errorf(codes.InvalidArgument, "unable to parse '%s' as DeviceDistributionCounter parent name", name) 65 } 66 67 func MustParseParentName(name string) *ParentName { 68 result, err := ParseParentName(name) 69 if err != nil { 70 panic(err) 71 } 72 return result 73 } 74 75 func (name *ParentName) SetFromSegments(segments gotenresource.NameSegments) error { 76 if len(segments) == 2 && segments[0].CollectionLowerJson == "projects" && segments[1].CollectionLowerJson == "regions" { 77 name.Pattern = NamePattern_Project_Region 78 name.ProjectId = segments[0].Id 79 name.RegionId = segments[1].Id 80 return nil 81 } 82 return status.Errorf(codes.InvalidArgument, "unable to use segments %s to form DeviceDistributionCounter parent name", segments) 83 } 84 85 func (name *ParentName) GetProjectName() *project.Name { 86 if name == nil { 87 return nil 88 } 89 90 switch name.Pattern { 91 case NamePattern_Project_Region: 92 return project.NewNameBuilder(). 93 SetId(name.ProjectId). 94 Name() 95 default: 96 return nil 97 } 98 } 99 100 func (name *ParentName) IsSpecified() bool { 101 if name == nil || name.Pattern == "" { 102 return false 103 } 104 switch name.Pattern { 105 case NamePattern_Project_Region: 106 return name.ProjectId != "" && name.RegionId != "" 107 } 108 return false 109 } 110 111 func (name *ParentName) IsFullyQualified() bool { 112 if name == nil || name.Pattern == "" { 113 return false 114 } 115 116 switch name.Pattern { 117 case NamePattern_Project_Region: 118 return name.ProjectId != "" && name.ProjectId != gotenresource.WildcardId && name.RegionId != "" && name.RegionId != gotenresource.WildcardId 119 } 120 121 return false 122 } 123 124 func (name *ParentName) FullyQualifiedName() (string, error) { 125 if !name.IsFullyQualified() { 126 return "", status.Errorf(codes.InvalidArgument, "Parent name for DeviceDistributionCounter is not fully qualified") 127 } 128 return fmt.Sprintf("//devices.edgelq.com/%s", name.String()), nil 129 } 130 131 func (name *ParentName) GetResourceDescriptor() gotenresource.Descriptor { 132 return descriptor 133 } 134 135 func (name *ParentName) GetPattern() gotenresource.NamePattern { 136 if name == nil { 137 return "" 138 } 139 return name.Pattern 140 } 141 142 func (name *ParentName) GetIdParts() map[string]string { 143 if name != nil { 144 return map[string]string{ 145 "projectId": name.ProjectId, 146 "regionId": name.RegionId, 147 } 148 } 149 return map[string]string{ 150 "projectId": "", 151 "regionId": "", 152 } 153 } 154 155 func (name *ParentName) GetSegments() gotenresource.NameSegments { 156 if name == nil { 157 return nil 158 } 159 160 switch name.Pattern { 161 case NamePattern_Project_Region: 162 return gotenresource.NameSegments{ 163 gotenresource.NameSegment{ 164 CollectionLowerJson: "projects", 165 Id: name.ProjectId, 166 }, 167 gotenresource.NameSegment{ 168 CollectionLowerJson: "regions", 169 Id: name.RegionId, 170 }, 171 } 172 } 173 return nil 174 } 175 176 func (name *ParentName) GetIParentName() gotenresource.Name { 177 return nil 178 } 179 180 func (name *ParentName) GetIUnderlyingParentName() gotenresource.Name { 181 return nil 182 } 183 184 func (name *ParentName) String() string { 185 if name == nil { 186 return "<nil>" 187 } 188 189 if valueStr, err := name.ProtoString(); err != nil { 190 panic(err) 191 } else { 192 return valueStr 193 } 194 } 195 196 func (name *ParentName) DescendsFrom(ancestor string) bool { 197 if name == nil { 198 return false 199 } 200 201 switch name.Pattern { 202 case NamePattern_Project_Region: 203 return ancestor == "projects" || ancestor == "regions" 204 } 205 206 return false 207 } 208 209 func (name *ParentName) AsReference() *ParentReference { 210 return &ParentReference{ParentName: *name} 211 } 212 213 func (name *ParentName) AsRawReference() gotenresource.Reference { 214 return name.AsReference() 215 } 216 217 // implement methods required by protobuf-go library for string-struct conversion 218 219 func (name *ParentName) ProtoString() (string, error) { 220 if name == nil { 221 return "", nil 222 } 223 switch name.Pattern { 224 case NamePattern_Project_Region: 225 return "projects/" + name.ProjectId + "/regions/" + name.RegionId, nil 226 } 227 return "", nil 228 } 229 230 func (name *ParentName) ParseProtoString(data string) error { 231 parsed, err := ParseParentName(data) 232 if err != nil { 233 return err 234 } 235 *name = *parsed 236 return nil 237 } 238 239 // GotenEqual returns true if other is of same type and paths are equal (implements goten.Equaler interface) 240 func (name *ParentName) GotenEqual(other interface{}) bool { 241 if other == nil { 242 return name == nil 243 } 244 other1, ok := other.(*ParentName) 245 if !ok { 246 other2, ok := other.(ParentName) 247 if ok { 248 other1 = &other2 249 } else { 250 return false 251 } 252 } 253 if other1 == nil { 254 return name == nil 255 } else if name == nil { 256 return false 257 } 258 if name.ProjectId != other1.ProjectId { 259 return false 260 } 261 if name.RegionId != other1.RegionId { 262 return false 263 } 264 if name.Pattern != other1.Pattern { 265 return false 266 } 267 268 return true 269 } 270 271 // Matches is same as GotenEqual, but also will accept "other" if name is wildcard. 272 func (name *ParentName) Matches(other interface{}) bool { 273 if other == nil { 274 return name == nil 275 } 276 other1, ok := other.(*ParentName) 277 if !ok { 278 other2, ok := other.(ParentName) 279 if ok { 280 other1 = &other2 281 } else { 282 return false 283 } 284 } 285 if other1 == nil { 286 return name == nil 287 } else if name == nil { 288 return false 289 } 290 291 if name.Pattern != other1.Pattern { 292 return false 293 } 294 switch name.Pattern { 295 case NamePattern_Project_Region: 296 if name.ProjectId != other1.ProjectId && 297 name.ProjectId != gotenresource.WildcardId { 298 return false 299 } 300 if name.RegionId != other1.RegionId && 301 name.RegionId != gotenresource.WildcardId { 302 return false 303 } 304 } 305 306 return true 307 } 308 309 // implement CustomTypeCliValue method 310 func (name *ParentName) SetFromCliFlag(raw string) error { 311 parsedName, err := ParseParentName(raw) 312 if err != nil { 313 return err 314 } 315 *name = *parsedName 316 return nil 317 } 318 319 type ParentReference struct { 320 ParentName 321 project *project.Project 322 } 323 324 func MakeParentReference(name *ParentName) (*ParentReference, error) { 325 return &ParentReference{ 326 ParentName: *name, 327 }, nil 328 } 329 330 func ParseParentReference(name string) (*ParentReference, error) { 331 parsedName, err := ParseParentName(name) 332 if err != nil { 333 return nil, err 334 } 335 return MakeParentReference(parsedName) 336 } 337 338 func MustParseParentReference(name string) *ParentReference { 339 result, err := ParseParentReference(name) 340 if err != nil { 341 panic(err) 342 } 343 return result 344 } 345 func (ref *ParentReference) GetProjectReference() *project.Reference { 346 if ref == nil { 347 return nil 348 } 349 350 switch ref.Pattern { 351 case NamePattern_Project_Region: 352 return project.NewNameBuilder(). 353 SetId(ref.ProjectId). 354 Reference() 355 default: 356 return nil 357 } 358 } 359 360 func (ref *ParentReference) GetUnderlyingReference() gotenresource.Reference { 361 if ref == nil { 362 return nil 363 } 364 projectRef := ref.GetProjectReference() 365 if projectRef != nil { 366 return projectRef 367 } 368 369 return nil 370 } 371 372 func (ref *ParentReference) ResolveRaw(res gotenresource.Resource) error { 373 switch typedRes := res.(type) { 374 case *project.Project: 375 if name := ref.GetProjectName(); name == nil { 376 return status.Errorf(codes.InvalidArgument, "cannot set Project as parent of DeviceDistributionCounter, because pattern does not match") 377 } 378 ref.project = typedRes 379 return nil 380 default: 381 return status.Errorf(codes.Internal, "Invalid parent type for DeviceDistributionCounter, got %s", reflect.TypeOf(res).Elem().Name()) 382 } 383 } 384 385 func (ref *ParentReference) Resolved() bool { 386 if name := ref.GetProjectName(); name != nil { 387 return ref.project != nil 388 } 389 return true 390 } 391 392 func (ref *ParentReference) ClearCached() { 393 ref.project = nil 394 } 395 396 func (ref *ParentReference) GetProject() *project.Project { 397 if ref == nil { 398 return nil 399 } 400 return ref.project 401 } 402 403 func (ref *ParentReference) GetRawResource() gotenresource.Resource { 404 if name := ref.ParentName.GetProjectName(); name != nil { 405 return ref.project 406 } 407 return nil 408 } 409 410 func (ref *ParentReference) IsFullyQualified() bool { 411 if ref == nil { 412 return false 413 } 414 return ref.ParentName.IsFullyQualified() 415 } 416 417 func (ref *ParentReference) IsSpecified() bool { 418 if ref == nil { 419 return false 420 } 421 return ref.ParentName.IsSpecified() 422 } 423 424 func (ref *ParentReference) GetResourceDescriptor() gotenresource.Descriptor { 425 return descriptor 426 } 427 428 func (ref *ParentReference) GetPattern() gotenresource.NamePattern { 429 if ref == nil { 430 return "" 431 } 432 return ref.Pattern 433 } 434 435 func (ref *ParentReference) GetIdParts() map[string]string { 436 if ref != nil { 437 return ref.ParentName.GetIdParts() 438 } 439 return map[string]string{ 440 "projectId": "", 441 "regionId": "", 442 } 443 } 444 445 func (ref *ParentReference) GetSegments() gotenresource.NameSegments { 446 if ref != nil { 447 return ref.ParentName.GetSegments() 448 } 449 return nil 450 } 451 452 func (ref *ParentReference) GetIParentName() gotenresource.Name { 453 return nil 454 } 455 456 func (ref *ParentReference) GetIUnderlyingParentName() gotenresource.Name { 457 return nil 458 } 459 460 func (ref *ParentReference) String() string { 461 if ref == nil { 462 return "<nil>" 463 } 464 return ref.ParentName.String() 465 } 466 467 // implement methods required by protobuf-go library for string-struct conversion 468 469 func (ref *ParentReference) ProtoString() (string, error) { 470 if ref == nil { 471 return "", nil 472 } 473 return ref.ParentName.ProtoString() 474 } 475 476 func (ref *ParentReference) ParseProtoString(data string) error { 477 parsed, err := ParseParentReference(data) 478 if err != nil { 479 return err 480 } 481 *ref = *parsed 482 return nil 483 } 484 485 // GotenEqual returns true if other is of same type and paths are equal (implements goten.Equaler interface) 486 func (ref *ParentReference) GotenEqual(other interface{}) bool { 487 if other == nil { 488 return ref == nil 489 } 490 other1, ok := other.(*ParentReference) 491 if !ok { 492 other2, ok := other.(ParentReference) 493 if ok { 494 other1 = &other2 495 } else { 496 return false 497 } 498 } 499 if other1 == nil { 500 return ref == nil 501 } else if ref == nil { 502 return false 503 } 504 if ref.project != other1.project { 505 return false 506 } 507 508 return ref.ParentName.GotenEqual(other1.ParentName) 509 } 510 511 // Matches is same as GotenEqual, but also will accept "other" if name is wildcard. 512 func (name *ParentReference) Matches(other interface{}) bool { 513 if other == nil { 514 return name == nil 515 } 516 other1, ok := other.(*ParentReference) 517 if !ok { 518 other2, ok := other.(ParentReference) 519 if ok { 520 other1 = &other2 521 } else { 522 return false 523 } 524 } 525 if other1 == nil { 526 return name == nil 527 } else if name == nil { 528 return false 529 } 530 return name.ParentName.Matches(&other1.ParentName) 531 } 532 533 // implement CustomTypeCliValue method 534 func (ref *ParentReference) SetFromCliFlag(raw string) error { 535 parsedRef, err := ParseParentReference(raw) 536 if err != nil { 537 return err 538 } 539 *ref = *parsedRef 540 return nil 541 }