Class OptionsMapper
GuiceBundle.Builder.options(Map). May recognize multiple prefixed options from
properties (see props(String)).
Only existing properties are mapped. If value is null or property is not defined - it's ignored.
Example usage:
builder.options(new OptionsMapper()
.env("ENV_PROP", MyOptions.Opt1)
.env("ENV_PROP_CUST", MyOptions.Opt2, val -> convertVal(val))
.prop("sys.prop", MyOptions.Opt3)
.string(MyOptions.Opt4, "value")
.map())
Only string, boolean, integer, double, short. byte enum by value, enum by class, array and EnumSet option types are
directly supported (see StringConverter). For other types explicit converters must be used.
To enable generic options definition from properties use:
.options(new OptionsMapper().props().map())
This will map system properties prefixed with "option." and with full enum class and value. For example,
-Doption.ru.vyarus.dropwizard.guice.GuiceyOptions.InjectorStage=DEVELOPMENT.
In order to apply special conversion for some property (when default converters can't properly convert value) declare manual property binding before mass processing:
.options(new OptionsMapper()
.prop("option.ru.vyarus.dropwizard.guice.GuiceyOptions.InjectorStage", GuiceyOptions.InjectorStage,
val -> someConversion(val))
.props()
.map())
And option will be ignored during mass processing (because it's processed manually).
To see actual assignments (for debug) use printMappings().
- Since:
- 26.04.2018
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription<T extends Enum<? extends Option> & Option>
OptionsMapperShortcut version ofenv(String, Enum, Function).<V,T extends Enum<? extends Option> & Option>
OptionsMapperMap environment variable value to option.map()Enable assignment logging (to system out, as loggers are not yet initialized).<T extends Enum<? extends Option> & Option>
OptionsMapperShortcut version ofprop(String, Enum, Function).<V,T extends Enum<? extends Option> & Option>
OptionsMapperDirectly map system property value to option.props()Shortcut forprops(String)with "option." prefix.<T extends Enum<? extends Option> & Option>
OptionsMapperSearch for system properties, prefixed with provided string.<V,T extends Enum<? extends Option> & Option>
OptionsMapperShortcut version ofstring(Enum, String, Function).<V,T extends Enum<? extends Option> & Option>
OptionsMapperMap string to option.
-
Constructor Details
-
OptionsMapper
public OptionsMapper()
-
-
Method Details
-
printMappings
Enable assignment logging (to system out, as loggers are not yet initialized).- Returns:
- mapper instance for chained calls
-
props
Shortcut forprops(String)with "option." prefix.- Returns:
- mapper instance for chained calls
-
props
Search for system properties, prefixed with provided string. Property format must be: "(prefix)enumClassName.enumValue". For example: option.ru.vyarus.dropwizard.guice.GuiceyOptions.UseHkBridge where "option." is prefix.Could be used only for string, boolean, integer, double, short and byte option types.
If some options require special handling then map them directly using
prop(String, Enum, Function)and only AFTER that call mass processing.- Type Parameters:
T- helper option type- Parameters:
prefix- options prefix- Returns:
- mapper instance for chained calls
-
prop
Shortcut version ofprop(String, Enum, Function). Used when default converters could be used.- Type Parameters:
T- helper option type- Parameters:
name- property nameoption- option- Returns:
- mapper instance for chained calls
-
prop
public <V,T extends Enum<? extends Option> & Option> OptionsMapper prop(String name, T option, Function<String, V> converter) Directly map system property value to option.Directly mapped properties excluded from mass processing in
props(String), but direct mapping must appear before mass-processing.- Type Parameters:
T- helper option typeV- value type- Parameters:
name- property nameoption- optionconverter- value converter (may be null to use default converters)- Returns:
- mapper instance for chained calls
-
env
Shortcut version ofenv(String, Enum, Function). Used when default converters could be used.- Type Parameters:
T- helper option type- Parameters:
name- environment variable nameoption- option- Returns:
- mapper instance for chained calls
-
env
public <V,T extends Enum<? extends Option> & Option> OptionsMapper env(String name, T option, Function<String, V> converter) Map environment variable value to option.- Type Parameters:
V- helper value typeT- helper option type- Parameters:
name- environment variable nameoption- optionconverter- value converter (may be null to use default converters)- Returns:
- mapper instance for chained calls
-
string
Shortcut version ofstring(Enum, String, Function). Used when default converters could be used.- Type Parameters:
V- helper value typeT- helper option type- Parameters:
option- optionvalue- value string (could be null)- Returns:
- mapper instance for chained calls
-
string
public <V,T extends Enum<? extends Option> & Option> OptionsMapper string(T option, String value, Function<String, V> converter) Map string to option. When value is null or empty - nothing happens.- Type Parameters:
V- helper value typeT- helper option type- Parameters:
option- optionvalue- value string (could be null)converter- value converter (may be null to use default converters)- Returns:
- mapper instance for chained calls
-
map
- Returns:
- map of resolved options
-