| 737 | |
| 738 | |
| 739 | Try<vector<Resource>> Resources::fromJSON( |
| 740 | const JSON::Array& resourcesJSON, |
| 741 | const string& defaultRole) |
| 742 | { |
| 743 | // Convert the JSON Array into a protobuf message and use |
| 744 | // that to construct a vector of Resource object. |
| 745 | Try<RepeatedPtrField<Resource>> resourcesProtobuf = |
| 746 | protobuf::parse<RepeatedPtrField<Resource>>(resourcesJSON); |
| 747 | |
| 748 | if (resourcesProtobuf.isError()) { |
| 749 | return Error( |
| 750 | "Some JSON resources were not formatted properly: " + |
| 751 | resourcesProtobuf.error()); |
| 752 | } |
| 753 | |
| 754 | vector<Resource> result; |
| 755 | |
| 756 | foreach (Resource& resource, resourcesProtobuf.get()) { |
| 757 | // Set the default role if none was specified. |
| 758 | // |
| 759 | // NOTE: We rely on the fact that the result of this function is |
| 760 | // converted to the "post-reservation-refinement" format. |
| 761 | if (!resource.has_role() && resource.reservations_size() == 0) { |
| 762 | resource.set_role(defaultRole); |
| 763 | } |
| 764 | |
| 765 | upgradeResource(&resource); |
| 766 | |
| 767 | // We add the Resource object even if it is empty or invalid. |
| 768 | result.push_back(resource); |
| 769 | } |
| 770 | |
| 771 | return result; |
| 772 | } |
| 773 | |
| 774 | |
| 775 | Try<vector<Resource>> Resources::fromSimpleString( |