---
schema_version: "1"
id: 1190d85be89b597579abbb0a2365dedff97e7fc28656e39f58bb0643db3d0e0e
title: DataWeave 式の使用
collection: mulesoft
version: current
locale: ja-jp
portal_url: https://docs.salesforce.com/mulesoft/current/ja-jp/pdk/1.4/policies-pdk-configure-features-dataweave.md
canonical_url: https://docs.mulesoft.com/jp/pdk/1.4/policies-pdk-configure-features-dataweave
source_site: docs.mulesoft.com
content_type: documentation
content_hash: 41372d42070caab1f9a25b46b82e76cc9b2352d8c4fffa3f23dc7eb41d774341
description: MuleSoft Documentation Site
release: "1.4"
latest: false
---
# DataWeave 式の使用

<table><tbody><tr><td class="icon"><i class="fa icon-note" title="Note"></i></td><td class="content">DataWeave 式を使用するポリシープロジェクトの例は、 <a href="https://github.com/mulesoft/pdk-custom-policy-examples/blob/1.4.0/dataweave/README.md" target="_blank" rel="noopener">「DataWeave ポリシーの例」</a>​を参照してください。</td></tr></tbody></table>

入力 DataWeave 式パラメーターが有効な場合、ポリシーは式を解析して ​`pdk::script::Script`​ 式に変換します。スクリプトの結果を得る手順は次のとおりです。

