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

@ConverterClass annotation #38

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

@ConverterClass annotation #38

lviggiano opened this issue Jun 30, 2013 · 2 comments

Comments

@lviggiano
Copy link
Collaborator

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

Implemented fa56efc

@lviggiano
Copy link
Collaborator Author

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

1 participant