Skip to content

Commit f98116b

Browse files
committed
fix resp2 floats
1 parent 8ca538a commit f98116b

6 files changed

Lines changed: 51 additions & 24 deletions

File tree

packages/client/lib/commands/GEORADIUSBYMEMBER_RO_WITH.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ describe('GEORADIUSBYMEMBER_RO WITH', () => {
3434

3535
assert.equal(reply.length, 1);
3636
assert.equal(reply[0].member, 'member');
37-
assert.equal(typeof reply[0].distance, 'string');
37+
assert.equal(typeof reply[0].distance, 'number');
3838
assert.equal(typeof reply[0].hash, 'number');
39-
assert.equal(typeof reply[0].coordinates?.longitude, 'string');
40-
assert.equal(typeof reply[0].coordinates?.latitude, 'string');
39+
assert.equal(typeof reply[0].coordinates?.longitude, 'number');
40+
assert.equal(typeof reply[0].coordinates?.latitude, 'number');
4141
}, {
4242
client: GLOBAL.SERVERS.OPEN,
4343
cluster: GLOBAL.CLUSTERS.OPEN

packages/client/lib/commands/GEORADIUSBYMEMBER_WITH.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ describe('GEORADIUSBYMEMBER WITH', () => {
3434

3535
assert.equal(reply.length, 1);
3636
assert.equal(reply[0].member, 'member');
37-
assert.equal(typeof reply[0].distance, 'string');
37+
assert.equal(typeof reply[0].distance, 'number');
3838
assert.equal(typeof reply[0].hash, 'number');
39-
assert.equal(typeof reply[0].coordinates!.longitude, 'string');
40-
assert.equal(typeof reply[0].coordinates!.latitude, 'string');
39+
assert.equal(typeof reply[0].coordinates!.longitude, 'number');
40+
assert.equal(typeof reply[0].coordinates!.latitude, 'number');
4141
}, {
4242
client: GLOBAL.SERVERS.OPEN,
4343
cluster: GLOBAL.CLUSTERS.OPEN

packages/client/lib/commands/GEORADIUS_RO_WITH.spec.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,23 @@ describe('GEORADIUS_RO WITH', () => {
1919
);
2020
});
2121

22+
it('transformReply should parse RESP2 floating-point strings', () => {
23+
const reply = GEORADIUS_RO_WITH.transformReply([
24+
['member', '0.5', 1, ['1.23', '4.56']]
25+
] as any, [
26+
GEO_REPLY_WITH.DISTANCE,
27+
GEO_REPLY_WITH.HASH,
28+
GEO_REPLY_WITH.COORDINATES
29+
]);
30+
31+
assert.equal(reply.length, 1);
32+
assert.equal(reply[0].member, 'member');
33+
assert.equal(reply[0].distance, 0.5);
34+
assert.equal(reply[0].hash, 1);
35+
assert.equal(reply[0].coordinates!.longitude, 1.23);
36+
assert.equal(reply[0].coordinates!.latitude, 4.56);
37+
});
38+
2239
testUtils.testAll('geoRadiusRoWith', async client => {
2340
const [, reply] = await Promise.all([
2441
client.geoAdd('key', {
@@ -38,10 +55,10 @@ describe('GEORADIUS_RO WITH', () => {
3855

3956
assert.equal(reply.length, 1);
4057
assert.equal(reply[0].member, 'member');
41-
assert.equal(typeof reply[0].distance, 'string');
58+
assert.equal(typeof reply[0].distance, 'number');
4259
assert.equal(typeof reply[0].hash, 'number');
43-
assert.equal(typeof reply[0].coordinates!.longitude, 'string');
44-
assert.equal(typeof reply[0].coordinates!.latitude, 'string');
60+
assert.equal(typeof reply[0].coordinates!.longitude, 'number');
61+
assert.equal(typeof reply[0].coordinates!.latitude, 'number');
4562
}, {
4663
client: GLOBAL.SERVERS.OPEN,
4764
cluster: GLOBAL.CLUSTERS.OPEN

packages/client/lib/commands/GEORADIUS_WITH.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ describe('GEORADIUS WITH', () => {
3838

3939
assert.equal(reply.length, 1);
4040
assert.equal(reply[0].member, 'member');
41-
assert.equal(typeof reply[0].distance, 'string');
41+
assert.equal(typeof reply[0].distance, 'number');
4242
assert.equal(typeof reply[0].hash, 'number');
43-
assert.equal(typeof reply[0].coordinates?.longitude, 'string');
44-
assert.equal(typeof reply[0].coordinates?.latitude, 'string');
43+
assert.equal(typeof reply[0].coordinates?.longitude, 'number');
44+
assert.equal(typeof reply[0].coordinates?.latitude, 'number');
4545
}, {
4646
client: GLOBAL.SERVERS.OPEN,
4747
cluster: GLOBAL.CLUSTERS.OPEN

packages/client/lib/commands/GEOSEARCH_WITH.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ describe('GEOSEARCH WITH', () => {
3939

4040
assert.equal(reply.length, 1);
4141
assert.equal(reply[0].member, 'member');
42-
assert.equal(typeof reply[0].distance, 'string');
42+
assert.equal(typeof reply[0].distance, 'number');
4343
assert.equal(typeof reply[0].hash, 'number');
44-
assert.equal(typeof reply[0].coordinates!.longitude, 'string');
45-
assert.equal(typeof reply[0].coordinates!.latitude, 'string');
44+
assert.equal(typeof reply[0].coordinates!.longitude, 'number');
45+
assert.equal(typeof reply[0].coordinates!.latitude, 'number');
4646
}, {
4747
client: GLOBAL.SERVERS.OPEN,
4848
cluster: GLOBAL.CLUSTERS.OPEN

packages/client/lib/commands/GEOSEARCH_WITH.ts

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { CommandParser } from '../client/parser';
2-
import { RedisArgument, ArrayReply, TuplesReply, BlobStringReply, NumberReply, DoubleReply, UnwrapReply, Command } from '../RESP/types';
2+
import { RedisArgument, ArrayReply, TuplesReply, BlobStringReply, NumberReply, DoubleReply, UnwrapReply, Command, TypeMapping } from '../RESP/types';
33
import GEOSEARCH, { GeoSearchBy, GeoSearchFrom, GeoSearchOptions } from './GEOSEARCH';
4+
import { transformDoubleReply } from './generic-transformers';
45

56
export const GEO_REPLY_WITH = {
67
DISTANCE: 'WITHDIST',
@@ -12,7 +13,7 @@ export type GeoReplyWith = typeof GEO_REPLY_WITH[keyof typeof GEO_REPLY_WITH];
1213

1314
export interface GeoReplyWithMember {
1415
member: BlobStringReply;
15-
distance?: BlobStringReply;
16+
distance?: DoubleReply;
1617
hash?: NumberReply;
1718
coordinates?: {
1819
longitude: DoubleReply;
@@ -45,14 +46,23 @@ export default {
4546
},
4647
transformReply(
4748
reply: UnwrapReply<ArrayReply<TuplesReply<[BlobStringReply, ...Array<any>]>>>,
48-
replyWith: Array<GeoReplyWith>
49+
replyWith: Array<GeoReplyWith>,
50+
typeMapping?: TypeMapping
4951
) {
5052
const replyWithSet = new Set(replyWith);
5153
let index = 0;
5254
const distanceIndex = replyWithSet.has(GEO_REPLY_WITH.DISTANCE) && ++index,
5355
hashIndex = replyWithSet.has(GEO_REPLY_WITH.HASH) && ++index,
5456
coordinatesIndex = replyWithSet.has(GEO_REPLY_WITH.COORDINATES) && ++index;
55-
57+
58+
const parseDouble = (value: unknown) => {
59+
return (
60+
typeof value === 'number' ?
61+
value as unknown as DoubleReply :
62+
transformDoubleReply[2](value as BlobStringReply, undefined, typeMapping)
63+
);
64+
};
65+
5666
return reply.map(raw => {
5767
const unwrapped = raw as unknown as UnwrapReply<typeof raw>;
5868

@@ -61,18 +71,18 @@ export default {
6171
};
6272

6373
if (distanceIndex) {
64-
item.distance = unwrapped[distanceIndex];
74+
item.distance = parseDouble(unwrapped[distanceIndex]);
6575
}
66-
76+
6777
if (hashIndex) {
6878
item.hash = unwrapped[hashIndex];
6979
}
70-
80+
7181
if (coordinatesIndex) {
7282
const [longitude, latitude] = unwrapped[coordinatesIndex];
7383
item.coordinates = {
74-
longitude,
75-
latitude
84+
longitude: parseDouble(longitude),
85+
latitude: parseDouble(latitude)
7686
};
7787
}
7888

0 commit comments

Comments
 (0)