Skip to content

Commit 6c6dee3

Browse files
Refactor ValueDecoder to streamline parsing logic and improve error handling
- Simplified the validation of multiple primitives at root level by reducing redundant checks. - Introduced a new method for processing lines in tabular arrays, enhancing readability and maintainability. - Improved handling of blank lines and termination conditions in both tabular and list arrays. - Updated comments for clarity and better understanding of the parsing flow.
1 parent 6345301 commit 6c6dee3

File tree

1 file changed

+54
-52
lines changed

1 file changed

+54
-52
lines changed

src/main/java/com/felipestanzani/jtoon/decoder/ValueDecoder.java

Lines changed: 54 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -234,19 +234,16 @@ private Object parseBareScalarValue(String content, int depth) {
234234
* Validates that there are no multiple primitives at root level in strict mode.
235235
*/
236236
private void validateNoMultiplePrimitivesAtRoot() {
237-
// Skip blank lines and check for more content at root level
238-
while (currentLine < lines.length) {
239-
String nextLine = lines[currentLine];
240-
if (isBlankLine(nextLine)) {
241-
currentLine++;
242-
continue;
243-
}
244-
int nextDepth = getDepth(nextLine);
237+
int lineIndex = currentLine;
238+
while (lineIndex < lines.length && isBlankLine(lines[lineIndex])) {
239+
lineIndex++;
240+
}
241+
if (lineIndex < lines.length) {
242+
int nextDepth = getDepth(lines[lineIndex]);
245243
if (nextDepth == 0) {
246244
throw new IllegalArgumentException(
247-
"Multiple primitives at root depth in strict mode at line " + (currentLine + 1));
245+
"Multiple primitives at root depth in strict mode at line " + (lineIndex + 1));
248246
}
249-
break;
250247
}
251248
}
252249

@@ -387,29 +384,38 @@ private List<Object> parseTabularArray(String header, int depth, String arrayDel
387384
currentLine++;
388385

389386
while (currentLine < lines.length) {
390-
String line = lines[currentLine];
391-
392-
if (isBlankLine(line)) {
393-
if (handleBlankLineInTabularArray(depth)) {
394-
break;
395-
}
396-
continue;
397-
}
398-
399-
int lineDepth = getDepth(line);
400-
if (shouldTerminateTabularArray(line, lineDepth, depth)) {
387+
if (!processTabularArrayLine(depth, keys, arrayDelimiter, result)) {
401388
break;
402389
}
403-
404-
if (processTabularRow(line, lineDepth, depth, keys, arrayDelimiter, result)) {
405-
currentLine++;
406-
}
407390
}
408391

409392
validateArrayLength(header, result.size());
410393
return result;
411394
}
412395

396+
/**
397+
* Processes a single line in a tabular array.
398+
* Returns true if parsing should continue, false if array should terminate.
399+
*/
400+
private boolean processTabularArrayLine(int depth, List<String> keys, String arrayDelimiter,
401+
List<Object> result) {
402+
String line = lines[currentLine];
403+
404+
if (isBlankLine(line)) {
405+
return !handleBlankLineInTabularArray(depth);
406+
}
407+
408+
int lineDepth = getDepth(line);
409+
if (shouldTerminateTabularArray(line, lineDepth, depth)) {
410+
return false;
411+
}
412+
413+
if (processTabularRow(line, lineDepth, depth, keys, arrayDelimiter, result)) {
414+
currentLine++;
415+
}
416+
return true;
417+
}
418+
413419
/**
414420
* Handles blank line processing in tabular array.
415421
* Returns true if array should terminate, false if line should be skipped.
@@ -499,22 +505,22 @@ private List<Object> parseListArray(int depth, String header) {
499505
List<Object> result = new ArrayList<>();
500506
currentLine++;
501507

502-
while (currentLine < lines.length) {
508+
boolean shouldContinue = true;
509+
while (shouldContinue && currentLine < lines.length) {
503510
String line = lines[currentLine];
504511

505512
if (isBlankLine(line)) {
506513
if (handleBlankLineInListArray(depth)) {
507-
break;
514+
shouldContinue = false;
515+
}
516+
} else {
517+
int lineDepth = getDepth(line);
518+
if (shouldTerminateListArray(lineDepth, depth, line)) {
519+
shouldContinue = false;
520+
} else {
521+
processListArrayItem(line, lineDepth, depth, result);
508522
}
509-
continue;
510-
}
511-
512-
int lineDepth = getDepth(line);
513-
if (shouldTerminateListArray(lineDepth, depth, line)) {
514-
break;
515523
}
516-
517-
processListArrayItem(line, lineDepth, depth, result);
518524
}
519525

520526
if (header != null) {
@@ -822,24 +828,26 @@ private void parseListItemFields(Map<String, Object> item, int depth) {
822828
int lineDepth = getDepth(line);
823829

824830
if (lineDepth < depth + 2) {
825-
break;
831+
return;
826832
}
827833

828834
if (lineDepth == depth + 2) {
829835
String fieldContent = line.substring((depth + 2) * options.indent());
830836

831837
// Try to parse as keyed array first, then as key-value pair
832-
if (parseKeyedArrayField(fieldContent, item, depth)) {
833-
continue;
834-
}
835-
836-
if (parseKeyValueField(fieldContent, item, depth)) {
837-
continue;
838+
boolean wasParsed = parseKeyedArrayField(fieldContent, item, depth);
839+
if (!wasParsed) {
840+
wasParsed = parseKeyValueField(fieldContent, item, depth);
838841
}
839842

840843
// If neither pattern matched, skip this line to avoid infinite loop
844+
if (!wasParsed) {
845+
currentLine++;
846+
}
847+
} else {
848+
// lineDepth > depth + 2, skip this line
849+
currentLine++;
841850
}
842-
currentLine++;
843851
}
844852
}
845853

@@ -895,17 +903,11 @@ private void validateKeysDelimiter(String keysStr, String expectedDelimiter) {
895903
char c = keysStr.charAt(i);
896904
if (escaped) {
897905
escaped = false;
898-
continue;
899-
}
900-
if (c == '\\') {
906+
} else if (c == '\\') {
901907
escaped = true;
902-
continue;
903-
}
904-
if (c == '"') {
908+
} else if (c == '"') {
905909
inQuotes = !inQuotes;
906-
continue;
907-
}
908-
if (!inQuotes) {
910+
} else if (!inQuotes) {
909911
checkDelimiterMismatch(expectedChar, c);
910912
}
911913
}

0 commit comments

Comments
 (0)