102 lines
3.1 KiB
C++
102 lines
3.1 KiB
C++
#include "google/registry/ids.h"
|
|||
|
|
|
||
|
|
#include <vector>
|
||
|
|
|
||
|
|
#include "google/errors.h"
|
||
|
|
#include "google/registry/caps.h"
|
||
|
|
#include "google/resource.h"
|
||
|
|
|
||
|
|
namespace molo::google::registry {
|
||
|
|
|
||
|
|
namespace {
|
||
|
|
std::vector<std::string> split(const std::string& s) {
|
||
|
|
std::vector<std::string> out;
|
||
|
|
std::string cur;
|
||
|
|
for (char c : s) {
|
||
|
|
if (c == '/') {
|
||
|
|
out.push_back(cur);
|
||
|
|
cur.clear();
|
||
|
|
} else {
|
||
|
|
cur += c;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
out.push_back(cur);
|
||
|
|
return out;
|
||
|
|
}
|
||
|
|
bool known_collection(const std::string& c) {
|
||
|
|
return c == "agents" || c == "mcpServers" || c == "endpoints" ||
|
||
|
|
c == "skills" || c == "services";
|
||
|
|
}
|
||
|
|
} // namespace
|
||
|
|
|
||
|
|
bool Ids::service_id(const std::string& id) {
|
||
|
|
if (id.size() < RegistryCaps::kMinServiceId || id.size() > RegistryCaps::kMaxServiceId)
|
||
|
|
return false;
|
||
|
|
if (!(id[0] >= 'a' && id[0] <= 'z')) return false;
|
||
|
|
for (char c : id)
|
||
|
|
if (!((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '-')) return false;
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
void Ids::require_service_id(const std::string& id) {
|
||
|
|
if (!service_id(id))
|
||
|
|
throw GoogleError::invalid("service id must be 4-63 of a-z 0-9 - and start with a letter");
|
||
|
|
}
|
||
|
|
|
||
|
|
void Ids::require_segment(const std::string& id, const char* what) {
|
||
|
|
if (!valid_segment(id)) throw GoogleError::invalid(std::string("bad ") + what);
|
||
|
|
}
|
||
|
|
|
||
|
|
bool Ids::asset_name(const std::string& name) {
|
||
|
|
if (name.size() > 512) return false;
|
||
|
|
auto p = split(name);
|
||
|
|
if (p.size() != 6) return false;
|
||
|
|
return p[0] == "projects" && p[2] == "locations" && known_collection(p[4]) &&
|
||
|
|
valid_segment(p[1]) && valid_segment(p[3]) && valid_segment(p[5]);
|
||
|
|
}
|
||
|
|
|
||
|
|
void Ids::require_asset_name(const std::string& name) {
|
||
|
|
if (!asset_name(name))
|
||
|
|
throw GoogleError::invalid(
|
||
|
|
"asset name must be projects/{p}/locations/{l}/{agents|mcpServers|endpoints|skills|services}/{id}");
|
||
|
|
}
|
||
|
|
|
||
|
|
std::string Ids::collection_of(const std::string& name) {
|
||
|
|
require_asset_name(name);
|
||
|
|
return split(name)[4];
|
||
|
|
}
|
||
|
|
|
||
|
|
void Ids::require_template_name(const std::string& name) {
|
||
|
|
auto p = split(name);
|
||
|
|
if (name.size() > 512 || p.size() != 6 || p[0] != "projects" || p[2] != "locations" ||
|
||
|
|
p[4] != "templates" || !valid_segment(p[1]) || !valid_segment(p[3]) ||
|
||
|
|
!valid_segment(p[5]))
|
||
|
|
throw GoogleError::invalid("template must be projects/{p}/locations/{l}/templates/{t}");
|
||
|
|
}
|
||
|
|
|
||
|
|
void Ids::require_request_id(const std::string& id) {
|
||
|
|
if (id.empty()) return;
|
||
|
|
bool nonzero = false;
|
||
|
|
bool ok = id.size() == 36;
|
||
|
|
for (std::size_t i = 0; ok && i < id.size(); ++i) {
|
||
|
|
char c = id[i];
|
||
|
|
if (i == 8 || i == 13 || i == 18 || i == 23) {
|
||
|
|
ok = c == '-';
|
||
|
|
} else {
|
||
|
|
ok = (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F');
|
||
|
|
if (ok && c != '0') nonzero = true;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if (!ok || !nonzero) throw GoogleError::invalid("request id must be a non-zero UUID");
|
||
|
|
}
|
||
|
|
|
||
|
|
void Ids::require_text(const std::string& text, std::size_t max, const char* what) {
|
||
|
|
if (text.size() > max)
|
||
|
|
throw GoogleError::invalid(std::string(what) + " is too long");
|
||
|
|
for (unsigned char c : text)
|
||
|
|
if (c < 0x20 || c == 0x7f)
|
||
|
|
throw GoogleError::invalid(std::string(what) + " has control characters");
|
||
|
|
}
|
||
|
|
|
||
|
|
} // namespace molo::google::registry
|