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

io.github.sskorol.data.YamlReader Maven / Gradle / Ivy

package io.github.sskorol.data;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.val;
import one.util.streamex.StreamEx;

import static com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES;
import static java.lang.String.format;

@Getter
@RequiredArgsConstructor
public class YamlReader implements DataReader {

    private final Class entityClass;
    private final String path;

    public YamlReader(final Class entityClass) {
        this(entityClass, "");
    }

    @SuppressWarnings("unchecked")
    public StreamEx read() {
        try {
            val yamlFactory = new YAMLFactory();
            return StreamEx.of(new ObjectMapper(yamlFactory)
                    .configure(FAIL_ON_UNKNOWN_PROPERTIES, false)
                    .readValues(yamlFactory.createParser(getUrl()), entityClass)
                    .readAll());
        } catch (Exception ex) {
            throw new IllegalArgumentException(format("Unable to read YAML data to %s.", entityClass), ex);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy