【问题标题】:How to build a multiple IF conditional in Mule 4 and DW 2.0?如何在 Mule 4 和 DW 2.0 中构建多个 IF 条件?
【发布时间】:2019-05-30 15:24:29
【问题描述】:

我需要创建一个具有类似伪代码条件的函数:

var consent = []

function buildConsent() {
   if (condition1) {
       consent += values1
   }

   if (condition2) {
       consent += values2
   }

   if (condition3) {
       consent += values3
   }
}

这就是我在 Mule4 和 DW 2.0 上的做法:

%dw 2.0
var consent = []
var factIntake = vars.facts

fun buildConsent() =
    if (factIntake.miscFactItems[?($.value1 == true)] != null) {
        consent + {
            "Consent_Type": "some1",
            "Consent_Given_By": "some2"
        }
    }

    if (factIntake.miscFactItems[?($.value2 == true)] != null) {
        consent + {
            "Consent_Type": "some3",
            "Consent_Given_By": "some4"
        }
    }

output application/json
--
{
    "Consent_Data": buildConsent()
}

但我从 IDE (AnypointStudio 7) 收到以下错误:

输入“+”无效,预期命名空间或 属性(第 11 行,第 11 列):

其中第 11 行第 11 列是consent + 的第一次出现。如果我尝试调试项目,我在控制台中得到的只是:

消息:解析脚本时出错:%dw 2.0

这是一个输入/输出示例,让您更好地了解我想要实现的目标:

// Input
{
    "miscFactItems": [{
            "factId": "designeeFirstName",
            "factValue": "test test",
            "factValueType": "System.String"
        }, {
            "factId": "designeeLastName",
            "factValue": "test test",
            "factValueType": "System.String"
        },{
            "factId": "value1",
            "factValue": true,
            "factValueType": "System.Boolean"
        }, {
            "factId": "value2",
            "factValue": true,
            "factValueType": "System.Boolean"
        }, {
            "factId": "value3",
            "factValue": true,
            "factValueType": "System.Boolean"
        }
    ]
}

// Output
consent = [{
             "Consent_Type": "type1",
             "Consent_Given_By": miscFactItems.designeeFirstName
         }, {
             "Consent_Type": "type2",
             "Consent_Given_By": miscFactItems.designeeFirstName
         }, {
             "Consent_Type": "type3",
             "Consent_Given_By": miscFactItems.designeeFirstName
         }
]

我在这里缺少什么?如何将这三个条件添加到我的函数并将值附加到 consent var?

【问题讨论】:

  • 条件是否互斥?
  • @Shoki 不,它们不在有效载荷中,我可以同时满足这两个条件,因此必须将其添加到数组 consent
  • 通过预期的输入/输出提供准确的答案会更容易
  • @Shoki 我的编辑有帮助吗?我添加了一组 INPUT/OUTPUT :)

标签: mule anypoint-studio dataweave mule4


【解决方案1】:

在DataWeave中变量是不可变的,所以不能在同一个变量中累积东西,需要创建新的变量。

所以它看起来像这样:

%dw 2.0
output application/json  

var consent1 = if (condition1) [{"Consent_Type": "some1", "Consent_Given_By": "some2"}] else []
var consent2 = if (condition2) [{"Consent_Type": "some3", "Consent_Given_By": "some4"}] else []
---
consent1 ++ consent2

【讨论】:

    【解决方案2】:

    您的要求看起来很好地使用了reduce 函数。根据您提供的伪代码,您可以执行以下操作

    output application/json
    var payload = [  
        {"name":"Ram", "email":"Ram@gmail.com", "state": "CA","age":21},  
        {"name":"Bob", "email":"bob32@gmail.com","state": "CA","age":30},
        {"name":"john", "email":"bob32@gmail.com","state": "NY","age":43} 
    
    ] 
    ---
    payload reduce ((item, consent = []) -> consent +
    {
        (state: item.state) if(item.state=='CA'),
        (age: item.age) if(item.age >25)
    }
    )
    

    【讨论】:

    • 好吧,我并没有减少有效负载,因为它看起来与输出不同,所以我需要根据函数的条件构建一个新的 JSON,不确定我是否错过了理解您的解决方案可能会提供输出会有所帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-23
    • 2021-03-05
    • 1970-01-01
    • 1970-01-01
    • 2017-07-13
    • 2010-11-15
    相关资源
    最近更新 更多