harmony-jolt 0.11.0

JSON to JSON transformation library using the JOLT spec
Documentation
use crate::{Error, Result};
use serde_json::Value;
use super::budget::RunBudget;
use super::function_parser::{FunctionArg, PathExpr, FunctionCall};
use super::functions::{execute_function, is_known_function};

/// Run a function call against the current data and context.
///
/// The name is checked first, so a typo fails whatever the input holds. Every argument is
/// evaluated before the call decides it has no value, so an argument error does not depend
/// on argument order. `Ok(None)` means the call has no value, and the caller leaves its key
/// unwritten. That happens when the function itself has no value, as Java JOLT's
/// `substring` does, or when a nested call has no value. Java JOLT has no nested calls, so
/// the second rule is this crate's own.
pub fn evaluate_call(
    call: &FunctionCall,
    current: &Value,
    root: &Value,
    context: Option<&Value>,
    path_stack: &[String],
    budget: &mut RunBudget,
) -> Result<Option<Value>> {
    if !is_known_function(&call.name) {
        return Err(Error::InvalidFunction(call.name.clone()));
    }

    let mut evaluated = Vec::with_capacity(call.args.len());
    let mut has_value = true;
    for arg in &call.args {
        match evaluate_arg(arg, current, root, context, path_stack, budget)? {
            Some(value) => evaluated.push(value),
            None => has_value = false,
        }
    }
    if !has_value {
        return Ok(None);
    }

    execute_function(&call.name, &evaluated)
}

/// Evaluate a function argument against the current data and context.
///
/// A path that names a key or index the input lacks gives `null`, as in Java JOLT, and the
/// function decides what that means. `Ok(None)` comes only from a nested call with no value.
fn evaluate_arg(
    arg: &FunctionArg,
    current: &Value,
    root: &Value,
    context: Option<&Value>,
    path_stack: &[String],
    budget: &mut RunBudget,
) -> Result<Option<Value>> {
    match arg {
        FunctionArg::Literal(s) => Ok(Some(Value::String(s.clone()))),
        FunctionArg::Number(n) => Ok(Some(Value::Number((*n).into()))),
        FunctionArg::PathExpr(path_expr) => {
            evaluate_path_expr(path_expr, current, root, path_stack, budget).map(Some)
        }
        FunctionArg::ContextRef(key) => {
            // Context references like ^value look up in the context object
            if let Some(ctx) = context {
                let value = ctx.get(key).ok_or_else(|| {
                    Error::PathEvaluationError(format!("Context key not found: {}", key))
                })?;
                budget.charge_value(value)?;
                Ok(Some(value.clone()))
            } else {
                Err(Error::PathEvaluationError(
                    "No context provided".to_string(),
                ))
            }
        }
        FunctionArg::Identifier(ident) => {
            // Try to look up as a path in current
            match current.get(ident) {
                Some(value) => {
                    budget.charge_value(value)?;
                    Ok(Some(value.clone()))
                }
                None => Ok(Some(Value::String(ident.clone()))),
            }
        }
        FunctionArg::Function(nested_call) => {
            let result = evaluate_call(nested_call, current, root, context, path_stack, budget)?;
            if let Some(value) = &result {
                budget.charge_value(value)?;
            }
            Ok(result)
        }
    }
}

/// Walk exact path segments from `base`. Segments can come from body keys, so they are never
/// parsed as path syntax.
fn resolve_segments<'a>(base: &'a Value, segments: &[String]) -> Option<&'a Value> {
    segments
        .iter()
        .try_fold(base, |current, segment| child(current, segment))
}

/// Evaluate a path expression like @(1,name[0])
///
/// In JOLT modifier semantics, @(n,path) navigates the context stack:
/// - @(1,path): reference from current object (the one being modified)
/// - @(2,path): go up 1 level (parent), then navigate path  
/// - @(n,path): go up n-1 levels from current, then navigate path
///
/// Note: @(0,...) is non-standard; we treat it as root for compatibility but
/// it should generally not be used.
fn evaluate_path_expr(
    path_expr: &PathExpr,
    current: &Value,
    root: &Value,
    path_stack: &[String],
    budget: &mut RunBudget,
) -> Result<Value> {
    // Get the base value by going up `level` levels
    let base = if path_expr.level == 0 {
        // @(0,...) means reference from root
        root
    } else if path_expr.level == 1 {
        // @(1,...) means reference from current object
        current
    } else if path_expr.level > path_stack.len() + 1 {
        return Err(Error::PathEvaluationError(format!(
            "Path level {} is above the root",
            path_expr.level
        )));
    } else {
        // Navigate to the appropriate level by going up from current position
        // path_stack represents where we are in the spec hierarchy
        // level=2 means go up 1 from current, level=3 means go up 2, etc.
        let levels_up = path_expr.level - 1;
        let target_depth = path_stack.len().saturating_sub(levels_up);
        let target_path = &path_stack[..target_depth];

        // Start from root and navigate to the target depth. A failure must not fall back to
        // root, or the shape of the body would decide which object the path reads.
        budget.charge_path(target_path)?;
        resolve_segments(root, target_path).ok_or_else(|| {
            Error::PathEvaluationError(format!(
                "Path level {} has no value in the input",
                path_expr.level
            ))
        })?
    };

    // A missing ancestor level stays an error above. Only the path below an existing level
    // may name a key the input lacks, and that key reads as null.
    let value = if path_expr.path.is_empty() {
        base
    } else {
        match navigate_path(base, &path_expr.path)? {
            Some(value) => value,
            None => return Ok(Value::Null),
        }
    };

    budget.charge_value(value)?;
    Ok(value.clone())
}

