-
Notifications
You must be signed in to change notification settings - Fork 406
#RI-3612-zset infinity score #1285
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './redis-string'; | ||
export * from './zset-score'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './zset-score.decorator'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { | ||
registerDecorator, | ||
ValidationOptions, | ||
} from 'class-validator'; | ||
import { ZSetScoreValidator } from 'src/common/validators'; | ||
|
||
export function isZSetScore(validationOptions?: ValidationOptions) { | ||
return (object: any, propertyName: string) => { | ||
registerDecorator({ | ||
name: 'isZSetScore', | ||
target: object.constructor, | ||
propertyName, | ||
options: validationOptions, | ||
validator: ZSetScoreValidator, | ||
}); | ||
}; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './redis-string.validator'; | ||
export * from './zset-score.validator'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { isNumber } from 'lodash'; | ||
import { | ||
ValidationArguments, | ||
ValidatorConstraint, | ||
ValidatorConstraintInterface, | ||
} from 'class-validator'; | ||
|
||
@ValidatorConstraint({ name: 'RedisStringValidator', async: true }) | ||
export class ZSetScoreValidator implements ValidatorConstraintInterface { | ||
async validate(value: any) { | ||
return value === 'inf' || value === '-inf' || isNumber(value); | ||
} | ||
|
||
defaultMessage(args: ValidationArguments) { | ||
return `${args.property || 'field'} must be a string or a number`; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,7 @@ import { | |
IsNotEmptyObject, | ||
IsNumber, IsString, | ||
Min, | ||
ValidateNested | ||
ValidateNested, | ||
} from 'class-validator'; | ||
import { Type } from 'class-transformer'; | ||
import { SortOrder } from 'src/constants'; | ||
|
@@ -18,7 +18,7 @@ import { | |
DeleteMembersFromSetResponse, | ||
} from 'src/modules/browser/dto/set.dto'; | ||
import { RedisString } from 'src/common/constants'; | ||
import { IsRedisString, RedisStringType } from 'src/common/decorators'; | ||
import { IsRedisString, RedisStringType, isZSetScore } from 'src/common/decorators'; | ||
import { | ||
KeyDto, KeyResponse, KeyWithExpireDto, ScanDataTypeDto, | ||
} from './keys.dto'; | ||
|
@@ -77,13 +77,12 @@ export class ZSetMemberDto { | |
|
||
@ApiProperty({ | ||
description: 'Member score value.', | ||
type: Number, | ||
type: Number || String, | ||
default: 1, | ||
}) | ||
@IsDefined() | ||
@IsNumber({ maxDecimalPlaces: 15 }) | ||
@Type(() => Number) | ||
score: number; | ||
@isZSetScore() | ||
score: number | 'inf' | '-inf'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is better to create a type (like |
||
} | ||
|
||
export class AddMembersToZSetDto extends KeyDto { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ import { | |
Logger, | ||
NotFoundException, | ||
} from '@nestjs/common'; | ||
import { isNull } from 'lodash'; | ||
import { isNull, isNaN } from 'lodash'; | ||
import * as isGlob from 'is-glob'; | ||
import config from 'src/utils/config'; | ||
import { catchAclError, catchTransactionError, unescapeGlob } from 'src/utils'; | ||
|
@@ -277,8 +277,10 @@ export class ZSetBusinessService { | |
BrowserToolZSetCommands.ZScore, | ||
[keyName, member], | ||
); | ||
const formattedScore = isNaN(parseFloat(score)) ? String(score) : parseFloat(score); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we move this to some function to not duplicate logic everywhere? |
||
|
||
if (!isNull(score)) { | ||
result.members.push(plainToClass(ZSetMemberDto, { name: member, score })); | ||
result.members.push(plainToClass(ZSetMemberDto, { name: member, score: formattedScore })); | ||
} | ||
} else { | ||
const scanResult = await this.scanZSet(clientOptions, dto); | ||
|
@@ -375,7 +377,6 @@ export class ZSetBusinessService { | |
nextCursor: null, | ||
members: [], | ||
}; | ||
|
||
while (result.nextCursor !== 0 && result.members.length < count) { | ||
const scanResult = await this.browserTool.execCommand( | ||
clientOptions, | ||
|
@@ -406,11 +407,13 @@ export class ZSetBusinessService { | |
const result: ZSetMemberDto[] = []; | ||
while (reply.length) { | ||
const member = reply.splice(0, 2); | ||
const score = isNaN(parseFloat(member[1])) ? String(member[1]) : parseFloat(member[1]); | ||
result.push(plainToClass(ZSetMemberDto, { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we are using "plainToClass" here. Maybe it is better to create own transformer and |
||
name: member[0], | ||
score: parseFloat(member[1]), | ||
score, | ||
})); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,9 +22,9 @@ const dataSchema = Joi.object({ | |
member: Joi.object().keys({ | ||
name: Joi.string().required(), | ||
// todo: allow(true) - is incorrect but will be transformed to number by BE. Investigate/fix it | ||
score: Joi.number().required().allow(true), | ||
score: Joi.number().required().allow('inf', '-inf').label('.score'), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it is better to create own type here the same way as for |
||
}).messages({ | ||
'number.base': '{#lavel} must be a number', | ||
'number.base': '{#lavel} must be a string or a number', | ||
}), | ||
}).strict(); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,8 @@ import { | |
requirements, | ||
generateInvalidDataTestCases, | ||
validateInvalidDataTestCase, | ||
getMainCheckFn, JoiRedisString | ||
getMainCheckFn, | ||
JoiRedisString, | ||
} from '../deps'; | ||
const { server, request, constants, rte } = deps; | ||
|
||
|
@@ -38,7 +39,7 @@ const responseSchema = Joi.object().keys({ | |
total: Joi.number().integer().required(), | ||
members: Joi.array().items(Joi.object().keys({ | ||
name: JoiRedisString.required(), | ||
score: Joi.number().required(), | ||
score: Joi.number().required().allow('inf', '-inf'), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the same |
||
})).required(), | ||
}).required(); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,7 +41,7 @@ const responseSchema = Joi.object().keys({ | |
nextCursor: Joi.number().integer().required(), | ||
members: Joi.array().items(Joi.object().keys({ | ||
name: JoiRedisString.required(), | ||
score: Joi.number().required(), | ||
score: Joi.number().required().allow('inf', '-inf'), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the same |
||
})).required(), | ||
}).required(); | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
must be a string? "hello world" will be not a valid value here...