仅当变量具有值时才过滤DataWeave中的变量值,否则忽略过滤器

时间:2016-05-20 19:04:18

标签: mule mule-studio dataweave

在我的应用程序中,一些queryparams是可选的。 我将这些查询文件存储在变量中。

我想使用这些变量来过滤使用DataWeave的对象。 如果没有传入queryparam且变量为null或者不存在,则需要返回所有对象,因此不进行过滤。

让我们以传入的能量类型参数为例。 存储在流量变量中:energytypeVar(值可能类似于“gas”,“electric”,“water”)

应用于底部对象的过滤器: 过滤$ .attributes.energyType == flowVars.energytypeVar

我尝试使用条件逻辑的各种方法,否则只在提供实际值时进行过滤。 否则我根本不想执行过滤器,因此返回所有对象。 在变量没有有效'energyType'时,完整的数据对象为空。

DW脚本

%dw 1.0
%output application/json
%var resourceUrl="https://hostname/devices"
%var collectionSize = sizeOf payload.Envelope.Body.GetEnergyDevicesResponse.GetEnergyDevicesResult
---
{
meta:{code: 200, timestamp: now},

data: payload.Envelope.Body.GetEnergyDevicesResponse.GetEnergyDevicesResult map {

        type: "device",
        id: $.EnergyDeviceId,
        attributes: {
        energyType: $.EnergyType,
        deviceModel: $.HWModel,
        serialNumber: $.SerialNumber,
        name: $.Name,

        applianceType: $.applianceType,
        isCentralMeter: $.IsCentralMeter as :boolean,
        isSwitchable: $.IsSwitchable as :boolean,
        isOnline: $.IsOnline as :boolean,
        isProducer: $.IsProducer as :boolean,
        isSwitchedOn: $.IsSwitchedOn as :boolean,
        isLiveUsageEnabled: $.IsLiveUsageEnabled as :boolean,
        index: {
                value: $.MeterIndexValue,
                unit: 'm3' unless $.EnergyType == "electricity" otherwise "Wh",
                timestamp: $.MeterIndexTimestamp
                } when ($.IsCentralMeter == "true") otherwise null  

    },
    links: {
        self: resourceUrl ++ '/' ++ $.EnergyDeviceId
    }

} filter $.attributes.energyType == flowVars.energytypeVar
}

回答后的解决方案 (应用额外过滤)

%dw 1.0
%output application/json
%var resourceUrl="https://hostname/devices"
%var collectionSize = sizeOf payload.Envelope.Body.GetEnergyDevicesResponse.GetEnergyDevicesResult
---
using (result = payload.Envelope.Body.GetEnergyDevicesResponse.GetEnergyDevicesResult map {

        type: "device",
        id: $.EnergyDeviceId,
        attributes: {
        energyType: $.EnergyType,
        deviceModel: $.HWModel,
        serialNumber: $.SerialNumber,
        name: $.Name,

        applianceType: $.applianceType,
        isCentralMeter: $.IsCentralMeter as :boolean,
        isSwitchable: $.IsSwitchable as :boolean,
        isOnline: $.IsOnline as :boolean,
        isProducer: $.IsProducer as :boolean,
        isSwitchedOn: $.IsSwitchedOn as :boolean,
        isLiveUsageEnabled: $.IsLiveUsageEnabled as :boolean,
        index: {
                value: $.MeterIndexValue,
                unit: 'm3' unless $.EnergyType == "electricity" otherwise "Wh",
                timestamp: $.MeterIndexTimestamp
                } when ($.IsCentralMeter == "true") otherwise null  

    },
    links: {
        self: resourceUrl ++ '/' ++ $.EnergyDeviceId
    }

})

{
meta:{code: 200, timestamp: now},

data: result filter ($.id == flowVars.deviceId) when (flowVars.deviceId != 0) otherwise {
data: result filter ($.attributes.energyType == flowVars.energyType) when flowVars.energyType != 0
otherwise result 
} distinctBy $.data
}

1 个答案:

答案 0 :(得分:1)

请试一试。

            %dw 1.0
        %output application/json
        %var resourceUrl="https://hostname/devices"
        %var collectionSize = sizeOf payload.Envelope.Body.GetEnergyDevicesResponse.GetEnergyDevicesResult
        ---
        {
        meta:{code: 200, timestamp: now},
        data: (payload.Envelope.Body.GetEnergyDevicesResponse.GetEnergyDevicesResult map {

                type: "device",
                id: $.EnergyDeviceId,
                attributes: {
                energyType: $.EnergyType,
                deviceModel: $.HWModel,
                serialNumber: $.SerialNumber,
                name: $.Name,

                applianceType: $.applianceType,
                isCentralMeter: $.IsCentralMeter as :boolean,
                isSwitchable: $.IsSwitchable as :boolean,
                isOnline: $.IsOnline as :boolean,
                isProducer: $.IsProducer as :boolean,
                isSwitchedOn: $.IsSwitchedOn as :boolean,
                isLiveUsageEnabled: $.IsLiveUsageEnabled as :boolean,
                index: {
                        value: $.MeterIndexValue,
                        unit: 'm3' unless $.EnergyType == "electricity" otherwise "Wh",
                        timestamp: $.MeterIndexTimestamp
                        } when ($.IsCentralMeter == "true") otherwise null  

            },
            links: {
                self: resourceUrl ++ '/' ++ $.EnergyDeviceId
            }

        } filter $.attributes.energyType == flowVars.energytypeVar) when flowVars.energytypeVar? otherwise (payload.Envelope.Body.GetEnergyDevicesResponse.GetEnergyDevicesResult map {

                type: "device",
                id: $.EnergyDeviceId,
                attributes: {
                energyType: $.EnergyType,
                deviceModel: $.HWModel,
                serialNumber: $.SerialNumber,
                name: $.Name,

                applianceType: $.applianceType,
                isCentralMeter: $.IsCentralMeter as :boolean,
                isSwitchable: $.IsSwitchable as :boolean,
                isOnline: $.IsOnline as :boolean,
                isProducer: $.IsProducer as :boolean,
                isSwitchedOn: $.IsSwitchedOn as :boolean,
                isLiveUsageEnabled: $.IsLiveUsageEnabled as :boolean,
                index: {
                        value: $.MeterIndexValue,
                        unit: 'm3' unless $.EnergyType == "electricity" otherwise "Wh",
                        timestamp: $.MeterIndexTimestamp
                        } when ($.IsCentralMeter == "true") otherwise null  

            },
            links: {
                self: resourceUrl ++ '/' ++ $.EnergyDeviceId
            }

        })
        }

希望这有帮助。