Search in sources :

Example 1 with IgnoreErrorsBindHandler

use of cn.taketoday.context.properties.bind.handler.IgnoreErrorsBindHandler in project today-framework by TAKETODAY.

the class ConfigurationPropertiesBinder method getBindHandler.

private <T> BindHandler getBindHandler(Bindable<T> target, ConfigurationProperties annotation) {
    List<Validator> validators = getValidators(target);
    BindHandler handler = getHandler();
    handler = new ConfigurationPropertiesBindHandler(handler);
    if (annotation.ignoreInvalidFields()) {
        handler = new IgnoreErrorsBindHandler(handler);
    }
    if (!annotation.ignoreUnknownFields()) {
        UnboundElementsSourceFilter filter = new UnboundElementsSourceFilter();
        handler = new NoUnboundElementsBindHandler(handler, filter);
    }
    if (!validators.isEmpty()) {
        handler = new ValidationBindHandler(handler, validators.toArray(new Validator[0]));
    }
    for (ConfigurationPropertiesBindHandlerAdvisor advisor : getBindHandlerAdvisors()) {
        handler = advisor.apply(handler);
    }
    return handler;
}
Also used : IgnoreErrorsBindHandler(cn.taketoday.context.properties.bind.handler.IgnoreErrorsBindHandler) NoUnboundElementsBindHandler(cn.taketoday.context.properties.bind.handler.NoUnboundElementsBindHandler) UnboundElementsSourceFilter(cn.taketoday.context.properties.source.UnboundElementsSourceFilter) ValidationBindHandler(cn.taketoday.context.properties.bind.validation.ValidationBindHandler) BoundPropertiesTrackingBindHandler(cn.taketoday.context.properties.bind.BoundPropertiesTrackingBindHandler) BindHandler(cn.taketoday.context.properties.bind.BindHandler) IgnoreErrorsBindHandler(cn.taketoday.context.properties.bind.handler.IgnoreErrorsBindHandler) IgnoreTopLevelConverterNotFoundBindHandler(cn.taketoday.context.properties.bind.handler.IgnoreTopLevelConverterNotFoundBindHandler) AbstractBindHandler(cn.taketoday.context.properties.bind.AbstractBindHandler) NoUnboundElementsBindHandler(cn.taketoday.context.properties.bind.handler.NoUnboundElementsBindHandler) Validator(cn.taketoday.validation.Validator) ValidationBindHandler(cn.taketoday.context.properties.bind.validation.ValidationBindHandler)

Example 2 with IgnoreErrorsBindHandler

use of cn.taketoday.context.properties.bind.handler.IgnoreErrorsBindHandler in project today-framework by TAKETODAY.

the class JavaBeanBinderTests method bindToClassWhenPropertyCannotBeConvertedAndIgnoreErrorsShouldNotSetValue.

@Test
void bindToClassWhenPropertyCannotBeConvertedAndIgnoreErrorsShouldNotSetValue() {
    MockConfigurationPropertySource source = new MockConfigurationPropertySource();
    source.put("foo.int-value", "12");
    source.put("foo.long-value", "bang");
    source.put("foo.string-value", "foo");
    source.put("foo.enum-value", "foo-bar");
    this.sources.add(source);
    IgnoreErrorsBindHandler handler = new IgnoreErrorsBindHandler();
    ExampleValueBean bean = this.binder.bind("foo", Bindable.of(ExampleValueBean.class), handler).get();
    assertThat(bean.getIntValue()).isEqualTo(12);
    assertThat(bean.getLongValue()).isEqualTo(0);
    assertThat(bean.getStringValue()).isEqualTo("foo");
    assertThat(bean.getEnumValue()).isEqualTo(ExampleEnum.FOO_BAR);
}
Also used : IgnoreErrorsBindHandler(cn.taketoday.context.properties.bind.handler.IgnoreErrorsBindHandler) MockConfigurationPropertySource(cn.taketoday.context.properties.source.MockConfigurationPropertySource) Test(org.junit.jupiter.api.Test)

Example 3 with IgnoreErrorsBindHandler

use of cn.taketoday.context.properties.bind.handler.IgnoreErrorsBindHandler in project today-infrastructure by TAKETODAY.

the class JavaBeanBinderTests method bindToClassWhenPropertyCannotBeConvertedAndIgnoreErrorsShouldNotSetValue.

@Test
void bindToClassWhenPropertyCannotBeConvertedAndIgnoreErrorsShouldNotSetValue() {
    MockConfigurationPropertySource source = new MockConfigurationPropertySource();
    source.put("foo.int-value", "12");
    source.put("foo.long-value", "bang");
    source.put("foo.string-value", "foo");
    source.put("foo.enum-value", "foo-bar");
    this.sources.add(source);
    IgnoreErrorsBindHandler handler = new IgnoreErrorsBindHandler();
    ExampleValueBean bean = this.binder.bind("foo", Bindable.of(ExampleValueBean.class), handler).get();
    assertThat(bean.getIntValue()).isEqualTo(12);
    assertThat(bean.getLongValue()).isEqualTo(0);
    assertThat(bean.getStringValue()).isEqualTo("foo");
    assertThat(bean.getEnumValue()).isEqualTo(ExampleEnum.FOO_BAR);
}
Also used : IgnoreErrorsBindHandler(cn.taketoday.context.properties.bind.handler.IgnoreErrorsBindHandler) MockConfigurationPropertySource(cn.taketoday.context.properties.source.MockConfigurationPropertySource) Test(org.junit.jupiter.api.Test)

Aggregations

IgnoreErrorsBindHandler (cn.taketoday.context.properties.bind.handler.IgnoreErrorsBindHandler)3 MockConfigurationPropertySource (cn.taketoday.context.properties.source.MockConfigurationPropertySource)2 Test (org.junit.jupiter.api.Test)2 AbstractBindHandler (cn.taketoday.context.properties.bind.AbstractBindHandler)1 BindHandler (cn.taketoday.context.properties.bind.BindHandler)1 BoundPropertiesTrackingBindHandler (cn.taketoday.context.properties.bind.BoundPropertiesTrackingBindHandler)1 IgnoreTopLevelConverterNotFoundBindHandler (cn.taketoday.context.properties.bind.handler.IgnoreTopLevelConverterNotFoundBindHandler)1 NoUnboundElementsBindHandler (cn.taketoday.context.properties.bind.handler.NoUnboundElementsBindHandler)1 ValidationBindHandler (cn.taketoday.context.properties.bind.validation.ValidationBindHandler)1 UnboundElementsSourceFilter (cn.taketoday.context.properties.source.UnboundElementsSourceFilter)1 Validator (cn.taketoday.validation.Validator)1