noleme/noleme-vault

API: make the `VaultBuilder` bean/injection-friendly

eledhwen opened this issue · 0 comments

Current design relies a lot on the Vault class static methods as main entry points for building vaults.

While it is possible to work with differentiated VaultParser instances, the current API design does not make it clear nor easy. As a result, the path of least resistance systematically leads to the registration of custom modules directly into the default parser, eventually through static blocks, which is far from ideal.

Instead, we should make it possible for the user to be able to assemble a VaultBuilder providing the exact same facilities as the Vault static methods.

So just like we can currently do things like :

try (var vault = Vault.with(
  variables(defsdefs.set("some_var", 123)),
  "conf/first-conf.yml",
  "conf/second-conf.yml"
)) {
  SomeService s = vault.instance(SomeService.class);
}

where, prior to this if we need a custom module and/or preprocessor we have to do something like :

VaultFactory.defaultParser
  .register(new MyCustomModule())
  .registerPreprocessor(new MyCustomPreprocessor())
;

if one wants an injectable vault setting, it is already possible, but they have to write the following, which I find particularly impractical:

//this would be done in the setup phase somewhere
//requires knowledge of the inner workings (factory model, parser implementation)
var factory = new VaultFactory(new VaultCompositeParser()
    .register(new MyCustomModule())
    .registerPreprocessor(new MyCustomPreprocessor())
);

//verbose / unintuitive ; it does provide the tools for a multi-stage build however
var builder = Vault.builder()
    .setFactory(factory)
    .with(
        List.of(
            "conf/first-conf.yml",
            "conf/second-conf.yml"
        ),
        variables(defsdefs.set("some_var", 123))
    );

try (var vault = builder.build()) {
    SomeService s = vault.instance(SomeService.class);
}

instead, we could enable the following pattern :

/* Some place where core configuration logic happens */
VaultBuilder builder = Vault.builder(parserparser
  .register(new MyCustomModule())
  .registerPreprocessor(new MyCustomPreprocessor())
);

/* Someplace else where we need to load a configuration graph */
try (var vault = builder.with(
  variables(defsdefs.set("some_var", 123)),
  "conf/first-conf.yml",
  "conf/second-conf.yml"
)) {
  SomeService s = vault.instance(SomeService.class);
}