Package org.instancio

Interface LenientSelector

All Superinterfaces:
TargetSelector
All Known Subinterfaces:
FieldSelectorBuilder, GroupableSelector, PredicateSelector, ScopeableSelector, Selector, TypeSelectorBuilder

public interface LenientSelector extends TargetSelector
A lenient selector does not trigger the "unused selector" error if it does not match any targets.
Since:
4.4.0
  • Method Summary

    Modifier and Type
    Method
    Description
    Marks this selector as lenient, which prevents the selector from producing "unused selector" error if it does not match any target.
  • Method Details

    • lenient

      TargetSelector lenient()
      Marks this selector as lenient, which prevents the selector from producing "unused selector" error if it does not match any target. This provides an alternative to LenientMode.lenient(), which treats all selectors as lenient (not recommended).

      This method can be useful when using Instancio to create a generic Model, or in a helper method for creating objects of arbitrary types.

      For example, the following method will set all lastUpdated fields to a date in the past:

      
       static <T> Model<T> baseModel(Class<T> klass) {
           TargetSelector lastUpdated = Select.fields()
               .ofType(Instant.class).named("lastUpdated").lenient();
      
           return Instancio.of(klass)
               .generate(lastUpdated, gen -> gen.temporal().instant().past())
               .toModel();
       }
       

      Marking the selector as lenient will prevent the unused selector error if a given klass does not have a lastUpdated field.

      See also: Selector Strictness section of the User Guide.

      Returns:
      a lenient selector
      Since:
      4.4.0
      See Also: