open_feature_flagd/
lib.rs

1//! [Generated by cargo-readme: `cargo readme --no-title --no-license > README.md`]::
2//!  # flagd Provider for OpenFeature
3//!
4//! A Rust implementation of the OpenFeature provider for flagd, enabling dynamic
5//! feature flag evaluation in your applications.
6//!
7//! This provider supports multiple evaluation modes, advanced targeting rules, caching strategies,
8//! and connection management. It is designed to work seamlessly with the OpenFeature SDK and the flagd service.
9//!
10//! ## Core Features
11//!
12//! - **Multiple Evaluation Modes**
13//!     - **RPC Resolver (Remote Evaluation):** Uses gRPC to perform flag evaluations remotely at a flagd instance. Supports bi-directional streaming, retry backoff, and custom name resolution (including Envoy support).
14//!     - **REST Resolver:** Uses the OpenFeature Remote Evaluation Protocol (OFREP) over HTTP to evaluate flags.
15//!     - **In-Process Resolver:** Performs evaluations locally using an embedded evaluation engine. Flag configurations can be retrieved via gRPC (sync mode).
16//!     - **File Resolver:** Operates entirely from a flag definition file, updating on file changes in a best-effort manner.
17//!
18//! - **Advanced Targeting**
19//!     - **Fractional Rollouts:** Uses consistent hashing (implemented via murmurhash3) to split traffic between flag variants in configurable proportions.
20//!     - **Semantic Versioning:** Compare values using common operators such as '=', '!=', '<', '<=', '>', '>=', '^', and '~'.
21//!     - **String Operations:** Custom operators for performing “starts_with” and “ends_with” comparisons.
22//!     - **Complex Targeting Rules:** Leverages JSONLogic and custom operators to support nested conditions and dynamic evaluation.
23//!
24//! - **Caching Strategies**
25//!     - Built-in support for LRU caching as well as an in-memory alternative. Flag evaluation results can be cached and later returned with a “CACHED” reason until the configuration updates.
26//!
27//! - **Connection Management**
28//!     - Automatic connection establishment with configurable retries, timeout settings, and custom TLS or Unix-socket options.
29//!     - Support for upstream name resolution including a custom resolver for Envoy proxy integration.
30//!
31//! ## Installation
32//! Add the dependency in your `Cargo.toml`:
33//! ```bash
34//! cargo add open-feature-flagd
35//! cargo add open-feature
36//! ```
37//!
38//! ## Cargo Features
39//!
40//! This crate uses cargo features to allow clients to include only the evaluation modes they need,
41//! keeping the dependency footprint minimal. By default, all features are enabled.
42//!
43//! | Feature | Description | Enabled by Default |
44//! |---------|-------------|-------------------|
45//! | `rpc` | gRPC-based remote evaluation via flagd service | ✅ |
46//! | `rest` | HTTP/OFREP-based remote evaluation | ✅ |
47//! | `in-process` | Local evaluation with embedded engine (includes File mode) | ✅ |
48//!
49//! ### Using Specific Features
50//!
51//! To include only specific evaluation modes:
52//!
53//! ```toml
54//! # Only RPC evaluation
55//! open-feature-flagd = { version = "0.0.8", default-features = false, features = ["rpc"] }
56//!
57//! # Only REST evaluation (lightweight, no gRPC dependencies)
58//! open-feature-flagd = { version = "0.0.8", default-features = false, features = ["rest"] }
59//!
60//! # Only in-process/file evaluation
61//! open-feature-flagd = { version = "0.0.8", default-features = false, features = ["in-process"] }
62//!
63//! # RPC and REST (no local evaluation engine)
64//! open-feature-flagd = { version = "0.0.8", default-features = false, features = ["rpc", "rest"] }
65//! ```
66//!
67//! Then integrate it into your application:
68//!
69//! ```rust,no_run
70//! use open_feature_flagd::{FlagdOptions, FlagdProvider, ResolverType};
71//! use open_feature::provider::FeatureProvider;
72//! use open_feature::EvaluationContext;
73//!
74//! #[tokio::main]
75//! async fn main() {
76//!     // Example using the REST resolver mode.
77//!     let provider = FlagdProvider::new(FlagdOptions {
78//!         host: "localhost".to_string(),
79//!         port: 8016,
80//!         resolver_type: ResolverType::Rest,
81//!         ..Default::default()
82//!     }).await.unwrap();
83//!
84//!     let context = EvaluationContext::default().with_targeting_key("user-123");
85//!     let result = provider.resolve_bool_value("bool-flag", &context).await.unwrap();
86//!     println!("Flag value: {}", result.value);
87//! }
88//! ```
89//!
90//! ## Evaluation Modes
91//! ### Remote Resolver (RPC)
92//! In RPC mode, the provider communicates with flagd via gRPC. It supports features like streaming updates, retry mechanisms, and name resolution (including Envoy).
93//!
94//! ```rust,no_run
95//! use open_feature_flagd::{FlagdOptions, FlagdProvider, ResolverType};
96//! use open_feature::provider::FeatureProvider;
97//! use open_feature::EvaluationContext;
98//!
99//! #[tokio::main]
100//! async fn main() {
101//!     let provider = FlagdProvider::new(FlagdOptions {
102//!         host: "localhost".to_string(),
103//!         port: 8013,
104//!         resolver_type: ResolverType::Rpc,
105//!         ..Default::default()
106//!     }).await.unwrap();
107//!
108//!     let context = EvaluationContext::default().with_targeting_key("user-123");
109//!     let bool_result = provider.resolve_bool_value("feature-enabled", &context).await.unwrap();
110//!     println!("Feature enabled: {}", bool_result.value);
111//! }
112//! ```
113//!
114//! ### REST Resolver
115//! In REST mode the provider uses the OpenFeature Remote Evaluation Protocol (OFREP) over HTTP.
116//! It is useful when gRPC is not an option.
117//! ```rust,no_run
118//! use open_feature_flagd::{FlagdOptions, FlagdProvider, ResolverType};
119//! use open_feature::provider::FeatureProvider;
120//! use open_feature::EvaluationContext;
121//!
122//! #[tokio::main]
123//! async fn main() {
124//!     let provider = FlagdProvider::new(FlagdOptions {
125//!         host: "localhost".to_string(),
126//!         port: 8016,
127//!         resolver_type: ResolverType::Rest,
128//!         ..Default::default()
129//!     }).await.unwrap();
130//!
131//!     let context = EvaluationContext::default().with_targeting_key("user-456");
132//!     let result = provider.resolve_string_value("feature-variant", &context).await.unwrap();
133//!     println!("Variant: {}", result.value);
134//! }
135//! ```
136//!
137//! ### In-Process Resolver
138//! In-process evaluation is performed locally. Flag configurations are sourced via gRPC sync stream.
139//! This mode supports advanced targeting operators (fractional, semver, string comparisons)
140//! using the built-in evaluation engine.
141//! ```rust,no_run
142//! use open_feature_flagd::{CacheSettings, FlagdOptions, FlagdProvider, ResolverType};
143//! use open_feature::provider::FeatureProvider;
144//! use open_feature::EvaluationContext;
145//!
146//! #[tokio::main]
147//! async fn main() {
148//!     let provider = FlagdProvider::new(FlagdOptions {
149//!         host: "localhost".to_string(),
150//!         port: 8015,
151//!         resolver_type: ResolverType::InProcess,
152//!         selector: Some("my-service".to_string()),
153//!         cache_settings: Some(CacheSettings::default()),
154//!         ..Default::default()
155//!     }).await.unwrap();
156//!
157//!     let context = EvaluationContext::default()
158//!         .with_targeting_key("user-abc")
159//!         .with_custom_field("environment", "production")
160//!         .with_custom_field("semver", "2.1.0");
161//!
162//!     let dark_mode = provider.resolve_bool_value("dark-mode", &context).await.unwrap();
163//!     println!("Dark mode enabled: {}", dark_mode.value);
164//! }
165//! ```
166//!
167//! ### File Mode
168//! File mode is an in-process variant where flag configurations are read from a file.
169//! This is useful for development or environments without network access.
170//! ```rust,no_run
171//! use open_feature_flagd::{FlagdOptions, FlagdProvider, ResolverType};
172//! use open_feature::provider::FeatureProvider;
173//! use open_feature::EvaluationContext;
174//!
175//! #[tokio::main]
176//! async fn main() {
177//!     let file_path = "./path/to/flagd-config.json".to_string();
178//!     let provider = FlagdProvider::new(FlagdOptions {
179//!         host: "localhost".to_string(),
180//!         resolver_type: ResolverType::File,
181//!         source_configuration: Some(file_path),
182//!         ..Default::default()
183//!     }).await.unwrap();
184//!
185//!     let context = EvaluationContext::default();
186//!     let result = provider.resolve_int_value("rollout-percentage", &context).await.unwrap();
187//!     println!("Rollout percentage: {}", result.value);
188//! }
189//! ```
190//!
191//! ## Configuration Options
192//! Configurations can be provided as constructor options or via environment variables (with constructor options taking priority). The following options are supported:
193//!
194//! | Option                                  | Env Variable                            | Type / Supported Value            | Default                             | Compatible Resolver            |
195//! |-----------------------------------------|-----------------------------------------|-----------------------------------|-------------------------------------|--------------------------------|
196//! | Host                                    | FLAGD_HOST                              | string                            | "localhost"                         | RPC, REST, In-Process, File    |
197//! | Port                                    | FLAGD_PORT                              | number                            | 8013 (RPC), 8016 (REST)             | RPC, REST, In-Process, File    |
198//! | Target URI                              | FLAGD_TARGET_URI                        | string                            | ""                                  | RPC, In-Process                |
199//! | TLS                                     | FLAGD_TLS                               | boolean                           | false                               | RPC, In-Process                |
200//! | Socket Path                             | FLAGD_SOCKET_PATH                       | string                            | ""                                  | RPC                            |
201//! | Certificate Path                        | FLAGD_SERVER_CERT_PATH                  | string                            | ""                                  | RPC, In-Process                |
202//! | Cache Type (LRU / In-Memory / Disabled) | FLAGD_CACHE                             | string ("lru", "mem", "disabled") | In-Process: disabled, others: lru   | RPC, In-Process, File          |
203//! | Cache TTL (Seconds)                     | FLAGD_CACHE_TTL                         | number                            | 60                                  | RPC, In-Process, File          |
204//! | Max Cache Size                          | FLAGD_MAX_CACHE_SIZE                    | number                            | 1000                                | RPC, In-Process, File          |
205//! | Offline File Path                       | FLAGD_OFFLINE_FLAG_SOURCE_PATH          | string                            | ""                                  | File                           |
206//! | Retry Backoff (ms)                      | FLAGD_RETRY_BACKOFF_MS                  | number                            | 1000                                | RPC, In-Process                |
207//! | Retry Backoff Maximum (ms)              | FLAGD_RETRY_BACKOFF_MAX_MS              | number                            | 120000                              | RPC, In-Process                |
208//! | Retry Grace Period                      | FLAGD_RETRY_GRACE_PERIOD                | number                            | 5                                   | RPC, In-Process                |
209//! | Event Stream Deadline (ms)              | FLAGD_STREAM_DEADLINE_MS                | number                            | 600000                              | RPC                            |
210//! | Offline Poll Interval (ms)              | FLAGD_OFFLINE_POLL_MS                   | number                            | 5000                                | File                           |
211//! | Source Selector                         | FLAGD_SOURCE_SELECTOR                   | string                            | ""                                  | In-Process                     |
212//!
213//! ## License
214//! Apache 2.0 - See [LICENSE](./../../LICENSE) for more information.
215//!
216
217pub mod cache;
218pub mod error;
219pub mod resolver;
220
221use crate::error::FlagdError;
222#[cfg(feature = "in-process")]
223use crate::resolver::in_process::resolver::{FileResolver, InProcessResolver};
224use async_trait::async_trait;
225#[cfg(feature = "rpc")]
226use open_feature::EvaluationContextFieldValue;
227use open_feature::provider::{FeatureProvider, ProviderMetadata, ResolutionDetails};
228use open_feature::{EvaluationContext, EvaluationError, StructValue, Value};
229#[cfg(feature = "rest")]
230use resolver::rest::RestResolver;
231use tracing::debug;
232use tracing::instrument;
233
234#[cfg(feature = "rpc")]
235use std::collections::BTreeMap;
236use std::sync::Arc;
237
238pub use cache::{CacheService, CacheSettings, CacheType};
239#[cfg(feature = "rpc")]
240pub use resolver::rpc::RpcResolver;
241
242// Include the generated protobuf code
243#[cfg(any(feature = "rpc", feature = "in-process"))]
244pub mod flagd {
245    #[cfg(feature = "rpc")]
246    pub mod evaluation {
247        pub mod v1 {
248            include!(concat!(env!("OUT_DIR"), "/flagd.evaluation.v1.rs"));
249        }
250    }
251    #[cfg(feature = "in-process")]
252    pub mod sync {
253        pub mod v1 {
254            include!(concat!(env!("OUT_DIR"), "/flagd.sync.v1.rs"));
255        }
256    }
257}
258
259/// Configuration options for the flagd provider
260#[derive(Debug, Clone)]
261pub struct FlagdOptions {
262    /// Host address for the service
263    pub host: String,
264    /// Port number for the service
265    pub port: u16,
266    /// Target URI for custom name resolution (e.g. "envoy://service/flagd")
267    pub target_uri: Option<String>,
268    /// Type of resolver to use
269    pub resolver_type: ResolverType,
270    /// Whether to use TLS
271    pub tls: bool,
272    /// Path to TLS certificate
273    pub cert_path: Option<String>,
274    /// Request timeout in milliseconds
275    pub deadline_ms: u32,
276    /// Cache configuration settings
277    pub cache_settings: Option<CacheSettings>,
278    /// Initial backoff duration in milliseconds for retry attempts (default: 1000ms)
279    /// Not supported in OFREP (REST) evaluation
280    pub retry_backoff_ms: u32,
281    /// Maximum backoff duration in milliseconds for retry attempts, prevents exponential backoff from growing indefinitely (default: 120000ms)
282    /// Not supported in OFREP (REST) evaluation
283    pub retry_backoff_max_ms: u32,
284    /// Maximum number of retry attempts before giving up (default: 5)
285    /// Not supported in OFREP (REST) evaluation
286    pub retry_grace_period: u32,
287    /// Source selector for filtering flag configurations
288    /// Used to scope flag sync requests in in-process evaluation
289    pub selector: Option<String>,
290    /// Unix domain socket path for connecting to flagd
291    /// When provided, this takes precedence over host:port configuration
292    /// Example: "/var/run/flagd.sock"
293    /// Only works with GRPC resolver
294    pub socket_path: Option<String>,
295    /// Source configuration for file-based resolver
296    pub source_configuration: Option<String>,
297    /// The deadline in milliseconds for event streaming operations. Set to 0 to disable.
298    /// Recommended to prevent infrastructure from killing idle connections.
299    pub stream_deadline_ms: u32,
300    /// HTTP/2 keepalive time in milliseconds. Sends pings to keep connections alive during
301    /// idle periods, allowing RPCs to start quickly without delay. Set to 0 to disable.
302    pub keep_alive_time_ms: u64,
303    /// Offline polling interval in milliseconds
304    pub offline_poll_interval_ms: Option<u32>,
305    /// Provider ID for identifying this provider instance to flagd
306    /// Used in in-process resolver for sync requests
307    pub provider_id: Option<String>,
308}
309/// Type of resolver to use for flag evaluation
310#[derive(Debug, Clone, PartialEq)]
311pub enum ResolverType {
312    /// Remote evaluation using gRPC connection to flagd service
313    #[cfg(feature = "rpc")]
314    Rpc,
315    /// Remote evaluation using REST connection to flagd service
316    #[cfg(feature = "rest")]
317    Rest,
318    /// Local evaluation with embedded flag engine using gRPC connection
319    #[cfg(feature = "in-process")]
320    InProcess,
321    /// Local evaluation with no external dependencies
322    #[cfg(feature = "in-process")]
323    File,
324}
325impl Default for FlagdOptions {
326    fn default() -> Self {
327        let resolver_type = Self::default_resolver_type();
328
329        let port = Self::default_port(&resolver_type);
330
331        #[allow(unused_mut)]
332        let mut options = Self {
333            host: std::env::var("FLAGD_HOST").unwrap_or_else(|_| "localhost".to_string()),
334            port: std::env::var("FLAGD_PORT")
335                .ok()
336                .and_then(|p| p.parse().ok())
337                .unwrap_or(port),
338            target_uri: std::env::var("FLAGD_TARGET_URI").ok(),
339            resolver_type,
340            tls: std::env::var("FLAGD_TLS")
341                .map(|v| v.to_lowercase() == "true")
342                .unwrap_or(false),
343            cert_path: std::env::var("FLAGD_SERVER_CERT_PATH").ok(),
344            deadline_ms: std::env::var("FLAGD_DEADLINE_MS")
345                .ok()
346                .and_then(|v| v.parse().ok())
347                .unwrap_or(500),
348            retry_backoff_ms: std::env::var("FLAGD_RETRY_BACKOFF_MS")
349                .ok()
350                .and_then(|v| v.parse().ok())
351                .unwrap_or(1000),
352            retry_backoff_max_ms: std::env::var("FLAGD_RETRY_BACKOFF_MAX_MS")
353                .ok()
354                .and_then(|v| v.parse().ok())
355                .unwrap_or(120000),
356            retry_grace_period: std::env::var("FLAGD_RETRY_GRACE_PERIOD")
357                .ok()
358                .and_then(|v| v.parse().ok())
359                .unwrap_or(5),
360            stream_deadline_ms: std::env::var("FLAGD_STREAM_DEADLINE_MS")
361                .ok()
362                .and_then(|v| v.parse().ok())
363                .unwrap_or(600000),
364            keep_alive_time_ms: std::env::var("FLAGD_KEEP_ALIVE_TIME_MS")
365                .ok()
366                .and_then(|v| v.parse().ok())
367                .unwrap_or(0), // Disabled by default, per gherkin spec
368            socket_path: std::env::var("FLAGD_SOCKET_PATH").ok(),
369            selector: std::env::var("FLAGD_SOURCE_SELECTOR").ok(),
370            cache_settings: Some(CacheSettings::default()),
371            source_configuration: std::env::var("FLAGD_OFFLINE_FLAG_SOURCE_PATH").ok(),
372            offline_poll_interval_ms: Some(
373                std::env::var("FLAGD_OFFLINE_POLL_MS")
374                    .ok()
375                    .and_then(|s| s.parse().ok())
376                    .unwrap_or(5000),
377            ),
378            provider_id: std::env::var("FLAGD_PROVIDER_ID").ok(),
379        };
380
381        #[cfg(feature = "in-process")]
382        {
383            let resolver_env_set = std::env::var("FLAGD_RESOLVER").is_ok();
384            if options.source_configuration.is_some() && !resolver_env_set {
385                // Only override to File if FLAGD_RESOLVER wasn't explicitly set
386                options.resolver_type = ResolverType::File;
387            }
388            // Disable caching for in-process/file modes per spec (caching is RPC-only)
389            if matches!(
390                options.resolver_type,
391                ResolverType::InProcess | ResolverType::File
392            ) {
393                options.cache_settings = None;
394            }
395        }
396
397        options
398    }
399}
400
401impl FlagdOptions {
402    fn default_resolver_type() -> ResolverType {
403        if let Ok(r) = std::env::var("FLAGD_RESOLVER") {
404            match r.to_uppercase().as_str() {
405                #[cfg(feature = "rpc")]
406                "RPC" => return ResolverType::Rpc,
407                #[cfg(feature = "rest")]
408                "REST" => return ResolverType::Rest,
409                #[cfg(feature = "in-process")]
410                "IN-PROCESS" | "INPROCESS" => return ResolverType::InProcess,
411                #[cfg(feature = "in-process")]
412                "FILE" | "OFFLINE" => return ResolverType::File,
413                _ => {}
414            }
415        }
416        // Return first available resolver type as default
417        #[cfg(feature = "rpc")]
418        return ResolverType::Rpc;
419        #[cfg(all(feature = "rest", not(feature = "rpc")))]
420        return ResolverType::Rest;
421        #[cfg(all(feature = "in-process", not(feature = "rpc"), not(feature = "rest")))]
422        return ResolverType::InProcess;
423        #[cfg(not(any(feature = "rpc", feature = "rest", feature = "in-process")))]
424        compile_error!("At least one resolver feature must be enabled: rpc, rest, or in-process");
425    }
426
427    fn default_port(resolver_type: &ResolverType) -> u16 {
428        match resolver_type {
429            #[cfg(feature = "rpc")]
430            ResolverType::Rpc => 8013,
431            #[cfg(feature = "in-process")]
432            ResolverType::InProcess => 8015,
433            #[cfg(feature = "rest")]
434            ResolverType::Rest => 8016,
435            #[allow(unreachable_patterns)]
436            _ => 8013,
437        }
438    }
439}
440
441/// Main provider implementation for flagd
442#[derive(Clone)]
443pub struct FlagdProvider {
444    /// The underlying feature flag resolver
445    provider: Arc<dyn FeatureProvider + Send + Sync>,
446    /// Optional caching layer
447    cache: Option<Arc<CacheService<Value>>>,
448}
449
450impl FlagdProvider {
451    #[instrument(skip(options))]
452    pub async fn new(options: FlagdOptions) -> Result<Self, FlagdError> {
453        debug!("Initializing FlagdProvider with options: {:?}", options);
454
455        // Validate File resolver configuration
456        #[cfg(feature = "in-process")]
457        if options.resolver_type == ResolverType::File && options.source_configuration.is_none() {
458            return Err(FlagdError::Config(
459                "File resolver requires 'source_configuration' (FLAGD_OFFLINE_FLAG_SOURCE_PATH) to be set".to_string()
460            ));
461        }
462
463        let provider: Arc<dyn FeatureProvider + Send + Sync> = match options.resolver_type {
464            #[cfg(feature = "rpc")]
465            ResolverType::Rpc => {
466                debug!("Using RPC resolver");
467                Arc::new(RpcResolver::new(&options).await?)
468            }
469            #[cfg(feature = "rest")]
470            ResolverType::Rest => {
471                debug!("Using REST resolver");
472                Arc::new(RestResolver::new(&options))
473            }
474            #[cfg(feature = "in-process")]
475            ResolverType::InProcess => {
476                debug!("Using in-process resolver");
477                Arc::new(InProcessResolver::new(&options).await?)
478            }
479            #[cfg(feature = "in-process")]
480            ResolverType::File => {
481                debug!("Using file resolver");
482                Arc::new(
483                    FileResolver::new(
484                        options
485                            .source_configuration
486                            .expect("source_configuration validated above"),
487                        options.cache_settings.clone(),
488                    )
489                    .await?,
490                )
491            }
492        };
493
494        Ok(Self {
495            provider,
496            cache: options
497                .cache_settings
498                .map(|settings| Arc::new(CacheService::new(settings))),
499        })
500    }
501}
502
503impl std::fmt::Debug for FlagdProvider {
504    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
505        f.debug_struct("FlagdProvider")
506            .field("cache", &self.cache)
507            .finish()
508    }
509}
510
511#[cfg(feature = "rpc")]
512pub(crate) fn convert_context(context: &EvaluationContext) -> Option<prost_types::Struct> {
513    let mut fields = BTreeMap::new();
514
515    if let Some(targeting_key) = &context.targeting_key {
516        fields.insert(
517            "targetingKey".to_string(),
518            prost_types::Value {
519                kind: Some(prost_types::value::Kind::StringValue(targeting_key.clone())),
520            },
521        );
522    }
523
524    for (key, value) in &context.custom_fields {
525        let prost_value = match value {
526            EvaluationContextFieldValue::String(s) => prost_types::Value {
527                kind: Some(prost_types::value::Kind::StringValue(s.clone())),
528            },
529            EvaluationContextFieldValue::Bool(b) => prost_types::Value {
530                kind: Some(prost_types::value::Kind::BoolValue(*b)),
531            },
532            EvaluationContextFieldValue::Int(i) => prost_types::Value {
533                kind: Some(prost_types::value::Kind::NumberValue(*i as f64)),
534            },
535            EvaluationContextFieldValue::Float(f) => prost_types::Value {
536                kind: Some(prost_types::value::Kind::NumberValue(*f)),
537            },
538            EvaluationContextFieldValue::DateTime(dt) => prost_types::Value {
539                kind: Some(prost_types::value::Kind::StringValue(dt.to_string())),
540            },
541            EvaluationContextFieldValue::Struct(s) => prost_types::Value {
542                kind: Some(prost_types::value::Kind::StringValue(format!("{:?}", s))),
543            },
544        };
545        fields.insert(key.clone(), prost_value);
546    }
547
548    Some(prost_types::Struct { fields })
549}
550
551#[cfg(feature = "rpc")]
552pub(crate) fn convert_proto_struct_to_struct_value(
553    proto_struct: prost_types::Struct,
554) -> StructValue {
555    let fields = proto_struct
556        .fields
557        .into_iter()
558        .map(|(key, value)| {
559            (
560                key,
561                match value.kind.unwrap() {
562                    prost_types::value::Kind::NullValue(_) => Value::String(String::new()),
563                    prost_types::value::Kind::NumberValue(n) => Value::Float(n),
564                    prost_types::value::Kind::StringValue(s) => Value::String(s),
565                    prost_types::value::Kind::BoolValue(b) => Value::Bool(b),
566                    prost_types::value::Kind::StructValue(s) => Value::String(format!("{:?}", s)),
567                    prost_types::value::Kind::ListValue(l) => Value::String(format!("{:?}", l)),
568                },
569            )
570        })
571        .collect();
572
573    StructValue { fields }
574}
575
576impl FlagdProvider {
577    async fn get_cached_value<T>(
578        &self,
579        flag_key: &str,
580        context: &EvaluationContext,
581        value_converter: impl Fn(Value) -> Option<T>,
582    ) -> Option<T> {
583        if let Some(cache) = &self.cache
584            && let Some(cached_value) = cache.get(flag_key, context).await
585        {
586            return value_converter(cached_value);
587        }
588        None
589    }
590}
591
592#[async_trait]
593impl FeatureProvider for FlagdProvider {
594    fn metadata(&self) -> &ProviderMetadata {
595        self.provider.metadata()
596    }
597
598    async fn resolve_bool_value(
599        &self,
600        flag_key: &str,
601        context: &EvaluationContext,
602    ) -> Result<ResolutionDetails<bool>, EvaluationError> {
603        if let Some(value) = self
604            .get_cached_value(flag_key, context, |v| match v {
605                Value::Bool(b) => Some(b),
606                _ => None,
607            })
608            .await
609        {
610            return Ok(ResolutionDetails::new(value));
611        }
612
613        let result = self.provider.resolve_bool_value(flag_key, context).await?;
614
615        if let Some(cache) = &self.cache {
616            cache
617                .add(flag_key, context, Value::Bool(result.value))
618                .await;
619        }
620
621        Ok(result)
622    }
623
624    async fn resolve_int_value(
625        &self,
626        flag_key: &str,
627        context: &EvaluationContext,
628    ) -> Result<ResolutionDetails<i64>, EvaluationError> {
629        if let Some(value) = self
630            .get_cached_value(flag_key, context, |v| match v {
631                Value::Int(i) => Some(i),
632                _ => None,
633            })
634            .await
635        {
636            return Ok(ResolutionDetails::new(value));
637        }
638
639        let result = self.provider.resolve_int_value(flag_key, context).await?;
640
641        if let Some(cache) = &self.cache {
642            cache.add(flag_key, context, Value::Int(result.value)).await;
643        }
644
645        Ok(result)
646    }
647
648    async fn resolve_float_value(
649        &self,
650        flag_key: &str,
651        context: &EvaluationContext,
652    ) -> Result<ResolutionDetails<f64>, EvaluationError> {
653        if let Some(value) = self
654            .get_cached_value(flag_key, context, |v| match v {
655                Value::Float(f) => Some(f),
656                _ => None,
657            })
658            .await
659        {
660            return Ok(ResolutionDetails::new(value));
661        }
662
663        let result = self.provider.resolve_float_value(flag_key, context).await?;
664
665        if let Some(cache) = &self.cache {
666            cache
667                .add(flag_key, context, Value::Float(result.value))
668                .await;
669        }
670
671        Ok(result)
672    }
673
674    async fn resolve_string_value(
675        &self,
676        flag_key: &str,
677        context: &EvaluationContext,
678    ) -> Result<ResolutionDetails<String>, EvaluationError> {
679        if let Some(value) = self
680            .get_cached_value(flag_key, context, |v| match v {
681                Value::String(s) => Some(s),
682                _ => None,
683            })
684            .await
685        {
686            return Ok(ResolutionDetails::new(value));
687        }
688
689        let result = self
690            .provider
691            .resolve_string_value(flag_key, context)
692            .await?;
693
694        if let Some(cache) = &self.cache {
695            cache
696                .add(flag_key, context, Value::String(result.value.clone()))
697                .await;
698        }
699
700        Ok(result)
701    }
702
703    async fn resolve_struct_value(
704        &self,
705        flag_key: &str,
706        context: &EvaluationContext,
707    ) -> Result<ResolutionDetails<StructValue>, EvaluationError> {
708        if let Some(value) = self
709            .get_cached_value(flag_key, context, |v| match v {
710                Value::Struct(s) => Some(s),
711                _ => None,
712            })
713            .await
714        {
715            return Ok(ResolutionDetails::new(value));
716        }
717
718        let result = self
719            .provider
720            .resolve_struct_value(flag_key, context)
721            .await?;
722
723        if let Some(cache) = &self.cache {
724            cache
725                .add(flag_key, context, Value::Struct(result.value.clone()))
726                .await;
727        }
728
729        Ok(result)
730    }
731}