1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
|
# -*- coding: utf-8 -*-
# Copyright 2021 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
HEADER_FILE_TEMPLATE = """\
// Generated from gen_validator.py. DO NOT EDIT!
// source: structured.xml
#ifndef {file.guard_path}
#define {file.guard_path}
#include <memory>
#include <optional>
#include <string>
#include <string_view>
#include <unordered_map>
#include "base/no_destructor.h"
#include "components/metrics/structured/project_validator.h"
namespace metrics {{
namespace structured {{
namespace validator {{
class Validators final {{
public:
Validators();
Validators(const Validators&) = delete;
Validators& operator=(const Validators&) = delete;
void Initialize();
const ProjectValidator*
GetProjectValidator(std::string_view project_name) const;
std::optional<std::string_view>
GetProjectName(uint64_t project_name_hash) const;
static Validators* Get();
private:
friend class base::NoDestructor<Validators>;
std::unordered_map<std::string_view, std::unique_ptr<ProjectValidator>>
validators_;
std::unordered_map<uint64_t, std::string_view> project_name_map_;
}};
}} // namespace validator
}} // namespace structured
}} // namespace metrics
#endif // {file.guard_path}\
"""
IMPL_FILE_TEMPLATE = """\
// Generated from gen_validator.py. DO NOT EDIT!
// source: structured.xml
#include "components/metrics/structured/structured_metrics_validator.h"
#include <cstdint>
#include <string>
#include "components/metrics/structured/enums.h"
#include "components/metrics/structured/event.h"
#include "components/metrics/structured/event_validator.h"
#include "components/metrics/structured/project_validator.h"
#include <optional>
#include "third_party/metrics_proto/structured_data.pb.h"
namespace metrics {{
namespace structured {{
namespace {{
//---------------------EventValidator Classes----------------------------------
{event_code}
//---------------------ProjectValidator Classes---------------------------------
{projects_code}
}}
namespace validator {{
Validators::Validators() {{
Initialize();
}}
void Validators::Initialize() {{
{project_map};
{name_map};
}}
const ProjectValidator*
Validators::GetProjectValidator(std::string_view project_name) const {{
const auto it = validators_.find(project_name);
if (it == validators_.end())
return nullptr;
return it->second.get();
}}
std::optional<std::string_view>
Validators::GetProjectName(uint64_t project_name_hash) const {{
const auto it = project_name_map_.find(project_name_hash);
if (it == project_name_map_.end())
return std::nullopt;
// This lookup will never fail.
return it->second;
}}
// static
Validators* Validators::Get() {{
static base::NoDestructor<Validators> validators;
return validators.get();
}}
}} // namespace validator
}} // namespace structured
}} // namespace metrics\
"""
IMPL_PROJECT_VALIDATOR_TEMPLATE = """\
class {project.validator} final :
public ::metrics::structured::ProjectValidator {{
public:
{project.validator}();
~{project.validator}() override;
void Initialize();
static constexpr uint64_t kProjectNameHash = UINT64_C({project.name_hash});
static constexpr IdType kIdType = IdType::{project.id_type};
static constexpr IdScope kIdScope = IdScope::{project.id_scope};
static constexpr EventType kEventType =
StructuredEventProto_EventType_{project.event_type};
static constexpr int kKeyRotationPeriod =
{project.key_rotation_period};
}};
{project.validator}::{project.validator}() :
::metrics::structured::ProjectValidator(
{project.validator}::kProjectNameHash,
{project.validator}::kIdType,
{project.validator}::kIdScope,
{project.validator}::kEventType,
{project.validator}::kKeyRotationPeriod
)
{{
Initialize();
}}
void {project.validator}::Initialize() {{
{event_validator_map};
{event_name_map};
}}
{project.validator}::~{project.validator}() = default;
"""
IMPL_EVENT_VALIDATOR_TEMPLATE = """\
class {event.validator_name} final :
public ::metrics::structured::EventValidator {{
public:
{event.validator_name}();
~{event.validator_name}();
void Initialize();
static constexpr uint64_t kEventNameHash = UINT64_C({event.name_hash});
}};
{event.validator_name}::{event.validator_name}() :
::metrics::structured::EventValidator({event.validator_name}::kEventNameHash,
{event.force_record})
{{
Initialize();
}}
{event.validator_name}::~{event.validator_name}() = default;
void {event.validator_name}::Initialize() {{
metric_metadata_ = {{
{metric_hash_map}
}};
metrics_name_map_ = {{
{metrics_name_map}
}};
}}
"""
|