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

Nested configuration interfaces (#129) #145

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

ketan
Copy link

@ketan ketan commented Oct 1, 2015

This is only an initial draft to get feedback (please don't shoot me :)

This works by doing 2 things -

  • a converter that checks if the target type is an
    implementation of the Config interface. If it is, then
    any further conversion is handed off to ConfigFactory with a
    (configurable)namespace prefix.
  • The properties manager, that looks up @Source annotation
    will lookup the annotation on all interfaces.

Usage -

app.db.username =
app.db.password =

app.jetty.host =
app.jetty.port =
@Config.Sources({"/etc/app.properties"})
public interface BaseConfig extends Accessible {
}

public interface AppConfig extends BaseConfig {
   @DefaultValue("app.db")
    Database db();

    @DefaultValue("app.jetty")
    Jetty jetty();
}

public interface Database extends Accessible {
    @Key("${ns}.username")
    String host();

    @Key("${ns}.password")
    String password();
}

public interface Jetty extends Accessible {
    @Key("${ns}.host")
    String host();

    @Key("${ns}.port")
    int port();
}

This is only an initial draft to get feedback (please don't shoot me :)

This works by doing 2 things -

* a converter that checks if the target type is an
  implementation of the `Config` interface. If it is, then
  any further conversion is handed off to `ConfigFactory` with a
   (configurable)namespace prefix.
* The properties manager, that looks up `@Source` annotation
  will lookup the annotation on all interfaces.

Usage -

```properties
#/etc/app.properties
app.db.username =
app.db.password =

app.jetty.host =
app.jetty.port =
```

```java
@Config.Sources({"/etc/app.properties"})
public interface BaseConfig extends Accessible {
}

public interface AppConfig extends BaseConfig {
   @DefaultValue("app.db")
    Database db();

    @DefaultValue("app.jetty")
    Jetty jetty();
}

public interface Database extends Accessible {
    @key("${ns}.username")
    String host();

    @key("${ns}.password")
    String password();
}

public interface Jetty extends Accessible {
    @key("${ns}.host")
    String host();

    @key("${ns}.port")
    int port();
}

```
@lviggiano
Copy link
Collaborator

Hi @ketan,

I like your pull request. Can you add some unit tests?

namespace = "ns";
}
imports.put(namespace, text);
Object result = ConfigFactory.create((Class<? extends Config>) targetType, imports);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem here, is that every time this call is done, a new object (a dynamic proxy) is created; but that's also consistent with the rest of the code. Maybe we can add a class level annotation that allows owner to use a cached instance (see org.aeonbits.owner.ConfigCache.java, http://owner.aeonbits.org/docs/singleton/)

@AarjavP
Copy link

AarjavP commented May 23, 2017

I would like to see this feature get added as well, is there anything I can do to help?

@ketan
Copy link
Author

ketan commented May 24, 2017

Unfortunately I've not had time to look at this PR, I don't think the situation will change anytime sokn., If someone wants to pick up this PR and make improvements, I'm happy...

@shakedel
Copy link

I might give it a go. What improvements were you thinking of?

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

Successfully merging this pull request may close these issues.

4 participants