/// Get a child by key. An array takes the key as an index, because wildcard walks record
/// array positions in the path stack. Only the canonical form of an index matches.
fn child<'a>(value: &'a Value, key: &str) -> Option<&'a Value> {
    match value {
        Value::Array(items) => key
            .parse::<usize>()
            .ok()
            .filter(|index| index.to_string() == key)
            .and_then(|index| items.get(index)),
        _ => value.get(key),
    }
}

/// Navigate a path like "name[0]" or "lower.leading".
///
/// `Ok(None)` means the input has no value at the path. An error means the path itself is
/// malformed.
fn navigate_path<'a>(base: &'a Value, path: &str) -> Result<Option<&'a Value>> {
    let mut current = base;
    let mut remaining = path;

    while !remaining.is_empty() {
        // Check for array index notation: name[0]
        if let Some(open_bracket) = remaining.find('[') {
            // Get the key before the bracket
            let key = &remaining[..open_bracket];

            // Navigate to the key if not empty
            if !key.is_empty() {
                match child(current, key) {
                    Some(value) => current = value,
                    None => return Ok(None),
                }
            }

            // Find the closing bracket
            let close_bracket = remaining[open_bracket..]
                .find(']')
                .map(|offset| open_bracket + offset)
                .ok_or_else(|| {
                    Error::PathEvaluationError(format!("Unclosed bracket in path: {}", path))
                })?;

            // Extract the index
            let index_str = &remaining[open_bracket + 1..close_bracket];
            let index: usize = index_str.parse().map_err(|_| {
                Error::PathEvaluationError(format!("Invalid array index: {}", index_str))
            })?;

            // Navigate to the array element
            match current.get(index) {
                Some(value) => current = value,
                None => return Ok(None),
            }

            // Update remaining path (skip past the ']')
            remaining = &remaining[close_bracket + 1..];

            // Skip the dot if present
            if remaining.starts_with('.') {
                remaining = &remaining[1..];
            }
        } else {
            // No array notation, check for dot notation
            if let Some(dot_pos) = remaining.find('.') {
                let key = &remaining[..dot_pos];
                match child(current, key) {
                    Some(value) => current = value,
                    None => return Ok(None),
                }
                remaining = &remaining[dot_pos + 1..];
            } else {
                // Last segment
                match child(current, remaining) {
                    Some(value) => current = value,
                    None => return Ok(None),
                }
                break;
            }
        }
    }

    Ok(Some(current))
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    #[test]
    fn test_navigate_simple_key() {
        let data = json!({
            "name": "John",
            "age": 30
        });

        let result = navigate_path(&data, "name").unwrap().unwrap();
        assert_eq!(result, &json!("John"));
    }

    #[test]
    fn test_navigate_nested() {
        let data = json!({
            "person": {
                "name": "John"
            }
        });

        let result = navigate_path(&data, "person.name").unwrap().unwrap();
        assert_eq!(result, &json!("John"));
    }

    #[test]
    fn test_navigate_array() {
        let data = json!({
            "name": ["John", "Doe"]
        });

        let result = navigate_path(&data, "name[0]").unwrap().unwrap();
        assert_eq!(result, &json!("John"));

        let result = navigate_path(&data, "name[1]").unwrap().unwrap();
        assert_eq!(result, &json!("Doe"));
    }

    #[test]
    fn test_navigate_array_index_segment() {
        let data = json!({
            "items": [{ "name": "first" }, { "name": "second" }]
        });

        let result = navigate_path(&data, "items.1.name").unwrap().unwrap();
        assert_eq!(result, &json!("second"));

        assert_eq!(navigate_path(&data, "items.2.name").unwrap(), None);
        assert_eq!(navigate_path(&data, "items.x").unwrap(), None);
    }

    #[test]
    fn test_navigate_rejects_non_canonical_array_index() {
        let data = json!({ "items": ["p", "q"] });

        assert_eq!(navigate_path(&data, "items.+1").unwrap(), None);
        assert_eq!(navigate_path(&data, "items.01").unwrap(), None);
    }

    #[test]
    fn test_navigate_closing_bracket_before_opening_bracket_is_an_error() {
        let data = json!({ "a]b": ["p"], "a": { "]": 1 } });

        assert!(navigate_path(&data, "a]b[").is_err());
        assert_eq!(navigate_path(&data, "a]b[0]").unwrap(), Some(&json!("p")));
    }

    #[test]
    fn test_resolve_segments_reads_keys_literally() {
        let data = json!({ "a.b": { "c[0]": "exact" }, "a": { "b": "wrong" } });
        let segments = ["a.b".to_string(), "c[0]".to_string()];

        assert_eq!(resolve_segments(&data, &segments), Some(&json!("exact")));
    }

    #[test]
    fn test_navigate_complex() {
        let data = json!({
            "lower": {
                "leading": "hello"
            }
        });

        let result = navigate_path(&data, "lower.leading").unwrap().unwrap();
        assert_eq!(result, &json!("hello"));
    }
}