MCPcopy Create free account
hub / github.com/github/gh-aw / findPathInYAMLLines

Function findPathInYAMLLines

pkg/parser/json_path_locator.go:126–164  ·  view source on GitHub ↗
(yamlContent string, pathSegments []PathSegment)

Source from the content-addressed store, hash-verified

124}
125
126func 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
167func matchesPathAtLevel(line string, pathSegments []PathSegment, level int, arrayContexts map[int]int) (bool, int) {

Callers 1

LocateJSONPathInYAMLFunction · 0.85

Calls 1

matchesPathAtLevelFunction · 0.85

Tested by

no test coverage detected