commons-configuration icon indicating copy to clipboard operation
commons-configuration copied to clipboard

CONFIGURATION-822: Fix ambiguity on the section determining

Open BranislavBeno opened this issue 2 years ago • 2 comments

This PR contains small code changes in org.apache.commons.configuration2.INIConfiguration, which will treat inline comments on section declaration line consistently.

BranislavBeno avatar Oct 17 '22 21:10 BranislavBeno

Which INI specification are you implementing? AFIK a semicolon for a comment is only legal at the start of a line (https://en.m.wikipedia.org/wiki/INI_file)

garydgregory avatar Oct 17 '22 22:10 garydgregory

Which INI specification are you implementing? AFIK a semicolon for a comment is only legal at the start of a line (https://en.m.wikipedia.org/wiki/INI_file)

FWIW, I think I saw an INI file at $work this week with comments like that. And from that Wiki page:

image

kinow avatar Oct 17 '22 22:10 kinow

Which INI specification are you implementing? AFIK a semicolon for a comment is only legal at the start of a line (https://en.m.wikipedia.org/wiki/INI_file)

Honestly, none particular. But @kinow has, IMHO, correctly pointed out, that some implementations allow inline comments.

What should I do in case I have to process INI file with inline comments? Should I choose an implementation, which allows inline comments? Or should I reinvent the wheel and implement my own solution?

And is it really correct behavior to refuse section determining, just because it contains inline comment? But only in case this comment doesn't end with closing square bracket, otherwise the section is determined, however with obviously wrong section name.

I fully understand your concerns, that my proposal could violate backwards compatibility.

How about following solution?

    private boolean inLineCommentsAllowed = false;

    public INIConfiguration(boolean inLineCommentsAllowed) {
        this.inLineCommentsAllowed = inLineCommentsAllowed;
    }

    public INIConfiguration() {
    }

...

Field will be initialized only on instantiation. No setter will be available. With field set to false will INI file parsing proceed backwards compatible.

BranislavBeno avatar Oct 18 '22 22:10 BranislavBeno

It seems, that inline comments are already allowed, however, currently only on value lines.
Tested with e.g.: TestINIConfiguration#testValueWithComment

BranislavBeno avatar Oct 19 '22 21:10 BranislavBeno

Codecov Report

Merging #229 (776d79b) into master (0e3ce1c) will increase coverage by 0.00%. The diff coverage is 85.71%.

@@            Coverage Diff            @@
##             master     #229   +/-   ##
=========================================
  Coverage     89.02%   89.03%           
- Complexity     3529     3535    +6     
=========================================
  Files           183      183           
  Lines          9642     9648    +6     
  Branches       1196     1197    +1     
=========================================
+ Hits           8584     8590    +6     
  Misses          777      777           
  Partials        281      281           
Impacted Files Coverage Δ
...pache/commons/configuration2/INIConfiguration.java 96.47% <85.71%> (+0.09%) :arrow_up:

:mega: We’re building smart automated test selection to slash your CI/CD build times. Learn more

codecov-commenter avatar Oct 20 '22 03:10 codecov-commenter

I might update the constructor after a merge to be private and use a builder to avoid the proliferation of constructors...

garydgregory avatar Nov 06 '22 20:11 garydgregory

I might update the constructor after a merge to be private and use a builder to avoid the proliferation of constructors...

Thank you for PR accepting. Builder usage makes sense.

BranislavBeno avatar Nov 07 '22 07:11 BranislavBeno