1 // Copyright 2022 gRPC authors.
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 #ifndef GRPC_SRC_CORE_CONFIG_LOAD_CONFIG_H
16 #define GRPC_SRC_CORE_CONFIG_LOAD_CONFIG_H
17
18 #include <grpc/support/port_platform.h>
19 #include <stdint.h>
20
21 #include <string>
22 #include <vector>
23
24 #include "absl/flags/flag.h"
25 #include "absl/strings/string_view.h"
26 #include "absl/types/optional.h"
27
28 namespace grpc_core {
29
30 std::string LoadConfigFromEnv(absl::string_view environment_variable,
31 const char* default_value);
32 int32_t LoadConfigFromEnv(absl::string_view environment_variable,
33 int32_t default_value);
34 bool LoadConfigFromEnv(absl::string_view environment_variable,
35 bool default_value);
36
37 template <typename T, typename D>
LoadConfig(const absl::Flag<absl::optional<T>> & flag,absl::string_view environment_variable,const absl::optional<T> & override,D default_value)38 T LoadConfig(const absl::Flag<absl::optional<T>>& flag,
39 absl::string_view environment_variable,
40 const absl::optional<T>& override, D default_value) {
41 if (override.has_value()) return *override;
42 auto from_flag = absl::GetFlag(flag);
43 if (from_flag.has_value()) return std::move(*from_flag);
44 return LoadConfigFromEnv(environment_variable, default_value);
45 }
46
47 std::string LoadConfig(const absl::Flag<std::vector<std::string>>& flag,
48 absl::string_view environment_variable,
49 const absl::optional<std::string>& override,
50 const char* default_value);
51
52 } // namespace grpc_core
53
54 #endif // GRPC_SRC_CORE_CONFIG_LOAD_CONFIG_H
55