Google Cloud C++ Client  1.33.0
C++ Client Library for Google Cloud Platform
project.cc
Go to the documentation of this file.
1 // Copyright 2021 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "google/cloud/project.h"
16 #include <ostream>
17 #include <regex>
18 
19 namespace google {
20 namespace cloud {
22 
23 Project::Project(std::string project_id) : project_id_(std::move(project_id)) {}
24 
25 std::string Project::FullName() const { return "projects/" + project_id_; }
26 
27 bool operator==(Project const& a, Project const& b) {
28  return a.project_id_ == b.project_id_;
29 }
30 
31 bool operator!=(Project const& a, Project const& b) { return !(a == b); }
32 
33 std::ostream& operator<<(std::ostream& os, Project const& p) {
34  return os << p.FullName();
35 }
36 
37 StatusOr<Project> MakeProject(std::string const& full_name) {
38  std::regex re("projects/([^/]+)");
39  std::smatch matches;
40  if (!std::regex_match(full_name, matches, re)) {
42  "Improperly formatted Project: " + full_name);
43  }
44  return Project(std::move(matches[1]));
45 }
46 
48 } // namespace cloud
49 } // namespace google