Skip to content

Commit e3006a5

Browse files
authored
Merge pull request #345 from lutovich/1.6-temporal-getters
Flatten all temporal types
2 parents b20536f + f32f677 commit e3006a5

File tree

8 files changed

+587
-157
lines changed

8 files changed

+587
-157
lines changed

src/v1/internal/packstream-v2.js

+76-16
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,22 @@ import {
2424
DateTimeWithZoneId,
2525
DateTimeWithZoneOffset,
2626
Duration,
27-
Time,
2827
isDate,
2928
isDateTimeWithZoneId,
3029
isDateTimeWithZoneOffset,
3130
isDuration,
3231
isLocalDateTime,
3332
isLocalTime,
34-
isTime
33+
isTime,
34+
Time
3535
} from '../temporal-types';
3636
import {int} from '../integer';
3737
import {
3838
dateToEpochDay,
39-
localDateTimeToEpochSecond,
40-
localTimeToNanoOfDay,
4139
epochDayToDate,
4240
epochSecondAndNanoToLocalDateTime,
41+
localDateTimeToEpochSecond,
42+
localTimeToNanoOfDay,
4343
nanoOfDayToLocalTime
4444
} from '../internal/temporal-util';
4545

@@ -143,6 +143,12 @@ export class Unpacker extends v1.Unpacker {
143143
}
144144
}
145145

