BulkEncodeNextEntry encodes a JSON object using the template in the supplied contxt BULK_NOTEFORMAT_FLEX and BULK_NOTEFORMAT_NANOFLEX uint8 header containing BULKFLAGS uint16 header containing variableLengthOffset If BULKFLAGS is not NANO, int64 CombinedWhen (the merger of 'when' and 'whereWhen
(ctx context.Context, body map[string]interface{}, payload []byte, when int64, wherewhen int64, currentLocOLC string, noteID string, noOmitEmpty bool)
| 1115 | // If BULKFLAGS indicates it is present, [flagsLen] [flags] |
| 1116 | // If BULKFLAGS indicates it is present, [olclen] [olc] |
| 1117 | func (tmplContext *BulkTemplateContext) BulkEncodeNextEntry(ctx context.Context, body map[string]interface{}, payload []byte, when int64, wherewhen int64, currentLocOLC string, noteID string, noOmitEmpty bool) (output []byte, err error) { |
| 1118 | |
| 1119 | // If the OLC can be converted to an int64, do so. |
| 1120 | currentLocOLC64 := OLCToINT64(currentLocOLC) |
| 1121 | tmplContext.currentOLC64 = currentLocOLC64 |
| 1122 | |
| 1123 | // Exit if the payload is simply too large |
| 1124 | if tmplContext.NoteFormat == BulkNoteFormatOriginal { |
| 1125 | err = fmt.Errorf("format not supported") |
| 1126 | return |
| 1127 | } |
| 1128 | |
| 1129 | // Plug the noteID into the context for later substitution |
| 1130 | tmplContext.noteID = noteID |
| 1131 | |
| 1132 | // Prior to 2021-08-26, When was stored in nanoseconds, with the low order 1000000000 ALWAYS being 0. |
| 1133 | // Starting on that date, we changed the semantics to mean that the low order 1000000000 is 0, then |
| 1134 | // wherewhen is not supplied. Otherwise, it is the number of seconds prior to "when" that represents |
| 1135 | // the time when the location was measured, offset by 1. |
| 1136 | currentTimeSecs := uint32(time.Now().UTC().Unix()) |
| 1137 | relativeWhereWhenOffsetSecs := uint32(0) |
| 1138 | if wherewhen != 0 && uint32(wherewhen) <= currentTimeSecs { |
| 1139 | differenceSecs := currentTimeSecs - uint32(wherewhen) |
| 1140 | if differenceSecs < (1000000000 - 1) { |
| 1141 | relativeWhereWhenOffsetSecs = differenceSecs + 1 |
| 1142 | } |
| 1143 | } |
| 1144 | combinedWhen := uint64((uint64(currentTimeSecs) * 1000000000) + uint64(relativeWhereWhenOffsetSecs)) |
| 1145 | |
| 1146 | // For the new formats, append a flags byte as the very first thing. We put this at the top |
| 1147 | // because we will later come back and modify it, and it's convenient to be at the start. |
| 1148 | binHeader := uint8(0) |
| 1149 | if noOmitEmpty { |
| 1150 | binHeader = bulkflagNoOmitEmpty |
| 1151 | } |
| 1152 | variableLengthOffset := uint16(0) |
| 1153 | err = tmplContext.binAppendUint8(ctx, binHeader) // BULKFLAGS |
| 1154 | if err == nil { |
| 1155 | err = tmplContext.binAppendUint16(ctx, variableLengthOffset) |
| 1156 | } |
| 1157 | |
| 1158 | // Append the time and location so long as we're not operating in nano mode |
| 1159 | if tmplContext.NoteFormat != BulkNoteFormatFlexNano { |
| 1160 | if err == nil { |
| 1161 | err = tmplContext.binAppendUint64(ctx, combinedWhen) |
| 1162 | } |
| 1163 | if err == nil { |
| 1164 | err = tmplContext.binAppendInt64(ctx, currentLocOLC64) |
| 1165 | } |
| 1166 | } |
| 1167 | if err != nil { |
| 1168 | return |
| 1169 | } |
| 1170 | |
| 1171 | // Parse the template |
| 1172 | var p fastjson.Parser |
| 1173 | var template *fastjson.Value |
| 1174 | template, err = p.Parse(tmplContext.Template) |