From d6aace1a47ed41974a2916fd7576c59fbeeba9d2 Mon Sep 17 00:00:00 2001 From: YUE Daian Date: Wed, 27 Sep 2023 13:25:44 +0800 Subject: [PATCH] feat: implement evaluation details and re-design evaluation API (#24) Signed-off-by: YUE Daian Signed-off-by: YUE Daian Co-authored-by: Michael Beemer Co-authored-by: Justin Abrahms --- Cargo.toml | 1 - LICENSE | 202 ++++++++ README.md | 311 +++++++++++- src/api/api.rs | 301 +++++++++-- src/api/client.rs | 472 ++++++++++++++++-- src/api/global_evaluation_context.rs | 4 + src/api/mod.rs | 2 +- src/api/provider_registry.rs | 63 ++- src/evaluation/context.rs | 92 +++- ...{field_value.rs => context_field_value.rs} | 90 ++++ src/evaluation/details.rs | 144 ++++-- src/evaluation/error.rs | 57 +++ src/evaluation/mod.rs | 18 +- src/evaluation/options.rs | 3 + src/evaluation/value.rs | 4 +- src/lib.rs | 2 +- src/provider/details.rs | 17 +- src/provider/feature_provider.rs | 39 +- src/provider/fixed_value_provider.rs | 140 ------ src/provider/mod.rs | 5 +- src/provider/no_op_provider.rs | 400 +++++++++++++-- 21 files changed, 1983 insertions(+), 384 deletions(-) create mode 100644 LICENSE rename src/evaluation/{field_value.rs => context_field_value.rs} (73%) create mode 100644 src/evaluation/error.rs create mode 100644 src/evaluation/options.rs delete mode 100644 src/provider/fixed_value_provider.rs diff --git a/Cargo.toml b/Cargo.toml index c30b322..46289ca 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,5 +14,4 @@ tokio = { version = "1.32.0", features = [ "full" ] } typed-builder = "0.16.2" [dev-dependencies] -mockall = "0.11.4" spec = { path = "spec" } diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..d645695 --- /dev/null +++ b/LICENSE @@ -0,0 +1,202 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + 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 + + http://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. diff --git a/README.md b/README.md index 2867f73..c25f9e1 100644 --- a/README.md +++ b/README.md @@ -1,29 +1,310 @@ -# OpenFeature SDK for Rust + + +

+ + + OpenFeature Logo + +

-[![Project Status: WIP – Initial development is in progress, but there has not yet been a stable, usable release suitable for the public.](https://www.repostatus.org/badges/latest/wip.svg)](https://www.repostatus.org/#wip) +

OpenFeature Rust SDK

-This is the rust implementation of [OpenFeature](https://openfeature.dev), a vendor-agnostic abstraction library for evaluating feature flags. + + +

+ + Specification + + + -We support multiple data types for flags (numbers, strings, booleans, objects) as well as hooks, which can alter the lifecycle of a flag evaluation. + -## Installation + + + + +[OpenFeature](https://openfeature.dev) is an open standard that provides a vendor-agnostic, community-driven API for feature flagging that works with your favorite feature flag management tool. + + +## 🚀 Quick start + +### Requirements + +This package was built with Rust version `1.70.0`. Earlier versions might work, but is not guaranteed. + +### Install + +Add the following content to the `Cargo.toml` file: ```toml open-feature = { git = "https://github.com/open-feature/rust-sdk", branch = "main" } ``` -## Usage +### Usage + +#### Basic Usage + +```rust +#[derive(Clone, Default, Debug)] +struct MyStruct {} + +#[tokio::test] +async fn example() { + // Acquire an OpenFeature API instance. + // Note the `await` call here because asynchronous lock is used to guarantee thread safety. + let mut api = OpenFeature::singleton_mut().await; + + api.set_provider(NoOpProvider::builder().int_value(100).build()) + .await; + + // Create an unnamed client. + let client = api.create_client(); + + // Create an evaluation context. + // It supports types mentioned in the specification. + // + // You have multiple ways to add a custom field. + let evaluation_context = EvaluationContext::builder() + .targeting_key("Targeting") + .build() + .with_custom_field("bool_key", true) + .with_custom_field("int_key", 100) + .with_custom_field("float_key", 3.14) + .with_custom_field("string_key", "Hello".to_string()) + .with_custom_field("datetime_key", time::OffsetDateTime::now_utc()) + .with_custom_field( + "struct_key", + EvaluationContextFieldValue::Struct(Arc::new(MyStruct::default())), + ) + .with_custom_field("another_struct_key", Arc::new(MyStruct::default())) + .with_custom_field( + "yet_another_struct_key", + EvaluationContextFieldValue::new_struct(MyStruct::default()), + ); + + // This function returns a `Result`. You can process it with functions provided by std. + let is_feature_enabled = client + .get_bool_value("SomeFlagEnabled", Some(&evaluation_context), None) + .await + .unwrap_or(false); + + if is_feature_enabled { + // Do something. + } + + // Let's get evaluation details. + let result = client + .get_int_details( + "key", + Some(&EvaluationContext::default().with_custom_field("some_key", "some_value")), + None, + ) + .await; + + match result { + Ok(details) => { + assert_eq!(details.value, 100); + assert_eq!(details.reason, Some(EvaluationReason::Static)); + assert_eq!(details.variant, Some("Static".to_string())); + assert_eq!(details.flag_metadata.values.iter().count(), 2); + } + Err(error) => { + println!( + "Error: {}\nMessage: {:?}\n", + error.code.to_string(), + error.message + ); + } + } +} +``` + +#### Getting a Struct from a Provider + +It is possible to extract a struct from the provider. Internally, this SDK defines a type `StructValue` to store any structure value. The `client.get_struct_value()` functions takes a type parameter `T`. It will try to parse `StructValue` resolved by the provider to `T`, as long as `T` implements trait `FromStructValue`. + +You can pass in a type that satisfies this trait bound. When the conversion fails, it returns an `Err` with `EvaluationReason::TypeMismatch`. + +### API Reference + + + +## 🌟 Features + +| Status | Features | Description | +| ------ | ------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------- | +| ✅ | [Providers](#providers) | Integrate with a commercial, open source, or in-house feature management tool. | +| ✅ | [Targeting](#targeting) | Contextually-aware flag evaluation using [evaluation context](https://openfeature.dev/docs/reference/concepts/evaluation-context). | +| ❌ | [Hooks](#hooks) | Add functionality to various stages of the flag evaluation life-cycle. | +| ❌ | [Logging](#logging) | Integrate with popular logging packages. | +| ✅ | [Named clients](#named-clients) | Utilize multiple providers in a single application. | +| ❌ | [Eventing](#eventing) | React to state changes in the provider or flag management system. | +| ✅ | [Shutdown](#shutdown) | Gracefully clean up a provider during application shutdown. | +| ❌ | [Extending](#extending) | Extend OpenFeature with custom providers and hooks. | + +Implemented: ✅ | In-progress: ⚠️ | Not implemented yet: ❌ + +### Providers + +[Providers](https://openfeature.dev/docs/reference/concepts/provider) are an abstraction between a flag management system and the OpenFeature SDK. +Look [here](https://openfeature.dev/ecosystem?instant_search%5BrefinementList%5D%5Btype%5D%5B0%5D=Provider&instant_search%5BrefinementList%5D%5Btechnology%5D%5B0%5D=Rust) for a complete list of available providers. +If the provider you're looking for hasn't been created yet, see the [develop a provider](#develop-a-provider) section to learn how to build it yourself. + +Once you've added a provider as a dependency, it can be registered with OpenFeature like this: + +```rust +// Set the default feature provider. Please replace the `NoOpProvider` with the one you want. +// If you do not do that, [`NoOpProvider`] will be used by default. +// +// By default, [`NoOpProvider`] will simply return the default value of each type. +// You can inject value you want via its builder or evaluation context. See other sections +// for more details. +// +// If you set a new provider after creating some clients, the existing clients will pick up +// the new provider you just set. +// +// You must `await` it to let the provider's initialization to finish. +let mut api = OpenFeature::singleton_mut().await; +api.set_provider(NoOpProvider::default()).await; +``` + +In some situations, it may be beneficial to register multiple providers in the same application. +This is possible using [named clients](#named-clients), which is covered in more details below. + +### Targeting + +Sometimes, the value of a flag must consider some dynamic criteria about the application or user, such as the user's location, IP, email address, or the server's location. +In OpenFeature, we refer to this as [targeting](https://openfeature.dev/specification/glossary#targeting). +If the flag management system you're using supports targeting, you can provide the input data using the [evaluation context](https://openfeature.dev/docs/reference/concepts/evaluation-context). + +```rust +// Create a global evaluation context and set it into the API. +// Note that this is optional. By default it uses an empty one. +let mut api = OpenFeature::singleton_mut().await; +api.set_evaluation_context(global_evaluation_context).await; + +// Set client level evaluation context. +// It will overwrite the global one for the existing keys. +let mut client = api.create_client(); +client.set_evaluation_context(client_evaluation_context); + +// Pass evaluation context in evaluation functions. +// This one will overwrite the globla evaluation context and +// the client level one. +client.get_int_value("flag", &evaluation_context, None); +``` + +### Hooks + +[Hooks](https://openfeature.dev/docs/reference/concepts/hooks) allow for custom logic to be added at well-defined points of the flag evaluation life-cycle. +Look [here](https://openfeature.dev/ecosystem/?instant_search%5BrefinementList%5D%5Btype%5D%5B0%5D=Hook&instant_search%5BrefinementList%5D%5Btechnology%5D%5B0%5D=Rust) for a complete list of available hooks. +If the hook you're looking for hasn't been created yet, see the [develop a hook](#develop-a-hook) section to learn how to build it yourself. + +Once you've added a hook as a dependency, it can be registered at the global, client, or flag invocation level. + + + +### Logging + + + +### Named clients + +Clients can be given a name. +A name is a logical identifier which can be used to associate clients with a particular provider. +If a name has no associated provider, the global provider is used. + +```rust +// Create a named provider and bind it. +api.set_named_provider( + "named", + NoOpProvider::builder().int_value(42).build()) +.await; + +// This named client will use the feature provider bound to this name. +let client = api.create_named_client("named"); + +assert_eq!(client.get_int_value("key", None, None).await.unwrap(), 42); +``` + +### Eventing + + + + + +### Shutdown + +The OpenFeature API provides a close function to perform a cleanup of all registered providers. +This should only be called when your application is in the process of shutting down. + +```rust +// This will clean all the registered providers and invokes their `shutdown()` function. +let api = OpenFeature::singleton_mut().await; +api.shutdown(); +``` + +## Extending + +### Develop a provider + +To develop a provider, you need to create a new project and include the OpenFeature SDK as a dependency. +This can be a new repository or included in [the existing contrib repository](https://github.com/open-feature/rust-sdk-contrib) available under the OpenFeature organization. +You’ll then need to write the provider by implementing the `FeatureProvider` interface exported by the OpenFeature SDK. + +Check the source of [`NoOpProvider`](https://github.com/open-feature/rust-sdk/blob/main/src/provider/no_op_provider.rs) for an example. + +> Built a new provider? [Let us know](https://github.com/open-feature/openfeature.dev/issues/new?assignees=&labels=provider&projects=&template=document-provider.yaml&title=%5BProvider%5D%3A+) so we can add it to the docs! + +### Develop a hook + +To develop a hook, you need to create a new project and include the OpenFeature SDK as a dependency. +This can be a new repository or included in [the existing contrib repository](https://github.com/open-feature/rust-sdk-contrib) available under the OpenFeature organization. +Implement your own hook by conforming to the `Hook interface`. +To satisfy the interface, all methods (`Before`/`After`/`Finally`/`Error`) need to be defined. +To avoid defining empty functions make use of the `UnimplementedHook` struct (which already implements all the empty functions). + + + +> Built a new hook? [Let us know](https://github.com/open-feature/openfeature.dev/issues/new?assignees=&labels=hook&projects=&template=document-hook.yaml&title=%5BHook%5D%3A+) so we can add it to the docs! + + +## ⭐️ Support the project + +- Give this repo a ⭐️! +- Follow us on social media: + - Twitter: [@openfeature](https://twitter.com/openfeature) + - LinkedIn: [OpenFeature](https://www.linkedin.com/company/openfeature/) +- Join us on [Slack](https://cloud-native.slack.com/archives/C0344AANLA1) +- For more, check out our [community page](https://openfeature.dev/community/) + +## 🤝 Contributing -### Initialization +Interested in contributing? Great, we'd love your help! To get started, take a look at the [CONTRIBUTING](CONTRIBUTING.md) guide. -TBD +### Thanks to everyone that has already contributed -## Roadmap + + Pictures of the folks who have contributed to the project + -## Pending Feature List -- Some requirements of Flag Evaluation API. -- Provider hooks (2.3) -- Evaluation context levels and merging (3.2) -- Hooks (4) -- Events (5) +Made with [contrib.rocks](https://contrib.rocks). + diff --git a/src/api/api.rs b/src/api/api.rs index 0246a75..0fa13c7 100644 --- a/src/api/api.rs +++ b/src/api/api.rs @@ -3,7 +3,7 @@ use tokio::sync::{RwLock, RwLockReadGuard, RwLockWriteGuard}; use crate::{ provider::{FeatureProvider, ProviderMetadata}, - Client, + Client, EvaluationContext, }; use super::{ @@ -36,6 +36,14 @@ impl OpenFeature { SINGLETON.write().await } + /// Set the global evaluation context. + pub async fn set_evaluation_context(&mut self, evaluation_context: EvaluationContext) { + let mut context = self.evaluation_context.get_mut().await; + + context.targeting_key = evaluation_context.targeting_key; + context.custom_fields = evaluation_context.custom_fields; + } + /// Set the default provider. pub async fn set_provider(&mut self, provider: T) { self.provider_registry.set_default(provider).await; @@ -65,7 +73,7 @@ impl OpenFeature { } /// Create a new client with default name. - pub fn get_client(&self) -> Client { + pub fn create_client(&self) -> Client { Client::new( String::default(), self.evaluation_context.clone(), @@ -75,7 +83,7 @@ impl OpenFeature { /// Create a new client with specific `name`. /// It will use the provider bound to this name, if any. - pub fn get_named_client(&self, name: &str) -> Client { + pub fn create_named_client(&self, name: &str) -> Client { Client::new( name.to_string(), self.evaluation_context.clone(), @@ -91,15 +99,19 @@ impl OpenFeature { #[cfg(test)] mod tests { + use std::sync::Arc; + use super::*; - use crate::provider::*; + use crate::{ + provider::NoOpProvider, EvaluationContextFieldValue, EvaluationReason, FlagMetadataValue, + }; use spec::spec; - #[tokio::test] #[spec( number = "1.1.1", text = "The API, and any state it maintains SHOULD exist as a global singleton, even in cases wherein multiple versions of the API are present at runtime." )] + #[tokio::test] async fn singleton_multi_thread() { let reader1 = tokio::spawn(async move { let _ = OpenFeature::singleton().await.provider_metadata(); @@ -108,7 +120,7 @@ mod tests { let writer = tokio::spawn(async move { OpenFeature::singleton_mut() .await - .set_provider(FixedValueProvider::default()) + .set_provider(NoOpProvider::default()) .await; }); @@ -119,7 +131,7 @@ mod tests { let _ = (reader1.await, reader2.await, writer.await); assert_eq!( - "Fixed Value", + "No Operation", OpenFeature::singleton() .await .provider_metadata() @@ -128,100 +140,313 @@ mod tests { ); } - #[tokio::test] #[spec( number = "1.1.2.1", text = "The API MUST define a provider mutator, a function to set the default provider, which accepts an API-conformant provider implementation." )] + #[tokio::test] async fn set_provider() { let mut api = OpenFeature::default(); - let client = api.get_client(); + let client = api.create_client(); - assert_eq!(client.get_int_value("some-key", 32, None).await, 32); + assert_eq!( + client.get_int_value("some-key", None, None).await.unwrap(), + i64::default() + ); // Set the new provider and ensure the value comes from it. - let provider = FixedValueProvider::builder().int_value(200).build(); + let provider = NoOpProvider::builder().int_value(200).build(); api.set_provider(provider).await; - assert_eq!(client.get_int_value("some-key", 100, None).await, 200); + assert_eq!( + client.get_int_value("some-key", None, None).await.unwrap(), + 200 + ); } - #[tokio::test] #[spec( number = "1.1.2.2", text = "The provider mutator function MUST invoke the initialize function on the newly registered provider before using it to resolve flag values." )] + #[tokio::test] async fn set_provider_invoke_initialize() { - let mut provider = MockFeatureProvider::new(); - provider.expect_initialize().once().returning(|_| ()); + let provider = NoOpProvider::default(); + + assert_eq!(provider.metadata().name, "No Operation - Default"); let mut api = OpenFeature::default(); api.set_provider(provider).await; + + assert_eq!(api.provider_metadata().await.name, "No Operation"); } - #[tokio::test] + #[spec( + number = "1.1.2.3", + text = "The provider mutator function MUST invoke the shutdown function on the previously registered provider once it's no longer being used to resolve flag values." + )] + #[test] + fn invoke_shutdown_on_old_provider_checked_by_type_system() {} + #[spec( number = "1.1.3", text = "The API MUST provide a function to bind a given provider to one or more client names. If the client-name already has a bound provider, it is overwritten with the new mapping." )] + #[tokio::test] async fn set_named_provider() { let mut api = OpenFeature::default(); - api.set_named_provider("test", NoOpProvider::default()) + api.set_named_provider("test", NoOpProvider::builder().int_value(10).build()) .await; // Ensure the No-op provider is used. - let client = api.get_named_client("test"); - assert_eq!(client.get_int_value("", 10, None).await, 10); + let client = api.create_named_client("test"); + assert_eq!(client.get_int_value("", None, None).await.unwrap(), 10); - // Bind FixedValueProvider to the same name. - api.set_named_provider("test", FixedValueProvider::builder().int_value(30).build()) + // Bind provider to the same name. + api.set_named_provider("test", NoOpProvider::builder().int_value(30).build()) .await; - // Ensure the FixedValueProvider is used for existing clients. - assert_eq!(client.get_int_value("", 10, None).await, 30); + // Ensure the new provider is used for existing clients. + assert_eq!(client.get_int_value("", None, None).await.unwrap(), 30); - // Create a new client and ensure FixedValueProvideris used. - let new_client = api.get_named_client("test"); - assert_eq!(new_client.get_int_value("", 10, None).await, 30); + // Create a new client and ensure new provider is used. + let new_client = api.create_named_client("test"); + assert_eq!(new_client.get_int_value("", None, None).await.unwrap(), 30); } - #[tokio::test] - #[spec( - number = "1.1.4", - text = "The API MUST provide a function to add hooks which accepts one or more API-conformant hooks, and appends them to the collection of any previously added hooks. When new hooks are added, previously added hooks are not removed." - )] - async fn add_hooks() { - // Not implemented. - } - - #[tokio::test] #[spec( number = "1.1.5", text = "The API MUST provide a function for retrieving the metadata field of the configured provider." )] + #[tokio::test] async fn provider_metadata() { let mut api = OpenFeature::default(); api.set_provider(NoOpProvider::default()).await; - api.set_named_provider("test", FixedValueProvider::default()) + api.set_named_provider("test", NoOpProvider::default()) .await; assert_eq!(api.provider_metadata().await.name, "No Operation"); assert_eq!( api.named_provider_metadata("test").await.unwrap().name, - "Fixed Value" + "No Operation" ); assert!(api.named_provider_metadata("invalid").await.is_none()); } + #[spec( + number = "1.1.6", + text = "The API MUST provide a function for creating a client which accepts the following options: + * name (optional): A logical string identifier for the client." + )] #[tokio::test] + async fn get_client() { + let mut api = OpenFeature::default(); + api.set_provider(NoOpProvider::builder().int_value(100).build()) + .await; + api.set_named_provider("test", NoOpProvider::builder().int_value(200).build()) + .await; + + let client = api.create_client(); + assert_eq!(client.get_int_value("key", None, None).await.unwrap(), 100); + + let client = api.create_named_client("test"); + assert_eq!(client.get_int_value("key", None, None).await.unwrap(), 200); + + let client = api.create_named_client("another"); + assert_eq!(client.get_int_value("test", None, None).await.unwrap(), 100); + } + + #[spec( + number = "1.1.7", + text = "The client creation function MUST NOT throw, or otherwise abnormally terminate." + )] + #[test] + fn get_client_not_throw_checked_by_type_system() {} + + #[spec( + number = "1.1.8", + text = "The API SHOULD provide functions to set a provider and wait for the initialize function to return or throw." + )] + #[tokio::test] + async fn set_provider_should_block() { + let mut api = OpenFeature::default(); + api.set_provider(NoOpProvider::default()).await; + + api.set_named_provider("named", NoOpProvider::default()) + .await; + } + #[spec( number = "1.6.1", text = "The API MUST define a shutdown function which, when called, must call the respective shutdown function on the active provider." )] + #[tokio::test] async fn shutdown() { let mut api = OpenFeature::default(); api.set_provider(NoOpProvider::default()).await; api.shutdown().await; } + + #[spec( + number = "3.2.1.1", + text = "The API, Client and invocation MUST have a method for supplying evaluation context." + )] + #[spec( + number = "3.2.3", + text = "Evaluation context MUST be merged in the order: API (global; lowest precedence) -> client -> invocation -> before hooks (highest precedence), with duplicate values being overwritten." + )] + #[tokio::test] + async fn evaluation_context() { + // Set global client context and ensure its values are picked up. + let evaluation_context = EvaluationContext::builder() + .targeting_key("global_targeting_key") + .build() + .with_custom_field("key", "global_value"); + + let mut api = OpenFeature::default(); + api.set_evaluation_context(evaluation_context).await; + + let mut client = api.create_client(); + + let result = client.get_int_details("", None, None).await.unwrap(); + + assert_eq!( + *result.flag_metadata.values.get("TargetingKey").unwrap(), + FlagMetadataValue::String("global_targeting_key".to_string()) + ); + + assert_eq!( + *result.flag_metadata.values.get("key").unwrap(), + FlagMetadataValue::String("global_value".to_string()) + ); + + // Set client evaluation context and ensure its values overwrite the global ones. + let evaluation_context = EvaluationContext::builder() + .targeting_key("client_targeting_key") + .build() + .with_custom_field("key", "client_value"); + + client.set_evaluation_context(evaluation_context); + + let result = client.get_bool_details("", None, None).await.unwrap(); + + assert_eq!( + *result.flag_metadata.values.get("TargetingKey").unwrap(), + FlagMetadataValue::String("client_targeting_key".to_string()) + ); + + assert_eq!( + *result.flag_metadata.values.get("key").unwrap(), + FlagMetadataValue::String("client_value".to_string()) + ); + + // Use invocation level evaluation context and ensure its values are used. + let evaluation_context = EvaluationContext::builder() + .targeting_key("invocation_targeting_key") + .build() + .with_custom_field("key", "invocation_value"); + + let result = client + .get_string_details("", Some(&evaluation_context), None) + .await + .unwrap(); + + assert_eq!( + *result.flag_metadata.values.get("TargetingKey").unwrap(), + FlagMetadataValue::String("invocation_targeting_key".to_string()) + ); + + assert_eq!( + *result.flag_metadata.values.get("key").unwrap(), + FlagMetadataValue::String("invocation_value".to_string()) + ); + } + + #[spec( + number = "3.2.2.1", + text = "The API MUST have a method for setting the global evaluation context." + )] + #[spec( + number = "3.2.2.2", + text = "The Client and invocation MUST NOT have a method for supplying evaluation context." + )] + #[spec( + number = "3.2.4.1", + text = "When the global evaluation context is set, the on context changed handler MUST run." + )] + #[test] + fn static_context_not_applicable() {} + + #[derive(Clone, Default, Debug)] + struct MyStruct {} + + #[tokio::test] + async fn example() { + // Acquire an OpenFeature API instance. + // Note the `await` call here because asynchronous lock is used to guarantee thread safety. + let mut api = OpenFeature::singleton_mut().await; + + api.set_provider(NoOpProvider::builder().int_value(100).build()) + .await; + + // Create an unnamed client. + let client = api.create_client(); + + // Create an evaluation context. + // It supports types mentioned in the specification. + // + // You have multiple ways to add a custom field. + let evaluation_context = EvaluationContext::builder() + .targeting_key("Targeting") + .build() + .with_custom_field("bool_key", true) + .with_custom_field("int_key", 100) + .with_custom_field("float_key", 3.14) + .with_custom_field("string_key", "Hello".to_string()) + .with_custom_field("datetime_key", time::OffsetDateTime::now_utc()) + .with_custom_field( + "struct_key", + EvaluationContextFieldValue::Struct(Arc::new(MyStruct::default())), + ) + .with_custom_field("another_struct_key", Arc::new(MyStruct::default())) + .with_custom_field( + "yet_another_struct_key", + EvaluationContextFieldValue::new_struct(MyStruct::default()), + ); + + // This function returns a `Result`. You can process it with functions provided by std. + let is_feature_enabled = client + .get_bool_value("SomeFlagEnabled", Some(&evaluation_context), None) + .await + .unwrap_or(false); + + if is_feature_enabled { + // Do something. + } + + // Let's get evaluation details. + let result = client + .get_int_details( + "key", + Some(&EvaluationContext::default().with_custom_field("some_key", "some_value")), + None, + ) + .await; + + match result { + Ok(details) => { + assert_eq!(details.value, 100); + assert_eq!(details.reason, Some(EvaluationReason::Static)); + assert_eq!(details.variant, Some("Static".to_string())); + assert_eq!(details.flag_metadata.values.iter().count(), 2); + } + Err(error) => { + println!( + "Error: {}\nMessage: {:?}\n", + error.code.to_string(), + error.message + ); + } + } + } } diff --git a/src/api/client.rs b/src/api/client.rs index 98c130b..0059304 100644 --- a/src/api/client.rs +++ b/src/api/client.rs @@ -1,8 +1,13 @@ -use crate::{EvaluationContext, StructValue}; +use std::sync::Arc; + +use crate::{ + provider::{FeatureProvider, ResolutionDetails}, + EvaluationContext, EvaluationDetails, EvaluationError, EvaluationErrorCode, EvaluationOptions, + EvaluationResult, StructValue, +}; use super::{ - global_evaluation_context::GlobalEvaluationContext, - provider_registry::{FeatureProviderWrapper, ProviderRegistry}, + global_evaluation_context::GlobalEvaluationContext, provider_registry::ProviderRegistry, }; /// The metadata of OpenFeature client. @@ -19,7 +24,12 @@ pub struct Client { global_evaluation_context: GlobalEvaluationContext, } +pub trait FromStructValue { + fn from_struct_value(value: &StructValue) -> anyhow::Result; +} + impl Client { + /// Create a new [`Client`] instance. pub fn new( name: impl Into, global_evaluation_context: GlobalEvaluationContext, @@ -33,101 +43,231 @@ impl Client { } } + /// Return the metadata of current client. pub fn metadata(&self) -> &ClientMetadata { &self.metadata } + pub fn set_evaluation_context(&mut self, evaluation_context: EvaluationContext) { + self.evaluation_context = evaluation_context; + } + + /// Evaluate given `flag_key` with corresponding `evaluation_context` and `evaluation_options` + /// as a bool value. + #[allow(unused_variables)] pub async fn get_bool_value( &self, flag_key: &str, - default_value: bool, evaluation_context: Option<&EvaluationContext>, - ) -> bool { + evaluation_options: Option<&EvaluationOptions>, + ) -> EvaluationResult { let context = self.merge_evaluation_context(evaluation_context).await; - self.get_provider() - .await - .get() - .resolve_bool_value(flag_key, default_value, &context) + Ok(self + .get_provider() .await - .value + .resolve_bool_value(flag_key, &context) + .await? + .value) } + /// Evaluate given `flag_key` with corresponding `evaluation_context` and `evaluation_options` + /// as an int (i64) value. + #[allow(unused_variables)] pub async fn get_int_value( &self, flag_key: &str, - default_value: i64, evaluation_context: Option<&EvaluationContext>, - ) -> i64 { + evaluation_options: Option<&EvaluationOptions>, + ) -> EvaluationResult { let context = self.merge_evaluation_context(evaluation_context).await; - self.get_provider() - .await - .get() - .resolve_int_value(flag_key, default_value, &context) + Ok(self + .get_provider() .await - .value + .resolve_int_value(flag_key, &context) + .await? + .value) } + /// Evaluate given `flag_key` with corresponding `evaluation_context` and `evaluation_options` + /// as a float (f64) value. + /// If the resolution fails, the `default_value` is returned. + #[allow(unused_variables)] pub async fn get_float_value( &self, flag_key: &str, - default_value: f64, evaluation_context: Option<&EvaluationContext>, - ) -> f64 { + evaluation_options: Option<&EvaluationOptions>, + ) -> EvaluationResult { let context = self.merge_evaluation_context(evaluation_context).await; - self.get_provider() - .await - .get() - .resolve_float_value(flag_key, default_value, &context) + Ok(self + .get_provider() .await - .value + .resolve_float_value(flag_key, &context) + .await? + .value) } + /// Evaluate given `flag_key` with corresponding `evaluation_context` and `evaluation_options` + /// as a string value. + /// If the resolution fails, the `default_value` is returned. + #[allow(unused_variables)] pub async fn get_string_value( &self, flag_key: &str, - default_value: &str, evaluation_context: Option<&EvaluationContext>, - ) -> String { + evaluation_options: Option<&EvaluationOptions>, + ) -> EvaluationResult { + let context = self.merge_evaluation_context(evaluation_context).await; + + Ok(self + .get_provider() + .await + .resolve_string_value(flag_key, &context) + .await? + .value) + } + + /// Evaluate given `flag_key` with corresponding `evaluation_context` and `evaluation_options` + /// as a struct. + /// If the resolution fails, the `default_value` is returned. + /// The required type should implement [`From`] trait. + #[allow(unused_variables)] + pub async fn get_struct_value( + &self, + flag_key: &str, + evaluation_context: Option<&EvaluationContext>, + evaluation_options: Option<&EvaluationOptions>, + ) -> EvaluationResult { + let context = self.merge_evaluation_context(evaluation_context).await; + + let result = self + .get_provider() + .await + .resolve_struct_value(flag_key, &context) + .await?; + + match T::from_struct_value(&result.value) { + Ok(t) => Ok(t), + Err(error) => Err(EvaluationError { + code: EvaluationErrorCode::TypeMismatch, + message: Some("Unable to cast value to required type".to_string()), + }), + } + } + + /// Return the [`EvaluationDetails`] with given `flag_key`, `evaluation_context` and + /// `evaluation_options`. + #[allow(unused_variables)] + pub async fn get_bool_details( + &self, + flag_key: &str, + evaluation_context: Option<&EvaluationContext>, + evaluation_options: Option<&EvaluationOptions>, + ) -> EvaluationResult> { + let context = self.merge_evaluation_context(evaluation_context).await; + + Ok(self + .get_provider() + .await + .resolve_bool_value(flag_key, &context) + .await? + .to_evaluation_details(flag_key)) + } + + /// Return the [`EvaluationDetails`] with given `flag_key`, `evaluation_context` and + /// `evaluation_options`. + #[allow(unused_variables)] + pub async fn get_int_details( + &self, + flag_key: &str, + evaluation_context: Option<&EvaluationContext>, + evaluation_options: Option<&EvaluationOptions>, + ) -> EvaluationResult> { let context = self.merge_evaluation_context(evaluation_context).await; - self.get_provider() + Ok(self + .get_provider() .await - .get() - .resolve_string_value(flag_key, default_value, &context) + .resolve_int_value(flag_key, &context) + .await? + .to_evaluation_details(flag_key)) + } + + /// Return the [`EvaluationDetails`] with given `flag_key`, `evaluation_context` and + /// `evaluation_options`. + #[allow(unused_variables)] + pub async fn get_float_details( + &self, + flag_key: &str, + evaluation_context: Option<&EvaluationContext>, + evaluation_options: Option<&EvaluationOptions>, + ) -> EvaluationResult> { + let context = self.merge_evaluation_context(evaluation_context).await; + + Ok(self + .get_provider() .await - .value + .resolve_float_value(flag_key, &context) + .await? + .to_evaluation_details(flag_key)) } - pub async fn get_struct_value( + /// Return the [`EvaluationDetails`] with given `flag_key`, `evaluation_context` and + /// `evaluation_options`. + #[allow(unused_variables)] + pub async fn get_string_details( &self, flag_key: &str, - default_value: T, evaluation_context: Option<&EvaluationContext>, - ) -> T - where - T: From, - { + evaluation_options: Option<&EvaluationOptions>, + ) -> EvaluationResult> { + let context = self.merge_evaluation_context(evaluation_context).await; + + Ok(self + .get_provider() + .await + .resolve_string_value(flag_key, &context) + .await? + .to_evaluation_details(flag_key)) + } + + /// Return the [`EvaluationDetails`] with given `flag_key`, `evaluation_context` and + /// `evaluation_options`. + #[allow(unused_variables)] + pub async fn get_struct_details( + &self, + flag_key: &str, + evaluation_context: Option<&EvaluationContext>, + evaluation_options: Option<&EvaluationOptions>, + ) -> EvaluationResult> { let context = self.merge_evaluation_context(evaluation_context).await; let result = self .get_provider() .await - .get() - .resolve_struct_value(flag_key, StructValue::default(), &context) - .await; - - if result.is_error() { - default_value - } else { - result.value.into() + .resolve_struct_value(flag_key, &context) + .await?; + + match T::from_struct_value(&result.value) { + Ok(value) => Ok(EvaluationDetails { + flag_key: flag_key.to_string(), + value, + reason: result.reason, + variant: result.variant, + flag_metadata: result.flag_metadata.unwrap_or_default(), + }), + Err(error) => Err(EvaluationError { + code: EvaluationErrorCode::TypeMismatch, + message: Some("Unable to cast value to required type".to_string()), + }), } } - async fn get_provider(&self) -> FeatureProviderWrapper { - self.provider_registry.get(&self.metadata.name).await + async fn get_provider(&self) -> Arc { + self.provider_registry.get(&self.metadata.name).await.get() } /// Merge provided `flag_evaluation_context` (that is passed when evaluating a flag) with @@ -151,29 +291,259 @@ impl Client { } } +impl ResolutionDetails { + fn to_evaluation_details(self, flag_key: impl Into) -> EvaluationDetails { + EvaluationDetails { + flag_key: flag_key.into(), + value: self.value, + reason: self.reason, + variant: self.variant, + flag_metadata: self.flag_metadata.unwrap_or_default(), + } + } +} + #[cfg(test)] mod tests { + use std::sync::Arc; + use spec::spec; use crate::{ api::{ global_evaluation_context::GlobalEvaluationContext, provider_registry::ProviderRegistry, }, - Client, + provider::NoOpProvider, + Client, EvaluationReason, FlagMetadata, StructValue, }; + use super::FromStructValue; + #[spec( number = "1.2.2", text = "The client interface MUST define a metadata member or accessor, containing an immutable name field or accessor of type string, which corresponds to the name value supplied during client creation." )] #[test] fn get_metadata_name() { - let client = Client::new( - "test", + assert_eq!(create_default_client().metadata().name, "no_op"); + } + + #[derive(PartialEq, Debug)] + struct Student { + id: i64, + name: String, + } + + impl FromStructValue for Student { + fn from_struct_value(value: &StructValue) -> anyhow::Result { + Ok(Student { + id: value.fields.get("id").unwrap().as_i64().unwrap(), + name: value + .fields + .get("name") + .unwrap() + .as_str() + .unwrap() + .to_string(), + }) + } + } + + #[spec( + number = "1.3.1.1", + text = "The client MUST provide methods for typed flag evaluation, including boolean, numeric, string, and structure, with parameters flag key (string, required), default value (boolean | number | string | structure, required), evaluation context (optional), and evaluation options (optional), which returns the flag value." + )] + #[tokio::test] + async fn get_value() { + // Test bool. + let client = create_client(NoOpProvider::builder().bool_value(true).build()).await; + + assert_eq!( + client.get_bool_value("key", None, None).await.unwrap(), + true + ); + + // Test string. + let client = create_client(NoOpProvider::builder().string_value("result").build()).await; + + assert_eq!( + client.get_string_value("", None, None).await.unwrap(), + "result" + ); + + // Test struct. + let client = create_client( + NoOpProvider::builder() + .struct_value(Arc::new( + StructValue::default() + .with_field("id", 100) + .with_field("name", "Alex"), + )) + .build(), + ) + .await; + + assert_eq!( + client + .get_struct_value::("", None, None) + .await + .unwrap(), + Student { + id: 100, + name: "Alex".to_string() + } + ); + } + + #[spec( + number = "1.3.3.1", + text = "The client SHOULD provide functions for floating-point numbers and integers, consistent with language idioms." + )] + #[tokio::test] + async fn get_numeric_value() { + // Test int. + let client = create_client(NoOpProvider::builder().int_value(200).build()).await; + assert_eq!(client.get_int_value("key", None, None).await.unwrap(), 200); + + // Test float. + let client = create_client(NoOpProvider::builder().float_value(5.0).build()).await; + assert_eq!(client.get_float_value("", None, None).await.unwrap(), 5.0); + } + + #[spec( + number = "1.3.4", + text = "The client SHOULD guarantee the returned value of any typed flag evaluation method is of the expected type. If the value returned by the underlying provider implementation does not match the expected type, it's to be considered abnormal execution, and the supplied default value should be returned." + )] + #[test] + fn get_value_return_right_type_checked_by_type_system() {} + + #[spec( + number = "1.4.1.1", + text = "The client MUST provide methods for detailed flag value evaluation with parameters flag key (string, required), default value (boolean | number | string | structure, required), evaluation context (optional), and evaluation options (optional), which returns an evaluation details structure." + )] + #[spec( + number = "1.4.3", + text = "The evaluation details structure's value field MUST contain the evaluated flag value." + )] + #[spec( + number = "1.4.4.1", + text = "The evaluation details structure SHOULD accept a generic argument (or use an equivalent language feature) which indicates the type of the wrapped value field." + )] + #[spec( + number = "1.4.5", + text = "The evaluation details structure's flag key field MUST contain the flag key argument passed to the detailed flag evaluation method." + )] + #[spec( + number = "1.4.6", + text = "In cases of normal execution, the evaluation details structure's variant field MUST contain the value of the variant field in the flag resolution structure returned by the configured provider, if the field is set." + )] + #[spec( + number = "1.4.7", + text = "In cases of normal execution, the evaluation details structure's reason field MUST contain the value of the reason field in the flag resolution structure returned by the configured provider, if the field is set." + )] + #[spec( + number = "1.4.12", + text = "The client SHOULD provide asynchronous or non-blocking mechanisms for flag evaluation." + )] + #[tokio::test] + async fn get_details() { + let provider = NoOpProvider::builder().int_value(100).build(); + let client = create_client(provider).await; + + let result = client.get_int_details("key", None, None).await.unwrap(); + + assert_eq!(result.value, 100); + assert_eq!(result.flag_key, "key"); + assert_eq!(result.reason, Some(EvaluationReason::Static)); + assert_eq!(result.variant, Some("Static".to_string())); + + assert_eq!( + client + .get_bool_details("another_key", None, None) + .await + .unwrap() + .reason, + Some(EvaluationReason::Default) + ); + } + + #[spec( + number = "1.4.8", + text = "In cases of abnormal execution, the evaluation details structure's error code field MUST contain an error code." + )] + #[spec( + number = "1.4.9", + text = "In cases of abnormal execution (network failure, unhandled error, etc) the reason field in the evaluation details SHOULD indicate an error." + )] + #[spec( + number = "1.4.13", + text = "In cases of abnormal execution, the evaluation details structure's error message field MAY contain a string containing additional details about the nature of the error." + )] + #[test] + fn evaluation_details_contains_error_checked_by_type_system() {} + + #[spec( + number = "1.4.10", + text = "Methods, functions, or operations on the client MUST NOT throw exceptions, or otherwise abnormally terminate. Flag evaluation calls must always return the default value in the event of abnormal execution. Exceptions include functions or methods for the purposes for configuration or setup." + )] + #[test] + fn evaluation_return_default_value_covered_by_result() {} + + #[spec( + number = "1.4.14", + text = "If the flag metadata field in the flag resolution structure returned by the configured provider is set, the evaluation details structure's flag metadata field MUST contain that value. Otherwise, it MUST contain an empty record." + )] + #[spec( + number = "1.4.14.1", + text = "Condition: Flag metadata MUST be immutable." + )] + #[tokio::test] + async fn get_details_flag_metadata() { + let client = create_default_client(); + + let result = client.get_bool_details("", None, None).await.unwrap(); + assert_eq!( + *result.flag_metadata.values.get("Type").unwrap(), + "Bool".into() + ); + + assert_eq!( + client + .get_struct_details::("", None, None) + .await + .unwrap() + .flag_metadata, + FlagMetadata::default() + ) + } + + #[spec( + number = "1.3.2.1", + text = "The client MUST provide methods for typed flag evaluation, including boolean, numeric, string, and structure, with parameters flag key (string, required), default value (boolean | number | string | structure, required), and evaluation options (optional), which returns the flag value." + )] + #[spec( + number = "1.4.2.1", + text = "The client MUST provide methods for detailed flag value evaluation with parameters flag key (string, required), default value (boolean | number | string | structure, required), and evaluation options (optional), which returns an evaluation details structure." + )] + #[test] + fn static_context_not_applicable() {} + + fn create_default_client() -> Client { + Client::new( + "no_op", GlobalEvaluationContext::default(), ProviderRegistry::default(), - ); + ) + } + + async fn create_client(provider: NoOpProvider) -> Client { + let provider_registry = ProviderRegistry::default(); + provider_registry.set_named("custom", provider).await; - assert_eq!(client.metadata().name, "test"); + Client::new( + "custom", + GlobalEvaluationContext::default(), + provider_registry, + ) } } diff --git a/src/api/global_evaluation_context.rs b/src/api/global_evaluation_context.rs index 08ddcf9..0836780 100644 --- a/src/api/global_evaluation_context.rs +++ b/src/api/global_evaluation_context.rs @@ -8,6 +8,10 @@ use crate::EvaluationContext; pub struct GlobalEvaluationContext(Arc>); impl GlobalEvaluationContext { + pub fn new(evaluation_context: EvaluationContext) -> Self { + Self(Arc::new(RwLock::new(evaluation_context))) + } + pub async fn get(&self) -> RwLockReadGuard { self.0.read().await } diff --git a/src/api/mod.rs b/src/api/mod.rs index f4c836a..58ef818 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -2,7 +2,7 @@ mod api; pub use api::OpenFeature; mod client; -pub use client::Client; +pub use client::{Client, ClientMetadata, FromStructValue}; mod provider_registry; diff --git a/src/api/provider_registry.rs b/src/api/provider_registry.rs index 1f41c05..fca8e31 100644 --- a/src/api/provider_registry.rs +++ b/src/api/provider_registry.rs @@ -1,26 +1,43 @@ -use std::collections::HashMap; use std::sync::Arc; +use std::{borrow::Borrow, collections::HashMap}; use tokio::sync::RwLock; -use crate::{ - provider::{FeatureProvider, NoOpProvider}, - EvaluationContext, -}; +use crate::provider::{FeatureProvider, NoOpProvider}; -// ==================================================================== +use super::global_evaluation_context::GlobalEvaluationContext; + +// ============================================================ // ProviderRegistry -// ==================================================================== +// ============================================================ #[derive(Clone)] -pub struct ProviderRegistry(Arc>>); +pub struct ProviderRegistry { + global_evaluation_context: GlobalEvaluationContext, + providers: Arc>>, +} impl ProviderRegistry { + pub fn new(evaluation_context: GlobalEvaluationContext) -> Self { + let mut providers: HashMap = HashMap::new(); + providers.insert( + String::default(), + FeatureProviderWrapper::new(NoOpProvider::default()), + ); + + Self { + global_evaluation_context: evaluation_context, + providers: Arc::new(RwLock::new(providers)), + } + } + pub async fn set_default(&self, mut provider: T) { - let mut map = self.0.write().await; + let mut map = self.providers.write().await; map.remove(""); - provider.initialize(EvaluationContext::default()).await; + provider + .initialize(&self.global_evaluation_context.get().await.borrow()) + .await; map.insert(String::default(), FeatureProviderWrapper::new(provider)); } @@ -28,12 +45,14 @@ impl ProviderRegistry { pub async fn set_named(&self, name: &str, mut provider: T) { // Drop the already registered provider if any. if let Some(_) = self.get_named(name).await { - self.0.write().await.remove(name); + self.providers.write().await.remove(name); } - provider.initialize(EvaluationContext::default()).await; + provider + .initialize(&self.global_evaluation_context.get().await.borrow()) + .await; - self.0 + self.providers .write() .await .insert(name.to_string(), FeatureProviderWrapper::new(provider)); @@ -47,11 +66,11 @@ impl ProviderRegistry { } pub async fn get_default(&self) -> FeatureProviderWrapper { - self.0.read().await.get("").unwrap().clone() + self.providers.read().await.get("").unwrap().clone() } pub async fn get_named(&self, name: &str) -> Option { - self.0 + self.providers .read() .await .get(name) @@ -59,22 +78,20 @@ impl ProviderRegistry { } pub async fn clear(&self) { - self.0.write().await.clear(); + self.providers.write().await.clear(); } } impl Default for ProviderRegistry { fn default() -> Self { - let mut providers: HashMap = HashMap::new(); - providers.insert( - String::default(), - FeatureProviderWrapper::new(NoOpProvider::new()), - ); - - Self(Arc::new(RwLock::new(providers))) + Self::new(GlobalEvaluationContext::default()) } } +// ============================================================ +// FeatureProviderWrapper +// ============================================================ + #[derive(Clone)] pub struct FeatureProviderWrapper(Arc); diff --git a/src/evaluation/context.rs b/src/evaluation/context.rs index 11915ab..a3509dc 100644 --- a/src/evaluation/context.rs +++ b/src/evaluation/context.rs @@ -22,24 +22,26 @@ pub struct EvaluationContext { #[builder(default, setter(into, strip_option))] pub targeting_key: Option, + /// The evaluation context MUST support the inclusion of custom fields, having keys of type + /// string, and values of type boolean | string | number | datetime | structure. #[builder(default)] pub custom_fields: HashMap, } impl EvaluationContext { - pub fn with_custom_field, V: Into>( + pub fn with_custom_field( mut self, - key: S, - value: V, + key: impl Into, + value: impl Into, ) -> Self { self.add_custom_field(key, value); self } - pub fn add_custom_field, V: Into>( + pub fn add_custom_field( &mut self, - key: S, - value: V, + key: impl Into, + value: impl Into, ) { self.custom_fields.insert(key.into(), value.into()); } @@ -61,6 +63,11 @@ impl EvaluationContext { #[cfg(test)] mod tests { + use std::sync::Arc; + + use spec::spec; + use time::OffsetDateTime; + use super::*; #[test] @@ -130,5 +137,76 @@ mod tests { assert_eq!(context, other); } -} + #[derive(Clone, PartialEq, Eq, TypedBuilder, Debug)] + pub struct DummyStruct { + pub id: i64, + + #[builder(setter(into))] + pub name: String, + } + + #[spec( + number = "3.1.1", + text = "The evaluation context structure MUST define an optional targeting key field of type string, identifying the subject of the flag evaluation." + )] + #[spec( + number = "3.1.2", + text = "The evaluation context MUST support the inclusion of custom fields, having keys of type string, and values of type boolean | string | number | datetime | structure." + )] + #[spec( + number = "3.1.3", + text = "The evaluation context MUST support fetching the custom fields by key and also fetching all key value pairs." + )] + #[spec( + number = "3.1.4", + text = "The evaluation context fields MUST have an unique key." + )] + #[test] + fn fields_access() { + let now_time = OffsetDateTime::now_utc(); + let struct_value = DummyStruct::builder().id(200).name("Bob").build(); + + let context = EvaluationContext::builder() + .targeting_key("Key") + .build() + .with_custom_field("Bool", true) + .with_custom_field("Int", 100) + .with_custom_field("Float", 3.14) + .with_custom_field("String", "Hello") + .with_custom_field("Datetime", now_time) + .with_custom_field( + "Struct", + EvaluationContextFieldValue::Struct(Arc::new(struct_value.clone())), + ); + + assert_eq!(context.targeting_key, Some("Key".to_string())); + assert_eq!( + context.custom_fields.get("Int"), + Some(&EvaluationContextFieldValue::Int(100)) + ); + assert_eq!( + context.custom_fields.get("Float"), + Some(&EvaluationContextFieldValue::Float(3.14)) + ); + assert_eq!( + context.custom_fields.get("String"), + Some(&EvaluationContextFieldValue::String("Hello".to_string())) + ); + assert_eq!( + context.custom_fields.get("Datetime"), + Some(&EvaluationContextFieldValue::DateTime(now_time)) + ); + assert_eq!( + *context + .custom_fields + .get("Struct") + .unwrap() + .as_struct() + .unwrap() + .downcast::() + .unwrap(), + struct_value + ); + } +} diff --git a/src/evaluation/field_value.rs b/src/evaluation/context_field_value.rs similarity index 73% rename from src/evaluation/field_value.rs rename to src/evaluation/context_field_value.rs index 88b4354..caadaee 100644 --- a/src/evaluation/field_value.rs +++ b/src/evaluation/context_field_value.rs @@ -19,6 +19,90 @@ impl EvaluationContextFieldValue { { Self::Struct(Arc::new(value)) } + + pub fn is_bool(&self) -> bool { + match self { + Self::Bool(_) => true, + _ => false, + } + } + + pub fn as_bool(&self) -> Option { + match self { + Self::Bool(value) => Some(*value), + _ => None, + } + } + + pub fn is_i64(&self) -> bool { + match self { + Self::Int(_) => true, + _ => false, + } + } + + pub fn as_i64(&self) -> Option { + match self { + Self::Int(value) => Some(*value), + _ => None, + } + } + + pub fn is_f64(&self) -> bool { + match self { + Self::Float(_) => true, + _ => false, + } + } + + pub fn as_f64(&self) -> Option { + match self { + Self::Float(value) => Some(*value), + _ => None, + } + } + + pub fn is_str(&self) -> bool { + match self { + Self::String(_) => true, + _ => false, + } + } + + pub fn as_str(&self) -> Option<&str> { + match self { + Self::String(value) => Some(&value), + _ => None, + } + } + + pub fn is_date_time(&self) -> bool { + match self { + Self::DateTime(_) => true, + _ => false, + } + } + + pub fn as_date_time(&self) -> Option<&OffsetDateTime> { + match self { + Self::DateTime(value) => Some(value), + _ => None, + } + } + + pub fn is_struct(&self) -> bool { + match self { + Self::Struct(_) => true, + _ => false, + } + } + + pub fn as_struct(&self) -> Option> { + match self { + Self::Struct(value) => Some(value.clone()), + _ => None, + } + } } impl From for EvaluationContextFieldValue { @@ -99,6 +183,12 @@ impl From for EvaluationContextFieldValue { } } +impl From> for EvaluationContextFieldValue { + fn from(value: Arc) -> Self { + Self::Struct(value) + } +} + impl PartialEq for EvaluationContextFieldValue { fn eq(&self, other: &Self) -> bool { match (self, other) { diff --git a/src/evaluation/details.rs b/src/evaluation/details.rs index 907bb29..7c901bb 100644 --- a/src/evaluation/details.rs +++ b/src/evaluation/details.rs @@ -1,15 +1,26 @@ use std::collections::HashMap; +use crate::EvaluationError; + +pub type EvaluationResult = Result; + +// ============================================================ +// EvaluationDetails +// ============================================================ + #[derive(Clone, Default, Debug)] pub struct EvaluationDetails { pub flag_key: String, pub value: T, pub reason: Option, - pub error: Option, pub variant: Option, pub flag_metadata: FlagMetadata, } +// ============================================================ +// EvaluationReason +// ============================================================ + /// Reason for evaluation. #[derive(Clone, Default, Eq, PartialEq, Debug)] pub enum EvaluationReason { @@ -33,8 +44,8 @@ pub enum EvaluationReason { /// The resolved value was the result of the flag being disabled in the management system. Disabled, - #[default] /// The reason for the resolved value could not be determined. + #[default] Unknown, /// The resolved value was the result of an error. @@ -61,52 +72,115 @@ impl ToString for EvaluationReason { } } -/// Struct representing error -#[derive(Clone, Eq, PartialEq, Debug)] -pub struct EvaluationError { - pub code: EvaluationErrorCode, - pub message: Option, -} - -/// An enumerated error code represented idiomatically in the implementation language. -#[derive(Clone, Eq, PartialEq, Debug)] -pub enum EvaluationErrorCode { - /// The value was resolved before the provider was initialized. - ProviderNotReady, - - /// The flag could not be found. - FlagNotFound, - - /// An error was encountered parsing data, such as a flag configuration. - ParseError, - - /// The type of the flag value does not match the expected type. - TypeMismatch, - - /// The provider requires a targeting key and one was not provided in the evaluation context. - TargetingKeyMissing, - - /// The evaluation context does not meet provider requirements. - InvalidContext, - - /// The error was for a reason not enumerated above. - General(String), -} +// ============================================================ +// FlagMetadata +// ============================================================ /// A structure which supports definition of arbitrary properties, with keys of type string, and /// values of type boolean, string, or number. /// /// This structure is populated by a provider for use by an Application Author (via the Evaluation /// API) or an Application Integrator (via hooks). -#[derive(Clone, Default, Debug)] +#[derive(Clone, Default, PartialEq, Debug)] pub struct FlagMetadata { pub values: HashMap, } -#[derive(Clone, Debug)] +impl FlagMetadata { + pub fn with_value( + mut self, + key: impl Into, + value: impl Into, + ) -> Self { + self.add_value(key, value); + self + } + + pub fn add_value(&mut self, key: impl Into, value: impl Into) { + self.values.insert(key.into(), value.into()); + } +} + +// ============================================================ +// FlagMetadataValue +// ============================================================ + +#[derive(Clone, PartialEq, Debug)] pub enum FlagMetadataValue { Bool(bool), Int(i64), Float(f64), String(String), } + +impl From for FlagMetadataValue { + fn from(value: bool) -> Self { + Self::Bool(value) + } +} + +impl From for FlagMetadataValue { + fn from(value: i8) -> Self { + Self::Int(value.into()) + } +} + +impl From for FlagMetadataValue { + fn from(value: i16) -> Self { + Self::Int(value.into()) + } +} + +impl From for FlagMetadataValue { + fn from(value: i32) -> Self { + Self::Int(value.into()) + } +} + +impl From for FlagMetadataValue { + fn from(value: i64) -> Self { + Self::Int(value.into()) + } +} + +impl From for FlagMetadataValue { + fn from(value: u8) -> Self { + Self::Int(value.into()) + } +} + +impl From for FlagMetadataValue { + fn from(value: u16) -> Self { + Self::Int(value.into()) + } +} + +impl From for FlagMetadataValue { + fn from(value: u32) -> Self { + Self::Int(value.into()) + } +} + +impl From for FlagMetadataValue { + fn from(value: f32) -> Self { + Self::Float(value.into()) + } +} + +impl From for FlagMetadataValue { + fn from(value: f64) -> Self { + Self::Float(value.into()) + } +} + +impl From for FlagMetadataValue { + fn from(value: String) -> Self { + Self::String(value.into()) + } +} + +impl From<&str> for FlagMetadataValue { + fn from(value: &str) -> Self { + Self::String(value.into()) + } +} diff --git a/src/evaluation/error.rs b/src/evaluation/error.rs new file mode 100644 index 0000000..5b9340a --- /dev/null +++ b/src/evaluation/error.rs @@ -0,0 +1,57 @@ +// ============================================================ +// EvaluationError +// ============================================================ + +use typed_builder::TypedBuilder; + +/// Struct representing error +#[derive(Clone, Eq, PartialEq, TypedBuilder, Debug)] +pub struct EvaluationError { + pub code: EvaluationErrorCode, + + #[builder(default, setter(strip_option, into))] + pub message: Option, +} + +// ============================================================ +// EvaluationErrorCode +// ============================================================ + +/// An enumerated error code represented idiomatically in the implementation language. +#[derive(Clone, Eq, PartialEq, Debug)] +pub enum EvaluationErrorCode { + /// The value was resolved before the provider was initialized. + ProviderNotReady, + + /// The flag could not be found. + FlagNotFound, + + /// An error was encountered parsing data, such as a flag configuration. + ParseError, + + /// The type of the flag value does not match the expected type. + TypeMismatch, + + /// The provider requires a targeting key and one was not provided in the evaluation context. + TargetingKeyMissing, + + /// The evaluation context does not meet provider requirements. + InvalidContext, + + /// The error was for a reason not enumerated above. + General(String), +} + +impl ToString for EvaluationErrorCode { + fn to_string(&self) -> String { + match self { + Self::ProviderNotReady => "PROVIDER_NOT_READY".to_string(), + Self::FlagNotFound => "FLAG_NOT_FOUND".to_string(), + Self::ParseError => "PARSE_ERROR".to_string(), + Self::TypeMismatch => "TYPE_MISMATCH".to_string(), + Self::TargetingKeyMissing => "TARGETING_KEY_MISSING".to_string(), + Self::InvalidContext => "INVALID_CONTEXT".to_string(), + Self::General(message) => message.clone(), + } + } +} diff --git a/src/evaluation/mod.rs b/src/evaluation/mod.rs index ed30917..0555491 100644 --- a/src/evaluation/mod.rs +++ b/src/evaluation/mod.rs @@ -1,11 +1,19 @@ mod details; -pub use details::*; +pub use details::{ + EvaluationDetails, EvaluationReason, EvaluationResult, FlagMetadata, FlagMetadataValue, +}; + +mod error; +pub use error::{EvaluationError, EvaluationErrorCode}; mod context; -pub use context::*; +pub use context::EvaluationContext; -mod field_value; -pub use field_value::*; +mod context_field_value; +pub use context_field_value::EvaluationContextFieldValue; mod value; -pub use value::*; +pub use value::{StructValue, Value}; + +mod options; +pub use options::EvaluationOptions; diff --git a/src/evaluation/options.rs b/src/evaluation/options.rs new file mode 100644 index 0000000..f7ba739 --- /dev/null +++ b/src/evaluation/options.rs @@ -0,0 +1,3 @@ +pub struct EvaluationOptions { + // Not implemented yet. +} diff --git a/src/evaluation/value.rs b/src/evaluation/value.rs index 7c002c4..54244e0 100644 --- a/src/evaluation/value.rs +++ b/src/evaluation/value.rs @@ -191,12 +191,12 @@ impl From for Value { } impl StructValue { - pub fn with_field, V: Into>(mut self, key: S, value: V) -> Self { + pub fn with_field(mut self, key: impl Into, value: impl Into) -> Self { self.add_field(key, value); self } - pub fn add_field, V: Into>(&mut self, key: S, value: V) { + pub fn add_field(&mut self, key: impl Into, value: impl Into) { self.fields.insert(key.into(), value.into()); } } diff --git a/src/lib.rs b/src/lib.rs index 45b6dfa..508e37a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,5 @@ mod api; -pub use api::{Client, OpenFeature}; +pub use api::*; mod evaluation; pub use evaluation::*; diff --git a/src/provider/details.rs b/src/provider/details.rs index 237ff43..ed93739 100644 --- a/src/provider/details.rs +++ b/src/provider/details.rs @@ -1,6 +1,6 @@ use typed_builder::TypedBuilder; -use crate::{EvaluationError, EvaluationReason, FlagMetadata}; +use crate::{EvaluationReason, FlagMetadata}; /// A structure which contains a subset of the fields defined in the evaluation details, /// representing the result of the provider's flag resolution process. @@ -22,15 +22,6 @@ pub struct ResolutionDetails { #[builder(default, setter(strip_option))] pub reason: Option, - /// In cases of normal execution, the provider MUST NOT populate the resolution details - /// structure's error code field, or otherwise must populate it with a null or falsy value. - /// - /// In cases of abnormal execution, the provider MUST indicate an error using the idioms of the - /// implementation language, with an associated error code and optional associated error - /// message. - #[builder(default, setter(strip_option))] - pub error: Option, - /// The provider SHOULD populate the resolution details structure's flag metadata field. #[builder(default, setter(strip_option))] pub flag_metadata: Option, @@ -42,7 +33,6 @@ impl Default for ResolutionDetails { value: T::default(), variant: None, reason: None, - error: None, flag_metadata: None, } } @@ -54,12 +44,7 @@ impl ResolutionDetails { value: value.into(), variant: None, reason: None, - error: None, flag_metadata: None, } } - - pub fn is_error(&self) -> bool { - self.error.is_some() - } } diff --git a/src/provider/feature_provider.rs b/src/provider/feature_provider.rs index d55bb3a..1ffd224 100644 --- a/src/provider/feature_provider.rs +++ b/src/provider/feature_provider.rs @@ -1,12 +1,13 @@ use async_trait::async_trait; use typed_builder::TypedBuilder; -use crate::{EvaluationContext, StructValue}; +use crate::{EvaluationContext, EvaluationResult, StructValue}; use super::ResolutionDetails; -#[cfg(test)] -use mockall::{automock, predicate::*}; +// ============================================================ +// FeatureProvider +// ============================================================ /// This trait defines interfaces that Provider Authors can use to abstract a particular flag /// management system, thus enabling the use of the evaluation API by Application Authors. @@ -21,7 +22,6 @@ use mockall::{automock, predicate::*}; /// vendor SDK, embed an REST client, or read flags from a local file. /// /// See the [spec](https://openfeature.dev/specification/sections/providers). -#[cfg_attr(test, automock)] #[async_trait] pub trait FeatureProvider: Send + Sync + 'static { /// The provider MAY define an initialize function which accepts the global evaluation @@ -35,7 +35,7 @@ pub trait FeatureProvider: Send + Sync + 'static { /// * The provider SHOULD indicate an error if flag resolution is attempted before the provider /// is ready. #[allow(unused_variables)] - async fn initialize(&mut self, context: EvaluationContext) {} + async fn initialize(&mut self, context: &EvaluationContext) {} /// The provider MAY define a status field/accessor which indicates the readiness of the /// provider, with possible values NOT_READY, READY, or ERROR. @@ -53,43 +53,42 @@ pub trait FeatureProvider: Send + Sync + 'static { async fn resolve_bool_value( &self, flag_key: &str, - default_value: bool, evaluation_context: &EvaluationContext, - ) -> ResolutionDetails; + ) -> EvaluationResult>; /// Resolve given `flag_key` as an i64 value. async fn resolve_int_value( &self, flag_key: &str, - default_value: i64, evaluation_context: &EvaluationContext, - ) -> ResolutionDetails; + ) -> EvaluationResult>; /// Resolve given `flag_key` as a f64 value. async fn resolve_float_value( &self, flag_key: &str, - default_value: f64, evaluation_context: &EvaluationContext, - ) -> ResolutionDetails; + ) -> EvaluationResult>; /// Resolve given `flag_key` as a string value. async fn resolve_string_value( &self, flag_key: &str, - default_value: &str, evaluation_context: &EvaluationContext, - ) -> ResolutionDetails; + ) -> EvaluationResult>; /// Resolve given `flag_key` as a struct value. async fn resolve_struct_value( &self, flag_key: &str, - default_value: StructValue, evaluation_context: &EvaluationContext, - ) -> ResolutionDetails; + ) -> EvaluationResult>; } +// ============================================================ +// ProviderMetadata +// ============================================================ + /// The metadata of a feature provider. #[derive(Clone, TypedBuilder, Default, Debug)] pub struct ProviderMetadata { @@ -102,12 +101,18 @@ impl ProviderMetadata { Self { name: name.into() } } } +// +// ============================================================ +// ProviderStatus +// ============================================================ /// The status of a feature provider. -#[derive(Default, Debug)] +#[derive(Default, PartialEq, Eq, Debug)] pub enum ProviderStatus { - #[default] Ready, + + #[default] NotReady, + Error, } diff --git a/src/provider/fixed_value_provider.rs b/src/provider/fixed_value_provider.rs deleted file mode 100644 index 73e7b8a..0000000 --- a/src/provider/fixed_value_provider.rs +++ /dev/null @@ -1,140 +0,0 @@ -use std::sync::Arc; - -use async_trait::async_trait; -use typed_builder::TypedBuilder; - -use crate::{EvaluationContext, StructValue}; - -use super::{FeatureProvider, ProviderMetadata, ResolutionDetails}; - -const PROVIDER_NAME: &'static str = "Fixed Value"; - -// -------------------------------------------------------------------- -// FixedValueProvider -// -------------------------------------------------------------------- - -#[derive(TypedBuilder, Debug)] -pub struct FixedValueProvider { - #[builder(default)] - metadata: ProviderMetadata, - - #[builder(default)] - bool_value: bool, - - #[builder(default)] - int_value: i64, - - #[builder(default)] - float_value: f64, - - #[builder(default)] - string_value: String, - - #[builder(default)] - struct_value: Arc, -} - -impl Default for FixedValueProvider { - fn default() -> Self { - Self { - metadata: ProviderMetadata::new(PROVIDER_NAME), - bool_value: Default::default(), - int_value: Default::default(), - float_value: Default::default(), - string_value: Default::default(), - struct_value: Arc::new(DummyStruct::default().into()), - } - } -} - -#[async_trait] -impl FeatureProvider for FixedValueProvider { - fn metadata(&self) -> &ProviderMetadata { - &self.metadata - } - - async fn resolve_bool_value( - &self, - _flag_key: &str, - _default_value: bool, - _evaluation_context: &EvaluationContext, - ) -> ResolutionDetails { - ResolutionDetails::new(self.bool_value) - } - - async fn resolve_int_value( - &self, - _flag_key: &str, - _default_value: i64, - _evaluation_context: &EvaluationContext, - ) -> ResolutionDetails { - ResolutionDetails::new(self.int_value) - } - - async fn resolve_float_value( - &self, - _flag_key: &str, - _default_value: f64, - _evaluation_context: &EvaluationContext, - ) -> ResolutionDetails { - ResolutionDetails::new(self.float_value) - } - - async fn resolve_string_value( - &self, - _flag_key: &str, - _default_value: &str, - _evaluation_context: &EvaluationContext, - ) -> ResolutionDetails { - ResolutionDetails::new(self.string_value.clone()) - } - - async fn resolve_struct_value( - &self, - _flag_key: &str, - _default_value: StructValue, - _evaluation_context: &EvaluationContext, - ) -> ResolutionDetails { - ResolutionDetails::new((*self.struct_value).clone()) - } -} - -// -------------------------------------------------------------------- -// DummyStruct -// -------------------------------------------------------------------- - -#[derive(Clone, TypedBuilder, Default, Debug)] -pub struct DummyStruct { - #[builder(default)] - id: i64, - - #[builder(default, setter(into))] - name: String, -} - -impl From for StructValue { - fn from(value: DummyStruct) -> Self { - StructValue::default() - .with_field("id", value.id) - .with_field("name", value.name) - } -} - -#[cfg(test)] -mod tests { - use super::*; - use crate::*; - - #[test] - fn from_dummy_struct() { - let value = DummyStruct::builder().id(100).name("Alex").build(); - - let result: StructValue = value.into(); - - let expected = StructValue::default() - .with_field("id", 100) - .with_field("name", "Alex"); - - assert_eq!(expected, result); - } -} diff --git a/src/provider/mod.rs b/src/provider/mod.rs index 9f3c31c..a12367f 100644 --- a/src/provider/mod.rs +++ b/src/provider/mod.rs @@ -2,10 +2,7 @@ mod details; pub use details::ResolutionDetails; mod feature_provider; -pub use feature_provider::*; +pub use feature_provider::{FeatureProvider, ProviderMetadata, ProviderStatus}; mod no_op_provider; pub use no_op_provider::NoOpProvider; - -mod fixed_value_provider; -pub use fixed_value_provider::*; diff --git a/src/provider/no_op_provider.rs b/src/provider/no_op_provider.rs index cd5ecb7..37cce89 100644 --- a/src/provider/no_op_provider.rs +++ b/src/provider/no_op_provider.rs @@ -1,29 +1,101 @@ +use std::sync::Arc; + use async_trait::async_trait; +use typed_builder::TypedBuilder; -use crate::{EvaluationContext, StructValue}; +use crate::{ + EvaluationContext, EvaluationContextFieldValue, EvaluationError, EvaluationReason, + FlagMetadata, FlagMetadataValue, StructValue, +}; use super::{FeatureProvider, ProviderMetadata, ResolutionDetails}; -const PROVIDER_NAME: &'static str = "No Operation"; +// ============================================================ +// NoOpProvider +// ============================================================ -/// The default provider that simply returns given default value during _evaluation. -#[derive(Debug)] +/// The default provider that does nothing. +/// +/// By default, it returns the default value of each supported type. You can inject values (for +/// testing purpose or simply providing some fixed value) when creating an instance. +/// +/// Other tips: +/// * It will return reason `Default` when the value equals to the default, `Static` otherwise. +/// * It will return variant `"Default"` or `"Static"` respectively. +/// * It will return flag metadata with key `"Type"` and a string value that corresponds to the +/// real type, EXCEPT for `resolve_struct_value`. +/// * It will return flag metadata with keys "TargetingKey" and value of targeting key extracted +/// from the evaluation context, if the value is not `None`. +/// * It will return flag metadata with keys/values extracted from the evaluation context, as long +/// as the value is a bool, number or string. +#[derive(TypedBuilder, Debug)] pub struct NoOpProvider { + #[builder(default)] metadata: ProviderMetadata, + + #[builder(default)] + bool_value: bool, + + #[builder(default)] + int_value: i64, + + #[builder(default)] + float_value: f64, + + #[builder(default, setter(into))] + string_value: String, + + #[builder(default, setter(into))] + struct_value: Arc, } impl NoOpProvider { - pub fn new() -> Self { - Self { - metadata: ProviderMetadata::new(PROVIDER_NAME), + fn create_reason_variant(is_default: bool) -> (EvaluationReason, String) { + if is_default { + (EvaluationReason::Default, "Default".to_string()) + } else { + (EvaluationReason::Static, "Static".to_string()) + } + } + + fn populate_evaluation_context_values( + flag_metadata: &mut FlagMetadata, + evaluation_context: &EvaluationContext, + ) { + if let Some(value) = &evaluation_context.targeting_key { + flag_metadata.add_value("TargetingKey", value.clone()); } + + evaluation_context + .custom_fields + .iter() + .for_each(|(key, value)| match value { + EvaluationContextFieldValue::Bool(value) => { + flag_metadata.add_value(key, FlagMetadataValue::Bool(*value)) + } + EvaluationContextFieldValue::Int(value) => { + flag_metadata.add_value(key, FlagMetadataValue::Int(*value)) + } + EvaluationContextFieldValue::Float(value) => { + flag_metadata.add_value(key, FlagMetadataValue::Float(*value)) + } + EvaluationContextFieldValue::String(value) => { + flag_metadata.add_value(key, FlagMetadataValue::String(value.clone())) + } + _ => (), + }) } } impl Default for NoOpProvider { fn default() -> Self { Self { - metadata: ProviderMetadata::new(PROVIDER_NAME), + metadata: ProviderMetadata::new("No Operation - Default"), + bool_value: Default::default(), + int_value: Default::default(), + float_value: Default::default(), + string_value: Default::default(), + struct_value: Arc::new(DummyStruct::default().into()), } } } @@ -34,48 +106,320 @@ impl FeatureProvider for NoOpProvider { &self.metadata } + async fn initialize(&mut self, _evaluation_context: &EvaluationContext) { + self.metadata = ProviderMetadata::new("No Operation"); + } + async fn resolve_bool_value( &self, _flag_key: &str, - default_value: bool, - _evaluation_context: &EvaluationContext, - ) -> ResolutionDetails { - ResolutionDetails::new(default_value) + evaluation_context: &EvaluationContext, + ) -> Result, EvaluationError> { + let (reason, variant) = Self::create_reason_variant(self.bool_value == Default::default()); + + let mut flag_metadata = FlagMetadata::default().with_value("Type", "Bool"); + Self::populate_evaluation_context_values(&mut flag_metadata, &evaluation_context); + + Ok(ResolutionDetails::builder() + .value(self.bool_value) + .reason(reason) + .variant(variant) + .flag_metadata(flag_metadata) + .build()) } async fn resolve_int_value( &self, _flag_key: &str, - default_value: i64, - _evaluation_context: &EvaluationContext, - ) -> ResolutionDetails { - ResolutionDetails::new(default_value) + evaluation_context: &EvaluationContext, + ) -> Result, EvaluationError> { + let (reason, variant) = Self::create_reason_variant(self.int_value == Default::default()); + + let mut flag_metadata = FlagMetadata::default().with_value("Type", "Int"); + Self::populate_evaluation_context_values(&mut flag_metadata, &evaluation_context); + + Ok(ResolutionDetails::builder() + .value(self.int_value) + .reason(reason) + .variant(variant) + .flag_metadata(flag_metadata) + .build()) } async fn resolve_float_value( &self, _flag_key: &str, - default_value: f64, - _evaluation_context: &EvaluationContext, - ) -> ResolutionDetails { - ResolutionDetails::new(default_value) + evaluation_context: &EvaluationContext, + ) -> Result, EvaluationError> { + let (reason, variant) = Self::create_reason_variant(self.float_value == Default::default()); + + let mut flag_metadata = FlagMetadata::default().with_value("Type", "Float"); + Self::populate_evaluation_context_values(&mut flag_metadata, &evaluation_context); + + Ok(ResolutionDetails::builder() + .value(self.float_value) + .reason(reason) + .variant(variant) + .flag_metadata(flag_metadata) + .build()) } async fn resolve_string_value( &self, _flag_key: &str, - default_value: &str, - _evaluation_context: &EvaluationContext, - ) -> ResolutionDetails { - ResolutionDetails::new(default_value) + evaluation_context: &EvaluationContext, + ) -> Result, EvaluationError> { + let (reason, variant) = Self::create_reason_variant(self.string_value == String::default()); + + let mut flag_metadata = FlagMetadata::default().with_value("Type", "String"); + Self::populate_evaluation_context_values(&mut flag_metadata, &evaluation_context); + + Ok(ResolutionDetails::builder() + .value(self.string_value.clone()) + .reason(reason) + .variant(variant) + .flag_metadata(flag_metadata) + .build()) } async fn resolve_struct_value( &self, _flag_key: &str, - default_value: StructValue, - _evaluation_context: &EvaluationContext, - ) -> ResolutionDetails { - ResolutionDetails::new(default_value) + evaluation_context: &EvaluationContext, + ) -> Result, EvaluationError> { + let (reason, variant) = + Self::create_reason_variant(self.struct_value == Default::default()); + + let mut flag_metadata = FlagMetadata::default().with_value("Type", "Struct"); + Self::populate_evaluation_context_values(&mut flag_metadata, &evaluation_context); + + Ok(ResolutionDetails::builder() + .value((*self.struct_value).clone()) + .reason(reason) + .variant(variant) + .build()) + } +} + +// ============================================================ +// DummyStruct +// ============================================================ + +#[derive(Clone, TypedBuilder, Default, Debug)] +pub struct DummyStruct { + #[builder(default)] + id: i64, + + #[builder(default, setter(into))] + name: String, +} + +impl From for StructValue { + fn from(value: DummyStruct) -> Self { + StructValue::default() + .with_field("id", value.id) + .with_field("name", value.name) + } +} + +// ============================================================ +// Tests +// ============================================================ + +#[cfg(test)] +mod tests { + use spec::spec; + + use super::*; + use crate::{provider::ProviderStatus, *}; + + #[test] + fn from_dummy_struct() { + let value = DummyStruct::builder().id(100).name("Alex").build(); + + let result: StructValue = value.into(); + + let expected = StructValue::default() + .with_field("id", 100) + .with_field("name", "Alex"); + + assert_eq!(expected, result); } + + #[spec( + number = "2.1.1", + text = "The provider interface MUST define a metadata member or accessor, containing a name field or accessor of type string, which identifies the provider implementation." + )] + #[test] + fn metadata_name() { + let provider = NoOpProvider::default(); + + assert_eq!(provider.metadata().name, "No Operation - Default"); + } + + #[spec( + number = "2.2.1", + text = "The feature provider interface MUST define methods to resolve flag values, with parameters flag key (string, required), default value (boolean | number | string | structure, required) and evaluation context (optional), which returns a resolution details structure." + )] + #[spec( + number = "2.2.2.1", + text = "The feature provider interface MUST define methods for typed flag resolution, including boolean, numeric, string, and structure." + )] + #[spec( + number = "2.2.3", + text = "In cases of normal execution, the provider MUST populate the resolution details structure's value field with the resolved flag value." + )] + #[spec( + number = "2.2.4", + text = "In cases of normal execution, the provider SHOULD populate the resolution details structure's variant field with a string identifier corresponding to the returned flag value." + )] + #[spec( + number = "2.2.5", + text = r###"The provider SHOULD populate the resolution details structure's reason field with "STATIC", "DEFAULT", "TARGETING_MATCH", "SPLIT", "CACHED", "DISABLED", "UNKNOWN", "STALE", "ERROR" or some other string indicating the semantic reason for the returned flag value."### + )] + #[spec( + number = "2.2.6", + text = "In cases of normal execution, the provider MUST NOT populate the resolution details structure's error code field, or otherwise must populate it with a null or falsy value." + )] + #[spec( + number = "2.2.9", + text = "The provider SHOULD populate the resolution details structure's flag metadata field. " + )] + #[spec( + number = "2.2.10", + text = "flag metadata MUST be a structure supporting the definition of arbitrary properties, with keys of type string, and values of type boolean | string | number." + )] + #[tokio::test] + async fn resolve_value() { + let provider = NoOpProvider::builder() + .bool_value(true) + .int_value(100) + .string_value("Hello") + .struct_value(StructValue::default().with_field("Key", "Value")) + .build(); + + // Check bool. + let result = provider + .resolve_bool_value("key", &EvaluationContext::default()) + .await + .unwrap(); + + assert_eq!(result.value, true); + assert_eq!(result.reason, Some(EvaluationReason::Static)); + assert_eq!(result.variant, Some("Static".to_string())); + assert_eq!( + result.flag_metadata, + Some(FlagMetadata::default().with_value("Type", "Bool")) + ); + + // Check int. + let result = provider + .resolve_int_value("key", &EvaluationContext::default()) + .await + .unwrap(); + + assert_eq!(result.value, 100); + assert_eq!(result.reason, Some(EvaluationReason::Static)); + assert_eq!(result.variant, Some("Static".to_string())); + assert_eq!( + result.flag_metadata, + Some(FlagMetadata::default().with_value("Type", "Int")) + ); + + // Check float. + let result = provider + .resolve_float_value("key", &EvaluationContext::default()) + .await + .unwrap(); + + assert_eq!(result.value, 0.0); + assert_eq!(result.reason, Some(EvaluationReason::Default)); + assert_eq!(result.variant, Some("Default".to_string())); + assert_eq!( + result.flag_metadata, + Some(FlagMetadata::default().with_value("Type", "Float")) + ); + + // Check string. + let result = provider + .resolve_string_value("key", &EvaluationContext::default()) + .await + .unwrap(); + + assert_eq!(result.value, "Hello"); + assert_eq!(result.reason, Some(EvaluationReason::Static)); + assert_eq!(result.variant, Some("Static".to_string())); + assert_eq!( + result.flag_metadata, + Some(FlagMetadata::default().with_value("Type", "String")) + ); + + // Check struct. + let result = provider + .resolve_struct_value("key", &EvaluationContext::default()) + .await + .unwrap(); + + assert_eq!( + result.value, + StructValue::default().with_field("Key", "Value") + ); + assert_eq!(result.reason, Some(EvaluationReason::Static)); + assert_eq!(result.variant, Some("Static".to_string())); + assert_eq!(result.flag_metadata, None); + } + + #[spec( + number = "2.2.7", + text = "In cases of abnormal execution, the provider MUST indicate an error using the idioms of the implementation language, with an associated error code and optional associated error message." + )] + #[test] + fn error_code_message_provided_checked_by_type_system() {} + + #[spec( + number = "2.2.8.1", + text = "The resolution details structure SHOULD accept a generic argument (or use an equivalent language feature) which indicates the type of the wrapped value field." + )] + #[test] + fn resolution_details_generic_checked_by_type_system() {} + + #[spec( + number = "2.4.1", + text = "The provider MAY define an initialize function which accepts the global evaluation context as an argument and performs initialization logic relevant to the provider." + )] + #[tokio::test] + async fn initialize() { + let mut provider = NoOpProvider::default(); + + provider.initialize(&EvaluationContext::default()).await; + } + + #[spec( + number = "2.4.2", + text = "The provider MAY define a status field/accessor which indicates the readiness of the provider, with possible values NOT_READY, READY, or ERROR." + )] + #[spec( + number = "2.4.3", + text = "The provider MUST set its status field/accessor to READY if its initialize function terminates normally." + )] + #[spec( + number = "2.4.4", + text = "The provider MUST set its status field to ERROR if its initialize function terminates abnormally." + )] + #[spec( + number = "2.4.5", + text = "The provider SHOULD indicate an error if flag resolution is attempted before the provider is ready." + )] + #[tokio::test] + async fn status() { + let provider = NoOpProvider::default(); + assert_eq!(provider.status(), ProviderStatus::Ready); + } + + #[spec( + number = "2.5.1", + text = "The provider MAY define a mechanism to gracefully shutdown and dispose of resources." + )] + #[test] + fn shutdown_covered_by_drop_trait() {} }