SYS-REQ-009, SYS-REQ-110, SYS-REQ-111
(keys []string, setValue []byte, comma, object bool)
| 764 | |
| 765 | // SYS-REQ-009, SYS-REQ-110, SYS-REQ-111 |
| 766 | func createInsertComponent(keys []string, setValue []byte, comma, object bool) []byte { |
| 767 | // guard: empty key component — not an array index. |
| 768 | isIndex := len(keys[0]) > 0 && string(keys[0][0]) == "[" |
| 769 | offset := 0 |
| 770 | lk := calcAllocateSpace(keys, setValue, comma, object) |
| 771 | buffer := make([]byte, lk, lk) |
| 772 | if comma { |
| 773 | offset += WriteToBuffer(buffer[offset:], ",") |
| 774 | } |
| 775 | if isIndex && !comma { |
| 776 | offset += WriteToBuffer(buffer[offset:], "[") |
| 777 | } else { |
| 778 | if object { |
| 779 | offset += WriteToBuffer(buffer[offset:], "{") |
| 780 | } |
| 781 | if !isIndex { |
| 782 | offset += WriteToBuffer(buffer[offset:], "\"") |
| 783 | offset += WriteToBuffer(buffer[offset:], keys[0]) |
| 784 | offset += WriteToBuffer(buffer[offset:], "\":") |
| 785 | } |
| 786 | } |
| 787 | |
| 788 | for i := 1; i < len(keys); i++ { |
| 789 | // guard: empty key component — treat as object key, not array index. |
| 790 | if len(keys[i]) > 0 && string(keys[i][0]) == "[" { |
| 791 | offset += WriteToBuffer(buffer[offset:], "[") |
| 792 | } else { |
| 793 | offset += WriteToBuffer(buffer[offset:], "{\"") |
| 794 | offset += WriteToBuffer(buffer[offset:], keys[i]) |
| 795 | offset += WriteToBuffer(buffer[offset:], "\":") |
| 796 | } |
| 797 | } |
| 798 | offset += WriteToBuffer(buffer[offset:], string(setValue)) |
| 799 | for i := len(keys) - 1; i > 0; i-- { |
| 800 | // guard: empty key component — treat as object key, not array index. |
| 801 | if len(keys[i]) > 0 && string(keys[i][0]) == "[" { |
| 802 | offset += WriteToBuffer(buffer[offset:], "]") |
| 803 | } else { |
| 804 | offset += WriteToBuffer(buffer[offset:], "}") |
| 805 | } |
| 806 | } |
| 807 | if isIndex && !comma { |
| 808 | offset += WriteToBuffer(buffer[offset:], "]") |
| 809 | } |
| 810 | if object && !isIndex { |
| 811 | offset += WriteToBuffer(buffer[offset:], "}") |
| 812 | } |
| 813 | return buffer |
| 814 | } |
| 815 | |
| 816 | // SYS-REQ-009, SYS-REQ-111 |
| 817 | func calcAllocateSpace(keys []string, setValue []byte, comma, object bool) int { |