1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
use async_trait::async_trait;
use open_feature::{
provider::ResolutionDetails, EvaluationContext, EvaluationError, EvaluationErrorCode,
EvaluationResult, StructValue,
};
use serde_json::{Map, Value};
use crate::{conversions, types::Result};
/// Trait for experiment variant resolution.
///
/// Implementors provide the ability to determine which experiment variants
/// are applicable for a given evaluation context.
#[async_trait]
pub trait FeatureExperimentMeta: Send + Sync {
/// Get the list of applicable experiment variant IDs for the given context.
async fn get_applicable_variants(
&self,
context: EvaluationContext,
prefix_filter: Option<Vec<String>>,
exclude_prefix_filter: Option<Vec<String>>,
) -> Result<Vec<String>>;
}
/// Trait for bulk configuration resolution.
///
/// Implementors provide the ability to resolve all feature flags at once,
/// optionally filtered by key prefixes.
#[async_trait]
pub trait AllFeatureProvider: Send + Sync {
/// Resolve all features for the given evaluation context.
async fn resolve_all_features(
&self,
context: EvaluationContext,
) -> Result<Map<String, Value>> {
self.resolve_all_features_with_filter(context, None, None)
.await
}
/// Resolve all features for the given evaluation context, optionally
/// filtered to only include keys matching the provided prefixes.
async fn resolve_all_features_with_filter(
&self,
context: EvaluationContext,
prefix_filter: Option<Vec<String>>,
exclude_prefix_filter: Option<Vec<String>>,
) -> Result<Map<String, Value>>;
/// Resolve a flag and extract it as `T`.
///
/// Error reasons need no handling here: the failure paths return `EvaluationError`, which has
/// no `reason` field, and the SDK stamps `EvaluationReason::Error` on the details it builds
/// from it. The Java and Python providers return the details object themselves, so they set it
/// explicitly — same outcome, different seam.
///
/// TODO: successful resolutions leave `reason` unset. Reporting it accurately (STATIC for a
/// default-config value, TARGETING_MATCH for a context override, SPLIT for an experiment
/// variant) needs `eval_config` in `superposition_core` to say, per key, where the value came
/// from. Until it does, guessing would be worse than saying nothing — a flag no experiment
/// touched would still be labelled SPLIT. The same TODO applies to the Java and Python clients.
async fn resolve_typed<T: Send + Sync>(
&self,
flag_key: &str,
evaluation_context: EvaluationContext,
type_name: &str,
extractor: impl Fn(Value) -> Option<T> + Send + Sync,
) -> EvaluationResult<ResolutionDetails<T>> {
match self.resolve_all_features(evaluation_context).await {
Ok(mut config) => {
match config.remove(flag_key) {
Some(value) => extractor(value)
.map(ResolutionDetails::new)
.ok_or_else(|| EvaluationError {
code: EvaluationErrorCode::TypeMismatch,
message: Some(format!(
"Flag '{flag_key}' is not a {type_name}",
)),
}),
None => Err(EvaluationError {
code: EvaluationErrorCode::FlagNotFound,
message: Some(format!("Flag '{}' not found", flag_key)),
}),
}
}
Err(e) => {
log::error!("Error evaluating {} flag {}: {}", type_name, flag_key, e);
Err(EvaluationError {
code: EvaluationErrorCode::General(format!(
"Error evaluating flag '{}': {}",
flag_key, e
)),
message: Some(format!("Error evaluating flag '{}': {}", flag_key, e)),
})
}
}
}
async fn resolve_bool(
&self,
flag_key: &str,
evaluation_context: EvaluationContext,
) -> EvaluationResult<ResolutionDetails<bool>> {
self.resolve_typed(flag_key, evaluation_context, "boolean", |v| v.as_bool())
.await
}
async fn resolve_string(
&self,
flag_key: &str,
evaluation_context: EvaluationContext,
) -> EvaluationResult<ResolutionDetails<String>> {
self.resolve_typed(flag_key, evaluation_context, "string", |v| match v {
Value::String(s) => Some(s),
_ => None,
})
.await
}
async fn resolve_int(
&self,
flag_key: &str,
evaluation_context: EvaluationContext,
) -> EvaluationResult<ResolutionDetails<i64>> {
self.resolve_typed(flag_key, evaluation_context, "integer", |v| v.as_i64())
.await
}
async fn resolve_float(
&self,
flag_key: &str,
evaluation_context: EvaluationContext,
) -> EvaluationResult<ResolutionDetails<f64>> {
self.resolve_typed(flag_key, evaluation_context, "float", |v| v.as_f64())
.await
}
async fn resolve_struct(
&self,
flag_key: &str,
evaluation_context: EvaluationContext,
) -> EvaluationResult<ResolutionDetails<StructValue>> {
self.resolve_typed(flag_key, evaluation_context, "struct", |v| {
conversions::value_to_struct(v).ok()
})
.await
}
/// Resolve a flag whose value is a JSON array.
///
/// `resolve_struct` cannot return one: OpenFeature models an object flag as a
/// `StructValue`, which has no array form, so a top-level array is a TypeMismatch there.
/// (An array *nested inside* an object flag is fine and needs no special handling.)
/// This is the typed way to read one; the alternative is `resolve_all_features`, which
/// hands back the raw `serde_json::Value`.
///
/// The Java and Python clients return top-level arrays from their object accessor
/// directly, because their SDKs' object type admits one. This method exists to close
/// that gap, not to add a capability the other clients lack.
async fn resolve_array(
&self,
flag_key: &str,
evaluation_context: EvaluationContext,
) -> EvaluationResult<ResolutionDetails<Vec<open_feature::Value>>> {
self.resolve_typed(flag_key, evaluation_context, "array", |v| match v {
Value::Array(items) => items
.into_iter()
.map(|item| conversions::value_to_openfeature_value(item).ok())
.collect(),
_ => None,
})
.await
}
}