Skip to main content

dw_parser/
dw2pel.rs

1// Copyright (c) 2026, Salesforce, Inc.,
2// All rights reserved.
3// For full license text, see the LICENSE.txt file
4use crate::pel::InputContext;
5
6const ALLOWED_FUNCTIONS: [(&str, &str); 16] = [
7    ("dw::Core::++", "++"),
8    ("dw::Core::--", "--"),
9    ("dw::Core::contains", "contains"),
10    ("dw::Core::splitBy", "splitBy"),
11    ("dw::Core::trim", "trim"),
12    ("dw::Core::lower", "lower"),
13    ("dw::Core::upper", "upper"),
14    ("dw::Core::sizeOf", "sizeOf"),
15    ("dw::Core::uuid", "uuid"),
16    ("dw::Core::isEmpty", "isEmpty"),
17    ("dw::core::Strings::substringBefore", "substringBefore"),
18    ("dw::core::Strings::substringAfter", "substringAfter"),
19    (
20        "dw::core::Strings::substringBeforeLast",
21        "substringBeforeLast",
22    ),
23    (
24        "dw::core::Strings::substringAfterLast",
25        "substringAfterLast",
26    ),
27    ("dw::core::Binaries::toBase64", "toBase64"),
28    ("dw::core::Binaries::fromBase64", "fromBase64"),
29];
30
31const ALLOWED_INPUTS: [&str; 4] = ["payload", "attributes", "vars", "authentication"];
32
33/// Transform a DataWeave expression to PEL (Policy Expression Language) so it can be consumed by the PDK.
34///
35/// # Example
36///
37/// ```ignore
38/// use pdk_unit::dw2pel;
39///
40/// let mut tester = UnitTestBuilder::default()
41///     .with_config(json!({"dw": dw2pel("atributes.headers.test")}).to_string())
42/// ```
43pub fn dw2pel(dw: &str) -> String {
44    let ctx = InputContext {
45        inputs: ALLOWED_INPUTS.to_vec(),
46        functions: ALLOWED_FUNCTIONS.iter().map(|(k, _)| *k).collect(),
47    };
48
49    let result = crate::pel::compile_to_pel_expr("", dw, &ctx);
50    if result.pel.is_some() {
51        format!(
52            "P[{}, \"#[{}]\"]",
53            result.pel.unwrap(),
54            dw.replace("\"", "\\\"")
55        )
56    } else {
57        panic!("dw2pel failed: {:?}", result.messages)
58    }
59}