Skip to content

Commit

Permalink
minor YamlUtils code cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
giulong committed Dec 28, 2023
1 parent 57c9a20 commit 1a632f6
Showing 1 changed file with 21 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import io.github.giulong.spectrum.browsers.Browser;
import io.github.giulong.spectrum.internals.jackson.deserializers.*;
import io.github.giulong.spectrum.internals.jackson.views.Views;
import io.github.giulong.spectrum.internals.jackson.views.Views.Public;
import io.github.giulong.spectrum.pojos.DynamicDeserializersConfiguration;
import lombok.Getter;
import lombok.SneakyThrows;
Expand All @@ -30,6 +30,7 @@ public final class YamlUtils {
private static final YamlUtils INSTANCE = new YamlUtils();
private static final Path RESOURCES = Path.of("src", "test", "resources");

private final ClassLoader classLoader = YamlUtils.class.getClassLoader();
private final ObjectMapper propertiesMapper = new JavaPropsMapper();

private final ObjectMapper yamlMapper = new YAMLMapper()
Expand Down Expand Up @@ -96,14 +97,13 @@ public boolean notExists(final String file, final boolean internal) {
return false;
}

@SneakyThrows
public <T> T read(final String file, final Class<T> clazz, final boolean internal) {
if (notExists(file, internal)) {
return null;
}

log.debug("Reading {} file '{}' onto an instance of {}", internal ? "internal" : "client", file, clazz.getSimpleName());
return yamlMapper.readValue(YamlUtils.class.getClassLoader().getResource(file), clazz);
return read(yamlMapper, file, clazz);
}

public <T> T read(final String file, final Class<T> clazz) {
Expand All @@ -114,10 +114,9 @@ public <T> T readInternal(final String file, final Class<T> clazz) {
return read(file, clazz, true);
}

@SneakyThrows
public <T> T readProperties(final String file, final Class<T> clazz) {
log.debug("Reading properties file '{}' onto an instance of {}", file, clazz.getSimpleName());
return propertiesMapper.readValue(YamlUtils.class.getClassLoader().getResource(file), clazz);
return read(propertiesMapper, file, clazz);
}

@SneakyThrows
Expand All @@ -127,7 +126,7 @@ public <T> T readNode(final String node, final String file, final Class<T> clazz
}

log.debug("Reading node '{}' of {} file '{}' onto an instance of {}", node, internal ? "internal" : "client", file, clazz.getSimpleName());
final JsonNode root = yamlMapper.readTree(YamlUtils.class.getClassLoader().getResource(file));
final JsonNode root = yamlMapper.readTree(classLoader.getResource(file));
return yamlMapper.convertValue(root.at(node), clazz);
}

Expand All @@ -142,8 +141,10 @@ public <T> T readInternalNode(final String node, final String file, final Class<
@SneakyThrows
public <T> T readDynamicDeserializable(final String configFile, final Class<T> clazz, final JsonNode jsonNode) {
log.debug("Reading dynamic conf file '{}' onto an instance of {}", configFile, clazz.getSimpleName());
final T t = dynamicConfYamlMapper.readValue(YamlUtils.class.getClassLoader().getResource(configFile), clazz);
return dynamicConfYamlMapper.readerForUpdating(t).readValue(jsonNode);
final T t = read(dynamicConfYamlMapper, configFile, clazz);
return dynamicConfYamlMapper
.readerForUpdating(t)
.readValue(jsonNode);
}

@SneakyThrows
Expand All @@ -155,13 +156,23 @@ public <T> void updateWithFile(final T t, final String file) {
}

log.debug("Updating the instance of {} with file '{}'", t.getClass().getSimpleName(), file);
yamlMapper.readerForUpdating(t).withView(Views.Public.class).readValue(YamlUtils.class.getClassLoader().getResource(file));
yamlMapper
.readerForUpdating(t)
.withView(Public.class)
.readValue(classLoader.getResource(file));
}

@SneakyThrows
public <T> void updateWithInternalFile(final T t, final String file) {
log.debug("Updating the instance of {} with internal file '{}'", t.getClass().getSimpleName(), file);
yamlMapper.readerForUpdating(t).readValue(YamlUtils.class.getClassLoader().getResource(file));
yamlMapper
.readerForUpdating(t)
.readValue(classLoader.getResource(file));
}

@SneakyThrows
public <T> T read(final ObjectMapper objectMapper, final String fileName, final Class<T> clazz) {
return objectMapper.readValue(classLoader.getResource(fileName), clazz);
}

@SneakyThrows
Expand Down

0 comments on commit 1a632f6

Please sign in to comment.