1. Evaluator を作成します。
2. スキーマ定義で定義されている各パラメーターのバインディング関数を呼び出します。
    
    - `vars`​: 各ポリシーの ​`vars`​ の ​`bind_vars`​ メソッドを呼び出します。すべての ​`vars`​ がバインドされた後、Evaluator は式の解決を試みます。式にスキーマで定義されている以外の ​`vars`​ が含まれている場合、それらは ​`null`​ に解決されます。
    - `attributes`​: `bind_attributes`​ メソッドを ​`AttributesBinding`​ trait の実装で呼び出します。Flex Gateway ポリシー開発キット (PDK) は、​`RequestHeaderState`​、​`RequestHeaderState`​、および ​[HTTP コール](https://docs.mulesoft.com/jp/pdk/1.4/policies-pdk-configure-features-http-request)​の応答のそれぞれに対して新しい ​`HandlerAttributesBinding`​ をインスタンス化することで、この trait を実装します。
    - `authentication`​: `bind_authentication`​ メソッドを ​`AuthenticationBinding`​ trait の実装で呼び出します。PDK は、​`AuthenticationData`​ に対してこの trait を実装しています。ポリシーに認証情報を挿入するには、​[「認証情報の挿入」](https://docs.mulesoft.com/jp/pdk/1.4/policies-pdk-configure-features-inject-parameters#authentication)​を参照してください。
    - `payload`​: `bind_payload`​ メソッドを ​`PayloadBinding`​ の実装で呼び出します。PDK は、​`RequestBodyState`​、​`ResponseBodyState`​、および ​[HTTP コール](https://docs.mulesoft.com/jp/pdk/1.4/policies-pdk-configure-features-http-request)​の応答に対して、この trait の実装を提供します。
        
        <table>
<tbody><tr>
<td class="icon">
<i class="fa icon-note" title="Note"></i>
</td>
<td class="content">
<div class="paragraph">
<p>スキーマ定義で定義されているバインディングのメソッドのみを呼び出します。たとえば、​<code>attributes</code>​ バインディングを使用しない場合は、​<code>bind_attributes</code>​ メソッドを呼び出さないでください。</p>
</div>
<div class="paragraph">
<p>DataWeave 式の定義の詳細については、​<a href="https://docs.mulesoft.com/jp/pdk/1.4/policies-pdk-create-schema-definition#dataweave-expressions" class="xref page">「DataWeave 式」</a>​を参照してください。</p>
</div>
</td>
</tr>
</tbody></table>
3. すべての式変数をバインドしたら、​`eval`​ メソッドを呼び出して結果を取得します。

## [](#example-evaluator-code)DataWeave 式の評価

たとえば、次のポリシースキーマ定義の ​`tokenExtractor`​ DataWeave 式を評価する手順は、次のとおりです。

```yaml
apiVersion: gateway.mulesoft.com/v1alpha1
kind: Extension
metadata:
 labels:
   title: jwt-auth
   category: Custom
spec:
 extends:
   - name: extension-definition
     namespace: default
 properties:
   tokenExtractor:
     type: string
     format: dataweave
     default: "#[vars.myVar]"
     bindings:
       payload:
         mimeType: text
       attributes: true
       authentication: true
       vars:
         - myVar
 required:
   - tokenExtractor
```

<table>
<tbody><tr>
<td class="icon">
<i class="fa icon-note" title="Note"></i>
</td>
<td class="content">
<div class="paragraph">
<p>Flex Gateway では、バイナリ型の結果を伴う DataWeave 式はサポートされません。​<code>dw::Binaries::fromBase64('dXNlcjpwYXNz')</code>​ 関数を使用するには、結果を文字列に変換してください。​<code>dw::util::Coercions::toString(binary: Binary, encoding: String): String</code>​ 関数を使用してバイナリ出力を文字列に変換します。たとえば、​<code><mark>[dw::util::Coercions::toString(dw::core::Binaries::fromBase64('dXNlcjpwYXNz'), 'UTF-8')]</mark></code>​ となります。デフォルトで ​<code>[splitBy(dw::core::Binaries::fromBase64('dXNlcjpwYXNz'), ':')]</code>​ などのバイナリを変換する別の関数を使用している場合は、バイナリの手動変換をスキップできます。</p>
</div>
</td>
</tr>
</tbody></table>

以下の Rust スニペットを使用します。

```rust
// Copyright 2024 Salesforce, Inc. All rights reserved.
mod generated;


use anyhow::Result;


use crate::generated::config::Config;
use pdk::authentication::{Authentication, AuthenticationHandler};
use pdk::logger::info;
use pdk::hl::*;
use pdk::script::{Evaluator, HandlerAttributesBinding, Value};


async fn request_filter(
   state: RequestState,
   stream: StreamProperties,
   auth: Authentication,
   mut evaluator: Evaluator<'_>,
) {
   evaluator.bind_vars("myVar", "myVal");

   evaluator.bind_authentication(&auth.authentication());

   let state = state.into_headers_state().await;
   evaluator.bind_attributes(&HandlerAttributesBinding::new(state.handler(), &stream));

   let state = state.into_body_state().await;
   evaluator.bind_payload(&state);

   if let Ok(value) = evaluator.eval() {
       match value {
           Value::Null => info!("value was null!"),
           Value::Bool(val) => info!("value was Bool: {val}"),
           Value::Number(val) => info!("value was Number: {val}"),
           Value::String(val) => info!("value was String: {val}"),
           Value::Array(val) => info!("value was Array: {val:?}"),
           Value::Object(val) => info!("value was Object: {val:?}"),
       }
   }
}


#[entrypoint]
async fn configure(launcher: Launcher, Configuration(bytes): Configuration) -> Result<()> {
   let config: Config = serde_json::from_slice(&bytes)?;


   launcher
       .launch(on_request(|request, stream, auth| {
           request_filter(request, stream, auth, config.token_extractor.evaluator())
       }))
       .await?;
   Ok(())
}
```

## [](#式が解決されたことの確認)式が解決されたことの確認

すべての値がバインドされる前に、いくつかの式を解決することができます。たとえば、​`#[vars.myVar]`​ という式を解決するためには、​`attributes`​、​`payload`​、そしていずれの ​`authentication`​ も解決する必要はありません。

バインディングメソッドのいずれかが呼び出された後、そのメソッドを呼び出すことで、式が完全に解決されたことを確認できます。

```rust
evaluator.is_ready()
```

## [](#関連情報)関連情報

- [Rust でのカスタムポリシー機能の実装](https://docs.mulesoft.com/jp/pdk/1.4/policies-pdk-configure-features)