MCPcopy Create free account
hub / github.com/cli/cli / setPath

Function setPath

script/ghmock/set.go:391–424  ·  view source on GitHub ↗
(node any, segments []string, val any, walked string)

Source from the content-addressed store, hash-verified

389}
390
391func setPath(node any, segments []string, val any, walked string) (any, error) {
392 if len(segments) == 0 {
393 return val, nil
394 }
395 seg := segments[0]
396 here := seg
397 if walked != "" {
398 here = walked + "." + seg
399 }
400 switch n := node.(type) {
401 case map[string]any:
402 child, err := setPath(n[seg], segments[1:], val, here)
403 if err != nil {
404 return nil, err
405 }
406 n[seg] = child
407 return n, nil
408 case []any:
409 i, err := strconv.Atoi(seg)
410 if err != nil || i < 0 || i >= len(n) {
411 return nil, fmt.Errorf("%s: %q is not an index of this %d-item array", where(walked), seg, len(n))
412 }
413 child, err := setPath(n[i], segments[1:], val, here)
414 if err != nil {
415 return nil, err
416 }
417 n[i] = child
418 return n, nil
419 case nil:
420 return setPath(map[string]any{}, segments, val, walked)
421 default:
422 return nil, fmt.Errorf("%s: cannot set %q inside a %s", where(walked), seg, jsonKind(n))
423 }
424}
425
426// where names a position in the body for error messages.
427func where(walked string) string {

Callers 1

setAtPathFunction · 0.85

Calls 3

whereFunction · 0.85
jsonKindFunction · 0.85
ErrorfMethod · 0.65

Tested by

no test coverage detected