146+
/**
147+
* Pack given 2D or 3D point.
148+
* @param {Point} point the point value to pack.
149+
* @param {Packer} packer the packer to use.
150+
* @param {function} onError the error callback.
151+
*/
146152
function packPoint(point, packer, onError) {
147153
const is2DPoint = point.z === null || point.z === undefined;
148154
if (is2DPoint) {
@@ -152,6 +158,12 @@ function packPoint(point, packer, onError) {
152158
}
153159
}
154160

161+
/**
162+
* Pack given 2D point.
163+
* @param {Point} point the point value to pack.
164+
* @param {Packer} packer the packer to use.
165+
* @param {function} onError the error callback.
166+
*/
155167
function packPoint2D(point, packer, onError) {
156168
const packableStructFields = [
157169
packer.packable(int(point.srid), onError),
@@ -161,6 +173,12 @@ function packPoint2D(point, packer, onError) {
161173
packer.packStruct(POINT_2D, packableStructFields, onError);
162174
}
163175

176+
/**
177+
* Pack given 3D point.
178+
* @param {Point} point the point value to pack.
179+
* @param {Packer} packer the packer to use.
180+
* @param {function} onError the error callback.
181+
*/
164182
function packPoint3D(point, packer, onError) {
165183
const packableStructFields = [
166184
packer.packable(int(point.srid), onError),
@@ -171,6 +189,13 @@ function packPoint3D(point, packer, onError) {
171189
packer.packStruct(POINT_3D, packableStructFields, onError);
172190
}
173191

192+
/**
193+
* Unpack 2D point value using the given unpacker.
194+
* @param {Unpacker} unpacker the unpacker to use.
195+
* @param {number} structSize the retrieved struct size.
196+
* @param {BaseBuffer} buffer the buffer to unpack from.
197+
* @return {Point} the unpacked 2D point value.
198+
*/
174199
function unpackPoint2D(unpacker, structSize, buffer) {
175200
unpacker._verifyStructSize('Point2D', POINT_2D_STRUCT_SIZE, structSize);
176201

@@ -182,6 +207,13 @@ function unpackPoint2D(unpacker, structSize, buffer) {
182207
);
183208
}
184209

210+
/**
211+
* Unpack 3D point value using the given unpacker.
212+
* @param {Unpacker} unpacker the unpacker to use.
213+
* @param {number} structSize the retrieved struct size.
214+
* @param {BaseBuffer} buffer the buffer to unpack from.
215+
* @return {Point} the unpacked 3D point value.
216+
*/
185217
function unpackPoint3D(unpacker, structSize, buffer) {
186218
unpacker._verifyStructSize('Point3D', POINT_3D_STRUCT_SIZE, structSize);
187219

@@ -193,6 +225,12 @@ function unpackPoint3D(unpacker, structSize, buffer) {
193225
);
194226
}
195227

228+
/**
229+
* Pack given duration.
230+
* @param {Duration} value the duration value to pack.
231+
* @param {Packer} packer the packer to use.
232+
* @param {function} onError the error callback.
233+
*/
196234
function packDuration(value, packer, onError) {
197235
const months = int(value.months);
198236
const days = int(value.days);
@@ -208,6 +246,13 @@ function packDuration(value, packer, onError) {
208246
packer.packStruct(DURATION, packableStructFields, onError);
209247
}
210248

249+
/**
250+
* Unpack duration value using the given unpacker.
251+
* @param {Unpacker} unpacker the unpacker to use.
252+
* @param {number} structSize the retrieved struct size.
253+
* @param {BaseBuffer} buffer the buffer to unpack from.
254+
* @return {Duration} the unpacked duration value.
255+
*/
211256
function unpackDuration(unpacker, structSize, buffer) {
212257
unpacker._verifyStructSize('Duration', DURATION_STRUCT_SIZE, structSize);
213258

@@ -219,15 +264,28 @@ function unpackDuration(unpacker, structSize, buffer) {
219264
return new Duration(months, days, seconds, nanoseconds);
220265
}
221266

267+
/**
268+
* Pack given local time.
269+
* @param {LocalTime} value the local time value to pack.
270+
* @param {Packer} packer the packer to use.
271+
* @param {function} onError the error callback.
272+
*/
222273
function packLocalTime(value, packer, onError) {
223-
const nanoOfDay = localTimeToNanoOfDay(value);
274+
const nanoOfDay = localTimeToNanoOfDay(value.hour, value.minute, value.second, value.nanosecond);
224275

225276
const packableStructFields = [
226277
packer.packable(nanoOfDay, onError)
227278
];
228279
packer.packStruct(LOCAL_TIME, packableStructFields, onError);
229280
}
230281

282+
/**
283+
* Unpack local time value using the given unpacker.
284+
* @param {Unpacker} unpacker the unpacker to use.
285+
* @param {number} structSize the retrieved struct size.
286+
* @param {BaseBuffer} buffer the buffer to unpack from.
287+
* @return {LocalTime} the unpacked local time value.
288+
*/
231289
function unpackLocalTime(unpacker, structSize, buffer) {
232290
unpacker._verifyStructSize('LocalTime', LOCAL_TIME_STRUCT_SIZE, structSize);
233291

@@ -242,7 +300,7 @@ function unpackLocalTime(unpacker, structSize, buffer) {
242300
* @param {function} onError the error callback.
243301
*/
244302
function packTime(value, packer, onError) {
245-
const nanoOfDay = localTimeToNanoOfDay(value.localTime);
303+
const nanoOfDay = localTimeToNanoOfDay(value.hour, value.minute, value.second, value.nanosecond);
246304
const offsetSeconds = int(value.offsetSeconds);
247305

248306
const packableStructFields = [
@@ -266,7 +324,7 @@ function unpackTime(unpacker, structSize, buffer) {
266324
const offsetSeconds = unpacker.unpack(buffer);
267325

268326
const localTime = nanoOfDayToLocalTime(nanoOfDay);
269-
return new Time(localTime, offsetSeconds);
327+
return new Time(localTime.hour, localTime.minute, localTime.second, localTime.nanosecond, offsetSeconds);
270328
}
271329

272330
/**
@@ -276,7 +334,7 @@ function unpackTime(unpacker, structSize, buffer) {
276334
* @param {function} onError the error callback.
277335
*/
278336
function packDate(value, packer, onError) {
279-
const epochDay = dateToEpochDay(value);
337+
const epochDay = dateToEpochDay(value.year, value.month, value.day);
280338

281339
const packableStructFields = [
282340
packer.packable(epochDay, onError)
@@ -305,8 +363,8 @@ function unpackDate(unpacker, structSize, buffer) {
305363
* @param {function} onError the error callback.
306364
*/
307365
function packLocalDateTime(value, packer, onError) {
308-
const epochSecond = localDateTimeToEpochSecond(value);
309-
const nano = int(value.localTime.nanosecond);
366+
const epochSecond = localDateTimeToEpochSecond(value.year, value.month, value.day, value.hour, value.minute, value.second, value.nanosecond);
367+
const nano = int(value.nanosecond);
310368

311369
const packableStructFields = [
312370
packer.packable(epochSecond, onError),
@@ -338,8 +396,8 @@ function unpackLocalDateTime(unpacker, structSize, buffer) {
338396
* @param {function} onError the error callback.
339397
*/
340398
function packDateTimeWithZoneOffset(value, packer, onError) {
341-
const epochSecond = localDateTimeToEpochSecond(value.localDateTime);
342-
const nano = int(value.localDateTime.localTime.nanosecond);
399+
const epochSecond = localDateTimeToEpochSecond(value.year, value.month, value.day, value.hour, value.minute, value.second, value.nanosecond);
400+
const nano = int(value.nanosecond);
343401
const offsetSeconds = int(value.offsetSeconds);
344402

345403
const packableStructFields = [
@@ -365,7 +423,8 @@ function unpackDateTimeWithZoneOffset(unpacker, structSize, buffer) {
365423
const offsetSeconds = unpacker.unpack(buffer);
366424

367425
const localDateTime = epochSecondAndNanoToLocalDateTime(epochSecond, nano);
368-
return new DateTimeWithZoneOffset(localDateTime, offsetSeconds);
426+
return new DateTimeWithZoneOffset(localDateTime.year, localDateTime.month, localDateTime.day, localDateTime.hour,
427+
localDateTime.minute, localDateTime.second, localDateTime.nanosecond, offsetSeconds);
369428
}
370429

371430
/**
@@ -375,8 +434,8 @@ function unpackDateTimeWithZoneOffset(unpacker, structSize, buffer) {
375434
* @param {function} onError the error callback.
376435
*/
377436
function packDateTimeWithZoneId(value, packer, onError) {
378-
const epochSecond = localDateTimeToEpochSecond(value.localDateTime);
379-
const nano = int(value.localDateTime.localTime.nanosecond);
437+
const epochSecond = localDateTimeToEpochSecond(value.year, value.month, value.day, value.hour, value.minute, value.second, value.nanosecond);
438+
const nano = int(value.nanosecond);
380439
const zoneId = value.zoneId;
381440

382441
const packableStructFields = [
@@ -402,5 +461,6 @@ function unpackDateTimeWithZoneId(unpacker, structSize, buffer) {
402461
const zoneId = unpacker.unpack(buffer);
403462

404463
const localDateTime = epochSecondAndNanoToLocalDateTime(epochSecond, nano);
405-
return new DateTimeWithZoneId(localDateTime, zoneId);
464+
return new DateTimeWithZoneId(localDateTime.year, localDateTime.month, localDateTime.day, localDateTime.hour,
465+
localDateTime.minute, localDateTime.second, localDateTime.nanosecond, zoneId);
406466
}

0 commit comments

Comments
 (0)