java.lang.Object
ru.vyarus.dropwizard.guice.module.context.option.mapper.OptionsMapper

public class OptionsMapper extends Object
Helper utility class for mapping options from system properties, environment variables or direct string. Used with 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 Details

    • OptionsMapper

      public OptionsMapper()
  • Method Details

    • printMappings

      public OptionsMapper printMappings()
      Enable assignment logging (to system out, as loggers are not yet initialized).
      Returns:
      mapper instance for chained calls
    • props

      public OptionsMapper props()
      Shortcut for props(String) with "option." prefix.
      Returns:
      mapper instance for chained calls
    • props

      public <T extends Enum<? extends Option> & Option> OptionsMapper props(String prefix)
      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

      public <T extends Enum<? extends Option> & Option> OptionsMapper prop(String name, T option)
      Shortcut version of prop(String, Enum, Function). Used when default converters could be used.
      Type Parameters:
      T - helper option type
      Parameters:
      name - property name
      option - 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 type
      V - value type
      Parameters:
      name - property name
      option - option
      converter - value converter (may be null to use default converters)
      Returns:
      mapper instance for chained calls
    • env

      public <T extends Enum<? extends Option> & Option> OptionsMapper env(String name, T option)
      Shortcut version of env(String, Enum, Function). Used when default converters could be used.
      Type Parameters:
      T - helper option type
      Parameters:
      name - environment variable name
      option - 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 type
      T - helper option type
      Parameters:
      name - environment variable name
      option - option
      converter - value converter (may be null to use default converters)
      Returns:
      mapper instance for chained calls
    • string

      public <V, T extends Enum<? extends Option> & Option> OptionsMapper string(T option, String value)
      Shortcut version of string(Enum, String, Function). Used when default converters could be used.
      Type Parameters:
      V - helper value type
      T - helper option type
      Parameters:
      option - option
      value - 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 type
      T - helper option type
      Parameters:
      option - option
      value - value string (could be null)
      converter - value converter (may be null to use default converters)
      Returns:
      mapper instance for chained calls
    • map

      public Map<Enum<? extends Option>,Object> map()
      Returns:
      map of resolved options