MCPcopy
hub / github.com/buger/jsonparser / createInsertComponent

Function createInsertComponent

parser.go:634–679  ·  view source on GitHub ↗

SYS-REQ-009

(keys []string, setValue []byte, comma, object bool)

Source from the content-addressed store, hash-verified

632
633// SYS-REQ-009
634func createInsertComponent(keys []string, setValue []byte, comma, object bool) []byte {
635 isIndex := string(keys[0][0]) == "["
636 offset := 0
637 lk := calcAllocateSpace(keys, setValue, comma, object)
638 buffer := make([]byte, lk, lk)
639 if comma {
640 offset += WriteToBuffer(buffer[offset:], ",")
641 }
642 if isIndex && !comma {
643 offset += WriteToBuffer(buffer[offset:], "[")
644 } else {
645 if object {
646 offset += WriteToBuffer(buffer[offset:], "{")
647 }
648 if !isIndex {
649 offset += WriteToBuffer(buffer[offset:], "\"")
650 offset += WriteToBuffer(buffer[offset:], keys[0])
651 offset += WriteToBuffer(buffer[offset:], "\":")
652 }
653 }
654
655 for i := 1; i < len(keys); i++ {
656 if string(keys[i][0]) == "[" {
657 offset += WriteToBuffer(buffer[offset:], "[")
658 } else {
659 offset += WriteToBuffer(buffer[offset:], "{\"")
660 offset += WriteToBuffer(buffer[offset:], keys[i])
661 offset += WriteToBuffer(buffer[offset:], "\":")
662 }
663 }
664 offset += WriteToBuffer(buffer[offset:], string(setValue))
665 for i := len(keys) - 1; i > 0; i-- {
666 if string(keys[i][0]) == "[" {
667 offset += WriteToBuffer(buffer[offset:], "]")
668 } else {
669 offset += WriteToBuffer(buffer[offset:], "}")
670 }
671 }
672 if isIndex && !comma {
673 offset += WriteToBuffer(buffer[offset:], "]")
674 }
675 if object && !isIndex {
676 offset += WriteToBuffer(buffer[offset:], "}")
677 }
678 return buffer
679}
680
681// SYS-REQ-009
682func calcAllocateSpace(keys []string, setValue []byte, comma, object bool) int {

Callers 1

SetFunction · 0.85

Calls 2

calcAllocateSpaceFunction · 0.85
WriteToBufferFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…