Class YAMLLoader

java.lang.Object
uk.ac.ebi.utils.opt.config.YAMLLoader

public class YAMLLoader extends Object
A simple YAML document/file loader, which is focused on configuration needs. The basic function of this component is loading YAML files and mapping them to POJO classes. It also offers features that are often useful when dealing with application configurations:
  • It allows for inclusions, using the INCLUDES_FIELD at the YAML document/file root level (ie, not in a nested level. Optional inclusions are also supported, via INCLUDES_OPTIONAL_FIELD.
  • When including a file from a parent, you can extend (instead of override) fields that the parent defined as arrays or objects. Append MERGE_SUFFIX to the field and it's values will be added to the field with the same name (minus the postfix). For instance, if a parent file has: options: [default] and an included file has options @merge: [advanced], the result for options will be [default, advanced]. When mapping to JavaBean properties of type collection, the collection is populated with the merge result (and properties like order or repetitions depend on the exact collection type used for the JavaBean).
  • Property interpolation: When using ${propName} in YAML values (not field names), the referred property replaced with values in the JVM properties, or environment variables, or even properties defined via PROPDEF_FIELD. This is based on Spring
See the unit tests for examples of use.

TODO: support for URLs
TODO: Support Spring SpEL for interpolation.
TODO: support for charsets?

Author:
brandizi
Date:
14 Jun 2022
  • Field Details

  • Constructor Details

    • YAMLLoader

      public YAMLLoader()
  • Method Details

    • loadYAMLFromFile

      public static <T> T loadYAMLFromFile(String filePath, Class<T> targetClass) throws UncheckedIOException
      Throws:
      UncheckedIOException
    • loadYAMLFromString

      public static <T> T loadYAMLFromString(String yamlStr, Class<T> targetClass)
      Maps a YAML file to a target, after it has been processes as explained above.