如何在Mule 4和DW 2.0中构建多重IF条件?

如何在Mule 4和DW 2.0中构建多重IF条件?,mule,anypoint-studio,dataweave,mule4,Mule,Anypoint Studio,Dataweave,Mule4,我需要创建一个具有如下伪代码条件的函数: var consent = [] function buildConsent() { if (condition1) { consent += values1 } if (condition2) { consent += values2 } if (condition3) { consent += values3 } } 我在Mule4和DW 2.0上就是这样做的: %

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

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列是
同意+
的第一次出现。如果我尝试调试项目,我在控制台中得到的结果是:

消息:分析脚本时出错:%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
         }
]

我错过了什么?如何将这三个条件添加到函数中,并将值附加到
同意
变量中?

您的需求看起来是
减少
函数的一个很好的用途。根据您提供的伪代码,您可以执行以下操作

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)
}
)

在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

我没有减少有效负载,因为它看起来与输出不同,所以我需要基于函数中的条件构建一个新的JSON,不确定我是否没有理解您的解决方案,也许提供一个输出会有所帮助。这些条件是相互排斥的吗?@Shoki不,它们不在有效负载中,我可以同时具备这两个条件,因此必须将其添加到数组
同意
使用预期输入更容易提供准确答案/output@Shoki我的编辑有帮助吗?我添加了一组输入/输出:)