pdk-unit 1.8.0

PDK Unit Test Framework
Documentation
// Copyright (c) 2026, Salesforce, Inc.,
// All rights reserved.
// For full license text, see the LICENSE.txt file

use dw_parser::pel::InputContext;

const ALLOWED_FUNCTIONS: [(&str, &str); 16] = [
    ("dw::Core::++", "++"),
    ("dw::Core::--", "--"),
    ("dw::Core::contains", "contains"),
    ("dw::Core::splitBy", "splitBy"),
    ("dw::Core::trim", "trim"),
    ("dw::Core::lower", "lower"),
    ("dw::Core::upper", "upper"),
    ("dw::Core::sizeOf", "sizeOf"),
    ("dw::Core::uuid", "uuid"),
    ("dw::Core::isEmpty", "isEmpty"),
    ("dw::core::Strings::substringBefore", "substringBefore"),
    ("dw::core::Strings::substringAfter", "substringAfter"),
    (
        "dw::core::Strings::substringBeforeLast",
        "substringBeforeLast",
    ),
    (
        "dw::core::Strings::substringAfterLast",
        "substringAfterLast",
    ),
    ("dw::core::Binaries::toBase64", "toBase64"),
    ("dw::core::Binaries::fromBase64", "fromBase64"),
];

const ALLOWED_INPUTS: [&str; 4] = ["payload", "attributes", "vars", "authentication"];

/// Transform a DataWeave expression to PEL (Policy Expression Language) so it can be consumed by the PDK.
///
/// # Example
///
/// ```ignore
/// use pdk_unit::dw2pel;
///
/// let mut tester = UnitTestBuilder::default()
///     .with_config(json!({"dw": dw2pel("atributes.headers.test")}).to_string())
/// ```
pub fn dw2pel(dw: &str) -> String {
    let ctx = InputContext {
        inputs: ALLOWED_INPUTS.to_vec(),
        functions: ALLOWED_FUNCTIONS.iter().map(|(k, _)| *k).collect(),
    };

    let result = dw_parser::pel::compile_to_pel_expr("", dw, &ctx);
    if result.pel.is_some() {
        format!(
            "P[{}, \"#[{}]\"]",
            result.pel.unwrap(),
            dw.replace("\"", "\\\"")
        )
    } else {
        panic!("dw2pel failed: {:?}", result.messages)
    }
}