All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.decathlon.tzatziki.steps.DynamicTransformers Maven / Gradle / Ivy

There is a newer version: 1.7.0
Show newest version
package com.decathlon.tzatziki.steps;

import com.decathlon.tzatziki.utils.Mapper;
import io.cucumber.java.DefaultParameterTransformer;

import java.lang.reflect.Type;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.function.Function;

import static com.decathlon.tzatziki.utils.Types.rawTypeOf;


public class DynamicTransformers {

    private static final Map> PARAMETER_TRANSFORMERS = new LinkedHashMap<>();

    @SuppressWarnings("unchecked")
    public static  void register(Class type, Function method) {
        PARAMETER_TRANSFORMERS.put(type, (Function) method);
    }

    @DefaultParameterTransformer
    public Object defaultTransformer(Object fromValue, Type toValueType) {
        if (toValueType.equals(String.class)) {
            return fromValue;
        }
        return PARAMETER_TRANSFORMERS.getOrDefault(toValueType, v -> {
            Class clazz = rawTypeOf(toValueType);
            if (v != null && !(clazz.equals(Object.class) || clazz.equals(String.class))) {
                return Mapper.read(v, clazz);
            }
            return v;
        }).apply((String) fromValue);
    }
}