gnish | 2489917 | 2022-05-13 06:02:55 -0700 | [diff] [blame] | 1 | // 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> |
Googler | 9877fca | 2022-09-26 04:28:32 -0700 | [diff] [blame] | 18 | #include <map> |
gnish | 2489917 | 2022-05-13 06:02:55 -0700 | [diff] [blame] | 19 | |
| 20 | namespace bazel { |
| 21 | namespace tools { |
| 22 | namespace cpp { |
| 23 | |
| 24 | BuildInfoEntrySet::~BuildInfoEntrySet() = default; |
| 25 | |
| 26 | bool 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 | |
Googler | e3a8e5d | 2022-12-21 08:43:43 -0800 | [diff] [blame] | 46 | void 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 | |
Googler | 9877fca | 2022-09-26 04:28:32 -0700 | [diff] [blame] | 55 | std::map<std::string, BuildInfoEntrySet::BuildInfoEntry> |
gnish | 2489917 | 2022-05-13 06:02:55 -0700 | [diff] [blame] | 56 | BuildInfoEntrySet::TranslateKeys( |
Googler | 9877fca | 2022-09-26 04:28:32 -0700 | [diff] [blame] | 57 | const std::map<std::string, std::string>& translation_keys, |
gnish | 2489917 | 2022-05-13 06:02:55 -0700 | [diff] [blame] | 58 | std::unordered_map<std::string, KeyDescription>& keys, |
| 59 | std::unordered_map<std::string, std::string>& values) { |
Googler | 9877fca | 2022-09-26 04:28:32 -0700 | [diff] [blame] | 60 | std::map<std::string, BuildInfoEntrySet::BuildInfoEntry> translated_keys = |
| 61 | std::map<std::string, BuildInfoEntrySet::BuildInfoEntry>(); |
| 62 | for (const auto& [translation, key] : translation_keys) { |
gnish | 2489917 | 2022-05-13 06:02:55 -0700 | [diff] [blame] | 63 | std::string key_value; |
| 64 | if (GetKeyValue(key, keys, values, key_value) && |
| 65 | keys.find(key) != keys.end()) { |
Googler | e3a8e5d | 2022-12-21 08:43:43 -0800 | [diff] [blame] | 66 | AddSlashes(key_value); |
gnish | 2489917 | 2022-05-13 06:02:55 -0700 | [diff] [blame] | 67 | 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 |