SYS-REQ-009, SYS-REQ-110, SYS-REQ-111
(keys []string, setValue []byte, comma, object bool)
| 784 | |
| 785 | // SYS-REQ-009, SYS-REQ-110, SYS-REQ-111 |
| 786 | func createInsertComponent(keys []string, setValue []byte, comma, object bool) []byte { |
| 787 | // guard: empty key component — not an array index. |
| 788 | isIndex := len(keys[0]) > 0 && string(keys[0][0]) == "[" |
| 789 | offset := 0 |
| 790 | lk := calcAllocateSpace(keys, setValue, comma, object) |
| 791 | buffer := make([]byte, lk, lk) |
| 792 | if comma { |
| 793 | offset += WriteToBuffer(buffer[offset:], ",") |
| 794 | } |
| 795 | if isIndex && !comma { |
| 796 | offset += WriteToBuffer(buffer[offset:], "[") |
| 797 | } else { |
| 798 | if object { |
| 799 | offset += WriteToBuffer(buffer[offset:], "{") |
| 800 | } |
| 801 | if !isIndex { |
| 802 | offset += WriteToBuffer(buffer[offset:], "\"") |
| 803 | offset += WriteToBuffer(buffer[offset:], keys[0]) |
| 804 | offset += WriteToBuffer(buffer[offset:], "\":") |
| 805 | } |
| 806 | } |
| 807 | |
| 808 | for i := 1; i < len(keys); i++ { |
| 809 | // guard: empty key component — treat as object key, not array index. |
| 810 | if len(keys[i]) > 0 && string(keys[i][0]) == "[" { |
| 811 | offset += WriteToBuffer(buffer[offset:], "[") |
| 812 | } else { |
| 813 | offset += WriteToBuffer(buffer[offset:], "{\"") |
| 814 | offset += WriteToBuffer(buffer[offset:], keys[i]) |
| 815 | offset += WriteToBuffer(buffer[offset:], "\":") |
| 816 | } |
| 817 | } |
| 818 | offset += WriteToBuffer(buffer[offset:], string(setValue)) |
| 819 | for i := len(keys) - 1; i > 0; i-- { |
| 820 | // guard: empty key component — treat as object key, not array index. |
| 821 | if len(keys[i]) > 0 && string(keys[i][0]) == "[" { |
| 822 | offset += WriteToBuffer(buffer[offset:], "]") |
| 823 | } else { |
| 824 | offset += WriteToBuffer(buffer[offset:], "}") |
| 825 | } |
| 826 | } |
| 827 | if isIndex && !comma { |
| 828 | offset += WriteToBuffer(buffer[offset:], "]") |
| 829 | } |
| 830 | if object && !isIndex { |
| 831 | offset += WriteToBuffer(buffer[offset:], "}") |
| 832 | } |
| 833 | return buffer |
| 834 | } |
| 835 | |
| 836 | // SYS-REQ-009, SYS-REQ-111 |
| 837 | func calcAllocateSpace(keys []string, setValue []byte, comma, object bool) int { |