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

Implement comments for maps and collections with snakeyaml #34

Open
wants to merge 26 commits into
base: master
Choose a base branch
from

Conversation

Brikster
Copy link

@Brikster Brikster commented Aug 5, 2022

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

@@ -6,9 +6,9 @@
<parent>
<groupId>eu.okaeri</groupId>
<artifactId>okaeri-configs</artifactId>
<version>4.0.6</version>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No project version bumps please

@@ -169,6 +178,12 @@ public ConfigPostprocessor updateLinesKeys(@NonNull ConfigSectionWalker walker)

lastIndent = indent;
String updatedLine = walker.update(line, currentPath.get(currentPath.size() - 1), currentPath);

while (collectionsStartCount != 0) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic changes in this class are really convoluted and have a lot of mutation based whiles. This needs better structure, should be very much doable with at least semi-simple result.

@dasavick
Copy link
Member

dasavick commented Aug 5, 2022

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

@P3ridot
Copy link

P3ridot commented Jun 19, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants