Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/copilot-instructions.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Key characteristics:

### Core Functionality

1. **JSON Logic Evaluation** - Full support for [JSON Logic](https://jsonlogic.com/) operations via [datalogic-rs](https://github.com/cozylogic/datalogic-rs)
1. **JSON Logic Evaluation** - Full support for [JSON Logic](https://jsonlogic.com/) operations via [datalogic-rs](https://github.com/GoPlasmatic/datalogic-rs)
2. **Custom Operators** - Feature-flag specific operators for:
- `fractional` - Consistent bucketing for A/B testing and gradual rollouts
- `sem_ver` - Semantic version comparison (=, !=, <, <=, >, >=, ^, ~)
Expand Down Expand Up @@ -61,7 +61,7 @@ Key characteristics:
### Related Technologies

- **[JSON Logic](https://jsonlogic.com/)** - The rule evaluation engine
- **[datalogic-rs](https://github.com/cozylogic/datalogic-rs)** - Rust implementation of JSON Logic
- **[datalogic-rs](https://github.com/GoPlasmatic/datalogic-rs)** - Rust implementation of JSON Logic
- **[Chicory](https://github.com/nicknisi/chicory)** - Pure Java WebAssembly runtime (no JNI required)

## Relationship to flagd Ecosystem
Expand Down
2 changes: 1 addition & 1 deletion CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,5 +128,5 @@ Launch multiple agents **in parallel** when their work is independent (e.g., res
- [flagd Custom Operations](https://flagd.dev/reference/specifications/custom-operations/)
- [Flag Definitions Schema](https://flagd.dev/reference/flag-definitions/)
- [JSON Logic](https://jsonlogic.com/)
- [datalogic-rs](https://github.com/cozylogic/datalogic-rs)
- [datalogic-rs](https://github.com/GoPlasmatic/datalogic-rs)
- [Chicory WASM Runtime](https://github.com/nicknisi/chicory)
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ crate-type = ["cdylib", "rlib"]

[dependencies]
datalogic-rs = "4.0"
serde = { version = "1.0", features = ["derive", "alloc"], default-features = false }
serde = { version = "1.0", features = ["derive", "alloc", "rc"], default-features = false }
serde_json = { version = "1.0", features = ["alloc"], default-features = false }
boon = "0.6"
murmurhash3 = "0.0.5"
Expand Down Expand Up @@ -65,9 +65,9 @@ name = "scale"
harness = false

[profile.release]
# Optimize for speed instead of size for better runtime performance
opt-level = 2
# Optimize for size — the release profile is primarily used for WASM builds
opt-level = "z"
lto = true
codegen-units = 16
codegen-units = 1
strip = true
panic = "abort"
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,15 +195,15 @@ The WASM file is output to `target/wasm32-unknown-unknown/release/flagd_evaluato
- [flagd Flag Definitions](https://flagd.dev/reference/flag-definitions/)
- [flagd Custom Operations](https://flagd.dev/reference/specifications/custom-operations/)
- [flagd Provider Specification](https://github.com/open-feature/flagd/blob/main/docs/reference/specifications/providers.md)
- [JSON Logic](https://jsonlogic.com/) | [datalogic-rs](https://github.com/cozylogic/datalogic-rs)
- [JSON Logic](https://jsonlogic.com/) | [datalogic-rs](https://github.com/GoPlasmatic/datalogic-rs)

## License

Apache License, Version 2.0 — see [LICENSE](LICENSE).

## Acknowledgments

- [datalogic-rs](https://github.com/cozylogic/datalogic-rs) — JSON Logic engine
- [datalogic-rs](https://github.com/GoPlasmatic/datalogic-rs) — JSON Logic engine
- [Chicory](https://github.com/dylibso/chicory) — Pure Java WASM runtime
- [wazero](https://wazero.io/) — Pure Go WASM runtime
- [PyO3](https://pyo3.rs/) — Rust-Python bindings
Expand Down
123 changes: 19 additions & 104 deletions src/evaluator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ impl FlagEvaluator {
}
}

// Parse the configuration
let new_parsing_result = match ParsingResult::parse(json_config) {
// Parse the configuration, reusing the evaluator's DataLogic engine
let new_parsing_result = match ParsingResult::parse_with_engine(json_config, &self.logic) {
Ok(result) => result,
Err(e) => {
return Ok(UpdateStateResponse {
Expand Down Expand Up @@ -273,28 +273,20 @@ impl FlagEvaluator {
let flag = match state.flags.get(flag_key) {
Some(f) => f,
None => {
// Flag not found - return flag-set metadata per spec (best effort)
let flag_set_metadata =
Self::merge_metadata_flag_set_only(&state.flag_set_metadata);
// Flag not found - return pre-filtered flag-set metadata per spec
return EvaluationResult {
value: JsonValue::Null,
variant: None,
reason: ResolutionReason::FlagNotFound,
error_code: Some(ErrorCode::FlagNotFound),
error_message: Some(format!("Flag '{}' not found in configuration", flag_key)),
flag_metadata: flag_set_metadata,
flag_metadata: state.filtered_flag_set_metadata.clone(),
};
}
};

// Perform the evaluation
let result = self.evaluate_flag_core(
flag,
flag_key,
context,
needs_enrichment,
&state.flag_set_metadata,
);
let result = self.evaluate_flag_core(flag, flag_key, context, needs_enrichment);

// Apply type checking if requested
match expected_type {
Expand All @@ -310,18 +302,16 @@ impl FlagEvaluator {
flag_key: &str,
context: Value,
needs_enrichment: bool,
flag_set_metadata: &HashMap<String, JsonValue>,
) -> EvaluationResult {
// Check if flag is disabled - still return metadata per spec
if flag.state == "DISABLED" {
let merged_metadata = Self::merge_metadata(flag_set_metadata, &flag.metadata);
return EvaluationResult {
value: JsonValue::Null,
variant: None,
reason: ResolutionReason::Disabled,
error_code: Some(ErrorCode::FlagNotFound),
error_message: Some(format!("flag: {} is disabled", flag_key)),
flag_metadata: merged_metadata,
flag_metadata: flag.merged_metadata.clone(),
};
}

Expand All @@ -340,8 +330,7 @@ impl FlagEvaluator {
Some(value) => {
let result =
EvaluationResult::static_result(value.clone(), default_variant.clone());
// Lazy metadata: only merge if there's actually metadata
Self::with_lazy_metadata(flag_set_metadata, &flag.metadata, result)
result.with_precomputed_metadata(flag.merged_metadata.clone())
}
None => EvaluationResult::error(
ErrorCode::General,
Expand Down Expand Up @@ -386,7 +375,7 @@ impl FlagEvaluator {
value.clone(),
default_variant.clone(),
);
Self::with_lazy_metadata(flag_set_metadata, &flag.metadata, result)
result.with_precomputed_metadata(flag.merged_metadata.clone())
}
None => EvaluationResult::error(
ErrorCode::General,
Expand Down Expand Up @@ -430,7 +419,7 @@ impl FlagEvaluator {
match flag.variants.get(&variant_name) {
Some(value) => {
let result = EvaluationResult::targeting_match(value.clone(), variant_name);
Self::with_lazy_metadata(flag_set_metadata, &flag.metadata, result)
result.with_precomputed_metadata(flag.merged_metadata.clone())
}
None => EvaluationResult::error(
ErrorCode::General,
Expand Down Expand Up @@ -568,13 +557,8 @@ impl FlagEvaluator {
for (flag_key, flag) in &parsing_result.flags {
// Pre-evaluate disabled flags
if flag.state == "DISABLED" {
let result = self.evaluate_flag_core(
flag,
flag_key,
Value::Object(Map::new()),
false,
&parsing_result.flag_set_metadata,
);
let result =
self.evaluate_flag_core(flag, flag_key, Value::Object(Map::new()), false);
results.insert(flag_key.clone(), result);
continue;
}
Expand All @@ -587,13 +571,8 @@ impl FlagEvaluator {
};

if is_static {
let result = self.evaluate_flag_core(
flag,
flag_key,
Value::Object(Map::new()),
false,
&parsing_result.flag_set_metadata,
);
let result =
self.evaluate_flag_core(flag, flag_key, Value::Object(Map::new()), false);
results.insert(flag_key.clone(), result);
}
}
Expand Down Expand Up @@ -651,86 +630,22 @@ impl FlagEvaluator {
// Get current Unix timestamp (seconds since epoch)
let timestamp = crate::get_current_time();

// Create $flagd object with nested properties
let mut flagd_props = Map::new();
// Create $flagd object with nested properties (pre-allocate for 2 entries)
let mut flagd_props = Map::with_capacity(2);
flagd_props.insert("flagKey".to_string(), Value::String(flag_key.to_string()));
flagd_props.insert("timestamp".to_string(), Value::Number(timestamp.into()));

// Add $flagd object to context
enriched.insert("$flagd".to_string(), Value::Object(flagd_props));

// Ensure targetingKey exists (use existing or empty string)
if !enriched.contains_key("targetingKey") {
enriched.insert("targetingKey".to_string(), Value::String(String::new()));
}
// Ensure targetingKey exists (use entry API to avoid double lookup)
enriched
.entry("targetingKey")
.or_insert_with(|| Value::String(String::new()));

Value::Object(enriched)
}

/// Merges flag-set metadata with flag-level metadata.
fn merge_metadata(
flag_set_metadata: &HashMap<String, JsonValue>,
flag_metadata: &HashMap<String, JsonValue>,
) -> Option<HashMap<String, JsonValue>> {
// Filter out internal fields (those starting with $) from flag-set metadata
let filtered_flag_set: HashMap<String, JsonValue> = flag_set_metadata
.iter()
.filter(|(key, _)| !key.starts_with('$'))
.map(|(k, v)| (k.clone(), v.clone()))
.collect();

// If both are empty after filtering, return None
if filtered_flag_set.is_empty() && flag_metadata.is_empty() {
return None;
}

// Start with filtered flag-set metadata as the base
let mut merged = filtered_flag_set;

// Override with flag-level metadata (flag metadata takes priority)
for (key, value) in flag_metadata {
merged.insert(key.clone(), value.clone());
}

Some(merged)
}

/// Merges only flag-set metadata (no flag-level metadata).
/// Used in flag-not-found paths to avoid creating an empty HashMap.
fn merge_metadata_flag_set_only(
flag_set_metadata: &HashMap<String, JsonValue>,
) -> Option<HashMap<String, JsonValue>> {
let filtered: HashMap<String, JsonValue> = flag_set_metadata
.iter()
.filter(|(key, _)| !key.starts_with('$'))
.map(|(k, v)| (k.clone(), v.clone()))
.collect();
if filtered.is_empty() {
None
} else {
Some(filtered)
}
}

/// Lazy metadata attachment - only merges metadata if there's actually metadata to merge.
/// This avoids the cost of creating HashMaps when both sources are empty.
fn with_lazy_metadata(
flag_set_metadata: &HashMap<String, JsonValue>,
flag_metadata: &HashMap<String, JsonValue>,
result: EvaluationResult,
) -> EvaluationResult {
// Fast path: if both are empty, skip merging entirely
if flag_set_metadata.is_empty() && flag_metadata.is_empty() {
return result;
}

// Only merge if there's actual metadata
match Self::merge_metadata(flag_set_metadata, flag_metadata) {
Some(metadata) => result.with_metadata(metadata),
None => result,
}
}

/// Evaluates a flag by its numeric index (from `flag_indices` in `UpdateStateResponse`).
///
/// This is a fast path that avoids flag key string handling by using O(1) Vec lookup.
Expand Down
Loading
Loading