Added flag reader interface

PiperOrigin-RevId: 514551069
This commit is contained in:
Guogang Li
2023-03-06 15:55:10 -08:00
committed by Copybara-Service
parent f1ad8f4b9a
commit ead3df1ff8
5 changed files with 178 additions and 0 deletions
+1
View File
@@ -417,6 +417,7 @@ let package = Package(
"internal/platform/implementation/BUILD",
"internal/platform/BUILD",
"internal/analytics/BUILD",
"internal/flags/BUILD",
"internal/network/BUILD",
"internal/base/BUILD",
// tests
+26
View File
@@ -0,0 +1,26 @@
# Copyright 2023 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
licenses(["notice"])
cc_library(
name = "flag_reader",
hdrs = [
"default_flag_reader.h",
"flag.h",
"flag_reader.h",
],
visibility = ["//:__subpackages__"],
deps = ["@com_google_absl//absl/strings"],
)
+55
View File
@@ -0,0 +1,55 @@
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef THIRD_PARTY_NEARBY_INTERNAL_FLAGS_DEFAULT_FLAG_READER_H_
#define THIRD_PARTY_NEARBY_INTERNAL_FLAGS_DEFAULT_FLAG_READER_H_
#include <cstdint>
#include <string>
#include "absl/strings/string_view.h"
#include "internal/flags/flag_reader.h"
namespace nearby {
namespace flags {
class DefaultFlagReader : public FlagReader {
public:
~DefaultFlagReader() override = default;
// Returns the default bool value of the flag.
bool GetBoolFlag(const Flag<bool>& flag) override {
return flag.default_value();
}
// Returns the default int64_t value of the flag.
int64_t GetInt64Flag(const Flag<int64_t>& flag) override {
return flag.default_value();
}
// Returns the default double value of the flag.
double GetDoubleFlag(const Flag<double>& flag) override {
return flag.default_value();
}
// Returns the default string value of the flag.
std::string GetStringFlag(const Flag<absl::string_view>& flag) override {
return std::string(flag.default_value());
}
};
} // namespace flags
} // namespace nearby
#endif // THIRD_PARTY_NEARBY_INTERNAL_FLAGS_DEFAULT_FLAG_READER_H_
+45
View File
@@ -0,0 +1,45 @@
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef THIRD_PARTY_NEARBY_INTERNAL_FLAGS_FLAG_H_
#define THIRD_PARTY_NEARBY_INTERNAL_FLAGS_FLAG_H_
#include "absl/strings/string_view.h"
namespace nearby {
namespace flags {
template <typename T>
class Flag {
public:
constexpr Flag(const absl::string_view config_package_name,
const absl::string_view name, const T default_value)
: config_package_name_(config_package_name),
name_(name),
default_value_(default_value) {}
absl::string_view config_package_name() const { return config_package_name_; }
absl::string_view name() const { return name_; }
T default_value() const { return default_value_; }
private:
const absl::string_view config_package_name_;
const absl::string_view name_;
const T default_value_;
};
} // namespace flags
} // namespace nearby
#endif // THIRD_PARTY_NEARBY_INTERNAL_FLAGS_FLAG_H_
+51
View File
@@ -0,0 +1,51 @@
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef THIRD_PARTY_NEARBY_INTERNAL_FLAGS_FLAG_READER_H_
#define THIRD_PARTY_NEARBY_INTERNAL_FLAGS_FLAG_READER_H_
#include <cstdint>
#include <string>
#include "absl/strings/string_view.h"
#include "internal/flags/flag.h"
namespace nearby {
namespace flags {
// Allows caller to get flag values form backend system.
// The caller could implement flexiable control on Nearby SDK in granularity,
// such as feature, configuration etc. Default value will be returned if no
// flag-supported backend.
class FlagReader {
public:
virtual ~FlagReader() = default;
// Reads flag with boolean value.
virtual bool GetBoolFlag(const Flag<bool>& flag) = 0;
// Reads flag with int64_t value.
virtual int64_t GetInt64Flag(const Flag<int64_t>& flag) = 0;
// Reads flag with double value.
virtual double GetDoubleFlag(const Flag<double>& flag) = 0;
// Reads flag with string value.
virtual std::string GetStringFlag(const Flag<absl::string_view>& flag) = 0;
};
} // namespace flags
} // namespace nearby
#endif // THIRD_PARTY_NEARBY_INTERNAL_FLAGS_FLAG_READER_H_