night-config icon indicating copy to clipboard operation
night-config copied to clipboard

POJO serialization + Comments

Open Darkhax opened this issue 5 years ago • 2 comments

Hello, I am new to using NightConfig and was wondering if there was an easy way to handle comments when doing object serialization. I looked at the examples and how a few other projects used the library but I could not find any examples. Ideally I would be looking for something like an annotation that could be put on a field alongside the spec annotations.

Something like this

public class Configuration {
    
    @Path("file_database")
    @Comment("Login information for the file database.")
    private Database fileDatabase;
    
    @Path("file_processing")
    @Comment("File processing queue settings.")
    private Processing processing;
    
    class Database {
        
        @Path("host")
        @Comment("The host name for the database")
        private String host = "127.0.0.1:3090";
        
        @Path("username")
        @Comment("The username to use when logging in.")
        private String username = "root";
        
        @Path("password")
        @Comment("The password for the user in the database.")
        private String password = "";
    }
    
    class Processing {
        
        @Path("maxFileSize")
        @Comment("The maximum file size in bytes.")
        private long maxFileSize = 250_000;
        
        @Path("threadPool")
        @Comment("The size of the file processing thread pool.")
        private int poolSize = 4;
    }
}

And then it would generate something like this.

// Login information for the file database.
file_database : {

    // The host name for the database.
    host: "127.0.0.1:3090",
    
    // The username to use when logging in.
    username: "root",
    
    // The password for the user in the database.
    password: ""
}

// File processing queue settings.
processing : {

    // The maximum file size in bytes.
    maxFileSize: 250000,
    
    // The size of the file processing thread pool.
    poolSize: 4
}

Darkhax avatar Jan 24 '20 07:01 Darkhax

Hello! Unfortunately there is no such annotation at the moment, but it's planned for the next version.

For now, you can use the method setComment to add comments to your config (after pojo serialization).

TheElectronWill avatar Jan 27 '20 11:01 TheElectronWill

As a temporary solution i made my own interface

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface Comment {
    String value();
}

Then i use :

        binder.toConfig(object, config);

        for (Field field : object.getClass().getDeclaredFields())
        {
            if (field.isAnnotationPresent(Comment.class) && field.isAnnotationPresent(Path.class))
            {
                config.setComment(field.getAnnotation(Path.class).value(), field.getAnnotation(Comment.class).value());
            }
        }

        config.save();

to add the comment before saving

DenisD3D avatar Feb 01 '21 19:02 DenisD3D