Skip to content

Commit

Permalink
2080: ini: allow values to be updated
Browse files Browse the repository at this point in the history
Reviewed-by: erikj
  • Loading branch information
edvbld committed Oct 27, 2023
1 parent de78b01 commit 0c63816
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 16 deletions.
28 changes: 12 additions & 16 deletions ini/src/main/java/org/openjdk/skara/ini/INI.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@ public static INI parse(List<String> lines) {
var sections = new HashMap<String, Section>();

Section current = null;
boolean isSubsection = false;
for (var i = 0; i < lines.size(); i++) {
var line = lines.get(i);
if (line.isEmpty() || line.startsWith(";")) {
Expand All @@ -156,14 +155,14 @@ public static INI parse(List<String> lines) {
fail(i, "section header must be of format '[name (\"subsection\")?]'");
}

if (!isSubsection && current != null) {
sections.put(current.name(), current);
}

var name = parts[0];
if (parts.length == 1) {
current = new Section(name);
isSubsection = false;
if (sections.containsKey(name)) {
current = sections.get(name);
} else {
current = new Section(name);
sections.put(current.name(), current);
}
} else {
var subsection = parts[1];
if (!(subsection.startsWith("\"") && subsection.endsWith("\""))) {
Expand All @@ -180,11 +179,12 @@ public static INI parse(List<String> lines) {
}

var section = sections.get(name);
var next = new Section(subsectionName);
section.addSubsection(next);

isSubsection = true;
current = next;
if (section.hasSubsection(subsectionName)) {
current = section.subsection(subsectionName);
} else {
current = new Section(subsectionName);
section.addSubsection(current);
}
}
} else {
if (!line.contains("=")) {
Expand All @@ -202,10 +202,6 @@ public static INI parse(List<String> lines) {
}
}

if (!isSubsection && current != null) {
sections.put(current.name(), current);
}

return new INI(sections, globalEntries);
}
}
39 changes: 39 additions & 0 deletions ini/src/test/java/org/openjdk/skara/ini/INITests.java
Original file line number Diff line number Diff line change
Expand Up @@ -193,4 +193,43 @@ void testContains() {
assertTrue(ini.contains("project"));
assertFalse(ini.contains("bugs"));
}

@Test
void testUpdatingValueInGlobalSection() {
var lines = List.of(
"foo=bar",
"foo=baz"
);
var ini = INI.parse(lines);
assertEquals("baz", ini.get("foo").asString());
}

@Test
void testUpdatingValueInSection() {
var lines = List.of(
"[checks]",
" commits = reviews",
"",
"[checks]",
" commits = none"
);
var ini = INI.parse(lines);
assertEquals("none", ini.section("checks").get("commits").asString());
}

@Test
void testUpdatingValueInSubsection() {
var lines = List.of(
"[checks]",
" commits = reviews",
"",
"[checks \"reviews\"]",
" merge = ignore",
"",
"[checks \"reviews\"]",
" merge = check"
);
var ini = INI.parse(lines);
assertEquals("check", ini.section("checks").subsection("reviews").get("merge").asString());
}
}

1 comment on commit 0c63816

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

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

Please sign in to comment.