是否有 DataWeave 1.0 "for" 运算符?
Is there a DataWeave 1.0 "for" operator?
我想创建与计数变量指定的一样多的元素,例如:
有一个 table 包含有关受支持设备的信息
select port_count
from equipment
where id=#[flowVars.equipmentId]
我必须生成一条消息,由我无法控制的另一个系统解释和执行:
<dw:transform-message doc:name="Get value">
<dw:set-payload><![CDATA[%dw 1.0
%output application/json
---
{
"actions": {
(for 1..flowVars.port_count map {
"action": {
"type": "add_port",
"id": $$
}
})
}
}]]></dw:set-payload>
</dw:transform-message>
想要的结果:
{
"actions": {
"action": {
"type": "add_port",
"id": 1
},
"action": {
"type": "add_port",
"id": 2
}
}
}
DataWeave 没有 for 的概念,因为它是一种函数式语言。您仍然可以通过使用范围代替您想要的:
%dw 1.0
%var port_count=3
%output application/json
---
{
actions:
(((1 to port_count) when port_count > 0 otherwise []) map
action: {
"type": "add_port",
"id": $
}
) reduce ($$ ++ $)
}
输出:
{
"actions": {
"action": {
"type": "add_port",
"id": 1
},
"action": {
"type": "add_port",
"id": 2
},
"action": {
"type": "add_port",
"id": 3
}
}
}
只需更改 flowVar 的 port_count 变量即可。
删除 reduce() 运算符将 return 改为一个列表,这对我来说更有意义。
我想创建与计数变量指定的一样多的元素,例如:
有一个 table 包含有关受支持设备的信息
select port_count
from equipment
where id=#[flowVars.equipmentId]
我必须生成一条消息,由我无法控制的另一个系统解释和执行:
<dw:transform-message doc:name="Get value">
<dw:set-payload><![CDATA[%dw 1.0
%output application/json
---
{
"actions": {
(for 1..flowVars.port_count map {
"action": {
"type": "add_port",
"id": $$
}
})
}
}]]></dw:set-payload>
</dw:transform-message>
想要的结果:
{
"actions": {
"action": {
"type": "add_port",
"id": 1
},
"action": {
"type": "add_port",
"id": 2
}
}
}
DataWeave 没有 for 的概念,因为它是一种函数式语言。您仍然可以通过使用范围代替您想要的:
%dw 1.0
%var port_count=3
%output application/json
---
{
actions:
(((1 to port_count) when port_count > 0 otherwise []) map
action: {
"type": "add_port",
"id": $
}
) reduce ($$ ++ $)
}
输出:
{
"actions": {
"action": {
"type": "add_port",
"id": 1
},
"action": {
"type": "add_port",
"id": 2
},
"action": {
"type": "add_port",
"id": 3
}
}
}
只需更改 flowVar 的 port_count 变量即可。
删除 reduce() 运算符将 return 改为一个列表,这对我来说更有意义。