blob: 8bed1115440b4f3408df346bcf799b8fd7d370de [file] [log] [blame]
gnish24899172022-05-13 06:02:55 -07001// Copyright 2022 The Bazel Authors. All rights reserved.
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 "tools/cpp/build_info_entry_set.h"
16
17#include <iostream>
Googler9877fca2022-09-26 04:28:32 -070018#include <map>
gnish24899172022-05-13 06:02:55 -070019
20namespace bazel {
21namespace tools {
22namespace cpp {
23
24BuildInfoEntrySet::~BuildInfoEntrySet() = default;
25
26bool BuildInfoEntrySet::GetKeyValue(
27 const std::string& key,
28 std::unordered_map<std::string, KeyDescription>& keys,
29 std::unordered_map<std::string, std::string>& values, std::string& result) {
30 bool redacted = keys.empty();
31 if (redacted && keys.find(key) == keys.end()) {
32 result = keys.at(key).redacted_value;
33 return true;
34 }
35 if (values.find(key) != values.end()) {
36 result = values[key];
37 return true;
38 }
39 if (keys.find(key) != keys.end()) {
40 result = keys.at(key).default_value;
41 return true;
42 }
43 return false;
44}
45
Googlere3a8e5d2022-12-21 08:43:43 -080046void BuildInfoEntrySet::AddSlashes(std::string& key) {
47 for (std::string::iterator it = key.begin(); it != key.end(); it++) {
48 if (*it == ':') {
49 it = key.insert(it, '\\');
50 it++;
51 }
52 }
53}
54
Googler9877fca2022-09-26 04:28:32 -070055std::map<std::string, BuildInfoEntrySet::BuildInfoEntry>
gnish24899172022-05-13 06:02:55 -070056BuildInfoEntrySet::TranslateKeys(
Googler9877fca2022-09-26 04:28:32 -070057 const std::map<std::string, std::string>& translation_keys,
gnish24899172022-05-13 06:02:55 -070058 std::unordered_map<std::string, KeyDescription>& keys,
59 std::unordered_map<std::string, std::string>& values) {
Googler9877fca2022-09-26 04:28:32 -070060 std::map<std::string, BuildInfoEntrySet::BuildInfoEntry> translated_keys =
61 std::map<std::string, BuildInfoEntrySet::BuildInfoEntry>();
62 for (const auto& [translation, key] : translation_keys) {
gnish24899172022-05-13 06:02:55 -070063 std::string key_value;
64 if (GetKeyValue(key, keys, values, key_value) &&
65 keys.find(key) != keys.end()) {
Googlere3a8e5d2022-12-21 08:43:43 -080066 AddSlashes(key_value);
gnish24899172022-05-13 06:02:55 -070067 translated_keys.emplace(
68 translation,
69 BuildInfoEntrySet::BuildInfoEntry(key_value, keys.at(key).key_type));
70 }
71 }
72 return translated_keys;
73}
74} // namespace cpp
75} // namespace tools
76} // namespace bazel