matteobaccan / owner

Get rid of the boilerplate code in properties based configuration.
https://matteobaccan.github.io/owner/
BSD 3-Clause "New" or "Revised" License
915 stars 213 forks source link

Nested collections/arrays #37

Open alexeyr opened 11 years ago

alexeyr commented 11 years ago

Related to #2: support for collections (or arrays) of complex types. E.g.

public interface ServerConfig extends Config {
  String host();
  int port();
}

public interface AppConfig extends Config {
  ServerConfig[] servers(); // could also be List<ServerConfig> or Map<String, ServerConfig>
}

and in the properties file

servers.1.host=www.google.com
servers.1.port=80
servers.2.host=www.github.com
servers.2.port=80

should produce an array of 2 servers (or map with keys "1" and "2").

lviggiano commented 11 years ago

Hi @alexeyr.

This is possible (with the version available on the master branch which is currently not released yet):

servers=www.google.com:80, www.github.com:8080

public class Server {
    private final String name;
    private final Integer port;

    public Server(String spec) {  // you need to parse spec, ok... some work to do, but not that bad...
        String[] split = spec.split(":", -1);
        name = split[0];
        if (split.lenght > =2) 
            port = Integer.valueOf(split[1]);
        else 
            port = 80;
    }
    public String getName() { return name; }
    public Integer getPort() { return port; } 
}

public interface ServerConfig extends Config {
    Server[] servers;
    // or 
    List<Server> servers;
}

Another option I am thinking about is this:


public class Server {
    private final String name;
    private final Integer port;

    public Server(String name, Integer port) {  
        this.name = name; 
        this.port = port;
    }
    public String getName() { return name; }
    public Integer getPort() { return port; } 
}

public interface ServerConfig extends Config {
    @ConverterClass(ServerConverter.class)
    Server[] servers;
    // or 
    List<Server> servers;
}

public class ServerConverter extends Converter<Server> {
    public Server convert(Method targetMethod, String text) {
        String[] split = text.split(":", -1);
        String name = split[0];
        Integer port = 80;
        if (split.lenght >= 2) 
            port = Integer.valueOf(split[1]);
        return new Server(name, port);
    }
}

The @ConverterClass annotation is not implemented, but it is very easy to add, and I kind of like it. What's your opinion?

I have a different idea about config nesting, and I don't find it to be suitable for collections/arrays.

Notice that collections and arrays are already available in the master branch, and will be included in the next release.

Opinions?

alexeyr commented 11 years ago

I did consider this approach, but it doesn't scale well if you need more than 2 or 3 properties, or default values.

lviggiano commented 11 years ago

I'll consider this when I'll implement the nesting in config. Maybe it will be implemented as per your suggestion. This implementation makes sense.