(mr protoreflect.Message, shortMethod string)
| 484 | } |
| 485 | |
| 486 | func getResourceFromSingleRequest(mr protoreflect.Message, shortMethod string) string { |
| 487 | parentDesc := mr.Descriptor().Fields().ByName("parent") |
| 488 | if parentDesc != nil && proto.HasExtension(parentDesc.Options(), annotationsproto.E_ResourceReference) { |
| 489 | return mr.Get(parentDesc).String() |
| 490 | } |
| 491 | nameDesc := mr.Descriptor().Fields().ByName("name") |
| 492 | if nameDesc != nil && proto.HasExtension(nameDesc.Options(), annotationsproto.E_ResourceReference) { |
| 493 | return mr.Get(nameDesc).String() |
| 494 | } |
| 495 | // This is primarily used by Get/SetIAMPolicy(). |
| 496 | resourceFieldDesc := mr.Descriptor().Fields().ByName("resource") |
| 497 | if resourceFieldDesc != nil && proto.HasExtension(resourceFieldDesc.Options(), annotationsproto.E_ResourceReference) { |
| 498 | return mr.Get(resourceFieldDesc).String() |
| 499 | } |
| 500 | // This is primarily used by AddWebhook(). |
| 501 | projectFieldDesc := mr.Descriptor().Fields().ByName("project") |
| 502 | if projectFieldDesc != nil && proto.HasExtension(projectFieldDesc.Options(), annotationsproto.E_ResourceReference) { |
| 503 | return mr.Get(projectFieldDesc).String() |
| 504 | } |
| 505 | |
| 506 | // Listing top-level resources. |
| 507 | if strings.HasPrefix(shortMethod, "List") { |
| 508 | return "" |
| 509 | } |
| 510 | |
| 511 | isCreate := strings.HasPrefix(shortMethod, "Create") |
| 512 | isUpdate := strings.HasPrefix(shortMethod, "Update") |
| 513 | isRemove := strings.HasPrefix(shortMethod, "Remove") |
| 514 | isTest := strings.HasPrefix(shortMethod, "Test") |
| 515 | var resourceName string |
| 516 | if isCreate { |
| 517 | resourceName = strings.TrimPrefix(shortMethod, "Create") |
| 518 | } |
| 519 | if isUpdate { |
| 520 | resourceName = strings.TrimPrefix(shortMethod, "Update") |
| 521 | } |
| 522 | // RemoveWebhook. |
| 523 | if isRemove { |
| 524 | resourceName = strings.TrimPrefix(shortMethod, "Remove") |
| 525 | } |
| 526 | if isTest { |
| 527 | resourceName = strings.TrimPrefix(shortMethod, "Test") |
| 528 | } |
| 529 | resourceName = toSnakeCase(resourceName) |
| 530 | resourceDesc := mr.Descriptor().Fields().ByName(protoreflect.Name(resourceName)) |
| 531 | if resourceDesc == nil { |
| 532 | return "" |
| 533 | } |
| 534 | if proto.HasExtension(resourceDesc.Message().Options(), annotationsproto.E_Resource) { |
| 535 | // Parent-less resource. Return empty for Create() method (workspace resource). |
| 536 | if isCreate { |
| 537 | return "" |
| 538 | } |
| 539 | resourceValue := mr.Get(resourceDesc) |
| 540 | resourceNameDesc := resourceDesc.Message().Fields().ByName("name") |
| 541 | if resourceNameDesc != nil { |
| 542 | return resourceValue.Message().Get(resourceNameDesc).String() |
| 543 | } |
no test coverage detected