From ead3df1ff87ad2014df0215d1e1249701eaf6b3d Mon Sep 17 00:00:00 2001 From: Guogang Li Date: Mon, 6 Mar 2023 15:53:03 -0800 Subject: [PATCH] Added flag reader interface PiperOrigin-RevId: 514551069 --- Package.swift | 1 + internal/flags/BUILD | 26 +++++++++++++ internal/flags/default_flag_reader.h | 55 ++++++++++++++++++++++++++++ internal/flags/flag.h | 45 +++++++++++++++++++++++ internal/flags/flag_reader.h | 51 ++++++++++++++++++++++++++ 5 files changed, 178 insertions(+) create mode 100644 internal/flags/BUILD create mode 100644 internal/flags/default_flag_reader.h create mode 100644 internal/flags/flag.h create mode 100644 internal/flags/flag_reader.h diff --git a/Package.swift b/Package.swift index 38a7cd4c..98318cbb 100644 --- a/Package.swift +++ b/Package.swift @@ -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 diff --git a/internal/flags/BUILD b/internal/flags/BUILD new file mode 100644 index 00000000..673d19a3 --- /dev/null +++ b/internal/flags/BUILD @@ -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"], +) diff --git a/internal/flags/default_flag_reader.h b/internal/flags/default_flag_reader.h new file mode 100644 index 00000000..bfffa674 --- /dev/null +++ b/internal/flags/default_flag_reader.h @@ -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 +#include + +#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& flag) override { + return flag.default_value(); + } + + // Returns the default int64_t value of the flag. + int64_t GetInt64Flag(const Flag& flag) override { + return flag.default_value(); + } + + // Returns the default double value of the flag. + double GetDoubleFlag(const Flag& flag) override { + return flag.default_value(); + } + + // Returns the default string value of the flag. + std::string GetStringFlag(const Flag& flag) override { + return std::string(flag.default_value()); + } +}; + +} // namespace flags +} // namespace nearby + +#endif // THIRD_PARTY_NEARBY_INTERNAL_FLAGS_DEFAULT_FLAG_READER_H_ diff --git a/internal/flags/flag.h b/internal/flags/flag.h new file mode 100644 index 00000000..a0423be3 --- /dev/null +++ b/internal/flags/flag.h @@ -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 +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_ diff --git a/internal/flags/flag_reader.h b/internal/flags/flag_reader.h new file mode 100644 index 00000000..eacf5a7c --- /dev/null +++ b/internal/flags/flag_reader.h @@ -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 +#include + +#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& flag) = 0; + + // Reads flag with int64_t value. + virtual int64_t GetInt64Flag(const Flag& flag) = 0; + + // Reads flag with double value. + virtual double GetDoubleFlag(const Flag& flag) = 0; + + // Reads flag with string value. + virtual std::string GetStringFlag(const Flag& flag) = 0; +}; + +} // namespace flags +} // namespace nearby + +#endif // THIRD_PARTY_NEARBY_INTERNAL_FLAGS_FLAG_READER_H_