OkaeriPoland / okaeri-configs

Simple Java/POJO config library written with love and Lombok
MIT License
77 stars 11 forks source link

Implement comments for maps and collections with snakeyaml #34

Open Brikster opened 2 years ago

Brikster commented 2 years ago

Fix issue #17.

Rewritten ConfigPostprocessor to provide first line of collection to a walker (previously, lines that begins from "-" were written as is). YamlSectionWalker's implementations now resolve subtypes of Map and Collection and append comments of their fields.

Now there is an ability to create commented Lists, Maps, Lists of Maps, Maps of Lists etc.

Here you can see an example:

import eu.okaeri.configs.OkaeriConfig;
import eu.okaeri.configs.annotation.*;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.*;

@Getter
@SuppressWarnings("FieldMayBeFinal")
@Names(strategy = NameStrategy.HYPHEN_CASE, modifier = NameModifier.TO_LOWER_CASE)
public class ExampleConfig extends OkaeriConfig {

    @Exclude
    private static final ExampleSubConfig anotherExampleSubConfigValue = new ExampleSubConfig("Custom awesome value", 2022);

    @Comment
    private List<ExampleSubConfig> subConfigsList = Arrays.asList(new ExampleSubConfig(), anotherExampleSubConfigValue);

    @Comment
    private Map<String, ExampleSubConfig> subConfigsMap = new HashMap<String, ExampleSubConfig>() {{
        put("first-map-key", new ExampleSubConfig());
        put("second-map-key", anotherExampleSubConfigValue);
    }};

    @Comment
    private List<Map<String, ExampleSubConfig>> subConfigsMapsList = Collections.singletonList(new HashMap<String, ExampleSubConfig>() {{
        put("first-map-key", new ExampleSubConfig());
        put("second-map-key", anotherExampleSubConfigValue);
    }});

    @Comment
    private Map<String, List<ExampleSubConfig>> subConfigsListsMap = new HashMap<String, List<ExampleSubConfig>>() {{
        put("first-map-key", Arrays.asList(new ExampleSubConfig(), anotherExampleSubConfigValue));
        put("second-map-key", Arrays.asList(new ExampleSubConfig(), anotherExampleSubConfigValue));
    }};

    @Getter
    @SuppressWarnings("FieldMayBeFinal")
    @AllArgsConstructor
    @NoArgsConstructor
    @Names(strategy = NameStrategy.HYPHEN_CASE, modifier = NameModifier.TO_LOWER_CASE)
    public static class ExampleSubConfig extends OkaeriConfig {

        @Comment("You can see this comment inside maps and collections")
        private String someKeyFirst = "Awesome value";

        @Comment("And this comment too")
        private int someKeySecond = 1337;

    }

}

And config file:

sub-configs-list:
- # You can see this comment inside maps and collections
  some-key-first: Awesome value
  # And this comment too
  some-key-second: 1337
- # You can see this comment inside maps and collections
  some-key-first: Custom awesome value
  # And this comment too
  some-key-second: 2022

sub-configs-map:
  second-map-key:
    # You can see this comment inside maps and collections
    some-key-first: Custom awesome value
    # And this comment too
    some-key-second: 2022
  first-map-key:
    # You can see this comment inside maps and collections
    some-key-first: Awesome value
    # And this comment too
    some-key-second: 1337

sub-configs-maps-list:
- second-map-key:
    # You can see this comment inside maps and collections
    some-key-first: Custom awesome value
    # And this comment too
    some-key-second: 2022
  first-map-key:
    # You can see this comment inside maps and collections
    some-key-first: Awesome value
    # And this comment too
    some-key-second: 1337

sub-configs-lists-map:
  second-map-key:
  - # You can see this comment inside maps and collections
    some-key-first: Awesome value
    # And this comment too
    some-key-second: 1337
  - # You can see this comment inside maps and collections
    some-key-first: Custom awesome value
    # And this comment too
    some-key-second: 2022
  first-map-key:
  - # You can see this comment inside maps and collections
    some-key-first: Awesome value
    # And this comment too
    some-key-second: 1337
  - # You can see this comment inside maps and collections
    some-key-first: Custom awesome value
    # And this comment too
    some-key-second: 2022
dasavick commented 2 years ago

Including comments in each map/collection element is messy and confusing for the end user. It should be only in the first one.

P3ridot commented 1 year ago

Including comments in each map/collection element is messy and confusing for the end user. It should be only in the first one.

Would be better, if it would be configurable