adjustLineNumbersInFormattedError adjusts line numbers in yaml.FormatError() output by adding the specified offset to all line numbers
(formatted string, offset int)
| 157 | // adjustLineNumbersInFormattedError adjusts line numbers in yaml.FormatError() output |
| 158 | // by adding the specified offset to all line numbers |
| 159 | func adjustLineNumbersInFormattedError(formatted string, offset int) string { |
| 160 | if offset == 0 { |
| 161 | return formatted |
| 162 | } |
| 163 | |
| 164 | yamlErrorLog.Printf("Adjusting YAML error line numbers with offset: +%d", offset) |
| 165 | |
| 166 | // Pattern to match line numbers in the format: |
| 167 | // [line:col] at the start |
| 168 | // " 1 | content" in the source context |
| 169 | // "> 2 | content" with the error marker |
| 170 | |
| 171 | // Adjust [line:col] format at the start |
| 172 | formatted = lineColPatternParser.ReplaceAllStringFunc(formatted, func(match string) string { |
| 173 | var line, col int |
| 174 | if _, err := fmt.Sscanf(match, "[%d:%d]", &line, &col); err == nil { |
| 175 | return fmt.Sprintf("[%d:%d]", line+offset, col) |
| 176 | } |
| 177 | return match |
| 178 | }) |
| 179 | |
| 180 | // Adjust line numbers in "already defined at [line:col]" references |
| 181 | formatted = definedAtPattern.ReplaceAllStringFunc(formatted, func(match string) string { |
| 182 | var line, col int |
| 183 | if _, err := fmt.Sscanf(match, "already defined at [%d:%d]", &line, &col); err == nil { |
| 184 | return fmt.Sprintf("already defined at [%d:%d]", line+offset, col) |
| 185 | } |
| 186 | return match |
| 187 | }) |
| 188 | |
| 189 | // Adjust line numbers in source context lines (both " 1 |" and "> 1 |" formats) |
| 190 | formatted = sourceLinePattern.ReplaceAllStringFunc(formatted, func(match string) string { |
| 191 | var line int |
| 192 | if strings.Contains(match, ">") { |
| 193 | if _, err := fmt.Sscanf(match, "> %d |", &line); err == nil { |
| 194 | return fmt.Sprintf(">%3d |", line+offset) |
| 195 | } |
| 196 | } else { |
| 197 | if _, err := fmt.Sscanf(match, "%d |", &line); err == nil { |
| 198 | return fmt.Sprintf("%4d |", line+offset) |
| 199 | } |
| 200 | } |
| 201 | // If we can't parse it, extract parts manually |
| 202 | parts := strings.Split(match, "|") |
| 203 | if len(parts) == 2 { |
| 204 | prefix := strings.TrimRight(parts[0], "0123456789") |
| 205 | lineStr := strings.Trim(parts[0][len(prefix):], " ") |
| 206 | if n, err := fmt.Sscanf(lineStr, "%d", &line); err == nil && n == 1 { |
| 207 | if strings.Contains(prefix, ">") { |
| 208 | return fmt.Sprintf(">%3d |", line+offset) |
| 209 | } |
| 210 | return fmt.Sprintf("%4d |", line+offset) |
| 211 | } |
| 212 | } |
| 213 | return match |
| 214 | }) |
| 215 | |
| 216 | return formatted |
no test coverage detected