SYS-REQ-009, SYS-REQ-110, SYS-REQ-111
(keys []string, setValue []byte, comma, object bool)
| 1053 | |
| 1054 | // SYS-REQ-009, SYS-REQ-110, SYS-REQ-111 |
| 1055 | func createInsertComponent(keys []string, setValue []byte, comma, object bool) []byte { |
| 1056 | // guard: empty key component — not an array index. |
| 1057 | isIndex := len(keys[0]) > 0 && string(keys[0][0]) == "[" |
| 1058 | offset := 0 |
| 1059 | lk := calcAllocateSpace(keys, setValue, comma, object) |
| 1060 | buffer := make([]byte, lk, lk) |
| 1061 | if comma { |
| 1062 | offset += WriteToBuffer(buffer[offset:], ",") |
| 1063 | } |
| 1064 | if isIndex && !comma { |
| 1065 | offset += WriteToBuffer(buffer[offset:], "[") |
| 1066 | } else { |
| 1067 | if object { |
| 1068 | offset += WriteToBuffer(buffer[offset:], "{") |
| 1069 | } |
| 1070 | if !isIndex { |
| 1071 | offset += WriteToBuffer(buffer[offset:], "\"") |
| 1072 | offset += WriteToBuffer(buffer[offset:], keys[0]) |
| 1073 | offset += WriteToBuffer(buffer[offset:], "\":") |
| 1074 | } |
| 1075 | } |
| 1076 | |
| 1077 | for i := 1; i < len(keys); i++ { |
| 1078 | // guard: empty key component — treat as object key, not array index. |
| 1079 | if len(keys[i]) > 0 && string(keys[i][0]) == "[" { |
| 1080 | offset += WriteToBuffer(buffer[offset:], "[") |
| 1081 | } else { |
| 1082 | offset += WriteToBuffer(buffer[offset:], "{\"") |
| 1083 | offset += WriteToBuffer(buffer[offset:], keys[i]) |
| 1084 | offset += WriteToBuffer(buffer[offset:], "\":") |
| 1085 | } |
| 1086 | } |
| 1087 | offset += copy(buffer[offset:], setValue) |
| 1088 | for i := len(keys) - 1; i > 0; i-- { |
| 1089 | // guard: empty key component — treat as object key, not array index. |
| 1090 | if len(keys[i]) > 0 && string(keys[i][0]) == "[" { |
| 1091 | offset += WriteToBuffer(buffer[offset:], "]") |
| 1092 | } else { |
| 1093 | offset += WriteToBuffer(buffer[offset:], "}") |
| 1094 | } |
| 1095 | } |
| 1096 | if isIndex && !comma { |
| 1097 | offset += WriteToBuffer(buffer[offset:], "]") |
| 1098 | } |
| 1099 | if object && !isIndex { |
| 1100 | offset += WriteToBuffer(buffer[offset:], "}") |
| 1101 | } |
| 1102 | return buffer |
| 1103 | } |
| 1104 | |
| 1105 | // SYS-REQ-009, SYS-REQ-111 |
| 1106 | func calcAllocateSpace(keys []string, setValue []byte, comma, object bool) int { |