# PDK Script   [![Latest Version]][crates.io] [![pdk--script msrv]][Rust 1.87]
[Latest Version]: https://img.shields.io/badge/crates.io-v1.6.0-blue
[crates.io]: https://crates.io/crates/pdk-script
[pdk--script msrv]: https://img.shields.io/badge/pdk--script_msrv-1.87-lightgray
[Rust 1.87]: https://blog.rust-lang.org/2025/05/15/Rust-1.87.0
**pdk-script provides scripting utilities and helpers for PDK-based custom policies.**
---
You may be looking for:
- [An overview of PDK](https://docs.mulesoft.com/pdk/latest/policies-pdk-overview)
- [Defining input parameters as a DataWeave expression](https://docs.mulesoft.com/pdk/latest/policies-pdk-create-schema-definition#dataweave-expressions)
- [Using DataWeave Expressions](https://docs.mulesoft.com/pdk/latest/policies-pdk-configure-features-timer#sync-asynchronous-tasks-between-workers)
- [Examples](https://github.com/mulesoft/pdk-custom-policy-examples/tree/main/dataweave)
- [Release notes](https://docs.mulesoft.com/release-notes/pdk/pdk-release-notes)
## pdk-script in action
```toml
[dependencies]
# This package is included as a transitive dependency of the pdk crate.
pdk = { version = "1.6"}
```
```rust
mod generated {
use serde::Deserialize;
#[derive(Deserialize, Clone, Debug)]
pub struct Config {
#[serde(
alias = "exampleDateweaveProperty",
deserialize_with = "de_example_dateweave_property_0"
)]
pub example_dateweave_property: pdk::script::Script,
}
fn de_example_dateweave_property_0<'de, D>(
deserializer: D,
) -> Result<pdk::script::Script, D::Error>
where
D: serde::de::Deserializer<'de>,
{
let exp: pdk::script::Expression = serde::de::Deserialize::deserialize(
deserializer,
)?;
pdk::script::ScriptingEngine::script(&exp)
.input(pdk::script::Input::Attributes)
.input(pdk::script::Input::Authentication)
.input(pdk::script::Input::Payload(pdk::script::Format::Json))
.input(pdk::script::Input::Payload(pdk::script::Format::Xml))
.input(pdk::script::Input::Payload(pdk::script::Format::PlainText))
.input(pdk::script::Input::Vars("exampleVar"))
.compile()
.map_err(serde::de::Error::custom)
}
}
use std::collections::HashMap;
use anyhow::{anyhow, Result};
use pdk::authentication::{Authentication, AuthenticationHandler};
use pdk::hl::*;
use pdk::script::{EvaluationError, HandlerAttributesBinding, Value};
use crate::generated::config::Config;
async fn request_filter(request_state: RequestState, stream_properties: StreamProperties, authentication: Authentication, config: &Config) {
let headers_state = request_state.into_headers_state().await;
// Create an evaluator of the dataweave expression provided in the configuration.
let mut eval = config.example_dateweave_property.evaluator();
// bind "attributes" to the dataweave expression. Only necessary if the binding "attributes" was set to true in the gcl.yaml.
eval.bind_attributes(&HandlerAttributesBinding::new(headers_state.handler(), &stream_properties));
// bind "authentication" to the dataweave expression. Only necessary if the binding "authentication" was set to true in the gcl.yaml.
eval.bind_authentication(&authentication.authentication());
let body_state = headers_state.into_body_state().await;
// bind "payload" to the dataweave expression. Only necessary if the binding "payload" was configured in the gcl.yaml.
eval.bind_payload(&body_state);
// bind the var "exampleVar" to the dataweave expression. Only necessary if the binding "vars" was configured in the gcl.yaml.
eval.bind_vars("exampleVar", "exampleValue");
let result: Result<Value, EvaluationError> = eval.eval();
}
#[entrypoint]
async fn configure(launcher: Launcher, Configuration(bytes): Configuration) -> Result<()> {
let config: Config = serde_json::from_slice(&bytes).map_err(|err| {
anyhow!(
"Failed to parse configuration '{}'. Cause: {}",
String::from_utf8_lossy(&bytes),
err
)
})?;
// Inject the StreamProperties and Authentication to the on_request function.
let filter = on_request(|rs, stream_properties, authentication| request_filter(rs, stream_properties, authentication, &config));
launcher.launch(filter).await?;
Ok(())
}
```
#### License
<sup>
Licensed under <a href="../LICENSE.txt">Salesforce Inc License</a>.
</sup>