Skip to content

Commit

Permalink
refactor: parse subscript with helper
Browse files Browse the repository at this point in the history
  • Loading branch information
alexzielenski committed Jan 25, 2024
1 parent f4ff1f8 commit 11a9e91
Showing 1 changed file with 3 additions and 21 deletions.
24 changes: 3 additions & 21 deletions pkg/generators/markers.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,30 +360,13 @@ func extractCommentTags(marker string, lines []string) (map[string]string, error
}

out[key] = value
lastKey = key

{
// Lint arary indices if they exist
subscriptIdx := strings.Index(key, "[")
if subscriptIdx == -1 {
continue
}

arrayPath := key[:subscriptIdx]
subscript := strings.Split(key[subscriptIdx+1:], "]")[0]

if len(subscript) == 0 {
lintErrors = append(lintErrors, fmt.Errorf("error parsing %v: empty subscript not allowed", line))
continue
}

index, err := strconv.Atoi(subscript)

if arrayPath, index, hasSubscript, err := extractArraySubscript(key); hasSubscript {
// If index is non-zero, check that that previous line was for the same
// key and either the same or previous index
if err != nil {
lintErrors = append(lintErrors, fmt.Errorf("error parsing %v: expected integer index in key '%v'", line, line[:subscriptIdx]))
} else if index < 0 {
lintErrors = append(lintErrors, fmt.Errorf("error parsing %v: subscript '%v' is invalid. index must be positive", arrayPath, subscript))
lintErrors = append(lintErrors, fmt.Errorf("error parsing %v: expected integer index in key '%v'", line, key))
} else if previousArrayKey != arrayPath && index != 0 {
lintErrors = append(lintErrors, fmt.Errorf("error parsing %v: non-consecutive index %v for key '%v'", line, index, arrayPath))
} else if index != previousIndex+1 && index != previousIndex {
Expand All @@ -392,7 +375,6 @@ func extractCommentTags(marker string, lines []string) (map[string]string, error

lastIndex = index
lastArrayKey = arrayPath
lastKey = key
}
}

Expand Down

0 comments on commit 11a9e91

Please sign in to comment.