【发布时间】: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