MapValuesTransformer

This script converts the input value into another value using two lists. The input value is searched in the first list (input_values). If it exists, the value from the second list (mapped_values) at the same index is retutrned. If there is no match, the original value is returned. If the original input is a dictionary, then the script will look for a "stringified" version of the key/:/value pair in the input_values and then map the result in the output_values into the original "value". Example 1: input_values = "1,2,3,4" mapper_values = "4,3,2,1" value = 3 Output would be "2" Example 2: input_values ="firstkey: datahere,secondkey: datathere" mapper_values = "datathere,datahere" value(dict)= { "firstkey": "datahere" } Output would be: { "firstkey": "datathere" } The reason for matching the key AND value pair in a dictionary is to allow the mappig of values that have a specific key name. In most cases, dictionaries will continan key-value pairs in which the values are the same. You might want to change the value of KeyA, but not the value of KeyB. This method gives control over which key is changed. When the input is a dict, str , int, or list, the output is ALWAYS returned as a string.

python · Filters And Transformers

Details

IDMapValuesTransformer
Languagepython
From Version5.0.0
Docker Imagedemisto/python3:3.12.13.10404775
Tagstransformer string

README

MapValuesTransformer

This transformer script takes the input value and translates it based on two list arguments.

The script that takes two arguments:

input_values and mapped_values

Both arguments are comma-separated values. The total number of values for each input must be the same. For example:

input_values = "1,2,3,4"

mapped_values = "4,3,2,1"

If the length of each list is not the same, the script will error.

Example use 1

value = "3" (not specified by the user)

input_values = "1,2,3,4"

mapped_values = "4,3,2,1"

The resulting output would be “2”. The mapping looks up the value in the input_values and returns the value in
mapped_values at the same index.

Example use 2

value = {"testkey1": "testvalue1", "testkey2": "testvalue2"} (not specified by the user)

input_values = "key1: value1, testkey2: testvalue2"

mapped_values = "value1changed,testvalue2changed"

The resulting output would be {"key1": "value1", "key2": "testvalue2changed"}. Because the input_values can be
parsed as a JSON dictionary, it will match the key: value pair, but only alter the value in the pair.

from MapValuesTransformer import mapvalues


def test_mapvalues_dict():
    value = {"testkey1": "testvalue1", "testkey2": "testvalue2"}
    input_v = "testkey2: testvalue2"
    mapped_v = "testvalue2changed"
    assert mapvalues(value, input_v, mapped_v) == '{"testkey1": "testvalue1", "testkey2": "testvalue2changed"}'


def test_mapvalues_str():
    value = "3"
    input_v = "4,3,2,1"
    mapped_v = "1,2,3,4"
    assert mapvalues(value, input_v, mapped_v) == "2"


def test_mapvalues_int():
    value = 3
    input_v = "4,3,2,1"
    mapped_v = "1,2,3,4"
    assert mapvalues(value, input_v, mapped_v) == "2"


def test_mapvalues_dict_abnorm():
    value = {"testkey1": "testvalue1", "testkey2": "testvalue2"}
    input_v = "testkey2:testvalue2"
    mapped_v = "testvalue2changed"
    assert mapvalues(value, input_v, mapped_v) == '{"testkey1": "testvalue1", "testkey2": "testvalue2changed"}'