| 468 | namespace internal { |
| 469 | |
| 470 | Option<Error> validateRoles(const FrameworkInfo& frameworkInfo) |
| 471 | { |
| 472 | bool multiRole = protobuf::frameworkHasCapability( |
| 473 | frameworkInfo, |
| 474 | FrameworkInfo::Capability::MULTI_ROLE); |
| 475 | |
| 476 | // Ensure that the right fields are used. |
| 477 | if (multiRole) { |
| 478 | if (frameworkInfo.has_role()) { |
| 479 | return Error("'FrameworkInfo.role' must not be set when the" |
| 480 | " framework is MULTI_ROLE capable"); |
| 481 | } |
| 482 | } else { |
| 483 | if (frameworkInfo.roles_size() > 0) { |
| 484 | return Error("'FrameworkInfo.roles' must not be set when the" |
| 485 | " framework is not MULTI_ROLE capable"); |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | // Check for duplicate entries. |
| 490 | // |
| 491 | // TODO(bmahler): Use a generic duplicate check function. |
| 492 | if (multiRole) { |
| 493 | const hashset<string> duplicateRoles = [&]() { |
| 494 | hashset<string> roles; |
| 495 | hashset<string> duplicates; |
| 496 | |
| 497 | foreach (const string& role, frameworkInfo.roles()) { |
| 498 | if (roles.contains(role)) { |
| 499 | duplicates.insert(role); |
| 500 | } else { |
| 501 | roles.insert(role); |
| 502 | } |
| 503 | } |
| 504 | |
| 505 | return duplicates; |
| 506 | }(); |
| 507 | |
| 508 | if (!duplicateRoles.empty()) { |
| 509 | return Error("'FrameworkInfo.roles' contains duplicate items: " + |
| 510 | stringify(duplicateRoles)); |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | // Validate the role(s). |
| 515 | if (multiRole) { |
| 516 | foreach (const string& role, frameworkInfo.roles()) { |
| 517 | Option<Error> error = roles::validate(role); |
| 518 | if (error.isSome()) { |
| 519 | return Error("'FrameworkInfo.roles' contains invalid role: " + |
| 520 | error->message); |
| 521 | } |
| 522 | } |
| 523 | } else { |
| 524 | Option<Error> error = roles::validate(frameworkInfo.role()); |
| 525 | if (error.isSome()) { |
| 526 | return Error("'FrameworkInfo.role' is not a valid role: " + |
| 527 | error->message); |