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

Add configuration use helpers #19

Open
tokee opened this issue Mar 22, 2021 · 0 comments
Open

Add configuration use helpers #19

tokee opened this issue Mar 22, 2021 · 0 comments
Assignees
Labels
enhancement New feature or request

Comments

@tokee
Copy link
Member

tokee commented Mar 22, 2021

A common pattern is to have something like

public static final String SERVICE_PORT_KEY = ".network.port";
public static final int SERVICE_PORT_DEFAULT = 8090;

public static final String PURPOSE_KEY = ".purpose"; // No default

private final int servicePort;
private final String purpose;

public MyApp {
  YAML conf = getMajorConfiguration().getSubMap(".myapp");

  servicePort = conf.getInt(SERVICE_PORT_KEY, SERVICE_PORT_DEFAULT);
  purpose = conf.getString(PURPOSE_KEY); // throws exception if the param is not defined
}

Having the keys and the defaults defined as static final at the top of the file is great when reading the code, but maybe they could be collapsed to 1 line each and maybe it could be externalized when there is a problem with the configuration? Something like

public static final KeyPool KP = new KeyPool();

public static final Key SERVICE_PORT = KP.add(".network.port", 8090),
public static final Key PURPOSE = KP.addString("purpose")

public MyApp {
  // If the value for PURPOSE is not defined, an exception is thrown
  KP.setConf(getMajorConfiguration().getSubMap(".myapp"));

  // After setconf on KP, the values can be accessed with 
  // SERVICE_PORT.get()
  // PURPOSE.get();
}
@tokee tokee added the enhancement New feature or request label Mar 22, 2021
@tokee tokee self-assigned this Mar 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant