Property Mapping
Overview
Although config values are originally text, you can use the config system’s
built-in conversions or add your own to translate text into Java primitive types
and simple objects (such as Double), into enum values, and to express parts
of the config tree as complex types (List, Map, and custom types specific to
your application). This section introduces how to use the built-in mappings and
your own custom ones to convert to simple and complex types.
Converting Configuration to Simple Types
The Config class itself provides many conversions to Java types. See
the Javadoc for the complete list.
The methods which support Java primitive types and their related classes follow a common pattern. The examples in the table below deal with conversion to a boolean but the same pattern applies to many data types listed in the Javadoc.
Assume a local variable has been assigned something like
Built-in conversions to simple types:
| Java type | Example usage 1 |
|---|---|
boolean | boolean b = value. 2 |
boolean defaultedB = value. 3 | |
Optional< | ConfigValue already has all methods of an Optional. If actual optional is needed: Optional<Boolean> b = value.asOptional(); 4 |
Supplier< | Boolean b = value. |
boolean defaultedB = value. | |
Supplier< | Boolean b = value. |
Built-in Conversions to Simple Types:
- All conversions can throw
MissingValueException(if no value exists at the requested key and no default is provided) andConfigMappingException(if some error occurred while performing the data mapping). - The
Config.asXXXmethods internally use the Java-providedXXX.parseXXXmethods, so here a missing or un-parseable string givesfalsebecause that is howBoolean.parseBooleanbehaves. - User code defaults the value to
true. - User code defaults the value to
Boolean.TRUEif absent; otherwise parses the value usingBoolean.parseBoolean.
The numerous conversions defined on the Config class for other types
(integers, doubles, etc.) will satisfy many of your application’s needs. The
ConfigMappers class includes other related mappings from
String (rather than from Config) to Java types (described in the Javadoc).
For additional type mapping, you can use these methods defined on Config
which maps the current node to a type:
interface Config {
// ...
T as(Class<? extends T> type);
T as(Function<Config, T> mapper);
T as(GenericType<T> genericType);
}
The next example, and later ones below showing complex type mapping, use the
example application.properties configuration from the
config introduction. Part of that example includes this line:
bl.initial-id = 10000000000
Your application can use Config.as to interpret the value as a BigDecimal:
BigDecimal initialId = config.get("bl.initial-id").as(BigDecimal.class);
Converting Configuration to enum Values
Configuration can automatically map Config nodes to most enum types.
Your application code simply passes the enum class type to config.as(Class<? extends T> type). The built-in enum converter attempts to match the string
value in the config node to the name of one of the values declared for that
specific enum in the Java code.
Matching enum Names
The conversion applies the following algorithm to match config values to enum
names, stopping as soon as it finds a match:
- Select an exact match if one exists.
- Treat hyphens (
-) in config strings as underscores (_) and select an otherwise exact match if one exists. - Select a case-insensitive match (with or without hyphen substitution) if there is exactly one such match.
- Finding no match or multiple case-insensitive matches, throw a
ConfigMappingException.
Example
The following example illustrates how to use the built-in enum conversion
feature. The example code builds a simple Config tree itself which contains
simple test data; normally your application would load the config from a file or
some other location.
Why use heuristics in matching strings to enum values?
Short answer: ease-of-use.
Users composing config sources often adopt a style with hyphens within words to
improve readability and lower-case keys and values. With that style in mind,
users typing an enum value into a config source might accidentally enter a
hyphen instead of an underscore or use lower case instead of upper case. Users
might even prefer to make these changes so they can follow their preferred
config style.
With the heuristics, Helidon allows users to adopt a common config style and prevents unnecessary runtime exceptions and user frustration from inconsequential typos.
Remember:
- Helidon always finds exact matches unambiguously, without relying on the
heuristics. In our
Colorexample the textBLUE_GREENalways maps toColor.BLUE_GREEN. - Because hyphens cannot appear in a valid Java
enumvalue name, interpreting them as underscores duringenumconversion introduces no ambiguity.
Only in the following unusual situation are the heuristics unable to
unambiguously match a string to an enum value:
- The
enumhas values which differ only in their case (such asRedandRED), and - The string in the config source is not an exact match with an
enumvalue name (such asred).
If your application must deal with such cases, write your own function which
maps a Config node to the correct enum value, resolving the ambiguities
however makes sense in your use case. Your code tells config to use that
function instead of the built-in enum conversion when it converts values. A
later section describes this technique which works for
all types, not only enum types.
Converting Configuration to Complex Types
The hierarchical features section describes the tree structure used to represent config data. The config system can map subtrees of a config tree to complex Java types.
Built-in Conversions to List and Map
The Config class exposes several methods for mapping a structured config node
to a Java List or Map. The Javadoc contains complete details, but
briefly your application can convert a structured Config node into:
- a
List<T>of a given type - a
Map<String, String>in which each key is the fully-qualified keyStringfor a config entry and the value is itsStringvalue
Custom Conversions
Often your code will be simpler if you can treat parts of the configuration as
custom, application-specific Java objects, rather than as a group of String
keys and values. You will need customized conversions to do so.
The config system provides many ways to accomplish this, described in the
io.helidon.config package Javadoc.
Some of those approaches require that the target class — the class to which you want to convert the configuration data — have certain characteristics or that you add a method to the class to help do the mapping. You might want to avoid changing the target class else you might not even be able to if you do not control its source.
Here are two approaches that will always work without requiring changes to the target class. For both approaches, you write your own conversion function. The difference is in how your application triggers the use of that mapper.
Use Custom Mapper Explicitly: Config.as method
Any time your application has a Config instance to map to the target class it
invokes Config.as passing an instance of the corresponding conversion
function:
Config configNode = config.get("web");
ConfigValue<WebConfig> web = configNode.as(WebConfigMapper::map);
You do not necessarily need a new instance of the mapper every time you want to use it.
In this approach, everywhere your application needs to perform this conversion it specifies the mapper to use. If you decided to change which mapper to use you would need to update each of those places in your application.
Register Custom Mapper Once, Use Implicitly: Config.as method
In this approach, your application:
- Tells each
Config.Builderthat needs to know about the custom mapper by either:- registering an instance of your mapper by invoking
Config.Builder.addMapper, or - implementing
ConfigMapperProviderso it returns an instance of your mapper (see the Javadoc for complete information) and creating or editing the fileio.helidon.config.spi.ConfigMapperProviderso it contains a line with the fully-qualified class name of yourConfigMapperProvider. The config system will use the Java service loader to find and invoke allConfigMapperProviderclasses listed and add the mappers they provide to eachConfig.Builderautomatically.
- registering an instance of your mapper by invoking
- Converts using the mapper by invoking the
Config.asmethod which accepts the target type to convert to, not the mapper itself that does the conversion.
If your application converts to the same target type in several places in the code, this approach allows you to change which mapper it uses by changing only the registration of the mapper, not each use of it.
Continuing the Web Example
The following examples build on the example configuration from the
application.properties example file in the introduction.
Java POJO to Hold web Properties Config:
public class WebConfig {
private boolean debug;
private int pageSize;
private double ratio;
public WebConfig(boolean debug, int pageSize, double ratio) {
this.debug = debug;
this.pageSize = pageSize;
this.ratio = ratio;
}
public boolean isDebug() {
return debug;
}
public int getPageSize() {
return pageSize;
}
public double getRatio() {
return ratio;
}
}
Custom Mapper Class:
public class WebConfigMapper implements Function<Config, WebConfig> {
@Override
public WebConfig apply(Config config) {
return new WebConfig(
config.get("debug").asBoolean().orElse(false),
config.get("page-size").asInt().orElse(10),
config.get("ratio").asDouble().orElse(1.0)
);
}
}
Explicitly Using the Mapper:
Config config = Config.create(classpath("application.properties"));
WebConfig web = config.get("web")
.as(new WebConfigMapper())
.get();
Registering and Implicitly Using the Mapper:
Config config = Config.builder(classpath("application.properties"))
.addMapper(WebConfig.class, new WebConfigMapper())
.build();
WebConfig web = config.get("web")
.as(WebConfig.class)
.get();
Either of the two approaches just described will always work without requiring you to change the POJO class.
Advanced Conversions using Explicit Mapping Logic
If the target Java class you want to use meets certain conditions — or if you can change it to meet one of those conditions — you might not need to write a separate mapper class. Instead, you add the mapping logic to the POJO itself in one of several ways and the config system uses Java reflection to search for those ways to perform the mapping.
Your application facilitates this implicit mapping either by adding to the POJO class or by providing a builder class for it.
This feature is available in Object mapping module, and is added through Java
ServiceLoader mechanism. This is no longer part of core Config module, as it
depends on reflection and introduces a lot of magic (see the list of supported
mapping methods below, also uses reflection to invoke the methods and to map
configuration values to fields/methods etc.).
Config object mapping Dependency in pom.xml:
<dependency>
<groupId>io.helidon.config</groupId>
<artifactId>helidon-config-object-mapping</artifactId>
</dependency>
Adding the Mapping to the POJO
If you can change the target class you can add any one of the following methods or constructors to the POJO class which the config system will find and use for mapping.
Continuing with the WebConfig example introduced earlier:
Methods Supporting Auto-mapping
static WebConfig create(Config); |
static WebConfig from(Config); |
static WebConfig from(String); |
static WebConfig of(Config); |
static WebConfig of(String); |
static WebConfig valueOf(Config); |
static WebConfig valueOf(String); |
static WebConfig fromConfig(Config); |
static WebConfig fromString(String); |
Constructors Supporting Auto-mapping:
WebConfig(Config); |
WebConfig(String); |
If the config system finds any of these methods or constructors when the application invokes
WebConfig wc = config.as(WebConfig.class).get();
it will invoke the one it found to map the config data to a new instance of the
target class. You do not need to write a separate class to do the mapping or
register it with the Config.Builder for the config instance.
Writing a Builder Method and Class for the POJO
You can limit the changes to the POJO class by adding a single builder method
to the POJO which returns a builder class for the POJO:
public class WebConfig {
static WebConfigBuilder builder() {
return new WebConfigBuilder();
}
}
The builder class WebConfigBuilder is expected to be a Java Bean with
- bean properties named for the config properties of interest, and
- a method
WebConfig build()which creates the mapped instance from the builder’s own bean properties.
When your application invokes config.as(WebConfig.class) the config system
- finds and invokes the
WebConfig.builder()method, - assigns the bean properties on the returned builder from the config subtree
rooted at
config, and - invokes the builder’s
build()method yielding the resultingWebConfiginstance.
Conversions using JavaBean Deserialization
The config system can also interpret your classes as JavaBeans and use the normal bean naming conventions to map configuration data to your POJO classes, using one of these patterns:
- POJO as JavaBean - The config system treats the target class itself as a JavaBean, assigning values from the config to the bean properties of the POJO class.
- builder as JavaBean - The config system invokes the
POJO’s
builder()method to obtain a builder for that POJO type and treats the builder class as a JavaBean, assigning values from the config to the builder’s bean properties and then invoking the builder’sbuildmethod to create an instance of the target POJO class. - POJO with factory method or decorated constructor - The
config system finds a
frommethod or a constructor on the POJO class itself which accepts annotated arguments, then invokes that method or constructor passing the specified arguments based on the config. Thefrommethod returns an instance of the POJO class initialized with the values passed as arguments.
The following sections describe these patterns in more detail.
This feature is available in Object mapping module, and is added through Java
ServiceLoader mechanism. This is no longer part of core Config module, as it
depends on reflection.
Config object mapping Dependency in pom.xml:
<dependency>
<groupId>io.helidon.config</groupId>
<artifactId>helidon-config-object-mapping</artifactId>
</dependency>
POJO as JavaBean
If your POJO target class is already a JavaBean — or you can modify it to become one — you might be able to avoid writing any explicit mapping code yourself.
The config system invokes the no-args constructor on the target class to create a new instance. It treats each public setter method and each public non-final field as a JavaBean property. The config system processes any non-primitive property recursively as a JavaBean. In this way the config system builds up the target object from the config data.
By default, the system matches potential JavaBean property names with config keys in the configuration.
Use the Value annotation to control some of JavaBean processing for a
given property.
Value Annotation
| Attribute | Usage |
|---|---|
key | Indicates which config key should match this JavaBean property |
withDefault | String used for the bean property default value if none is set in the config |
withDefaultSupplier | Supplier of the default bean property value if not is set in the config |
To exclude a bean property from the config system bean processing annotate it
with Transient.
Here is an example using the app portion of the example configuration from the
introduction.
Java bean to load app properties into via setters:
Here is an example of code loading config and mapping part of it to the
AppConfig bean above.
Map app config node into AppConfig class:
Builder as JavaBean
If the target class includes the public static method builder() that returns
any object, then the config system will make sure that the return type has a
method build() which returns an instance of the target class. If so, the
config system treats the builder as a JavaBean and
- invokes the
builder()method to instantiate the builder class, - treats the builder as a JavaBean and maps the
Configsubtree to it, - invokes the builder’s
build()method to create the new instance of the target class.
You can augment the target class with the public static builder() method:
JavaBean for app properties, via a Builder:
Target Class with Annotated Factory Method or Constructor
Another option is to annotate the parameters to a factory method or to a
constructor on the target class. You can add a factory method to the target
class, a public static method from with parameters annotated to link them to
the corresponding config keys. Or you can add or modify a constructor with
parameters, similarly annotated to form the link from each parameter to the
corresponding config key.
from method or constructor with
@Value and specify the key to use for the mapping. The parameter names in
the Java code are not always available at runtime to map to config keys. (They
might be arg0, arg1, etc.)Target Class with Factory Method from:
Alternatively, you can use an annotated constructor instead of a static factory
method. Revising the example above, make the constructor public, annotate its
parameters, and remove the now-unneeded from factory method.
Target Class with Annotated Public Constructor:
When the application invokes config.as(AppConfig.class), the config system
locates the public annotated constructor and invokes it, passing as arguments
the data it fetches from the configuration matching the annotation key names
with the configuration keys.