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

io.datakernel.http.decoder.Mapper Maven / Gradle / Ivy

package io.datakernel.http.decoder;

import io.datakernel.common.collection.Either;

import java.util.List;
import java.util.function.Function;

import static java.util.Collections.singletonList;

/**
 * An enhanced mapping function which can return a list of errors for given input object.
 * This can be used to both map and put additional constraints on the parsed object from HTTP decoder.
 * For example to ensure, that age of the person given as a string is an integer in range 0-100
 * and convert it to that.
 */
@FunctionalInterface
public interface Mapper {
	Either> map(T value);

	static  Mapper of(Function fn) {
		return value -> Either.left(fn.apply(value));
	}

	static  Mapper of(Function fn, String message) {
		return value -> {
			try {
				return Either.left(fn.apply(value));
			} catch (Exception e) {
				return Either.right(singletonList(DecodeError.of(message, value)));
			}
		};
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy