@ConverterClass annotation #38

Closed
lviggiano opened this Issue Jun 30, 2013 · 2 comments

Comments

Projects
None yet
1 participant
@lviggiano
Owner

lviggiano commented Jun 30, 2013

As per discussion in issue #37

Given a properties file like:

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

and a Java source like

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

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 should allow the user to specify a class specific to a method to convert the associated property to the resulting object.

It should work also for collections and lists as in the above example.

@lviggiano

This comment has been minimized.

Show comment
Hide comment
@lviggiano

lviggiano Jul 9, 2013

Owner

Implemented fa56efc

Owner

lviggiano commented Jul 9, 2013

Implemented fa56efc

@lviggiano

This comment has been minimized.

Show comment
Hide comment

@lviggiano lviggiano closed this Jul 10, 2013

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