(yamlContent string, pathSegments []PathSegment)
| 124 | } |
| 125 | |
| 126 | func findPathInYAMLLines(yamlContent string, pathSegments []PathSegment) JSONPathLocation { |
| 127 | lines := strings.Split(yamlContent, "\n") |
| 128 | |
| 129 | // Start from the beginning |
| 130 | currentLevel := 0 |
| 131 | arrayContexts := make(map[int]int) // level -> current array index |
| 132 | |
| 133 | for lineNum, line := range lines { |
| 134 | lineNumber := lineNum + 1 // 1-based line numbers |
| 135 | trimmedLine := strings.TrimSpace(line) |
| 136 | |
| 137 | if trimmedLine == "" || strings.HasPrefix(trimmedLine, "#") { |
| 138 | continue |
| 139 | } |
| 140 | |
| 141 | // Calculate indentation level |
| 142 | lineLevel := (len(line) - len(strings.TrimLeft(line, " \t"))) / 2 |
| 143 | |
| 144 | // Check if this line matches our path |
| 145 | matches, column := matchesPathAtLevel(line, pathSegments, lineLevel, arrayContexts) |
| 146 | if matches { |
| 147 | return JSONPathLocation{Line: lineNumber, Column: column, Found: true} |
| 148 | } |
| 149 | |
| 150 | // Update array contexts for list items |
| 151 | if strings.HasPrefix(trimmedLine, "-") { |
| 152 | arrayContexts[lineLevel]++ |
| 153 | } else if lineLevel <= currentLevel { |
| 154 | // Reset array contexts for deeper levels when we move to a shallower level |
| 155 | for level := lineLevel + 1; level <= currentLevel; level++ { |
| 156 | delete(arrayContexts, level) |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | currentLevel = lineLevel |
| 161 | } |
| 162 | |
| 163 | return JSONPathLocation{Line: 1, Column: 1, Found: false} |
| 164 | } |
| 165 | |
| 166 | // matchesPathAtLevel checks if a line matches the target path at the current level |
| 167 | func matchesPathAtLevel(line string, pathSegments []PathSegment, level int, arrayContexts map[int]int) (bool, int) { |
no test coverage detected