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 collections/arrays #37

Open
alexeyr opened this issue Jun 30, 2013 · 3 comments
Open

Nested collections/arrays #37

alexeyr opened this issue Jun 30, 2013 · 3 comments

Comments

@alexeyr
Copy link

alexeyr commented Jun 30, 2013

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
Copy link
Collaborator

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
Copy link
Author

alexeyr commented Jun 30, 2013

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

@lviggiano
Copy link
Collaborator

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.

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

2 participants