Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support to load a single value #209

Open
jansupol opened this issue Nov 13, 2023 · 1 comment
Open

Support to load a single value #209

jansupol opened this issue Nov 13, 2023 · 1 comment
Labels
Milestone

Comments

@jansupol
Copy link
Contributor

Currently, Jakarta Config allows for loading config into an interface

@Configuration
public interface A {
   String value();
}

The most use-cases, however, are as follows:

if (config.property('NAME1')) {
     // do something
}
if (config.property('NAME2')) {
   // do something else
}

The convenient way is to allow the possibility to load the single value as Loader.bootstrap().path("NAME1").asString().
In MP config, there is a similar notion: config.getValue("NAME1", String.class)


There are the following possibilities:

  1. Allow just few methods: asInt(), asBoolean, asString
  2. Allow all primitive types: boolean, byte, short, float, double, int, long, char, and String
  3. Allow just asString() and more generic as(Class<T> type) and as(TypeToken type) - this has an advantage that later on the config could support as(UserDefinedType), but a disadvantage that we should define a way to pass convertors to the Loader.

The use-case usually checks whether the property is present. That can be implemented by:

  1. returning null when not available
  2. returning Optional<> (my favourite)
  3. throwing NoSuchObjectException
  4. implement a complementary isProperty()

In the case of Optional, it would be possible to use:
loader.path("my.property").asString().ifPresent(myProperty -> doSomething(myProperty))

@jansupol
Copy link
Contributor Author

Out of the discussion, should we keep load(Interface.class), or would it better be as(Interface.class) which then can be as(String.class) as well?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants