Skip to content

Commit

Permalink
feat: ✨ update SET to allow any FIELD value
Browse files Browse the repository at this point in the history
Previously FIELD were limited to integer values.
With Tile38 1.30.0 FIELDs can be string, number or complex objects.
  • Loading branch information
iwpnd committed Mar 23, 2023
1 parent 74f2f6f commit 116da95
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export enum SubCommand {
SECTOR = 'SECTOR',
}

export type CommandArgs = Array<SubCommand | string | number>;
export type CommandArgs = Array<SubCommand | string | number | object>;

enum Format {
RESP = 'resp',
Expand Down
18 changes: 18 additions & 0 deletions src/commands/Set.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,24 @@ describe('Set', () => {
'SET',
[key, id, 'FIELD', 'speed', 10, 'FIELD', 'weight', 100],
]);
expect(
query
.fields({ motor: { maxSpeed: 100, petrol: 'super' } })
.compile()
).toEqual([
'SET',
[
key,
id,
'FIELD',
'motor',
JSON.stringify({ maxSpeed: 100, petrol: 'super' }),
],
]);
expect(query.fields({ driver: 'Vincent' }).compile()).toEqual([
'SET',
[key, id, 'FIELD', 'driver', 'Vincent'],
]);
expect(query.fields({ speed: 1 }).compile()).toEqual([
'SET',
[key, id, 'FIELD', 'speed', 1],
Expand Down
14 changes: 9 additions & 5 deletions src/commands/Set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,15 @@ export class Set extends Executable implements SetInterface {
this._id,
...(this._fields
? Object.entries(this._fields)
.map(([name, value]) => [
SubCommand.FIELD,
name,
value,
])
.map(([name, value]) =>
typeof value === 'object'
? [
SubCommand.FIELD,
name,
JSON.stringify(value),
]
: [SubCommand.FIELD, name, value]
)
.flat()
: []),
...(typeof this._ex === 'number'
Expand Down
2 changes: 1 addition & 1 deletion src/responses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export type LatLon = {
lon: number;
};

export type Fields = Record<string, number>;
export type Fields<O extends object = {}> = Record<string, string | number | O>;

export type Meta = Record<string, string>;

Expand Down
39 changes: 39 additions & 0 deletions src/tests/get.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,45 @@ describe('get', () => {
expect(command).toHaveBeenCalledWith('GET', ['fleet', 'truck1']);
});

it.each([{ test: 'test' }, { test: 1 }, { test: { test: 1 } }])(
'should get as object with fields',
async (fields) => {
await expect(
tile38
.set('fleet', 'truck2')
.fields(fields)
.point(33.5123, -112.2693)
.exec()
).resolves.toEqual({
elapsed: expect.any(String) as string,
ok: true,
});

const expected: ObjectResponse<Point> = {
elapsed: expect.any(String) as string,
ok: true,
object: {
type: 'Point',
coordinates: [
expect.any(Number) as number,
expect.any(Number) as number,
],
},
fields,
};

await expect(
tile38.get('fleet', 'truck2').withFields().asObject()
).resolves.toEqual(expected);

expect(command).toHaveBeenNthCalledWith(2, 'GET', [
'fleet',
'truck2',
'WITHFIELDS',
]);
}
);

it('should get as string object', async () => {
const expected: StringObjectResponse = {
elapsed: expect.any(String) as string,
Expand Down
55 changes: 55 additions & 0 deletions src/tests/set.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,61 @@ describe('set', () => {
]);
});

it.each([
{
fields: { test: 'string' },
expected: [
'fleet',
'truck1',
'FIELD',
'test',
'string',
'POINT',
33.5123,
-112.2693,
],
},
{
fields: { test: 1 },
expected: [
'fleet',
'truck1',
'FIELD',
'test',
1,
'POINT',
33.5123,
-112.2693,
],
},
{
fields: { test: { test: 1 } },
expected: [
'fleet',
'truck1',
'FIELD',
'test',
JSON.stringify({ test: 1 }),
'POINT',
33.5123,
-112.2693,
],
},
])('should set point with fields', async ({ fields, expected }) => {
await expect(
tile38
.set('fleet', 'truck1')
.fields(fields)
.point(33.5123, -112.2693)
.exec()
).resolves.toEqual({
elapsed: expect.any(String) as string,
ok: true,
});

expect(command).toHaveBeenCalledWith('SET', expected);
});

it('should set object', async () => {
await expect(
tile38
Expand Down

0 comments on commit 116da95

Please sign in to comment.