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

io.atleon.protobuf.ProtobufMessages Maven / Gradle / Ivy

package io.atleon.protobuf;

import com.google.protobuf.Message;
import io.atleon.util.ConfigLoading;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;

public final class ProtobufMessages {

    private ProtobufMessages() {

    }

    public static  Optional> loadParser(
        Map configs,
        String key,
        Class inputType
    ) {
        return ConfigLoading.loadClass(configs, key).map(it -> createParser(it, inputType));
    }

    public static  Function loadParserOrThrow(
        Map configs,
        String key,
        Class inputType
    ) {
        return createParser(ConfigLoading.loadClassOrThrow(configs, key), inputType);
    }

    private static  Function createParser(Class messageType, Class inputType) {
        try {
            Method method = messageType.getDeclaredMethod("parseFrom", inputType);
            return input -> invoke(method, input);
        } catch (NoSuchMethodException e) {
            throw new IllegalArgumentException(
                "Either type=" + messageType + " is not a Message or there is no parser for inputType=" + inputType
            );
        }
    }

    private static  M invoke(Method method, Object input) {
        try {
            return (M) method.invoke(null, input);
        } catch (IllegalAccessException | InvocationTargetException e) {
            throw new IllegalArgumentException("Failed to invoke method=" + method);
        }
    }
}