Google Cloud Spanner C++ Client  1.32.0
A C++ Client Library for Google Cloud Spanner
instance.cc
Go to the documentation of this file.
1 // Copyright 2019 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/spanner/instance.h"
16 #include <ostream>
17 #include <regex>
18 
19 namespace google {
20 namespace cloud {
21 namespace spanner {
22 inline namespace SPANNER_CLIENT_NS {
23 
24 Instance::Instance(Project project, std::string instance_id)
25  : project_(std::move(project)), instance_id_(std::move(instance_id)) {}
26 
27 Instance::Instance(std::string project_id, std::string instance_id)
28  : Instance(Project(std::move(project_id)), std::move(instance_id)) {}
29 
30 std::string Instance::FullName() const {
31  return project_.FullName() + "/instances/" + instance_id_;
32 }
33 
34 bool operator==(Instance const& a, Instance const& b) {
35  return a.project_ == b.project_ && a.instance_id_ == b.instance_id_;
36 }
37 
38 bool operator!=(Instance const& a, Instance const& b) { return !(a == b); }
39 
40 std::ostream& operator<<(std::ostream& os, Instance const& in) {
41  return os << in.FullName();
42 }
43 
44 StatusOr<Instance> MakeInstance(std::string const& full_name) {
45  std::regex re("projects/([^/]+)/instances/([^/]+)");
46  std::smatch matches;
47  if (!std::regex_match(full_name, matches, re)) {
49  "Improperly formatted Instance: " + full_name);
50  }
51  return Instance(Project(std::move(matches[1])), std::move(matches[2]));
52 }
53 
54 } // namespace SPANNER_CLIENT_NS
55 } // namespace spanner
56 } // namespace cloud
57 } // namespace google