MCPcopy
hub / github.com/tidwall/sjson / setComplexPath

Function setComplexPath

sjson.go:576–624  ·  view source on GitHub ↗
(jstr, path, raw string, stringify bool)

Source from the content-addressed store, hash-verified

574}
575
576func setComplexPath(jstr, path, raw string, stringify bool) ([]byte, error) {
577 res := gjson.Get(jstr, path)
578 if !res.Exists() || !(res.Index != 0 || len(res.Indexes) != 0) {
579 return []byte(jstr), errNoChange
580 }
581 if res.Index != 0 {
582 njson := []byte(jstr[:res.Index])
583 if stringify {
584 njson = appendStringify(njson, raw)
585 } else {
586 njson = append(njson, raw...)
587 }
588 njson = append(njson, jstr[res.Index+len(res.Raw):]...)
589 jstr = string(njson)
590 }
591 if len(res.Indexes) > 0 {
592 type val struct {
593 index int
594 res gjson.Result
595 }
596 vals := make([]val, 0, len(res.Indexes))
597 res.ForEach(func(_, vres gjson.Result) bool {
598 vals = append(vals, val{res: vres})
599 return true
600 })
601 if len(res.Indexes) != len(vals) {
602 return []byte(jstr), errNoChange
603 }
604 for i := 0; i < len(res.Indexes); i++ {
605 vals[i].index = res.Indexes[i]
606 }
607 sort.SliceStable(vals, func(i, j int) bool {
608 return vals[i].index > vals[j].index
609 })
610 for _, val := range vals {
611 vres := val.res
612 index := val.index
613 njson := []byte(jstr[:index])
614 if stringify {
615 njson = appendStringify(njson, raw)
616 } else {
617 njson = append(njson, raw...)
618 }
619 njson = append(njson, jstr[index+len(vres.Raw):]...)
620 jstr = string(njson)
621 }
622 }
623 return []byte(jstr), nil
624}
625
626// SetOptions sets a json value for the specified path with options.
627// A path is in dot syntax, such as "name.last" or "age".

Callers 1

setFunction · 0.85

Calls 1

appendStringifyFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…