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