Open alexeyr opened 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?
I did consider this approach, but it doesn't scale well if you need more than 2 or 3 properties, or default values.
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.
Related to #2: support for collections (or arrays) of complex types. E.g.
and in the properties file
should produce an array of 2 servers (or map with keys
"1"
and"2"
).