owner icon indicating copy to clipboard operation
owner copied to clipboard

Preprocessor not executed when no @DefaultValue

Open drapostolos opened this issue 8 years ago • 0 comments

Would be nice if a Preprocessor is executed even when no @DefaultValue has been specified. Then it would for example (see below) be possible to add null checks for 'Mandatory' properties and solve #95. Further on, providing some sort of Context object in the process(String) method would be nice, for example getting the name of the property that is null in this case. Makes it possible to make a better error message.

import java.util.Objects;

import org.aeonbits.owner.Config;
import org.aeonbits.owner.ConfigFactory;
import org.aeonbits.owner.Preprocessor;

public class OwnerTester {

    public static void main(String[] args) {
        Conf conf = ConfigFactory.create(Conf.class);
        System.out.println(conf.p1());
    }

    public interface Conf extends Config {
        @PreprocessorClasses(NullChecker.class)
        String p1();
    }

    public static class NullChecker implements Preprocessor {

        @Override
        public String process(String input) {
            System.out.println("**Preprocessor**");
            Objects.requireNonNull(input, "Property is null");
            return input;
        }
    }
}

drapostolos avatar Oct 20 '16 08:10 drapostolos