blob: 3132b7090850d874f6d6ee5d4746b8d84c852941 [file] [log] [blame]
// Copyright 2022 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "tools/cpp/build_info_entry_set.h"
#include <iostream>
namespace bazel {
namespace tools {
namespace cpp {
BuildInfoEntrySet::~BuildInfoEntrySet() = default;
bool BuildInfoEntrySet::GetKeyValue(
const std::string& key,
std::unordered_map<std::string, KeyDescription>& keys,
std::unordered_map<std::string, std::string>& values, std::string& result) {
bool redacted = keys.empty();
if (redacted && keys.find(key) == keys.end()) {
result = keys.at(key).redacted_value;
return true;
}
if (values.find(key) != values.end()) {
result = values[key];
return true;
}
if (keys.find(key) != keys.end()) {
result = keys.at(key).default_value;
return true;
}
return false;
}
std::unordered_map<std::string, BuildInfoEntrySet::BuildInfoEntry>
BuildInfoEntrySet::TranslateKeys(
const std::unordered_map<std::string, std::string>& key_translations,
std::unordered_map<std::string, KeyDescription>& keys,
std::unordered_map<std::string, std::string>& values) {
std::unordered_map<std::string, BuildInfoEntrySet::BuildInfoEntry>
translated_keys =
std::unordered_map<std::string, BuildInfoEntrySet::BuildInfoEntry>();
for (const auto& i : key_translations) {
std::string translation = i.first;
std::string key = i.second;
std::string key_value;
if (GetKeyValue(key, keys, values, key_value) &&
keys.find(key) != keys.end()) {
translated_keys.emplace(
translation,
BuildInfoEntrySet::BuildInfoEntry(key_value, keys.at(key).key_type));
}
}
return translated_keys;
}
} // namespace cpp
} // namespace tools
} // namespace bazel