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

Filter on value of variable in DataWeave only when variable has value otherwise ignore filter

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

我想使用这些变量通过 DataWeave 过滤对象。 如果没有传入查询参数并且变量为 null 或不存在,则需要 return 所有对象,因此不进行过滤。

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

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

我尝试了多种使用条件逻辑的方法 when/otherwise 以仅在提供实际值时进行过滤。 否则我根本不想执行过滤器,所以所有对象都是 returned。 在变量没有有效的时刻 '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
}

请试试这个。

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

        })
        }

希望对您有所帮助。