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

Comments

Projects
None yet
1 participant
@lviggiano
Copy link
Owner

lviggiano commented Oct 28, 2013

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

@ghost ghost assigned lviggiano Oct 28, 2013

lviggiano added a commit that referenced this issue Dec 4, 2013

lviggiano added a commit that referenced this issue Dec 4, 2013

@lviggiano

This comment has been minimized.

Copy link
Owner

lviggiano commented Dec 6, 2013

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

lviggiano added a commit that referenced this issue Feb 8, 2014

@lviggiano lviggiano closed this Feb 8, 2014

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