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 for variables expansion in the @Key annotation #63

Closed
lviggiano opened this issue Oct 28, 2013 · 1 comment
Closed

Support for variables expansion in the @Key annotation #63

lviggiano opened this issue Oct 28, 2013 · 1 comment
Assignees
Milestone

Comments

@lviggiano
Copy link
Collaborator

Example:

<servers>
    <dev> <!-- development environment -->
        <name>DEV</name>
        <hostname>devhost</hostname>
        <port>6000</port>
        <user>myuser1</user>
        <password>mypass1</password>
    </dev>
    <uat> <!--- user acceptance test environment -->
        <name>UAT</name>
        <hostname>uathost</hostname>
        <port>60020</port>
        <user>myuser2</user>
        <password>mypass2</password>
    </uat>
</servers>

Mapping class and example usage:

@Sources("classpath:org/aeonbits/owner/variableexpansion/KeyExpansionExample.xml")
public interface ExpandsFromAnotherKey extends Config {

    @DefaultValue("dev")
    String env();

    @Key("servers.${env}.name")
    String name();

    @Key("servers.${env}.hostname")
    String hostname();

    @Key("servers.${env}.port")
    Integer port();

    @Key("servers.${env}.user")
    String user();

    @DisableFeature(VARIABLE_EXPANSION)
    @Key("servers.${env}.password")
    String password();
}

@Test
public void testKeyExpansionFromAnotherKey() {
    ExpandsFromAnotherKey cfg = ConfigFactory.create(ExpandsFromAnotherKey.class);

    assertEquals("DEV", cfg.name());
    assertEquals("devhost", cfg.hostname());
    assertEquals(new Integer(6000), cfg.port());
    assertEquals("myuser1", cfg.user());
    assertNull(cfg.password()); // expansion is disabled on method level
}

@Test
public void testKeyExpansionFromAnotherKeyWithImportOverriding() {
    ExpandsFromAnotherKey cfg = ConfigFactory.create(ExpandsFromAnotherKey.class, map("env", "uat"));

    assertEquals("UAT", cfg.name());
    assertEquals("uathost", cfg.hostname());
    assertEquals(new Integer(60020), cfg.port());
    assertEquals("myuser2", cfg.user());
    assertNull("mypass2", cfg.password()); // expansion is disabled on method level
}

This way you can select ${env} in the ConfigFactory (or from the system properties) and have the above interface map to the appropriate section.

See: #56 #48

@lviggiano
Copy link
Collaborator Author

Implemented few days ago, missing the docs still, before to close this task.

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

No branches or pull requests

1 participant