Android-cuttlefish cvd tool
cf_configs_common.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2022 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18
19#include <string>
20#include <type_traits>
21#include <vector>
22
23#include "absl/strings/string_view.h"
24#include "fmt/format.h"
25#include "fmt/ranges.h" // NOLINT(misc-include-cleaner): version difference
26#include "google/protobuf/message.h"
27#include "json/json.h"
28
29#include "cuttlefish/host/commands/cvd/cli/parser/load_config.pb.h"
31
32namespace cuttlefish {
33
34inline constexpr absl::string_view kFetchPrefix = "@ab/";
35
36template <typename T>
37std::string GenerateFlag(const std::string& name, const T& value) {
38 return fmt::format("--{}={}", name, value);
39}
40
41template <typename T>
42std::string GenerateVecFlag(const std::string& name, const T& collection) {
43 return fmt::format("--{}={}", name, fmt::join(collection, ","));
44}
45
46template <typename T>
48 const std::string& name,
49 const cvd::config::EnvironmentSpecification& config, T callback) {
50 if constexpr (std::is_invocable_v<T, const cvd::config::Instance&, size_t>) {
51 std::vector<decltype(callback(config.instances()[0], 0))> values;
52 for (size_t i = 0; i < config.instances_size(); ++i) {
53 values.emplace_back(callback(config.instances(i), i));
54 }
55 return GenerateVecFlag(name, values);
56 } else {
57 std::vector<decltype(callback(config.instances()[0]))> values;
58 for (size_t i = 0; i < config.instances_size(); ++i) {
59 values.emplace_back(callback(config.instances(i)));
60 }
61 return GenerateVecFlag(name, values);
62 }
63}
64
65template <typename T>
67 const std::string& name,
68 const cvd::config::EnvironmentSpecification& config, T callback) {
69 std::vector<std::decay_t<decltype(*callback(config.instances()[0]))>> values;
70 for (const auto& instance : config.instances()) {
71 values.emplace_back(CF_EXPECT(callback(instance)));
72 }
73 return GenerateVecFlag(name, values);
74}
75
76std::vector<std::string> MergeResults(std::vector<std::string> first_list,
77 std::vector<std::string> scond_list);
78
79void MergeTwoJsonObjs(Json::Value& dst, const Json::Value& src);
80
81} // namespace cuttlefish
#define CF_EXPECT(...)
Definition: expect.h:184
EventFormat format
Definition: kernel_log_server.cc:61
Definition: alloc_driver.h:21
constexpr absl::string_view kFetchPrefix
Definition: cf_configs_common.h:34
std::vector< std::string > MergeResults(std::vector< std::string > first_list, std::vector< std::string > scond_list)
Definition: cf_configs_common.cpp:26
std::string GenerateFlag(const std::string &name, const T &value)
Definition: cf_configs_common.h:37
void MergeTwoJsonObjs(Json::Value &dst, const Json::Value &src)
This function merges two json objects and override json tree in dst with src json keys.
Definition: cf_configs_common.cpp:42
std::string GenerateInstanceFlag(const std::string &name, const cvd::config::EnvironmentSpecification &config, T callback)
Definition: cf_configs_common.h:47
tl::expected< T, StackTraceError< E > > Result
Definition: result_type.h:30
std::string GenerateVecFlag(const std::string &name, const T &collection)
Definition: cf_configs_common.h:42
Result< std::string > ResultInstanceFlag(const std::string &name, const cvd::config::EnvironmentSpecification &config, T callback)
Definition: cf_configs_common.h:66