Skip to content

Mappers

Adrien LAUER edited this page Feb 23, 2017 · 2 revisions

A mapper is a Coffig component type responsible for mapping nodes to a specific Java type. A mapper implements the org.seedstack.coffig.spi.ConfigurationMapper interface.

Programmatic registration

To register a mapper programmatically, you create an instance of the evaluator and add it as a parameter to the withMappers() Coffig builder method:

Coffig.builder()
      .withMappers(new MyMapper())
      .build();

Declarative registration

You can also register a mapper automatically by adding its fully qualified class name in META-INF/services/org.seedstack.coffig.spi.ConfigurationMapper:

org.myorg.myapp.MyMapper

Mapping rules

The following mapping rules apply:

  • Fields are mapped by name from configuration nodes.
  • When a setter is present for the field, it is used instead of direct field access.
  • When a configuration node is missing for a field, the field is left untouched. This can be used to specify default values.
  • If an @Config annotation is specified on the mapped class, it is used as the default path prefix.

Generics

The mapping takes into account the full type of the field, including generics. As such it is possible to properly map complex types like:

  • List<URL>,
  • Class<? extends SomeInterface>,
  • or even Optional<Map<String, List<SomeEnum>>>.
Clone this wiki locally