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

Function matchesPathAtLevel

pkg/parser/json_path_locator.go:167–204  ·  view source on GitHub ↗

matchesPathAtLevel checks if a line matches the target path at the current level

(line string, pathSegments []PathSegment, level int, arrayContexts map[int]int)

Source from the content-addressed store, hash-verified

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) {
168 if len(pathSegments) == 0 {
169 return false, 0
170 }
171
172 trimmedLine := strings.TrimSpace(line)
173
174 // For now, implement a simple key matching approach
175 // This is a simplified version - in a full implementation we'd need to track
176 // the complete path context as we traverse the YAML
177
178 if level < len(pathSegments) {
179 segment := pathSegments[level]
180
181 switch segment.Type {
182 case "key":
183 // Look for "key:" pattern
184 keyPattern := regexp.MustCompile(`^` + regexp.QuoteMeta(segment.Value) + `\s*:`)
185 if keyPattern.MatchString(trimmedLine) {
186 // Found the key - return position after the colon
187 colonIndex := strings.Index(line, ":")
188 if colonIndex != -1 {
189 return level == len(pathSegments)-1, colonIndex + 2
190 }
191 }
192 case "index":
193 // For array elements, check if this is a list item at the right index
194 if strings.HasPrefix(trimmedLine, "-") {
195 currentIndex := arrayContexts[level]
196 if currentIndex == segment.Index {
197 return level == len(pathSegments)-1, strings.Index(line, "-") + 2
198 }
199 }
200 }
201 }
202
203 return false, 0
204}
205
206// parseJSONPath parses a JSON path string into segments
207func parseJSONPath(path string) []PathSegment {

Callers 1

findPathInYAMLLinesFunction · 0.85

Calls 1

MustCompileMethod · 0.80

Tested by

no test coverage detected